Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

Committer:
rvt
Date:
Thu Mar 10 14:05:02 2016 +0000
Revision:
4:2cc58c173de8
Parent:
3:0742b0b42ac9
Child:
5:a0bb17c379ce
expanded to read more digital ports for my new panel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rvt 0:33bc88c4ab31 1 #include "mbed.h"
rvt 0:33bc88c4ab31 2 #include "USBHID.h"
rvt 0:33bc88c4ab31 3 #include "USBJoystick.h"
rvt 0:33bc88c4ab31 4 #include "LowPassFilter.h"
rvt 0:33bc88c4ab31 5 #include "AnalogInFiltered.h"
rvt 4:2cc58c173de8 6 #include "Button.h"
rvt 0:33bc88c4ab31 7 #include "rtos.h"
rvt 0:33bc88c4ab31 8
rvt 1:5b2ab44eb31f 9 // When set, it will send debug data over USB serial
rvt 3:0742b0b42ac9 10 #define TTY_DEBUG true
rvt 0:33bc88c4ab31 11
rvt 0:33bc88c4ab31 12 // Value that defines when to start sending data this prevents the noise sending loads's of data over HID
rvt 0:33bc88c4ab31 13 #define DATA_CHANGE_TRIGGER 64
rvt 0:33bc88c4ab31 14
rvt 1:5b2ab44eb31f 15 // Activity LED for HID data
rvt 1:5b2ab44eb31f 16 #define HIDACTIVITYLED LED3
rvt 0:33bc88c4ab31 17
rvt 2:ae7a31a3c618 18 // Number of samples taken before a buttons as set to be pressed.
rvt 2:ae7a31a3c618 19 // It essentually wait's untel the value is stable enough to be said to be pressed
rvt 2:ae7a31a3c618 20 #define DEBOUNCERUNS 10
rvt 2:ae7a31a3c618 21
rvt 0:33bc88c4ab31 22
rvt 0:33bc88c4ab31 23 // Structure that hold's the dataset of the input's
rvt 0:33bc88c4ab31 24 Mutex analogValueMutex;
rvt 0:33bc88c4ab31 25 struct AnalogData {
rvt 4:2cc58c173de8 26 bool but5;
rvt 4:2cc58c173de8 27 bool but6;
rvt 4:2cc58c173de8 28 bool but7;
rvt 4:2cc58c173de8 29 bool but8;
rvt 4:2cc58c173de8 30 bool but9;
rvt 4:2cc58c173de8 31 bool but10;
rvt 4:2cc58c173de8 32 bool but11;
rvt 4:2cc58c173de8 33 bool but12;
rvt 4:2cc58c173de8 34 bool but13;
rvt 4:2cc58c173de8 35 bool but14;
rvt 4:2cc58c173de8 36 bool but15;
rvt 4:2cc58c173de8 37 bool but16;
rvt 4:2cc58c173de8 38 bool but17;
rvt 4:2cc58c173de8 39 bool but21;
rvt 4:2cc58c173de8 40 bool but22;
rvt 4:2cc58c173de8 41 bool but23;
rvt 4:2cc58c173de8 42 bool but24;
rvt 4:2cc58c173de8 43 bool but25;
rvt 0:33bc88c4ab31 44 long value1;
rvt 0:33bc88c4ab31 45 long value2;
rvt 0:33bc88c4ab31 46 long value3;
rvt 0:33bc88c4ab31 47 long value4;
rvt 0:33bc88c4ab31 48 } analogData;
rvt 0:33bc88c4ab31 49
rvt 3:0742b0b42ac9 50
rvt 1:5b2ab44eb31f 51
rvt 1:5b2ab44eb31f 52
rvt 1:5b2ab44eb31f 53 /**
rvt 1:5b2ab44eb31f 54 Debug thread to show some values from the system over USB serial.
rvt 1:5b2ab44eb31f 55 Ensure that TTY_DEBUG is defined so that these routines will get activated.
rvt 1:5b2ab44eb31f 56 This is what I do to view the values on OSX within a terminal
rvt 1:5b2ab44eb31f 57 $ cd /dev
rvt 1:5b2ab44eb31f 58 $ ls | grep usbmodem
rvt 1:5b2ab44eb31f 59 cu.usbmodemfa1232
rvt 1:5b2ab44eb31f 60 tty.usbmodemfa1232
rvt 1:5b2ab44eb31f 61 $ screen tty.usbmodemfa1232
rvt 1:5b2ab44eb31f 62 */
rvt 2:ae7a31a3c618 63 const char *byte_to_binary16(int x)
rvt 2:ae7a31a3c618 64 {
rvt 2:ae7a31a3c618 65 static char b[17];
rvt 2:ae7a31a3c618 66 b[0] = '\0';
rvt 2:ae7a31a3c618 67
rvt 2:ae7a31a3c618 68 int z;
rvt 2:ae7a31a3c618 69 for (z = 32768; z > 0; z >>= 1)
rvt 2:ae7a31a3c618 70 {
rvt 2:ae7a31a3c618 71 strcat(b, ((x & z) == z) ? "1" : "0");
rvt 2:ae7a31a3c618 72 }
rvt 2:ae7a31a3c618 73
rvt 2:ae7a31a3c618 74 return b;
rvt 2:ae7a31a3c618 75 }
rvt 2:ae7a31a3c618 76 const char *byte_to_binary8(int x)
rvt 2:ae7a31a3c618 77 {
rvt 2:ae7a31a3c618 78 static char b[9];
rvt 2:ae7a31a3c618 79 b[0] = '\0';
rvt 2:ae7a31a3c618 80
rvt 2:ae7a31a3c618 81 int z;
rvt 2:ae7a31a3c618 82 for (z = 128; z > 0; z >>= 1)
rvt 2:ae7a31a3c618 83 {
rvt 2:ae7a31a3c618 84 strcat(b, ((x & z) == z) ? "1" : "0");
rvt 2:ae7a31a3c618 85 }
rvt 2:ae7a31a3c618 86
rvt 2:ae7a31a3c618 87 return b;
rvt 2:ae7a31a3c618 88 }
rvt 2:ae7a31a3c618 89
rvt 0:33bc88c4ab31 90 void debug_thread(void const *args)
rvt 0:33bc88c4ab31 91 {
rvt 2:ae7a31a3c618 92 // Serial port for debug data
rvt 2:ae7a31a3c618 93 Serial pc(USBTX, USBRX); // tx, rx
rvt 2:ae7a31a3c618 94
rvt 1:5b2ab44eb31f 95 // Make a local copy
rvt 1:5b2ab44eb31f 96 AnalogData localCopy;
rvt 1:5b2ab44eb31f 97 AnalogData previous;
rvt 0:33bc88c4ab31 98 while (true) {
rvt 1:5b2ab44eb31f 99 // Lock and copy input values
rvt 0:33bc88c4ab31 100 analogValueMutex.lock();
rvt 0:33bc88c4ab31 101 memcpy (&localCopy, &analogData, sizeof(AnalogData));
rvt 0:33bc88c4ab31 102 analogValueMutex.unlock();
rvt 0:33bc88c4ab31 103
rvt 0:33bc88c4ab31 104 // Send to USB
rvt 0:33bc88c4ab31 105 pc.printf("\x1B[0;0H");
rvt 0:33bc88c4ab31 106 pc.printf("Yoke and Pedals!\n\r");
rvt 2:ae7a31a3c618 107 pc.printf("Analog in p20: %s %d \n\r",byte_to_binary16((localCopy.value1 + 32768)),localCopy.value1 + 32768);
rvt 0:33bc88c4ab31 108 pc.printf("Analog in p19: %d diff: %d \n\r",localCopy.value2,localCopy.value2-previous.value2);
rvt 0:33bc88c4ab31 109 pc.printf("Analog in p18: %d diff: %d \n\r",localCopy.value3,localCopy.value3-previous.value3);
rvt 0:33bc88c4ab31 110 pc.printf("Analog in p17: %d diff: %d \n\r",localCopy.value4,localCopy.value4-previous.value4);
rvt 4:2cc58c173de8 111 pc.printf("Button 5: %d \n\r",localCopy.but5);
rvt 4:2cc58c173de8 112 pc.printf("Button 6: %d \n\r",localCopy.but6);
rvt 4:2cc58c173de8 113 pc.printf("Button 7: %d \n\r",localCopy.but7);
rvt 4:2cc58c173de8 114 pc.printf("Button 8: %d \n\r",localCopy.but8);
rvt 4:2cc58c173de8 115 pc.printf("Button 9: %d \n\r",localCopy.but9);
rvt 1:5b2ab44eb31f 116
rvt 1:5b2ab44eb31f 117 // Make local copy so we can show diff version
rvt 0:33bc88c4ab31 118 memcpy (&previous, &localCopy, sizeof(AnalogData));
rvt 0:33bc88c4ab31 119 Thread::wait(1000);
rvt 0:33bc88c4ab31 120 }
rvt 0:33bc88c4ab31 121 }
rvt 3:0742b0b42ac9 122
rvt 0:33bc88c4ab31 123
rvt 2:ae7a31a3c618 124
rvt 0:33bc88c4ab31 125 void hid_thread(void const *args)
rvt 0:33bc88c4ab31 126 {
rvt 1:5b2ab44eb31f 127 //USB HID JoyStick
rvt 1:5b2ab44eb31f 128 USBJoystick joystick;
rvt 1:5b2ab44eb31f 129
rvt 1:5b2ab44eb31f 130 // Activity led for HID data transmissions
rvt 1:5b2ab44eb31f 131 DigitalOut hIDActivity(HIDACTIVITYLED);
rvt 2:ae7a31a3c618 132
rvt 2:ae7a31a3c618 133 // Locla copy of analog data
rvt 2:ae7a31a3c618 134 AnalogData localCopy;
rvt 1:5b2ab44eb31f 135
rvt 4:2cc58c173de8 136 uint32_t buttons=0x00;
rvt 0:33bc88c4ab31 137 while (true) {
rvt 0:33bc88c4ab31 138
rvt 0:33bc88c4ab31 139
rvt 0:33bc88c4ab31 140 // Wait for analog in to have some data
rvt 1:5b2ab44eb31f 141 hIDActivity=false;
rvt 0:33bc88c4ab31 142 Thread::signal_wait(0x1);
rvt 1:5b2ab44eb31f 143 hIDActivity=true;
rvt 0:33bc88c4ab31 144
rvt 0:33bc88c4ab31 145 // Make a local copy of the data
rvt 0:33bc88c4ab31 146 analogValueMutex.lock();
rvt 0:33bc88c4ab31 147 memcpy (&localCopy, &analogData, sizeof(AnalogData));
rvt 0:33bc88c4ab31 148 analogValueMutex.unlock();
rvt 0:33bc88c4ab31 149
rvt 4:2cc58c173de8 150 buttons=0x00;
rvt 4:2cc58c173de8 151 buttons = buttons | localCopy.but5 ;
rvt 4:2cc58c173de8 152 buttons = buttons | localCopy.but6 << 1;
rvt 4:2cc58c173de8 153 buttons = buttons | localCopy.but7 << 2;
rvt 4:2cc58c173de8 154 buttons = buttons | localCopy.but8 << 3;
rvt 4:2cc58c173de8 155 buttons = buttons | localCopy.but9 << 4;
rvt 4:2cc58c173de8 156 buttons = buttons | localCopy.but10 << 5;
rvt 4:2cc58c173de8 157 buttons = buttons | localCopy.but11 << 6;
rvt 4:2cc58c173de8 158 buttons = buttons | localCopy.but12 << 7;
rvt 4:2cc58c173de8 159 buttons = buttons | localCopy.but13 << 8;
rvt 4:2cc58c173de8 160 buttons = buttons | localCopy.but14 << 9;
rvt 4:2cc58c173de8 161 buttons = buttons | localCopy.but15 << 10;
rvt 4:2cc58c173de8 162 buttons = buttons | localCopy.but16 << 11;
rvt 4:2cc58c173de8 163 buttons = buttons | localCopy.but17 << 12;
rvt 4:2cc58c173de8 164 buttons = buttons | localCopy.but21 << 13;
rvt 4:2cc58c173de8 165 buttons = buttons | localCopy.but22 << 14;
rvt 4:2cc58c173de8 166 buttons = buttons | localCopy.but23 << 15;
rvt 4:2cc58c173de8 167 buttons = buttons | localCopy.but24 << 16;
rvt 4:2cc58c173de8 168 buttons = buttons | localCopy.but25 << 17;
rvt 2:ae7a31a3c618 169
rvt 0:33bc88c4ab31 170 // Update joystick's info
rvt 0:33bc88c4ab31 171 joystick.update(
rvt 4:2cc58c173de8 172 localCopy.value1,
rvt 0:33bc88c4ab31 173 localCopy.value2,
rvt 0:33bc88c4ab31 174 localCopy.value3,
rvt 0:33bc88c4ab31 175 localCopy.value4,
rvt 2:ae7a31a3c618 176 buttons);
rvt 0:33bc88c4ab31 177
rvt 0:33bc88c4ab31 178 // Wait 50 ms to send a other USB update
rvt 0:33bc88c4ab31 179 Thread::wait(50);
rvt 0:33bc88c4ab31 180 }
rvt 0:33bc88c4ab31 181 }
rvt 0:33bc88c4ab31 182
rvt 0:33bc88c4ab31 183
rvt 0:33bc88c4ab31 184
rvt 0:33bc88c4ab31 185 int main()
rvt 0:33bc88c4ab31 186 {
rvt 4:2cc58c173de8 187 analogData.value1=0.;
rvt 4:2cc58c173de8 188 analogData.value2=0.;
rvt 4:2cc58c173de8 189 analogData.value3=0.;
rvt 4:2cc58c173de8 190 analogData.value4=0.;
rvt 4:2cc58c173de8 191
rvt 4:2cc58c173de8 192 Button but5(p5, true, true);
rvt 4:2cc58c173de8 193 Button but6(p6, true, true);
rvt 4:2cc58c173de8 194 Button but7(p7, true, true);
rvt 4:2cc58c173de8 195 Button but8(p8, true, true);
rvt 4:2cc58c173de8 196 Button but9(p9, true, true);
rvt 4:2cc58c173de8 197 Button but10(p10, true, true);
rvt 4:2cc58c173de8 198 Button but11(p11, true, true);
rvt 4:2cc58c173de8 199 Button but12(p12, true, true);
rvt 4:2cc58c173de8 200 Button but13(p13, true, true);
rvt 4:2cc58c173de8 201 Button but14(p14, true, true);
rvt 4:2cc58c173de8 202 Button but15(p15, true, true);
rvt 4:2cc58c173de8 203 Button but16(p16, true, true);
rvt 4:2cc58c173de8 204 Button but17(p17, true, true);
rvt 4:2cc58c173de8 205 Button but21(p21, true, true);
rvt 4:2cc58c173de8 206 Button but22(p22, true, true);
rvt 4:2cc58c173de8 207 Button but23(p23, true, true);
rvt 4:2cc58c173de8 208 Button but24(p24, true, true);
rvt 4:2cc58c173de8 209 Button but25(p25, true, true);
rvt 0:33bc88c4ab31 210
rvt 3:0742b0b42ac9 211 if (TTY_DEBUG) {
rvt 3:0742b0b42ac9 212 Thread _debugThread(debug_thread);
rvt 3:0742b0b42ac9 213 }
rvt 4:2cc58c173de8 214
rvt 4:2cc58c173de8 215
rvt 0:33bc88c4ab31 216 Thread _hid_thread(hid_thread);
rvt 0:33bc88c4ab31 217
rvt 0:33bc88c4ab31 218 // Initialise moving average filters
rvt 4:2cc58c173de8 219 LowPassFilter lowPassFilter1(new AnalogFilterInterface(),0.95f); // The close the alpha value is to 1, the lower the cut-off frequency
rvt 1:5b2ab44eb31f 220 LowPassFilter lowPassFilter2(new AnalogFilterInterface(),0.95f);
rvt 0:33bc88c4ab31 221
rvt 0:33bc88c4ab31 222 // Initialise analog input and tell it what fulters to use
rvt 4:2cc58c173de8 223 AnalogInFiltered ai2(&lowPassFilter1, p19, DATA_CHANGE_TRIGGER);
rvt 4:2cc58c173de8 224 AnalogInFiltered ai1(&lowPassFilter2, p20, DATA_CHANGE_TRIGGER);
rvt 2:ae7a31a3c618 225
rvt 0:33bc88c4ab31 226 while (true) {
rvt 0:33bc88c4ab31 227 // Measure analog in's
rvt 0:33bc88c4ab31 228 ai1.measure();
rvt 0:33bc88c4ab31 229 ai2.measure();
rvt 4:2cc58c173de8 230
rvt 4:2cc58c173de8 231 but5.measure();
rvt 4:2cc58c173de8 232 but6.measure();
rvt 4:2cc58c173de8 233 but7.measure();
rvt 4:2cc58c173de8 234 but8.measure();
rvt 4:2cc58c173de8 235 but9.measure();
rvt 4:2cc58c173de8 236 but10.measure();
rvt 4:2cc58c173de8 237 but11.measure();
rvt 4:2cc58c173de8 238 but12.measure();
rvt 4:2cc58c173de8 239 but13.measure();
rvt 4:2cc58c173de8 240 but14.measure();
rvt 4:2cc58c173de8 241 but15.measure();
rvt 4:2cc58c173de8 242 but16.measure();
rvt 4:2cc58c173de8 243 but17.measure();
rvt 2:ae7a31a3c618 244
rvt 4:2cc58c173de8 245 but21.measure();
rvt 4:2cc58c173de8 246 but22.measure();
rvt 4:2cc58c173de8 247 but23.measure();
rvt 4:2cc58c173de8 248 but24.measure();
rvt 4:2cc58c173de8 249 but25.measure();
rvt 0:33bc88c4ab31 250
rvt 0:33bc88c4ab31 251 // test of any of the values have been changed, so we only update when data was actually changed
rvt 4:2cc58c173de8 252 bool isChanged =
rvt 4:2cc58c173de8 253 ai1.getIsChanged() ||
rvt 4:2cc58c173de8 254 ai2.getIsChanged() ||
rvt 4:2cc58c173de8 255 but5.getIsChanged() ||
rvt 4:2cc58c173de8 256 but6.getIsChanged() ||
rvt 4:2cc58c173de8 257 but7.getIsChanged() ||
rvt 4:2cc58c173de8 258 but8.getIsChanged() ||
rvt 4:2cc58c173de8 259 but9.getIsChanged() ||
rvt 4:2cc58c173de8 260 but10.getIsChanged() ||
rvt 4:2cc58c173de8 261 but11.getIsChanged() ||
rvt 4:2cc58c173de8 262 but12.getIsChanged() ||
rvt 4:2cc58c173de8 263 but13.getIsChanged() ||
rvt 4:2cc58c173de8 264 but14.getIsChanged() ||
rvt 4:2cc58c173de8 265 but15.getIsChanged() ||
rvt 4:2cc58c173de8 266 but16.getIsChanged() ||
rvt 4:2cc58c173de8 267 but17.getIsChanged() ||
rvt 4:2cc58c173de8 268 but21.getIsChanged() ||
rvt 4:2cc58c173de8 269 but22.getIsChanged() ||
rvt 4:2cc58c173de8 270 but23.getIsChanged() ||
rvt 4:2cc58c173de8 271 but24.getIsChanged() ||
rvt 4:2cc58c173de8 272 but25.getIsChanged();
rvt 0:33bc88c4ab31 273 if (
rvt 4:2cc58c173de8 274 isChanged
rvt 0:33bc88c4ab31 275 ) {
rvt 0:33bc88c4ab31 276 // Copy analog data to global data
rvt 0:33bc88c4ab31 277 analogValueMutex.lock();
rvt 4:2cc58c173de8 278 analogData.but5 = but5.getData();
rvt 4:2cc58c173de8 279 analogData.but6 = but6.getData();
rvt 4:2cc58c173de8 280 analogData.but7 = but7.getData();
rvt 4:2cc58c173de8 281 analogData.but8 = but8.getData();
rvt 4:2cc58c173de8 282 analogData.but9 = but9.getData();
rvt 4:2cc58c173de8 283 analogData.but10 = but10.getData();
rvt 4:2cc58c173de8 284 analogData.but11 = but11.getData();
rvt 4:2cc58c173de8 285 analogData.but12 = but12.getData();
rvt 4:2cc58c173de8 286 analogData.but13 = but13.getData();
rvt 4:2cc58c173de8 287 analogData.but14 = but14.getData();
rvt 4:2cc58c173de8 288 analogData.but15 = but15.getData();
rvt 4:2cc58c173de8 289 analogData.but16 = but16.getData();
rvt 4:2cc58c173de8 290 analogData.but17 = but17.getData();
rvt 4:2cc58c173de8 291 analogData.but21 = but21.getData();
rvt 4:2cc58c173de8 292 analogData.but22 = but22.getData();
rvt 4:2cc58c173de8 293 analogData.but23 = but23.getData();
rvt 4:2cc58c173de8 294 analogData.but24 = but24.getData();
rvt 4:2cc58c173de8 295 analogData.but25 = but25.getData();
rvt 4:2cc58c173de8 296
rvt 2:ae7a31a3c618 297 analogData.value1 = ai1.getData();
rvt 2:ae7a31a3c618 298 analogData.value2 = ai2.getData();
rvt 4:2cc58c173de8 299 analogData.value3 = 0.;
rvt 4:2cc58c173de8 300 analogData.value4 = 0.;
rvt 0:33bc88c4ab31 301 analogValueMutex.unlock();
rvt 0:33bc88c4ab31 302
rvt 0:33bc88c4ab31 303 // Signal that data has been changed
rvt 0:33bc88c4ab31 304 _hid_thread.signal_set(0x1);
rvt 0:33bc88c4ab31 305 }
rvt 0:33bc88c4ab31 306 Thread::wait(1);
rvt 0:33bc88c4ab31 307 }
rvt 0:33bc88c4ab31 308 }
rvt 0:33bc88c4ab31 309