This was made by Niko for bluetooth communication
Dependencies: m3pi_ng
main.cpp@0:e6896e4f41d0, 2014-05-22 (annotated)
- Committer:
- satyson
- Date:
- Thu May 22 12:14:37 2014 +0000
- Revision:
- 0:e6896e4f41d0
This was made by Niko to use bluetooth
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satyson | 0:e6896e4f41d0 | 1 | #include "mbed.h" |
satyson | 0:e6896e4f41d0 | 2 | #include "m3pi_ng.h" |
satyson | 0:e6896e4f41d0 | 3 | #include "btbee.h" |
satyson | 0:e6896e4f41d0 | 4 | |
satyson | 0:e6896e4f41d0 | 5 | // Made by Niko |
satyson | 0:e6896e4f41d0 | 6 | |
satyson | 0:e6896e4f41d0 | 7 | m3pi m3pi; |
satyson | 0:e6896e4f41d0 | 8 | btbee btbee; |
satyson | 0:e6896e4f41d0 | 9 | |
satyson | 0:e6896e4f41d0 | 10 | DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)}; |
satyson | 0:e6896e4f41d0 | 11 | DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)}; |
satyson | 0:e6896e4f41d0 | 12 | DigitalIn m3pi_pb(p21); |
satyson | 0:e6896e4f41d0 | 13 | |
satyson | 0:e6896e4f41d0 | 14 | int main() |
satyson | 0:e6896e4f41d0 | 15 | { |
satyson | 0:e6896e4f41d0 | 16 | // initialization stuff //////////////////////////////////////////////////////////////////////////////////////////////////// |
satyson | 0:e6896e4f41d0 | 17 | m3pi.locate(0,1); |
satyson | 0:e6896e4f41d0 | 18 | btbee.reset(); |
satyson | 0:e6896e4f41d0 | 19 | for (int i = 0; i <4; i++) { |
satyson | 0:e6896e4f41d0 | 20 | mbed_led[i] = 0; |
satyson | 0:e6896e4f41d0 | 21 | } |
satyson | 0:e6896e4f41d0 | 22 | for (int i = 0; i <8; i++) { |
satyson | 0:e6896e4f41d0 | 23 | m3pi_led[i]=0; |
satyson | 0:e6896e4f41d0 | 24 | } |
satyson | 0:e6896e4f41d0 | 25 | m3pi_pb.mode(PullUp); // expected would be 1 when pb is pressed, 0 when not, opposite is the case |
satyson | 0:e6896e4f41d0 | 26 | |
satyson | 0:e6896e4f41d0 | 27 | // end initialization stuff //////////////////////////////////////////////////////////////////////////////////////////////// |
satyson | 0:e6896e4f41d0 | 28 | |
satyson | 0:e6896e4f41d0 | 29 | m3pi.locate(0,0); |
satyson | 0:e6896e4f41d0 | 30 | m3pi.printf("B:%0.3f%V",m3pi.battery()); |
satyson | 0:e6896e4f41d0 | 31 | wait(0.3); |
satyson | 0:e6896e4f41d0 | 32 | |
satyson | 0:e6896e4f41d0 | 33 | m3pi.locate(0,0); |
satyson | 0:e6896e4f41d0 | 34 | m3pi.printf("%s","btTest"); |
satyson | 0:e6896e4f41d0 | 35 | m3pi.locate(0,1); |
satyson | 0:e6896e4f41d0 | 36 | m3pi.printf("%s","PBonLNK"); |
satyson | 0:e6896e4f41d0 | 37 | |
satyson | 0:e6896e4f41d0 | 38 | // wait for the user to push P21, should be pressed when the bt link is established (green led "link") |
satyson | 0:e6896e4f41d0 | 39 | while(m3pi_pb) { |
satyson | 0:e6896e4f41d0 | 40 | m3pi_led[0]=!m3pi_led[0]; |
satyson | 0:e6896e4f41d0 | 41 | wait(0.1); |
satyson | 0:e6896e4f41d0 | 42 | } |
satyson | 0:e6896e4f41d0 | 43 | |
satyson | 0:e6896e4f41d0 | 44 | int iline=1; |
satyson | 0:e6896e4f41d0 | 45 | char arr_read[30]; // this should be long enough to store any reply coming in over bt. |
satyson | 0:e6896e4f41d0 | 46 | int chars_read; // number of chars read in a bt reply |
satyson | 0:e6896e4f41d0 | 47 | |
satyson | 0:e6896e4f41d0 | 48 | while (true) { |
satyson | 0:e6896e4f41d0 | 49 | // this writes "Line 001\n" to "Line 005\n" and then "end\n" to the btbee |
satyson | 0:e6896e4f41d0 | 50 | if ( btbee.writeable() ) { |
satyson | 0:e6896e4f41d0 | 51 | if (iline==6) { |
satyson | 0:e6896e4f41d0 | 52 | btbee.printf("end\n"); |
satyson | 0:e6896e4f41d0 | 53 | iline++; |
satyson | 0:e6896e4f41d0 | 54 | }//if |
satyson | 0:e6896e4f41d0 | 55 | else { |
satyson | 0:e6896e4f41d0 | 56 | if (iline <6){ |
satyson | 0:e6896e4f41d0 | 57 | btbee.printf("Line %0.3d \n",iline); |
satyson | 0:e6896e4f41d0 | 58 | m3pi_led[0]=0; |
satyson | 0:e6896e4f41d0 | 59 | m3pi.locate(0,0); |
satyson | 0:e6896e4f41d0 | 60 | m3pi.printf("Sent %0.3d",iline); |
satyson | 0:e6896e4f41d0 | 61 | iline++; |
satyson | 0:e6896e4f41d0 | 62 | } |
satyson | 0:e6896e4f41d0 | 63 | }//else |
satyson | 0:e6896e4f41d0 | 64 | }//if_write |
satyson | 0:e6896e4f41d0 | 65 | |
satyson | 0:e6896e4f41d0 | 66 | // check for answers after each write /not write |
satyson | 0:e6896e4f41d0 | 67 | while ( btbee.readable() ) { |
satyson | 0:e6896e4f41d0 | 68 | m3pi_led[7]=1; |
satyson | 0:e6896e4f41d0 | 69 | btbee.read_all(arr_read, 30, &chars_read ); |
satyson | 0:e6896e4f41d0 | 70 | m3pi_led[6]=1; |
satyson | 0:e6896e4f41d0 | 71 | m3pi.locate(0,1); |
satyson | 0:e6896e4f41d0 | 72 | m3pi.print(arr_read,chars_read); |
satyson | 0:e6896e4f41d0 | 73 | m3pi_led[5]=1; |
satyson | 0:e6896e4f41d0 | 74 | }//while_readable |
satyson | 0:e6896e4f41d0 | 75 | wait(0.1); |
satyson | 0:e6896e4f41d0 | 76 | }//while_true |
satyson | 0:e6896e4f41d0 | 77 | |
satyson | 0:e6896e4f41d0 | 78 | }//main |