V1

Fork of 446STM32_Print_PC_I2CS by Hsieh JenYun

main.cpp

Committer:
Tanakacool
Date:
2017-11-10
Revision:
3:4d977dd77a4b
Parent:
2:764981c589b6

File content as of revision 3:4d977dd77a4b:

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
I2CSlave slave(PC_9, PA_8);
void boxslave();

int main() {
    char buffer[128];
    slave.address(0xA0);

    while(1){ 
        boxslave();
       
        if(pc.readable()) {  
            //slave.stop();
            pc.gets(buffer, 128);
            pc.printf("I got '%s'\n", buffer);
            //slave.stop();
        }
    }
}
void boxslave(){
    char buf[20];
    char msg[] = "Hi Master!";
    char all_buf[]="i'm here!";
     int i = slave.receive();
       switch (i) {
           case I2CSlave::ReadAddressed:
               if(!slave.write(msg, strlen(msg) + 1)) // Includes null char
                slave.stop();
                printf("ReadAddressed\n");
                //slave.stop();
               break;
           case I2CSlave::WriteGeneral:
               if(!slave.write(all_buf, strlen(all_buf) + 1)) // Includes null char
                slave.stop();
                printf("i've received the command...\n");
               break;
           case I2CSlave::WriteAddressed:
               slave.read(buf, 20);
               printf("master write to A: %s\n", buf);
               break;
       }
       for(int i = 0; i < 10; i++) {
           buf[i] = 0;    // Clear buffer
           }
}