
blinks on BLE timeout
Dependencies: NeoStrip_edit mbed
main.cpp@1:9091db1f995e, 2014-04-25 (annotated)
- Committer:
- rhodes42
- Date:
- Fri Apr 25 14:42:01 2014 +0000
- Revision:
- 1:9091db1f995e
- Parent:
- 0:7f09c539f8c5
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rhodes42 | 0:7f09c539f8c5 | 1 | #include "mbed.h" |
rhodes42 | 0:7f09c539f8c5 | 2 | #include "NeoStrip.h" |
rhodes42 | 0:7f09c539f8c5 | 3 | |
rhodes42 | 0:7f09c539f8c5 | 4 | |
rhodes42 | 0:7f09c539f8c5 | 5 | #define N 59 |
rhodes42 | 0:7f09c539f8c5 | 6 | |
rhodes42 | 0:7f09c539f8c5 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
rhodes42 | 0:7f09c539f8c5 | 8 | Serial device(p13,p14); |
rhodes42 | 0:7f09c539f8c5 | 9 | DigitalOut myled1(LED1); |
rhodes42 | 0:7f09c539f8c5 | 10 | |
rhodes42 | 0:7f09c539f8c5 | 11 | NeoStrip strip(p18, N); |
rhodes42 | 0:7f09c539f8c5 | 12 | |
rhodes42 | 0:7f09c539f8c5 | 13 | void blinkOn(); |
rhodes42 | 0:7f09c539f8c5 | 14 | void blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 15 | |
rhodes42 | 0:7f09c539f8c5 | 16 | Timer t; |
rhodes42 | 0:7f09c539f8c5 | 17 | void callback(){ |
rhodes42 | 0:7f09c539f8c5 | 18 | t.reset(); |
rhodes42 | 0:7f09c539f8c5 | 19 | //reply ack |
rhodes42 | 0:7f09c539f8c5 | 20 | device.putc('A'); |
rhodes42 | 0:7f09c539f8c5 | 21 | //clear serial buffer |
rhodes42 | 0:7f09c539f8c5 | 22 | while(device.readable()) device.getc(); |
rhodes42 | 0:7f09c539f8c5 | 23 | myled1=1; |
rhodes42 | 0:7f09c539f8c5 | 24 | t.start(); |
rhodes42 | 0:7f09c539f8c5 | 25 | blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 26 | } |
rhodes42 | 0:7f09c539f8c5 | 27 | |
rhodes42 | 0:7f09c539f8c5 | 28 | |
rhodes42 | 0:7f09c539f8c5 | 29 | void blinkOn() |
rhodes42 | 0:7f09c539f8c5 | 30 | { |
rhodes42 | 0:7f09c539f8c5 | 31 | for (int i = 0; i < N; i++) |
rhodes42 | 0:7f09c539f8c5 | 32 | { |
rhodes42 | 0:7f09c539f8c5 | 33 | if (i % 2) |
rhodes42 | 0:7f09c539f8c5 | 34 | strip.setPixel(i, 0xff, 0, 0); |
rhodes42 | 0:7f09c539f8c5 | 35 | else |
rhodes42 | 0:7f09c539f8c5 | 36 | strip.setPixel(i, 0, 0, 0); |
rhodes42 | 0:7f09c539f8c5 | 37 | } |
rhodes42 | 0:7f09c539f8c5 | 38 | wait_ms(50); |
rhodes42 | 0:7f09c539f8c5 | 39 | strip.write(); |
rhodes42 | 0:7f09c539f8c5 | 40 | } |
rhodes42 | 0:7f09c539f8c5 | 41 | |
rhodes42 | 0:7f09c539f8c5 | 42 | void blinkOff() |
rhodes42 | 0:7f09c539f8c5 | 43 | { |
rhodes42 | 0:7f09c539f8c5 | 44 | for (int i = 0; i < N; i++) |
rhodes42 | 0:7f09c539f8c5 | 45 | { |
rhodes42 | 0:7f09c539f8c5 | 46 | strip.setPixel(i, 0, 0, 0); |
rhodes42 | 0:7f09c539f8c5 | 47 | } |
rhodes42 | 0:7f09c539f8c5 | 48 | wait_ms(60); |
rhodes42 | 0:7f09c539f8c5 | 49 | strip.write(); |
rhodes42 | 0:7f09c539f8c5 | 50 | } |
rhodes42 | 0:7f09c539f8c5 | 51 | int main() { |
rhodes42 | 0:7f09c539f8c5 | 52 | |
rhodes42 | 0:7f09c539f8c5 | 53 | strip.setBrightness(.1); |
rhodes42 | 0:7f09c539f8c5 | 54 | |
rhodes42 | 0:7f09c539f8c5 | 55 | blinkOn(); |
rhodes42 | 0:7f09c539f8c5 | 56 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 57 | blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 58 | |
rhodes42 | 0:7f09c539f8c5 | 59 | |
rhodes42 | 0:7f09c539f8c5 | 60 | //set mbed baud rate the same as BLE mini baud rate |
rhodes42 | 0:7f09c539f8c5 | 61 | device.baud(57600); |
rhodes42 | 0:7f09c539f8c5 | 62 | device.attach(&callback); |
rhodes42 | 0:7f09c539f8c5 | 63 | |
rhodes42 | 0:7f09c539f8c5 | 64 | //wait until device is connected; |
rhodes42 | 0:7f09c539f8c5 | 65 | while(!device.readable()){} |
rhodes42 | 0:7f09c539f8c5 | 66 | |
rhodes42 | 0:7f09c539f8c5 | 67 | while(1){ |
rhodes42 | 0:7f09c539f8c5 | 68 | if(t.read_ms()>3000) { |
rhodes42 | 0:7f09c539f8c5 | 69 | |
rhodes42 | 0:7f09c539f8c5 | 70 | blinkOn(); |
rhodes42 | 0:7f09c539f8c5 | 71 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 72 | blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 73 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 74 | blinkOn(); |
rhodes42 | 0:7f09c539f8c5 | 75 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 76 | blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 77 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 78 | blinkOn(); |
rhodes42 | 0:7f09c539f8c5 | 79 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 80 | blinkOff(); |
rhodes42 | 0:7f09c539f8c5 | 81 | wait_ms(200); |
rhodes42 | 0:7f09c539f8c5 | 82 | } |
rhodes42 | 0:7f09c539f8c5 | 83 | |
rhodes42 | 0:7f09c539f8c5 | 84 | } |
rhodes42 | 0:7f09c539f8c5 | 85 | |
rhodes42 | 0:7f09c539f8c5 | 86 | } |