Ohnishi_Gundan / Mbed 2 deprecated Master-FM

Dependencies:   MODSERIAL mbed-rtos mbed

Fork of Master by Ohnishi_Gundan

Revision:
3:12e1f116ea42
Parent:
2:c610e1a7fbcd
Child:
4:aaaadb45cbd9
diff -r c610e1a7fbcd -r 12e1f116ea42 bluetooth.cpp
--- a/bluetooth.cpp	Thu Sep 11 15:09:05 2014 +0000
+++ b/bluetooth.cpp	Fri Sep 12 05:11:39 2014 +0000
@@ -2,7 +2,12 @@
 #include "bluetooth.h"
 #include "control.h"
 
+//master
 Serial bt(p13, p14);  // tx, rx
+//slave
+//Serial bt(p28, p27);
+
+DigitalOut l1(LED1);
 
 void btSetup(int role)
 {
@@ -16,18 +21,131 @@
     }
     else{
     //if this device is the slave
-        ;           //nothing to do
+        bt.attach( slaveRecieve, Serial::RxIrq );
+    }
 }
-/*
-void btLoop(int role)
+
+void sync(char option, char* b_data, float* f_data)
 {
-    if( role==BT_MASATER ){
-    //if this device is the master
-        
+    char pac[PACK_SIZE];
+    Cvt temp;
+    
+    //making pac
+    pac[0] = option;
+    if( option==SYNC_MOTOR ){
+        //PACK:     [option/function/pwm*4]
+        //function
+        pac[1]=b_data[0];
+        //pwm
+        temp.fl = f_data[0];
+        for( int i=0 ; i<4 ; i++ ){
+            pac[2+i] = temp.byte[i];    
+        }
+    }
+    else if( option==SYNC_FM ){
+        //PACK:     [option/request]
+        pac[1]=b_data[0];   //request
     }
     else{
-    //if this device is the slave
-        
+        ;
+    }
+    
+    //send pac
+    for( int i=0 ; i<PACK_SIZE ; i++ ){
+        pc.putc( pac[i] );
     }
 }
-*/
\ No newline at end of file
+
+void slaveRecieve(void)
+{
+    static int i;
+    char buf[PACK_SIZE];
+    char pac[PACK_SIZE];
+    float val[PACK_SIZE/4+1];
+    Cvt temp;
+    
+    wait(1/1000.0);
+    
+    l1=1;
+    wait(0.2);
+    l1=2;
+    
+    for( int i=0 ; i<PACK_SIZE ; i++ ){
+        buf[i]=bt.getc();
+    }  
+ 
+    if( buf[0]==SYNC_MOTOR ){
+        //PACK:     [option/function/pwm*4]
+        //pwm
+        for( int i=0 ; i<4 ; i++ ){
+            temp.byte[i]=buf[1+i];
+        }
+        motor( buf[1], temp.fl );
+    }
+    else if( buf[0]==SYNC_FM ){
+        //PACK:     [option/request]
+        ;       //not yet
+    }
+    else if( buf[0]==SYNC_SENSOR ){        
+        getSensor( &(val[0]), &(val[1]) );
+        //PACK:     [option/ir*4/fsr*4];
+        //option
+        pac[0] = SYNC_SENSOR;
+        //ir
+        temp.fl = val[0];
+        temp.fl = ++i%2;
+        for( int i=0 ; i<4 ; i++ ){
+            pac[1+i] = temp.byte[i];    
+        }
+        //fsr
+        temp.fl = val[1];
+        for( int i=0 ; i<4 ; i++ ){
+            pac[5+i] = temp.byte[i];    
+        }
+        
+        //send pac
+        for( int i=0 ; i<PACK_SIZE ; i++ ){
+            bt.putc( pac[i] );
+        }
+    }
+    else if( buf[0]==SYNC_FM ){
+        ;       //not yet
+        
+        //send pac
+        for( int i=0 ; i<PACK_SIZE ; i++ ){
+            bt.putc( pac[i] );
+        }
+    }
+}
+
+void receiveSensor(float* _ir, float* _fsr)
+{
+    char buf[PACK_SIZE];
+    Cvt temp;
+    
+    wait(1/1000.0);
+    
+    //Read
+    for( int i=0 ; i<PACK_SIZE ; i++ ){
+        buf[i]=bt.getc();
+    }
+    
+    //PACK:     [option/ir*4/fsr*4];
+    //option
+    if( buf[0]!=SYNC_SENSOR ){
+        return;
+    }
+    
+    //ir
+    for( int i=0 ; i<4 ; i++ ){
+        temp.byte[i]=buf[1+i];    
+    }
+    *_ir = temp.fl;
+    
+    //fsr
+    for( int i=0 ; i<4 ; i++ ){
+        temp.byte[i] = buf[5+i];    
+    }
+    *_fsr = temp.fl;
+        
+}
\ No newline at end of file