A short test program for all things m3pi and BtBee.

Dependencies:   btbee m3pi_ng mbed

Fork of m3pi_btTest by Nikolas Goldin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "m3pi_ng.h"
00003 #include "btbee.h"
00004 
00005 m3pi m3pi;
00006 btbee btbee;
00007 
00008 DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)};
00009 DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)};
00010 DigitalIn m3pi_pb(p21);
00011 
00012 Timer t1;
00013 
00014 //LocalFileSystem local("local");    
00015 
00016 int main()
00017 {
00018     // initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////////
00019     m3pi.locate(0,1);
00020     btbee.reset();
00021     for (int i = 0; i <4; i++) {
00022         mbed_led[i] = 0;
00023     }
00024     for (int i = 0; i <8; i++) {
00025         m3pi_led[i]=0;
00026     }
00027     m3pi_pb.mode(PullUp); // expected would be 1 when pb is pressed, 0 when not, opposite is the case
00028 
00029     // NOTE on baud rate: the baud rate to be used has to be set on the btBee beforehand. this uses the at mode which is activated by hardware switch.
00030     // only use the hw switch while powered off. use the configure_btbee program to write to it.
00031     //btbee.baud(9600);  // this is BtBee default for regular commands
00032     //btbee.baud(115200);  // this is BtBee regular command set baud rate
00033     //btbee.format();  // the default should be fine
00034 
00035     // end initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////
00036 
00037     // let me play you the song of my people...
00038     // V=volume max is 15::O5=octave5::G16=G16th note::R24=rest24th note...a rest is needed between any two note that are the same
00039     //char dixie[]= {'V','1','5','O','5','G','1','6','E','1','6','C','8','R','2','4','C','8','R','2','4','C','1','6','D','1','6','E','1','6','F','1','6','G','8','R','2','4','G','8','R','2','4','G','8','E','1','6'};
00040     //the number of characters in the array
00041     //int len=49;
00042     //m3pi.playtune(dixie,len);
00043     m3pi.locate(0,0);
00044     m3pi.printf("B:%0.3f%V",m3pi.battery());
00045     wait(0.3);
00046     
00047 /*    m3pi.locate(0,0);
00048     m3pi.printf("%s","fileTest");
00049 
00050     FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00051     fprintf(fp, "Hello World!");
00052     fclose(fp);
00053 
00054     m3pi.locate(0,1);
00055     m3pi.printf("%s","doneTest");
00056     
00057 */    
00058     m3pi.locate(0,0);
00059     m3pi.printf("%s","btTest");
00060     m3pi.locate(0,1);
00061     m3pi.printf("%s","PBonLNK");
00062 
00063     // wait for the user to push P21, should be pressed when the bt link is established (green led "link")
00064     while(m3pi_pb) {
00065         m3pi_led[0]=!m3pi_led[0];
00066         wait(0.1);
00067     }
00068 
00069     int iline=1;
00070     int write = 1; // this could also be a boolean
00071     char arr_read[30]; // this should be long enough to store any reply coming in over bt.
00072     int  chars_read;   // number of chars read in a bt reply
00073     
00074     while (true) {
00075        
00076         // this writes "Line 001\n" to "Line 005\n" and then "end\n" to the btbee
00077         if (btbee.writeable() && write ) {
00078             if (iline==6) {
00079                 btbee.printf("end\n");
00080                 write=1;
00081                 iline=7;
00082             }//if
00083             else {
00084                 if (iline <6){
00085                 btbee.printf("Line %0.3d \n",iline);
00086                 m3pi_led[0]=0;
00087                 m3pi.locate(0,0);
00088                 m3pi.printf("Sent %0.3d",iline);
00089                 iline++;
00090                 }
00091             }//else
00092         }//if_write
00093 
00094         // check for answers after each write /not write
00095         while (btbee.readable()) {
00096             m3pi_led[7]=1;
00097             btbee.read_all(arr_read, 30, &chars_read );
00098             m3pi_led[6]=1;
00099             m3pi.locate(0,1);
00100             m3pi.print(arr_read,chars_read);
00101             m3pi_led[5]=1;
00102         }//while_readable
00103         wait(0.1);
00104     }//while true
00105 
00106 }//main
00107 
00108 
00109