telecommand slave

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define interrupt D9
00004                     
00005 
00006 I2CSlave slave(D14,D15);                       
00007 Serial pc(USBTX,USBRX);
00008 InterruptIn m2s_interrupt(interrupt);
00009 
00010 void execute_command(char* cmd);
00011 
00012 void read_command()
00013 {
00014     printf("\nEntered slave \n\r");
00015     char *command = new char;   
00016     slave.address(0x20);
00017     int slave_status =1;
00018     slave.stop();
00019     while(slave.receive()!=3);
00020     while(slave_status)
00021         slave_status=slave.read(command,1);
00022     printf("\nread %c from master\n\r",*command);
00023     execute_command(command);   //assumed command is very important
00024     printf("\n done \n \r");
00025     delete command;
00026 }
00027 
00028 void execute_command(char* cmd)
00029 {
00030     switch(*cmd)
00031     {
00032         case '0':printf("\n command 0 executed \n\r");
00033         break;
00034         case '1':printf("\n command 1 executed \n\r");
00035         break;
00036         case '2':printf("\n command 2 executed \n\r");
00037         break;
00038         case '3':printf("\n command 3 executed \n\r");
00039         break;
00040         case '4':printf("\n command 4 executed \n\r");
00041         break;
00042     }
00043 }
00044     
00045 
00046                    
00047 int main()
00048 {   
00049     printf("\nwaiting for command from master\n\r");
00050     m2s_interrupt.rise(&read_command);
00051     while(1)
00052     {
00053         ;
00054     }
00055 }