001    /*----------------------------------------------------------------------------*/
002    /* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003    /* Open Source Software - may be modified and shared by FRC teams. The code   */
004    /* must be accompanied by the FIRST BSD license file in the root directory of */
005    /* the project.                                                               */
006    /*----------------------------------------------------------------------------*/
007    
008    package edu.wpi.first.wpilibj.communication;
009    
010    import com.sun.cldc.jna.Structure;
011    
012    /**
013     * Structure for data exchanged between the robot and the driver station.
014     */
015    public final class FRCCommonControlData extends Structure {
016    
017        public static final short RESET_BIT = 0x80;
018        public static final short ESTOP_BIT = 0x40;
019        public static final short ENABLED_BIT = 0x20;
020        public static final short AUTONOMOUS_BIT = 0x10;
021        public static final short FMS_ATTATCHED = 0x08;
022        public static final short RESYNCH = 0x04;
023        public static final short TEST_MODE_BIT = 0x02;
024        public static final short CHECK_VERSIONS_BIT = 0x01;
025    
026        /**
027         * The index of the packet
028         */
029        public /*UINT16*/ int packetIndex;
030        /**
031         * The control mode e.g. Autonomous, E-stop, enabled ...
032         */
033        public /*UINT8*/ short control;
034        // { reset, notEStop, enabled, autonomous, fmsAttached, resync, cRIOChkSum, fpgaChkSum }
035    
036        /**
037         * Determine if the robot should be enabled
038         * @return true if the robot is enabled
039         */
040        public boolean enabled() {
041            return (control & ENABLED_BIT) == ENABLED_BIT;
042        }
043        
044        /**
045         * Determine if the robot is in test mode
046         * @return true if the robot is in test mode
047         */
048        public boolean testMode() {
049            return (control & TEST_MODE_BIT) == TEST_MODE_BIT;
050        }
051    
052        /**
053         * Determine if the robot should be in autonomous
054         * @return true if the robot is in autonomous
055         */
056        public boolean autonomous() {
057            return (control & AUTONOMOUS_BIT) == AUTONOMOUS_BIT;
058        }
059        /**
060         * The state of the digital inputs on the ds
061         */
062        public /*UINT8*/ short dsDigitalIn;
063        /**
064         * The team number from the ds
065         */
066        public /*UINT16*/ int teamID;
067        /**
068         * Which alliance the robot is on
069         */
070        public char dsID_Alliance;
071        /**
072         * The position of the controls on the alliance station wall.
073         */
074        public char dsID_Position;
075        /**
076         * Position of the axes of the first js
077         */
078        public byte[] stick0Axes = new byte[6];
079        /**
080         * Button state of the first js
081         */
082        public short stick0Buttons;         // Left-most 4 bits are unused
083        /**
084         * Position of the axes of the second js
085         */
086        public byte[] stick1Axes = new byte[6];
087        /**
088         * Button state of the second js
089         */
090        public short stick1Buttons;         // Left-most 4 bits are unused
091        /**
092         * Position of the axes of the third js
093         */
094        public byte[] stick2Axes = new byte[6];
095        /**
096         * Button state of the third js
097         */
098        public short stick2Buttons;         // Left-most 4 bits are unused
099        /**
100         * Position of the axes of the fourth js
101         */
102        public byte[] stick3Axes = new byte[6];
103        /**
104         * Button state of the fourth js
105         */
106        public short stick3Buttons;         // Left-most 4 bits are unused
107        //Analog inputs are 10 bit right-justified
108        /** Driver Station analog input */
109        public short analog1;
110        /** Driver Station analog input */
111        public short analog2;
112        /** Driver Station analog input */
113        public short analog3;
114        /** Driver Station analog input */
115        public short analog4;
116    
117        // Other fields are used by the lower-level comm system and not needed by robot code:
118    
119        /**
120         * Create a new FRCControlData structure
121         */
122        public FRCCommonControlData() {
123            allocateMemory();
124        }
125    
126        /**
127         * Method to free the memory used by this structure
128         */
129        protected void free() {
130            freeMemory();
131        }
132    
133        /**
134         * Read new data in the structure
135         */
136        public void read() {
137            packetIndex = backingNativeMemory.getShort(0) & 0xFFFF;
138            control = (short)(backingNativeMemory.getByte(2) & 0xFF);
139    
140            dsDigitalIn = (short)(backingNativeMemory.getByte(3) & 0xFF);
141            teamID = backingNativeMemory.getShort(4) & 0xFFFF;
142    
143            dsID_Alliance = (char) backingNativeMemory.getByte(6);
144            dsID_Position = (char) backingNativeMemory.getByte(7);
145    
146            backingNativeMemory.getBytes(8, stick0Axes, 0, 6);
147            stick0Buttons = backingNativeMemory.getShort(14);
148    
149            backingNativeMemory.getBytes(16, stick1Axes, 0, 6);
150            stick1Buttons = backingNativeMemory.getShort(22);
151    
152            backingNativeMemory.getBytes(24, stick2Axes, 0, 6);
153            stick2Buttons = backingNativeMemory.getShort(30);
154    
155            backingNativeMemory.getBytes(32, stick3Axes, 0, 6);
156            stick3Buttons = backingNativeMemory.getShort(38);
157    
158            analog1 = backingNativeMemory.getShort(40);
159            analog2 = backingNativeMemory.getShort(42);
160            analog3 = backingNativeMemory.getShort(44);
161            analog4 = backingNativeMemory.getShort(46);
162    
163            // Other fields are used by the lower-level comm system and not needed by robot code
164        }
165    
166        /**
167         * Write new data in the structure
168         */
169        public void write() {
170            throw new IllegalStateException("FRCCommonControlData is not writable");
171        }
172    
173        /**
174         * Get the size of the structure
175         * @return size of the structure
176         */
177        public int size() {
178            return 80;
179        }
180    
181    }