Tomas Hyhlík
/
Nucleo_serial
Turns on and of LED depending on received serial commnad "on" or "off"
main.cpp@4:5ac470115649, 2018-04-08 (annotated)
- Committer:
- hyhlik
- Date:
- Sun Apr 08 14:49:52 2018 +0000
- Revision:
- 4:5ac470115649
- Parent:
- 2:08c13f9a3d5c
Demonstration of serial communication
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:028fac66239d | 1 | #include "mbed.h" |
bcostm | 1:e9d1c42a73ae | 2 | |
bcostm | 0:028fac66239d | 3 | Serial pc(SERIAL_TX, SERIAL_RX); |
hyhlik | 4:5ac470115649 | 4 | DigitalOut led01(LED1); |
bcostm | 2:08c13f9a3d5c | 5 | |
hyhlik | 4:5ac470115649 | 6 | int i; // for cycles |
hyhlik | 4:5ac470115649 | 7 | char cmd_on[] = "on"; |
hyhlik | 4:5ac470115649 | 8 | char cmd_off[] = "off"; |
hyhlik | 4:5ac470115649 | 9 | |
hyhlik | 4:5ac470115649 | 10 | char *getCmd(); |
hyhlik | 4:5ac470115649 | 11 | void processCmd(char *cmd); |
hyhlik | 4:5ac470115649 | 12 | |
hyhlik | 4:5ac470115649 | 13 | Thread t1; |
bcostm | 2:08c13f9a3d5c | 14 | |
hyhlik | 4:5ac470115649 | 15 | /////////////////////////////////////////////////////////////////////////////// |
hyhlik | 4:5ac470115649 | 16 | void tSerial_body() |
hyhlik | 4:5ac470115649 | 17 | { |
hyhlik | 4:5ac470115649 | 18 | while(1){ |
hyhlik | 4:5ac470115649 | 19 | if (pc.readable()) // if there is an character to read from the device |
hyhlik | 4:5ac470115649 | 20 | { |
hyhlik | 4:5ac470115649 | 21 | char *SerialCommand = getCmd(); |
hyhlik | 4:5ac470115649 | 22 | processCmd(SerialCommand); |
hyhlik | 4:5ac470115649 | 23 | free(SerialCommand); |
hyhlik | 4:5ac470115649 | 24 | } |
hyhlik | 4:5ac470115649 | 25 | } |
hyhlik | 4:5ac470115649 | 26 | } |
hyhlik | 4:5ac470115649 | 27 | /////////////////////////////////////////////////////////////////////////////// |
hyhlik | 4:5ac470115649 | 28 | char *getCmd() |
bcostm | 2:08c13f9a3d5c | 29 | { |
hyhlik | 4:5ac470115649 | 30 | char buff[128]; |
hyhlik | 4:5ac470115649 | 31 | unsigned char ch; |
hyhlik | 4:5ac470115649 | 32 | int buffIndex = 0; |
hyhlik | 4:5ac470115649 | 33 | memset(buff, 0, sizeof(buff)); // clean the buffer |
hyhlik | 4:5ac470115649 | 34 | |
hyhlik | 4:5ac470115649 | 35 | |
hyhlik | 4:5ac470115649 | 36 | char *cmd = (char*)malloc(128); |
hyhlik | 4:5ac470115649 | 37 | if (!cmd) |
hyhlik | 4:5ac470115649 | 38 | return NULL; |
hyhlik | 4:5ac470115649 | 39 | do |
hyhlik | 4:5ac470115649 | 40 | { |
hyhlik | 4:5ac470115649 | 41 | ch = pc.getc(); // read it |
hyhlik | 4:5ac470115649 | 42 | if (buffIndex < 128){ // just to avoid buffer overflow |
hyhlik | 4:5ac470115649 | 43 | buff[buffIndex] = ch; // put it into the value array and increment the index |
hyhlik | 4:5ac470115649 | 44 | buffIndex++; |
hyhlik | 4:5ac470115649 | 45 | } |
hyhlik | 4:5ac470115649 | 46 | } |
hyhlik | 4:5ac470115649 | 47 | while (ch != '\n' && ch != '\r' ); |
hyhlik | 4:5ac470115649 | 48 | buff[buffIndex]='\0'; // add the end-signalling char |
hyhlik | 4:5ac470115649 | 49 | |
hyhlik | 4:5ac470115649 | 50 | if(strlen(buff) != 0){ |
hyhlik | 4:5ac470115649 | 51 | |
hyhlik | 4:5ac470115649 | 52 | for (i = 0 ; i < sizeof(buff) ; i++){ |
hyhlik | 4:5ac470115649 | 53 | if (buff[i] == '\n' || buff[i] == '\r'){ |
hyhlik | 4:5ac470115649 | 54 | cmd[i] = '\0'; |
hyhlik | 4:5ac470115649 | 55 | return cmd; |
hyhlik | 4:5ac470115649 | 56 | } |
hyhlik | 4:5ac470115649 | 57 | cmd[i] = buff[i]; |
hyhlik | 4:5ac470115649 | 58 | } |
hyhlik | 4:5ac470115649 | 59 | } |
hyhlik | 4:5ac470115649 | 60 | return NULL; |
hyhlik | 4:5ac470115649 | 61 | } |
hyhlik | 4:5ac470115649 | 62 | /////////////////////////////////////////////////////////////////////////////// |
hyhlik | 4:5ac470115649 | 63 | void processCmd(char *cmd) |
hyhlik | 4:5ac470115649 | 64 | { |
hyhlik | 4:5ac470115649 | 65 | pc.printf("rec: \"%s\"\n\r", cmd); |
hyhlik | 4:5ac470115649 | 66 | if(strcmp(cmd,cmd_on) == 0){ |
hyhlik | 4:5ac470115649 | 67 | led01 = 1; |
hyhlik | 4:5ac470115649 | 68 | pc.printf("Turning the led on.\n\r"); |
hyhlik | 4:5ac470115649 | 69 | } else if (strcmp(cmd,cmd_off) == 0) { |
hyhlik | 4:5ac470115649 | 70 | led01 = 0; |
hyhlik | 4:5ac470115649 | 71 | pc.printf("Turning the led off.\n\r"); |
hyhlik | 4:5ac470115649 | 72 | |
bcostm | 2:08c13f9a3d5c | 73 | } |
bcostm | 0:028fac66239d | 74 | } |
hyhlik | 4:5ac470115649 | 75 | |
hyhlik | 4:5ac470115649 | 76 | /////////////////////////////////////////////////////////////////////////////// |
hyhlik | 4:5ac470115649 | 77 | int main() |
hyhlik | 4:5ac470115649 | 78 | { |
hyhlik | 4:5ac470115649 | 79 | t1.start(tSerial_body); |
hyhlik | 4:5ac470115649 | 80 | |
hyhlik | 4:5ac470115649 | 81 | led01 = 1; |
hyhlik | 4:5ac470115649 | 82 | |
hyhlik | 4:5ac470115649 | 83 | pc.printf("\n\r App started______Banik pyco\n\r"); |
hyhlik | 4:5ac470115649 | 84 | int counter = 1; |
hyhlik | 4:5ac470115649 | 85 | while(1) |
hyhlik | 4:5ac470115649 | 86 | { |
hyhlik | 4:5ac470115649 | 87 | pc.printf("seconds: %d\n\r", counter++); |
hyhlik | 4:5ac470115649 | 88 | wait(1); |
hyhlik | 4:5ac470115649 | 89 | } |
hyhlik | 4:5ac470115649 | 90 | } |