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: EventFramework mbed
main.cpp
00001 #include "mbed.h" 00002 #include "TSISensor.h" 00003 #include "MMA8451Q.h" 00004 #include "MAG3110.h" 00005 #include "EventFramework/EventFramework.h" 00006 #include <cstdlib> 00007 00008 #define OFF 0 00009 #define ON 1 00010 00011 #define DEBUG 00012 00013 PwmOut rled(PTE29); 00014 PwmOut gled(PTD5); 00015 00016 Serial pc(USBTX, USBRX); 00017 00018 MMA8451Q acc51(PTE25, PTE24, 0x1D<<1); 00019 MAG3110 mag(PTE25, PTE24); 00020 AnalogIn lightSensor(PTE22); 00021 TSISensor tsi; 00022 AnalogIn sinewave(PTB1); 00023 00024 // labels for different data channel 00025 char MAG_X = 0x00; 00026 char MAG_Y = 0x01; 00027 char MAG_Z = 0x02; 00028 char MAG_H = 0x03; 00029 char ACC_X = 0x04; 00030 char ACC_Y = 0x05; 00031 char ACC_Z = 0x06; 00032 char LIT_X = 0x07; 00033 char TOU_X = 0x08; 00034 char SIN_X = 0x09; 00035 00036 00037 int MAG_STATUS = OFF; 00038 int ACCL_STATUS = OFF; 00039 int LIGHT_STATUS = OFF; 00040 int TOUCH_STATUS = OFF; 00041 int SINE_STATUS = OFF; 00042 00043 Timer t1; 00044 Timer t2; 00045 Timer t3; 00046 Timer t4; 00047 Timer t5; 00048 00049 //time threshold, equals to 1/sampling_rate 00050 int interval1 = 10000; 00051 int interval2 = 10000; 00052 int interval3 = 10000; 00053 int interval4 = 10000; 00054 int interval5 = 10000; 00055 00056 // framework creation with cooperative scheduling mechanism. 00057 EventFramework kernel(EventFramework::SCHED_VALUE_COOPERATIVE); 00058 // Events creation 00059 Event accl_Evt(0); 00060 Event mag_Evt(0); 00061 Event light_Evt(0); 00062 Event touch_Evt(0); 00063 Event sine_Evt(0); 00064 // EventHandlers creation 00065 EventHandler eh_accl(0); 00066 EventHandler eh_mag(0); 00067 EventHandler eh_light(0); 00068 EventHandler eh_touch(0); 00069 EventHandler eh_sine(0); 00070 // declaration of event dispatching functions that will be attached to previous EventHandlers 00071 EventDispatchingRoutine ev_accl_handle_func; 00072 EventDispatchingRoutine ev_mag_handle_func; 00073 EventDispatchingRoutine ev_light_handle_func; 00074 EventDispatchingRoutine ev_touch_handle_func; 00075 EventDispatchingRoutine ev_sine_handle_func; 00076 00077 int START = OFF; 00078 00079 void sendint(char label,int x){ 00080 char *p = (char *)&x; 00081 pc.putc(label); 00082 pc.putc(*p); 00083 pc.putc(*(p+1)); 00084 pc.putc(*(p+2)); 00085 pc.putc(*(p+3)); 00086 } 00087 00088 void sendfloat(char label,float x){ 00089 char *p = (char *)&x; 00090 pc.putc(label); 00091 pc.putc(*p); 00092 pc.putc(*(p+1)); 00093 pc.putc(*(p+2)); 00094 pc.putc(*(p+3)); 00095 } 00096 00097 void calXY() //magnetometer calibration: finding max and min of X, Y axis 00098 { 00099 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; 00100 00101 rled = ON; 00102 00103 printf("Waiting for initial press\n"); 00104 // Wait for slider to be pressed 00105 while( tsi.readDistance() == 0 ) { 00106 rled = ON; 00107 wait(0.2); 00108 rled = OFF; 00109 wait(0.2); 00110 } 00111 00112 printf("Waiting for release\n"); 00113 00114 // Wait for release 00115 while( tsi.readDistance() != 0 ) { 00116 rled = OFF; 00117 wait(0.2); 00118 rled = ON; 00119 wait(0.2); 00120 } 00121 rled = OFF; 00122 wait(0.5); 00123 00124 printf("Rotate\n"); 00125 00126 tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); 00127 tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); 00128 00129 while(tsi.readDistance() == 0) { 00130 gled = ON; 00131 wait(0.1); 00132 gled = OFF; 00133 wait(0.1); 00134 newX = mag.readVal(MAG_OUT_X_MSB); 00135 newY = mag.readVal(MAG_OUT_Y_MSB); 00136 if (newX > tempXmax) tempXmax = newX; 00137 if (newX < tempXmin) tempXmin = newX; 00138 if (newY > tempYmax) tempYmax = newY; 00139 if (newY < tempYmin) tempYmin = newY; 00140 } 00141 00142 mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); 00143 00144 // Wait for release 00145 while( tsi.readDistance() != 0 ) { 00146 gled = OFF; 00147 wait(0.2); 00148 gled = ON; 00149 wait(0.2); 00150 } 00151 gled = OFF; 00152 wait(1.0); 00153 } 00154 00155 uint32_t ev_accl_handle_func(void* me, void* args){ 00156 float temp_x = acc51.getAccX(); 00157 float temp_y = acc51.getAccY(); 00158 float temp_z = acc51.getAccZ(); 00159 __disable_irq(); 00160 sendfloat(ACC_X,temp_x); 00161 sendfloat(ACC_Y,temp_y); 00162 sendfloat(ACC_Z,temp_z); 00163 __enable_irq(); 00164 } 00165 uint32_t ev_mag_handle_func(void* me, void* args){ 00166 int temp_x = 0, temp_y = 0, temp_z = 0; 00167 float temp_h = mag.getHeading(); 00168 mag.getValues(&temp_x, &temp_y, &temp_z); 00169 __disable_irq(); 00170 sendint(MAG_X,temp_x); 00171 sendint(MAG_Y,temp_y); 00172 sendint(MAG_Z,temp_z); 00173 sendfloat(MAG_H,temp_h); 00174 __enable_irq(); 00175 } 00176 uint32_t ev_light_handle_func(void* me, void* args){ 00177 float temp_x = lightSensor; 00178 __disable_irq(); 00179 sendfloat(LIT_X,temp_x); 00180 __enable_irq(); 00181 } 00182 uint32_t ev_touch_handle_func(void* me, void* args){ 00183 float temp_x = tsi.readPercentage();; 00184 __disable_irq(); 00185 sendfloat(TOU_X,temp_x); 00186 __enable_irq(); 00187 } 00188 uint32_t ev_sine_handle_func(void* me, void* args){ 00189 float temp_x = sinewave.read();; 00190 __disable_irq(); 00191 sendfloat(SIN_X,temp_x); 00192 __enable_irq(); 00193 } 00194 void pc_command(){ 00195 kernel.SaveContext(); 00196 char command = pc.getc(); 00197 float sr; 00198 char temp[4]; 00199 00200 switch(command) 00201 { 00202 case 0xEE : // configuration or reconfiguration header 00203 00204 command = pc.getc(); 00205 if (command == 0x01){ // check the status of magnetometer 00206 MAG_STATUS = ON;// turn it on 00207 temp[0] = pc.getc(); 00208 temp[1] = pc.getc(); 00209 temp[2] = pc.getc(); 00210 temp[3] = pc.getc(); 00211 sr = *(float*)temp;//sampling rate 00212 interval1 =(int) 1/sr *1000;//threshold 00213 t1.reset();//timer reset 00214 } 00215 else{ 00216 MAG_STATUS = OFF;//turn it off 00217 t1.stop(); 00218 } 00219 command = pc.getc(); 00220 if (command == 0x01){ // check the status of accelerometer 00221 ACCL_STATUS = ON; 00222 temp[0] = pc.getc(); 00223 temp[1] = pc.getc(); 00224 temp[2] = pc.getc(); 00225 temp[3] = pc.getc(); 00226 sr = *(float*)temp; 00227 interval2 =(int) 1/sr *1000; 00228 t2.reset(); 00229 } 00230 else{ 00231 ACCL_STATUS = OFF; 00232 t2.stop(); 00233 } 00234 command = pc.getc(); 00235 if (command == 0x01){ // check the status of the light sensor 00236 LIGHT_STATUS = ON; 00237 temp[0] = pc.getc(); 00238 temp[1] = pc.getc(); 00239 temp[2] = pc.getc(); 00240 temp[3] = pc.getc(); 00241 sr = *(float*)temp; 00242 interval3 =(int) 1/sr *1000; 00243 t3.reset(); 00244 } 00245 else{ 00246 LIGHT_STATUS = OFF; 00247 t3.stop(); 00248 } 00249 command = pc.getc(); 00250 if (command == 0x01){ // check the status of the touch sensor 00251 TOUCH_STATUS = ON; 00252 temp[0] = pc.getc(); 00253 temp[1] = pc.getc(); 00254 temp[2] = pc.getc(); 00255 temp[3] = pc.getc(); 00256 sr = *(float*)temp; 00257 interval4 =(int) 1/sr *1000; 00258 t4.reset(); 00259 } 00260 else{ 00261 TOUCH_STATUS = OFF; 00262 t4.stop(); 00263 } 00264 command = pc.getc(); 00265 if (command == 0x01){ // check the status of the sine wave receiver 00266 SINE_STATUS = ON; 00267 temp[0] = pc.getc(); 00268 temp[1] = pc.getc(); 00269 temp[2] = pc.getc(); 00270 temp[3] = pc.getc(); 00271 sr = *(float*)temp; 00272 interval5 =(int) 1/sr *1000; 00273 t5.reset(); 00274 } 00275 else{ 00276 SINE_STATUS = OFF; 00277 t5.stop(); 00278 } 00279 START = ON; 00280 rled = ON; 00281 break; 00282 } 00283 kernel.RestoreContext(); 00284 } 00285 00286 int main() 00287 { 00288 // events must be registered into the framework 00289 kernel.AddEvent(&mag_Evt); 00290 kernel.AddEvent(&accl_Evt); 00291 kernel.AddEvent(&light_Evt); 00292 kernel.AddEvent(&touch_Evt); 00293 kernel.AddEvent(&sine_Evt); 00294 00295 eh_mag.Attach(&ev_mag_handle_func); 00296 eh_accl.Attach(&ev_accl_handle_func); 00297 eh_light.Attach(&ev_light_handle_func); 00298 eh_touch.Attach(&ev_touch_handle_func); 00299 eh_sine.Attach(&ev_sine_handle_func); 00300 00301 // handlers are registered into the kernel to listen to specific events. [eh1] listens to [ev1], [eh2] listens to [ev2]. 00302 kernel.AddEventListener(&mag_Evt, &eh_mag); 00303 kernel.AddEventListener(&accl_Evt, &eh_accl); 00304 kernel.AddEventListener(&light_Evt, &eh_light); 00305 kernel.AddEventListener(&touch_Evt, &eh_touch); 00306 kernel.AddEventListener(&sine_Evt, &eh_sine); 00307 calXY(); 00308 pc.attach(&pc_command); 00309 00310 while(START == OFF){ 00311 pc.putc(0xFF); 00312 } 00313 pc.putc(0xFE); 00314 00315 t1.start(); 00316 t2.start(); 00317 t3.start(); 00318 t4.start(); 00319 t5.start(); 00320 // the event-driven kernel starts its operation. 00321 while(1) { 00322 kernel.Schedule(); 00323 if (MAG_STATUS == ON){ 00324 if (t1.read_ms()>interval1){ 00325 kernel.PublishEvent(&mag_Evt, 0); 00326 t1.reset(); 00327 } 00328 } 00329 if (ACCL_STATUS == ON){ 00330 if (t2.read_ms()>interval2){ 00331 kernel.PublishEvent(&accl_Evt, 0); 00332 t2.reset(); 00333 } 00334 } 00335 if (LIGHT_STATUS == ON){ 00336 if (t3.read_ms()>interval3){ 00337 kernel.PublishEvent(&light_Evt, 0); 00338 t3.reset(); 00339 } 00340 } 00341 if (TOUCH_STATUS == ON){ 00342 if (t4.read_ms()>interval4){ 00343 kernel.PublishEvent(&touch_Evt, 0); 00344 t4.reset(); 00345 } 00346 } 00347 if (SINE_STATUS == ON){ 00348 if (t5.read_ms()>interval5){ 00349 kernel.PublishEvent(&sine_Evt, 0); 00350 t5.reset(); 00351 } 00352 } 00353 } 00354 }
Generated on Tue Jul 12 2022 18:55:06 by
1.7.2