Ohnishi_Gundan / Mbed 2 deprecated Master-FM

Dependencies:   MODSERIAL mbed-rtos mbed

Fork of Master by Ohnishi_Gundan

bluetooth.cpp

Committer:
9uS7
Date:
2014-09-12
Revision:
3:12e1f116ea42
Parent:
2:c610e1a7fbcd
Child:
4:aaaadb45cbd9

File content as of revision 3:12e1f116ea42:

#include "mbed.h"
#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)
{
    if( role==BT_MASTER ){
    //if this device is the master
        bt.baud(9600);
        bt.printf("$$$");
        wait(0.5);
        bt.printf("C\r");
        wait(0.5);
    }
    else{
    //if this device is the slave
        bt.attach( slaveRecieve, Serial::RxIrq );
    }
}

void sync(char option, char* b_data, float* f_data)
{
    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{
        ;
    }
    
    //send pac
    for( int i=0 ; i<PACK_SIZE ; i++ ){
        pc.putc( pac[i] );
    }
}

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;
        
}