This program shows, how a serial interrupt can be handled. Here a state machine is set up where the states can be changed from \'offline\' to \'online\' and backwards over UART

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 Serial pc(USBTX, USBRX); // tx, rx
00004 PwmOut led(LED1);
00005 
00006 DigitalOut led2(LED2, "led2");
00007 DigitalOut led3(LED3, "led3");
00008 DigitalOut led4(LED4, "led4");
00009 bool lineState = 0;
00010 
00011 char string[BUFSIZ];
00012 
00013 void allOn()
00014 {
00015     pc.scanf("%s", &string);
00016  
00017     //if(pc.readable()){
00018     //pc.scanf("%s", &string);
00019 
00020     if(!strcmp(string, "#online"))
00021     {
00022         lineState = 1;
00023         pc.printf("This thing is online\n");
00024     }
00025     if(!strcmp(string, "#offline"))
00026     {
00027         lineState = 0;
00028         pc.printf("This thing is offline\n"); 
00029     }
00030     pc.printf("%s\n", string);      
00031 
00032     led2 =! led2;
00033     led3 =! led3;
00034     led4 =! led4; 
00035 }
00036 
00037 
00038 
00039 int main() {
00040     pc.printf("Serial interrupt handler state machine\nstate changeble by typing '#online' or '#offline'\n\n");
00041     pc.attach(&allOn);
00042     
00043     while(1) {
00044         wait(1);
00045         pc.printf("lineState: %i\n",lineState); 
00046     }
00047 }