test

Dependencies:   ESP8266 HCSR04 PID

Fork of car_test_v1 by 涂 桂旺

Committer:
tgw
Date:
Sat Nov 25 03:36:58 2017 +0000
Revision:
3:9e51de1050a1
Parent:
2:35738c77d454
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tgw 2:35738c77d454 1 #include "mbed.h"
tgw 2:35738c77d454 2
tgw 2:35738c77d454 3 #include "HALLFX_ENCODER.h"
tgw 2:35738c77d454 4 #include "MotorDriver.h"
tgw 2:35738c77d454 5
tgw 2:35738c77d454 6 #include "HCSR04.h"
tgw 2:35738c77d454 7 #include "AutomationElements.h"
tgw 2:35738c77d454 8
tgw 2:35738c77d454 9 #include "glibr.h"
tgw 2:35738c77d454 10
tgw 2:35738c77d454 11 Thread thread;
tgw 2:35738c77d454 12 DigitalOut led1(LED1);
tgw 2:35738c77d454 13 volatile bool running = true;
tgw 2:35738c77d454 14
tgw 2:35738c77d454 15 //----------------------motor
tgw 2:35738c77d454 16 MotorDriver mtrR(PA_0, PA_4, PA_1, 10000.0, true); // in1, in2, pwm, pwmFreq, isBrakeable
tgw 2:35738c77d454 17 MotorDriver mtrL(PC_1, PC_0, PB_0, 10000.0, true); // in1, in2, pwm, pwmFreq, isBrakeable
tgw 2:35738c77d454 18 HALLFX_ENCODER encR(PB_3);
tgw 2:35738c77d454 19 HALLFX_ENCODER encL(PB_5);
tgw 2:35738c77d454 20
tgw 2:35738c77d454 21 //------------------------hc-sr04
tgw 2:35738c77d454 22 Serial pc(USBTX, USBRX);
tgw 2:35738c77d454 23 HCSR04 sensor(PA_6, PA_7);
tgw 2:35738c77d454 24 float sampleTime = 0.1;
tgw 2:35738c77d454 25 PT1 filter(1, 2, sampleTime);
tgw 2:35738c77d454 26 Timer timer;
tgw 2:35738c77d454 27 float distance;
tgw 2:35738c77d454 28 float filteredDistance;
tgw 2:35738c77d454 29
tgw 2:35738c77d454 30 //----------------------------GSensor APDS-9960
tgw 2:35738c77d454 31 glibr GSensor(PB_9,PB_8);//i2c
tgw 2:35738c77d454 32 int ge_diff;//gesture differentiate
tgw 2:35738c77d454 33
tgw 2:35738c77d454 34 Thread thread_sonar;
tgw 2:35738c77d454 35 Thread thread_motor;
tgw 2:35738c77d454 36 Thread thread_gsensor;
tgw 2:35738c77d454 37
tgw 2:35738c77d454 38 int txlength_sonar;
tgw 2:35738c77d454 39 int txlength_motor;
tgw 2:35738c77d454 40 int txlength_gsensor;
tgw 2:35738c77d454 41
tgw 2:35738c77d454 42 char txbuf_sonar[256];
tgw 2:35738c77d454 43 char txbuf_motor[256];
tgw 2:35738c77d454 44 char txbuf_gsensor[256];
tgw 2:35738c77d454 45
tgw 2:35738c77d454 46 Thread thread_pc_send;
tgw 2:35738c77d454 47 void pc_send_Thread(){
tgw 2:35738c77d454 48 while(true)
tgw 2:35738c77d454 49 {
tgw 2:35738c77d454 50 if(txlength_sonar>0)
tgw 2:35738c77d454 51 {
tgw 2:35738c77d454 52 puts(txbuf_sonar);
tgw 2:35738c77d454 53 memset(&txbuf_sonar,0,256);
tgw 2:35738c77d454 54 txlength_sonar=0;
tgw 2:35738c77d454 55 }
tgw 2:35738c77d454 56 else if(txlength_motor>0)
tgw 2:35738c77d454 57 {
tgw 2:35738c77d454 58 puts(txbuf_motor);
tgw 2:35738c77d454 59 memset(&txbuf_motor,0,256);
tgw 2:35738c77d454 60 txlength_motor=0;
tgw 2:35738c77d454 61 }
tgw 2:35738c77d454 62 else if(txlength_gsensor>0)
tgw 2:35738c77d454 63 {
tgw 2:35738c77d454 64 puts(txbuf_gsensor);
tgw 2:35738c77d454 65 memset(&txbuf_gsensor,0,256);
tgw 2:35738c77d454 66 txlength_gsensor=0;
tgw 2:35738c77d454 67 led1=!led1;
tgw 2:35738c77d454 68 }
tgw 2:35738c77d454 69 //wait_ms(50);
tgw 2:35738c77d454 70 }
tgw 2:35738c77d454 71 }
tgw 2:35738c77d454 72 //void motorThread(void const *argument){
tgw 2:35738c77d454 73 void motorThread(){
tgw 2:35738c77d454 74 pc.printf("motor : start.....\r\n");
tgw 2:35738c77d454 75 encL.reset();
tgw 2:35738c77d454 76 encR.reset();
tgw 2:35738c77d454 77 while(true)
tgw 2:35738c77d454 78 {
tgw 2:35738c77d454 79 switch ( ge_diff )
tgw 2:35738c77d454 80 { // gesture differentiate
tgw 2:35738c77d454 81 case DIR_UP:
tgw 2:35738c77d454 82 mtrL.forceSetSpeed(0.1);
tgw 2:35738c77d454 83 mtrR.forceSetSpeed(0.1);
tgw 2:35738c77d454 84 break;
tgw 2:35738c77d454 85 case DIR_DOWN:
tgw 2:35738c77d454 86 mtrL.forceSetSpeed(-0.1);
tgw 2:35738c77d454 87 mtrR.forceSetSpeed(-0.1);
tgw 2:35738c77d454 88 break;
tgw 2:35738c77d454 89 case DIR_LEFT:
tgw 2:35738c77d454 90 mtrL.forceSetSpeed(-0.1);
tgw 2:35738c77d454 91 mtrR.forceSetSpeed(0.1);
tgw 2:35738c77d454 92 break;
tgw 2:35738c77d454 93 case DIR_RIGHT:
tgw 2:35738c77d454 94 mtrL.forceSetSpeed(0.1);
tgw 2:35738c77d454 95 mtrR.forceSetSpeed(-0.1);
tgw 2:35738c77d454 96 break;
tgw 2:35738c77d454 97 case DIR_NEAR:
tgw 2:35738c77d454 98 mtrL.forceSetSpeed(0);
tgw 2:35738c77d454 99 mtrR.forceSetSpeed(0);
tgw 2:35738c77d454 100 break;
tgw 2:35738c77d454 101 case DIR_FAR:
tgw 2:35738c77d454 102 mtrL.forceSetSpeed(0.1);
tgw 2:35738c77d454 103 mtrR.forceSetSpeed(0.1);
tgw 2:35738c77d454 104 break;
tgw 2:35738c77d454 105 default:
tgw 2:35738c77d454 106 if(filteredDistance>2 && filteredDistance <=25)
tgw 2:35738c77d454 107 {
tgw 2:35738c77d454 108 mtrL.forceSetSpeed(-0.1);
tgw 2:35738c77d454 109 mtrR.forceSetSpeed(0.1);
tgw 2:35738c77d454 110 }
tgw 2:35738c77d454 111 else if( filteredDistance>2 && filteredDistance <400)
tgw 2:35738c77d454 112 {
tgw 2:35738c77d454 113 mtrL.forceSetSpeed(0.1);
tgw 2:35738c77d454 114 mtrR.forceSetSpeed(0.1);
tgw 2:35738c77d454 115 }
tgw 2:35738c77d454 116 else
tgw 2:35738c77d454 117 {
tgw 2:35738c77d454 118 mtrL.forceSetSpeed(0);
tgw 2:35738c77d454 119 mtrR.forceSetSpeed(0);
tgw 2:35738c77d454 120 }
tgw 2:35738c77d454 121 break;
tgw 2:35738c77d454 122 }
tgw 2:35738c77d454 123
tgw 2:35738c77d454 124 /*
tgw 2:35738c77d454 125 // Set motor speed in open loop mode
tgw 2:35738c77d454 126 // Motor direction based on working setpoint var
tgw 2:35738c77d454 127 int dirL, dirR;
tgw 2:35738c77d454 128 if(working_setpoint < 0.0){
tgw 2:35738c77d454 129 dirL = -1; dirR = -1;
tgw 2:35738c77d454 130 }
tgw 2:35738c77d454 131 else{
tgw 2:35738c77d454 132 dirL = 1; dirR = 1;
tgw 2:35738c77d454 133 }
tgw 2:35738c77d454 134 float speed = abs(working_setpoint) / 90.0; // Normalize based on 90 RPM
tgw 2:35738c77d454 135 mtrL.forceSetSpeed(speed * dirL);
tgw 2:35738c77d454 136 mtrR.forceSetSpeed(speed * dirR);
tgw 2:35738c77d454 137 */
tgw 2:35738c77d454 138 wait(0.1);
tgw 2:35738c77d454 139 if(ge_diff!=DIR_NEAR)ge_diff=DIR_NONE;
tgw 2:35738c77d454 140 }
tgw 2:35738c77d454 141 }
tgw 2:35738c77d454 142
tgw 2:35738c77d454 143 //void sonarThread(void const *argument){
tgw 2:35738c77d454 144 void sonarThread(){
tgw 2:35738c77d454 145 //int t_ms=0;
tgw 2:35738c77d454 146 sensor.setRanges(2, 400);
tgw 2:35738c77d454 147 pc.printf("Minimum sensor range = %g cm\n\rMaximum sensor range = %g cm\n\r", sensor.getMinRange(), sensor.getMaxRange());
tgw 2:35738c77d454 148 pc.printf("Sensor: Filtered:\n\r");
tgw 2:35738c77d454 149 while(true) {
tgw 2:35738c77d454 150 led1=1;
tgw 2:35738c77d454 151 timer.reset();
tgw 2:35738c77d454 152 timer.start();
tgw 2:35738c77d454 153 sensor.startMeasurement();
tgw 2:35738c77d454 154
tgw 2:35738c77d454 155 /*
tgw 2:35738c77d454 156 while(!sensor.isNewDataReady()) {
tgw 2:35738c77d454 157 // waiting time depends on the distance
tgw 2:35738c77d454 158 // wait for new data
tgw 2:35738c77d454 159 }
tgw 2:35738c77d454 160 */
tgw 2:35738c77d454 161 wait_ms(50);
tgw 2:35738c77d454 162
tgw 2:35738c77d454 163 distance = sensor.getDistance_cm();
tgw 2:35738c77d454 164 filter.in(distance);
tgw 2:35738c77d454 165 filteredDistance = filter.out();
tgw 2:35738c77d454 166 //pc.printf("%7.1f cm %7.1f cm\n\r", distance, filteredDistance);
tgw 2:35738c77d454 167 sprintf(txbuf_sonar,"sensor : %7.1f cm %7.1f cm\n\r", distance, filteredDistance);
tgw 2:35738c77d454 168 txlength_sonar=8;
tgw 2:35738c77d454 169 //timer.stop();
tgw 2:35738c77d454 170 //t_ms=timer.read_ms();
tgw 2:35738c77d454 171 //if(t_ms<100)wait_ms(sampleTime * 1000 - timer.read_ms()); // time the loop
tgw 2:35738c77d454 172 led1=0;
tgw 2:35738c77d454 173 }
tgw 2:35738c77d454 174 }
tgw 2:35738c77d454 175
tgw 2:35738c77d454 176 //void gsensorThread(void const *argument){
tgw 2:35738c77d454 177 void gsensorThread(){
tgw 2:35738c77d454 178 // Initialize Sensor with I2C
tgw 2:35738c77d454 179 if ( GSensor.ginit() ) {
tgw 2:35738c77d454 180 pc.printf("APDS-9960 initialization complete\r\n");
tgw 2:35738c77d454 181 } else {
tgw 2:35738c77d454 182 pc.printf("Something went wrong during APDS-9960 init\r\n");
tgw 2:35738c77d454 183 }
tgw 2:35738c77d454 184
tgw 2:35738c77d454 185 // Start running the APDS-9960 gesture sensor engine
tgw 2:35738c77d454 186 if ( GSensor.enableGestureSensor(true) ) {
tgw 2:35738c77d454 187 pc.printf("Gesture sensor is now running\r\n");
tgw 2:35738c77d454 188 } else {
tgw 2:35738c77d454 189 pc.printf("Something went wrong during gesture sensor init!\r\n");
tgw 2:35738c77d454 190 }
tgw 2:35738c77d454 191
tgw 2:35738c77d454 192 while(1) {
tgw 2:35738c77d454 193 if ( GSensor.isGestureAvailable() ) { // gesture detect
tgw 2:35738c77d454 194
tgw 2:35738c77d454 195 switch ( GSensor.readGesture() ) { // gesture differentiate
tgw 2:35738c77d454 196 case DIR_UP:
tgw 2:35738c77d454 197 ge_diff=DIR_UP;
tgw 2:35738c77d454 198 //pc.printf("UP\r\n");
tgw 2:35738c77d454 199 sprintf(txbuf_gsensor,"GSensor : UP\r\n");
tgw 2:35738c77d454 200 txlength_gsensor=16;
tgw 2:35738c77d454 201 break;
tgw 2:35738c77d454 202 case DIR_DOWN:
tgw 2:35738c77d454 203 ge_diff=DIR_DOWN;
tgw 2:35738c77d454 204 //pc.printf("DOWN\r\n");
tgw 2:35738c77d454 205 sprintf(txbuf_gsensor,"GSensor : DOWN\r\n");
tgw 2:35738c77d454 206 txlength_gsensor=18;
tgw 2:35738c77d454 207 break;
tgw 2:35738c77d454 208 case DIR_LEFT:
tgw 2:35738c77d454 209 ge_diff=DIR_LEFT;
tgw 2:35738c77d454 210 //pc.printf("LEFT\r\n");
tgw 2:35738c77d454 211 sprintf(txbuf_gsensor,"GSensor : LEFT\r\n");
tgw 2:35738c77d454 212 txlength_gsensor=18;
tgw 2:35738c77d454 213 break;
tgw 2:35738c77d454 214 case DIR_RIGHT:
tgw 2:35738c77d454 215 ge_diff=DIR_RIGHT;
tgw 2:35738c77d454 216 //pc.printf("RIGHT\r\n");
tgw 2:35738c77d454 217 sprintf(txbuf_gsensor,"GSensor : RIGHT\r\n");
tgw 2:35738c77d454 218 txlength_gsensor=19;
tgw 2:35738c77d454 219 break;
tgw 2:35738c77d454 220 case DIR_NEAR:
tgw 2:35738c77d454 221 ge_diff=DIR_NEAR;
tgw 2:35738c77d454 222 //pc.printf("NEAR\r\n");
tgw 2:35738c77d454 223 sprintf(txbuf_gsensor,"GSensor : NEAR\r\n");
tgw 2:35738c77d454 224 txlength_gsensor=18;
tgw 2:35738c77d454 225 break;
tgw 2:35738c77d454 226 case DIR_FAR:
tgw 2:35738c77d454 227 ge_diff=DIR_FAR;
tgw 2:35738c77d454 228 //pc.printf("FAR\r\n");
tgw 2:35738c77d454 229 sprintf(txbuf_gsensor,"GSensor : FAR\r\n");
tgw 2:35738c77d454 230 txlength_gsensor=17;
tgw 2:35738c77d454 231 break;
tgw 2:35738c77d454 232 default:
tgw 2:35738c77d454 233 //ge_diff=DIR_NONE;
tgw 2:35738c77d454 234 //pc.printf("NONE\r\n");
tgw 2:35738c77d454 235 break;
tgw 2:35738c77d454 236 }
tgw 2:35738c77d454 237 }
tgw 2:35738c77d454 238 }
tgw 2:35738c77d454 239 }
tgw 2:35738c77d454 240 // Blink function toggles the led in a long running loop
tgw 2:35738c77d454 241 void blink(DigitalOut *led) {
tgw 2:35738c77d454 242 while (running) {
tgw 2:35738c77d454 243 *led = !*led;
tgw 2:35738c77d454 244 wait(1);
tgw 2:35738c77d454 245 }
tgw 2:35738c77d454 246 }
tgw 2:35738c77d454 247 void callback_ex() {
tgw 2:35738c77d454 248 // Note: you need to actually read from the serial to clear the RX interrupt
tgw 2:35738c77d454 249 int tmp=pc.getc();
tgw 2:35738c77d454 250 //printf("%c\n", tmp);
tgw 2:35738c77d454 251 }
tgw 2:35738c77d454 252 // Spawns a thread to run blink for 5 seconds
tgw 2:35738c77d454 253 int main() {
tgw 2:35738c77d454 254 pc.baud(9600);
tgw 2:35738c77d454 255 pc.attach(&callback_ex);
tgw 2:35738c77d454 256 thread_pc_send.start(callback(pc_send_Thread));
tgw 2:35738c77d454 257
tgw 2:35738c77d454 258 thread.start(callback(blink, &led1));
tgw 2:35738c77d454 259
tgw 2:35738c77d454 260 wait(5);
tgw 2:35738c77d454 261 running = false;
tgw 2:35738c77d454 262 thread.join();
tgw 2:35738c77d454 263
tgw 2:35738c77d454 264 thread_gsensor.start(callback(gsensorThread));
tgw 2:35738c77d454 265 wait(1);
tgw 2:35738c77d454 266 thread_motor.start(callback(motorThread));
tgw 2:35738c77d454 267 wait(1);
tgw 3:9e51de1050a1 268 thread_sonar.start(callback(sonarThread));
tgw 2:35738c77d454 269
tgw 2:35738c77d454 270 //Thread thread_sonar(sonarThread);
tgw 2:35738c77d454 271 }
tgw 2:35738c77d454 272
tgw 2:35738c77d454 273 /*
tgw 2:35738c77d454 274 主程序指定中断函数
tgw 2:35738c77d454 275 flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
tgw 2:35738c77d454 276
tgw 2:35738c77d454 277 主程序指定 类 的 方法
tgw 2:35738c77d454 278 t.attach(callback(&f, &Flipper::flip), 2.0); // the address of the object, member function, and interval
tgw 2:35738c77d454 279
tgw 2:35738c77d454 280
tgw 2:35738c77d454 281 1、线程
tgw 2:35738c77d454 282 需加载mbed-os
tgw 2:35738c77d454 283
mab5449 0:d4b2a035ffe3 284 #include "mbed.h"
mab5449 0:d4b2a035ffe3 285
mab5449 0:d4b2a035ffe3 286 Thread thread;
mab5449 0:d4b2a035ffe3 287 DigitalOut led1(LED1);
mab5449 0:d4b2a035ffe3 288 volatile bool running = true;
mab5449 0:d4b2a035ffe3 289
mab5449 0:d4b2a035ffe3 290 // Blink function toggles the led in a long running loop
mab5449 0:d4b2a035ffe3 291 void blink(DigitalOut *led) {
mab5449 0:d4b2a035ffe3 292 while (running) {
mab5449 0:d4b2a035ffe3 293 *led = !*led;
mab5449 0:d4b2a035ffe3 294 wait(1);
mab5449 0:d4b2a035ffe3 295 }
mab5449 0:d4b2a035ffe3 296 }
mab5449 0:d4b2a035ffe3 297
mab5449 0:d4b2a035ffe3 298 // Spawns a thread to run blink for 5 seconds
mab5449 0:d4b2a035ffe3 299 int main() {
mab5449 0:d4b2a035ffe3 300 thread.start(callback(blink, &led1));
mab5449 0:d4b2a035ffe3 301 wait(5);
mab5449 0:d4b2a035ffe3 302 running = false;
mab5449 0:d4b2a035ffe3 303 thread.join();
tgw 2:35738c77d454 304 }
tgw 2:35738c77d454 305
tgw 2:35738c77d454 306
tgw 2:35738c77d454 307 2、休眠+按键中断唤醒
tgw 2:35738c77d454 308 需更新mbed
tgw 2:35738c77d454 309 使用F401板时,IDD电流测试,运行时:18mA,灯亮时21 .2mA,睡眠时:6.26 mA,深度睡眠时:0.68 mA,
tgw 2:35738c77d454 310
tgw 2:35738c77d454 311 RTC唤醒使用第三方类(未测试)
tgw 2:35738c77d454 312
tgw 2:35738c77d454 313 #include "mbed.h"
tgw 2:35738c77d454 314
tgw 2:35738c77d454 315 InterruptIn event(USER_BUTTON);
tgw 2:35738c77d454 316 DigitalOut myled(LED1);
tgw 2:35738c77d454 317
tgw 2:35738c77d454 318 int go_to_sleep = 0;
tgw 2:35738c77d454 319
tgw 2:35738c77d454 320 void pressed()
tgw 2:35738c77d454 321 {
tgw 2:35738c77d454 322 printf("Button pressed\n");
tgw 2:35738c77d454 323 go_to_sleep = go_to_sleep + 1;
tgw 2:35738c77d454 324 if (go_to_sleep > 3) go_to_sleep = 0;
tgw 2:35738c77d454 325
tgw 2:35738c77d454 326 }
tgw 2:35738c77d454 327
tgw 2:35738c77d454 328 int main()
tgw 2:35738c77d454 329 {
tgw 2:35738c77d454 330 int i = 0;
tgw 2:35738c77d454 331
tgw 2:35738c77d454 332 printf("\nPress Button to enter/exit sleep & deepsleep\n");
tgw 2:35738c77d454 333
tgw 2:35738c77d454 334 event.fall(&pressed);
tgw 2:35738c77d454 335
tgw 2:35738c77d454 336 while (1) {
tgw 2:35738c77d454 337
tgw 2:35738c77d454 338 if ((go_to_sleep == 0) || (go_to_sleep == 2)) {
tgw 2:35738c77d454 339 printf("%d: Running\n", i);
tgw 2:35738c77d454 340 myled = !myled;
tgw 2:35738c77d454 341 wait(1.0);
tgw 2:35738c77d454 342 }
tgw 2:35738c77d454 343
tgw 2:35738c77d454 344 if (go_to_sleep == 1) {
tgw 2:35738c77d454 345 myled = 0;
tgw 2:35738c77d454 346 printf("%d: Entering sleep (press user button to resume)\n", i);
tgw 2:35738c77d454 347 sleep();
tgw 2:35738c77d454 348 }
tgw 2:35738c77d454 349
tgw 2:35738c77d454 350 if (go_to_sleep == 3) {
tgw 2:35738c77d454 351 myled = 0;
tgw 2:35738c77d454 352 printf("%d: Entering deepsleep (press user button to resume)\n", i);
tgw 2:35738c77d454 353 deepsleep();
tgw 2:35738c77d454 354 }
tgw 2:35738c77d454 355
tgw 2:35738c77d454 356 i++;
tgw 2:35738c77d454 357 }
tgw 2:35738c77d454 358 }
tgw 2:35738c77d454 359
tgw 2:35738c77d454 360
tgw 2:35738c77d454 361 3、系统定时器中断
tgw 2:35738c77d454 362 #include "mbed.h"
tgw 2:35738c77d454 363
tgw 2:35738c77d454 364 Ticker toggle_led_ticker;
tgw 2:35738c77d454 365
tgw 2:35738c77d454 366 DigitalOut led1(LED1);
tgw 2:35738c77d454 367
tgw 2:35738c77d454 368 void toggle_led() {
tgw 2:35738c77d454 369 led1 = !led1;
tgw 2:35738c77d454 370 }
tgw 2:35738c77d454 371
tgw 2:35738c77d454 372 int main() {
tgw 2:35738c77d454 373 // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
tgw 2:35738c77d454 374 toggle_led_ticker.attach(&toggle_led, 0.1);
tgw 2:35738c77d454 375 while (true) {
tgw 2:35738c77d454 376 // Do other things...
tgw 2:35738c77d454 377 }
tgw 2:35738c77d454 378 }
tgw 2:35738c77d454 379
tgw 2:35738c77d454 380 4、超时定时器中断
tgw 2:35738c77d454 381 #include "mbed.h"
tgw 2:35738c77d454 382
tgw 2:35738c77d454 383 // A class for flip()-ing a DigitalOut
tgw 2:35738c77d454 384 class Flipper {
tgw 2:35738c77d454 385 public:
tgw 2:35738c77d454 386 Flipper(PinName pin) : _pin(pin) {
tgw 2:35738c77d454 387 _pin = 0;
tgw 2:35738c77d454 388 }
tgw 2:35738c77d454 389 void flip() {
tgw 2:35738c77d454 390 _pin = !_pin;
tgw 2:35738c77d454 391 }
tgw 2:35738c77d454 392 private:
tgw 2:35738c77d454 393 DigitalOut _pin;
tgw 2:35738c77d454 394 };
tgw 2:35738c77d454 395
tgw 2:35738c77d454 396 DigitalOut led1(LED1);
tgw 2:35738c77d454 397 Flipper f(LED2);
tgw 2:35738c77d454 398 Timeout t;
tgw 2:35738c77d454 399
tgw 2:35738c77d454 400 int main() {
tgw 2:35738c77d454 401 // the address of the object, member function, and interval
tgw 2:35738c77d454 402 t.attach(callback(&f, &Flipper::flip), 2.0);
tgw 2:35738c77d454 403
tgw 2:35738c77d454 404 // spin in a main loop. flipper will interrupt it to call flip
tgw 2:35738c77d454 405 while(1) {
tgw 2:35738c77d454 406 led1 = !led1;
tgw 2:35738c77d454 407 wait(0.2);
tgw 2:35738c77d454 408 }
tgw 2:35738c77d454 409 }
tgw 2:35738c77d454 410
tgw 2:35738c77d454 411
tgw 2:35738c77d454 412 5、通用定时器中断
tgw 2:35738c77d454 413
tgw 2:35738c77d454 414 // Count the time to toggle a LED
tgw 2:35738c77d454 415
tgw 2:35738c77d454 416 #include "mbed.h"
tgw 2:35738c77d454 417
tgw 2:35738c77d454 418 Timer timer;
tgw 2:35738c77d454 419 DigitalOut led(LED1);
tgw 2:35738c77d454 420 int begin, end;
tgw 2:35738c77d454 421
tgw 2:35738c77d454 422 int main() {
tgw 2:35738c77d454 423 timer.start();
tgw 2:35738c77d454 424 begin = timer.read_us();
tgw 2:35738c77d454 425 led = !led;
tgw 2:35738c77d454 426 end = timer.read_us();
tgw 2:35738c77d454 427 printf("Toggle the led takes %d us", end - begin);
tgw 2:35738c77d454 428 }
tgw 2:35738c77d454 429
tgw 2:35738c77d454 430 6、外部中断附加事件
tgw 2:35738c77d454 431 需加载mbed-os
tgw 2:35738c77d454 432
tgw 2:35738c77d454 433 #include "mbed.h"
tgw 2:35738c77d454 434 #include "mbed_events.h"
tgw 2:35738c77d454 435
tgw 2:35738c77d454 436 DigitalOut led1(LED1);
tgw 2:35738c77d454 437 InterruptIn sw(SW2);
tgw 2:35738c77d454 438 EventQueue queue(32 * EVENTS_EVENT_SIZE);
tgw 2:35738c77d454 439 Thread t;
tgw 2:35738c77d454 440
tgw 2:35738c77d454 441 void rise_handler(void) {
tgw 2:35738c77d454 442 // Toggle LED
tgw 2:35738c77d454 443 led1 = !led1;
tgw 2:35738c77d454 444 }
tgw 2:35738c77d454 445
tgw 2:35738c77d454 446 void fall_handler(void) {
tgw 2:35738c77d454 447 printf("fall_handler in context %p\r\n", Thread::gettid());
tgw 2:35738c77d454 448 // Toggle LED
tgw 2:35738c77d454 449 led1 = !led1;
tgw 2:35738c77d454 450 }
tgw 2:35738c77d454 451
tgw 2:35738c77d454 452 int main() {
tgw 2:35738c77d454 453 // Start the event queue
tgw 2:35738c77d454 454 t.start(callback(&queue, &EventQueue::dispatch_forever));
tgw 2:35738c77d454 455 printf("Starting in context %p\r\n", Thread::gettid());
tgw 2:35738c77d454 456 // The 'rise' handler will execute in IRQ context
tgw 2:35738c77d454 457 sw.rise(rise_handler);
tgw 2:35738c77d454 458 // The 'fall' handler will execute in the context of thread 't'
tgw 2:35738c77d454 459 sw.fall(queue.event(fall_handler));
tgw 2:35738c77d454 460 }
tgw 2:35738c77d454 461
tgw 2:35738c77d454 462
tgw 2:35738c77d454 463 7、线程间信号量的传递
tgw 2:35738c77d454 464 需加载mbed-os
tgw 2:35738c77d454 465
tgw 2:35738c77d454 466 #include "mbed.h"
tgw 2:35738c77d454 467
tgw 2:35738c77d454 468 Thread thread;
tgw 2:35738c77d454 469 DigitalOut led(LED1);
tgw 2:35738c77d454 470
tgw 2:35738c77d454 471 void led_thread() {
tgw 2:35738c77d454 472 while (true) {
tgw 2:35738c77d454 473 // Signal flags that are reported as event are automatically cleared.
tgw 2:35738c77d454 474 Thread::signal_wait(0x1);
tgw 2:35738c77d454 475 led = !led;
tgw 2:35738c77d454 476 }
tgw 2:35738c77d454 477 }
tgw 2:35738c77d454 478
tgw 2:35738c77d454 479 int main (void) {
tgw 2:35738c77d454 480 thread.start(callback(led_thread));
tgw 2:35738c77d454 481
tgw 2:35738c77d454 482 while (true) {
tgw 2:35738c77d454 483 wait(1);
tgw 2:35738c77d454 484 thread.signal_set(0x1);
tgw 2:35738c77d454 485 }
tgw 2:35738c77d454 486 }
tgw 2:35738c77d454 487
tgw 2:35738c77d454 488 */