Tomas Hyhlík
/
Nucleo_serial
Turns on and of LED depending on received serial commnad "on" or "off"
Revision 4:5ac470115649, committed 2018-04-08
- Comitter:
- hyhlik
- Date:
- Sun Apr 08 14:49:52 2018 +0000
- Parent:
- 3:14dc18aceca9
- Commit message:
- Demonstration of serial communication
Changed in this revision
diff -r 14dc18aceca9 -r 5ac470115649 main.cpp --- a/main.cpp Wed Jun 07 13:16:58 2017 +0000 +++ b/main.cpp Sun Apr 08 14:49:52 2018 +0000 @@ -1,21 +1,90 @@ #include "mbed.h" -//------------------------------------ -// Hyperterminal configuration -// 9600 bauds, 8-bit data, no parity -//------------------------------------ - Serial pc(SERIAL_TX, SERIAL_RX); +DigitalOut led01(LED1); -DigitalOut myled(LED1); +int i; // for cycles + char cmd_on[] = "on"; + char cmd_off[] = "off"; + +char *getCmd(); +void processCmd(char *cmd); + +Thread t1; -int main() + /////////////////////////////////////////////////////////////////////////////// +void tSerial_body() +{ + while(1){ + if (pc.readable()) // if there is an character to read from the device + { + char *SerialCommand = getCmd(); + processCmd(SerialCommand); + free(SerialCommand); + } + } +} + /////////////////////////////////////////////////////////////////////////////// +char *getCmd() { - int i = 1; - pc.printf("Hello World !\n"); - while(1) { - wait(1); - pc.printf("This program runs since %d seconds.\n", i++); - myled = !myled; + char buff[128]; + unsigned char ch; + int buffIndex = 0; + memset(buff, 0, sizeof(buff)); // clean the buffer + + + char *cmd = (char*)malloc(128); + if (!cmd) + return NULL; + do + { + ch = pc.getc(); // read it + if (buffIndex < 128){ // just to avoid buffer overflow + buff[buffIndex] = ch; // put it into the value array and increment the index + buffIndex++; + } + } + while (ch != '\n' && ch != '\r' ); + buff[buffIndex]='\0'; // add the end-signalling char + + if(strlen(buff) != 0){ + + for (i = 0 ; i < sizeof(buff) ; i++){ + if (buff[i] == '\n' || buff[i] == '\r'){ + cmd[i] = '\0'; + return cmd; + } + cmd[i] = buff[i]; + } + } + return NULL; +} + /////////////////////////////////////////////////////////////////////////////// +void processCmd(char *cmd) +{ + pc.printf("rec: \"%s\"\n\r", cmd); + if(strcmp(cmd,cmd_on) == 0){ + led01 = 1; + pc.printf("Turning the led on.\n\r"); + } else if (strcmp(cmd,cmd_off) == 0) { + led01 = 0; + pc.printf("Turning the led off.\n\r"); + } } + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + t1.start(tSerial_body); + + led01 = 1; + + pc.printf("\n\r App started______Banik pyco\n\r"); + int counter = 1; + while(1) + { + pc.printf("seconds: %d\n\r", counter++); + wait(1); + } +} \ No newline at end of file
diff -r 14dc18aceca9 -r 5ac470115649 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Sun Apr 08 14:49:52 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#addec7ba10054be03849eff58a1d17f157391e7d
diff -r 14dc18aceca9 -r 5ac470115649 mbed.bld --- a/mbed.bld Wed Jun 07 13:16:58 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://mbed.org/users/mbed_official/code/mbed/builds/86740a56073b \ No newline at end of file