Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed-rtos mbed
Fork of JoyStick by
main.cpp
00001 #include "mbed.h" 00002 #include "USBHID.h" 00003 #include "USBJoystick.h" 00004 #include "LowPassFilter.h" 00005 #include "AnalogInFiltered.h" 00006 #include "AutoScale.h" 00007 #include "Button.h" 00008 #include "rtos.h" 00009 00010 // When set, it will send debug data over USB serial 00011 #define TTY_DEBUG false 00012 00013 // Value that defines when to start sending data this prevents the noise sending loads's of data over HID 00014 #define DATA_CHANGE_TRIGGER 8 00015 00016 // Activity LED for HID data 00017 #define HIDACTIVITYLED LED3 00018 00019 // Structure that hold's the dataset of the input's 00020 Mutex analogValueMutex; 00021 struct AnalogData { 00022 union { 00023 struct { 00024 uint32_t bit0:1; 00025 uint32_t bit1:1; 00026 uint32_t bit2:1; 00027 uint32_t bit3:1; 00028 uint32_t bit4:1; 00029 uint32_t bit5:1; 00030 uint32_t bit6:1; 00031 uint32_t bit7:1; 00032 uint32_t bit8:1; 00033 uint32_t bit9:1; 00034 uint32_t bit10:1; 00035 uint32_t bit11:1; 00036 uint32_t bit12:1; 00037 uint32_t bit13:1; 00038 uint32_t bit14:1; 00039 uint32_t bit15:1; 00040 uint32_t bit16:1; 00041 uint32_t bit17:1; 00042 uint32_t bit18:1; 00043 uint32_t bit19:1; 00044 uint32_t bit20:1; 00045 uint32_t bit21:1; 00046 uint32_t bit22:1; 00047 uint32_t bit23:1; 00048 uint32_t bit24:1; 00049 uint32_t bit25:1; 00050 uint32_t bit26:1; 00051 uint32_t bit27:1; 00052 uint32_t bit28:1; 00053 uint32_t bit29:1; 00054 uint32_t bit30:1; 00055 uint32_t bit31:1; 00056 }; 00057 uint32_t button; 00058 } buttons; 00059 long value1; 00060 long value2; 00061 long value3; 00062 long value4; 00063 } analogData; 00064 00065 00066 00067 00068 /** 00069 Debug thread to show some values from the system over USB serial. 00070 Ensure that TTY_DEBUG is defined so that these routines will get activated. 00071 This is what I do to view the values on OSX within a terminal 00072 $ cd /dev 00073 $ ls | grep usbmodem 00074 cu.usbmodemfa1232 00075 tty.usbmodemfa1232 00076 $ screen tty.usbmodemfa1232 00077 */ 00078 const char *byte_to_binary16(int x) 00079 { 00080 static char b[17]; 00081 b[0] = '\0'; 00082 00083 int z; 00084 for (z = 32768; z > 0; z >>= 1) { 00085 strcat(b, ((x & z) == z) ? "1" : "0"); 00086 } 00087 00088 return b; 00089 } 00090 00091 00092 void debug_thread(void const *args) 00093 { 00094 // Serial port for debug data 00095 Serial pc(USBTX, USBRX); // tx, rx 00096 00097 // Make a local copy 00098 AnalogData previous; 00099 while (true) { 00100 // Lock and copy input values 00101 analogValueMutex.lock(); 00102 const AnalogData localCopy = analogData; 00103 analogValueMutex.unlock(); 00104 00105 // Send to USB 00106 pc.printf("\x1B[0;0H"); 00107 pc.printf("Yoke and Pedals!\n\r"); 00108 pc.printf("Analog in p20: %d diff: %d \n\r",localCopy.value1,localCopy.value1-previous.value1); 00109 pc.printf("Analog in p19: %d diff: %d \n\r",localCopy.value2,localCopy.value2-previous.value2); 00110 pc.printf("Buttons: %d \n\r",localCopy.buttons.button); 00111 00112 // Make local copy so we can show diff version 00113 previous = localCopy; 00114 Thread::wait(1000); 00115 } 00116 } 00117 00118 00119 00120 void hid_thread(void const *args) 00121 { 00122 //USB HID JoyStick 00123 USBJoystick joystick; 00124 00125 // Activity led for HID data transmissions 00126 DigitalOut hIDActivity(HIDACTIVITYLED); 00127 00128 while (true) { 00129 // Wait for analog in to have some data 00130 hIDActivity=false; 00131 Thread::signal_wait(0x1); 00132 hIDActivity=true; 00133 00134 // Make a local copy of the data 00135 analogValueMutex.lock(); 00136 const AnalogData localCopy = analogData; 00137 analogValueMutex.unlock(); 00138 00139 // Update joystick's info 00140 joystick.update( 00141 localCopy.value1, 00142 localCopy.value2, 00143 localCopy.value3, 00144 localCopy.value4, 00145 localCopy.buttons.button); 00146 00147 // Wait 50 ms to send a other USB update 00148 Thread::wait(50); 00149 } 00150 } 00151 00152 00153 00154 int main() 00155 { 00156 analogData.buttons.button = 0; 00157 analogData.value1=0.; 00158 analogData.value2=0.; 00159 analogData.value3=0.; 00160 analogData.value4=0.; 00161 00162 Button but5(p5, true, true); 00163 Button but6(p6, true, true); 00164 Button but7(p7, true, true); 00165 Button but8(p8, true, true); 00166 Button but9(p9, true, true); 00167 Button but10(p10, true, true); 00168 Button but11(p11, true, true); 00169 Button but12(p12, true, true); 00170 Button but13(p13, true, true); 00171 Button but14(p14, true, true); 00172 Button but15(p15, true, true); 00173 Button but16(p16, true, true); 00174 Button but17(p17, true, true); 00175 Button but21(p21, true, true); 00176 Button but22(p22, true, true); 00177 Button but23(p23, true, true); 00178 Button but24(p24, true, true); 00179 Button but25(p25, true, true); 00180 00181 if (TTY_DEBUG) { 00182 Thread _debugThread(debug_thread); 00183 } 00184 00185 // Initialise moving average filters 00186 LowPassFilter lowPassFilter1(new AnalogFilterInterface(),0.99f); // The close the alpha value is to 1, the lower the cut-off frequency 00187 LowPassFilter lowPassFilter2(new AnalogFilterInterface(),0.99f); 00188 LowPassFilter lowPassFilter3(new AnalogFilterInterface(),0.96f); 00189 00190 AutoScale autoScale1(&lowPassFilter1, -32768, 32767, 1.01); 00191 AutoScale autoScale2(&lowPassFilter2, -32768, 32767, 1.01); 00192 AutoScale autoScale3(&lowPassFilter3, -32768, 32767, 1.01); 00193 00194 // Initialise analog input and tell it what fulters to use 00195 AnalogInFiltered ai1(&autoScale1, p20, DATA_CHANGE_TRIGGER); 00196 AnalogInFiltered ai2(&autoScale2, p19, DATA_CHANGE_TRIGGER); 00197 AnalogInFiltered ai3(&autoScale3, p18, DATA_CHANGE_TRIGGER); 00198 00199 Thread _hid_thread(hid_thread); 00200 while (true) { 00201 // Measure analog in's 00202 ai1.setData(0); 00203 ai2.setData(0); 00204 ai3.setData(0); 00205 00206 but5.measure(); 00207 but6.measure(); 00208 but7.measure(); 00209 but8.measure(); 00210 but9.measure(); 00211 but10.measure(); 00212 but11.measure(); 00213 but12.measure(); 00214 but13.measure(); 00215 but14.measure(); 00216 but15.measure(); 00217 but16.measure(); 00218 but17.measure(); 00219 00220 but21.measure(); 00221 but22.measure(); 00222 but23.measure(); 00223 but24.measure(); 00224 but25.measure(); 00225 00226 // test of any of the values have been changed, so we only update when data was actually changed 00227 const bool isChanged = 00228 ai1.getIsChanged() || 00229 ai2.getIsChanged() || 00230 ai3.getIsChanged() || 00231 but5.getIsChanged() || 00232 but6.getIsChanged() || 00233 but7.getIsChanged() || 00234 but8.getIsChanged() || 00235 but9.getIsChanged() || 00236 but10.getIsChanged() || 00237 but11.getIsChanged() || 00238 but12.getIsChanged() || 00239 but13.getIsChanged() || 00240 but14.getIsChanged() || 00241 but15.getIsChanged() || 00242 but16.getIsChanged() || 00243 but17.getIsChanged() || 00244 but21.getIsChanged() || 00245 but22.getIsChanged() || 00246 but23.getIsChanged() || 00247 but24.getIsChanged() || 00248 but25.getIsChanged(); 00249 if ( 00250 isChanged 00251 ) { 00252 // Copy analog data to global data 00253 analogValueMutex.lock(); 00254 analogData.buttons.bit0 = but5.getData(); 00255 analogData.buttons.bit1 = but6.getData(); 00256 analogData.buttons.bit2 = but7.getData(); 00257 analogData.buttons.bit3 = but8.getData(); 00258 analogData.buttons.bit4 = but9.getData(); 00259 analogData.buttons.bit5 = but10.getData(); 00260 analogData.buttons.bit6 = but11.getData(); 00261 analogData.buttons.bit7 = but12.getData(); 00262 analogData.buttons.bit8 = but13.getData(); 00263 analogData.buttons.bit9 = but14.getData(); 00264 analogData.buttons.bit10 = but15.getData(); 00265 analogData.buttons.bit11 = but16.getData(); 00266 analogData.buttons.bit12 = but17.getData(); 00267 analogData.buttons.bit13 = but21.getData(); 00268 analogData.buttons.bit14 = but22.getData(); 00269 analogData.buttons.bit15 = but23.getData(); 00270 analogData.buttons.bit16 = but24.getData(); 00271 analogData.buttons.bit17 = but25.getData(); 00272 00273 analogData.value1 = ai1.getData(); 00274 analogData.value2 = ai2.getData(); 00275 analogData.value3 = ai3.getData(); 00276 analogData.value4 = 0.; 00277 analogValueMutex.unlock(); 00278 00279 // Signal that data has been changed 00280 _hid_thread.signal_set(0x1); 00281 } 00282 Thread::wait(1); 00283 } 00284 } 00285
Generated on Wed Jul 13 2022 20:21:10 by
1.7.2
