PSTL - Robot Mobile / Mbed 2 deprecated Nucleo_i2c_slave

Dependencies:   mbed

main.cpp

Committer:
rodrigogenes
Date:
2017-03-03
Revision:
1:f77859e74071
Parent:
0:bdd449e55907

File content as of revision 1:f77859e74071:

 // Simple I2C responder
 #include <mbed.h>
 
   
 
I2CSlave slave(D14, D15);

Serial pc(USBTX, USBRX); // tx, rx

PwmOut pwmNO(D9);
DigitalOut dirNO(D7);
PwmOut pwmNE(D10);
DigitalOut dirNE(D8);

float power_NO = 0.0;
float power_NE = 0.0;

 
 int main() {
     
     DigitalOut myLED (LED2);
     
     pc.printf("Hello!\n");
     
    pwmNO.period_us(1000);
    pwmNE.period_us(1000);
    dirNO=0;
    dirNE=1;
    
    pwmNO.pulsewidth_us(0);
    pwmNE.pulsewidth_us(0);
  

     const int addr = 0x10;
     
     slave.address(addr);

    char buf[1]; 
    
    while (1) {
         int i = slave.receive();
       
         switch (i) {
           // case I2CSlave::ReadAddressed:
                
           //      slave.write(5); // Includes null char
                
           //     myLED=0; 
           //      break;
           //  case I2CSlave::WriteGeneral:
                 
                  //to implement
             case I2CSlave::WriteAddressed:
            
                 slave.read(buf, 1);
        char c= buf[0]+48;       
                 
        if((c == '1') && (power_NO < 0.8)) {
            power_NO += 0.2;
            pc.printf("Gauche Up!\n");
        }
        if((c == '2') && (power_NO > -0.8)) {
            power_NO -= 0.2;
            pc.printf("Gauche Down!\n");
        }
        if((c == '3') && (power_NE < 0.8)) {
            power_NE += 0.2;
            pc.printf("Droite Up!\n");
        }
        if((c == '4') && (power_NE > -0.8)) {
            power_NE -= 0.2;
            pc.printf("Droite Down!\n");
        }
        if((c == '5') && (power_NO < 0.8) && (power_NE < 0.8)) {
            power_NO += 0.2;
            power_NE += 0.2;
            pc.printf("Gauche Droite Up!\n");
        }
        if((c == '6') && (power_NO > -0.8)&& (power_NE > -0.8)) {
            power_NO -= 0.2;
            power_NE -= 0.2;
            pc.printf("Gauche Droite Down!\n");
        }
        if((c == '7')) {
            power_NO = 0;
            power_NE = 0;
            pc.printf("Stop!\n");
        } 
        




        if(power_NO>0){
            dirNO=0;
            pwmNO.pulsewidth_us(1000*power_NO);
        } else {
            dirNO=1;
            pwmNO.pulsewidth_us(-1000*power_NO);
        }
        if(power_NE>0){
            dirNE=1;
            pwmNE.pulsewidth_us(1000*power_NE);
        } else {
            dirNE=0;
            pwmNE.pulsewidth_us(-1000*power_NE);
        }
        
        
        wait(0.1);
                 
                 break;
         }
        for(int i = 0; i < 1; i++) buf[i] = 0;    // Clear buffer
     }
 }