A simple example of a serial communication interface for setting variables in an Mbed program with help of mydsc library.
Revision 4:e315db7c4dea, committed 2020-03-28
- Comitter:
- ghsalazar
- Date:
- Sat Mar 28 16:09:30 2020 +0000
- Parent:
- 3:584c4d2d10aa
- Commit message:
- Initial commit
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mydsc.lib | Show annotated file Show diff for this revision Revisions of this file |
diff -r 584c4d2d10aa -r e315db7c4dea main.cpp --- a/main.cpp Mon Mar 09 15:56:50 2020 +0000 +++ b/main.cpp Sat Mar 28 16:09:30 2020 +0000 @@ -6,102 +6,127 @@ #include "mbed.h" #include "stdio.h" #include "stdlib.h" +#include "string.h" #include "ctype.h" #include "mydsc_kvp.h" +#include "mydsc_error.h" -const unsigned VALUE_SIZE = 128; +// Hardware Interface +DigitalOut led (LED1); +Serial serial (USBTX, USBRX); +InterruptIn button (USER_BUTTON); + + +// Behavior interface +const unsigned MESSAGE_MAX_LEN = 126; +const char END_OF_LINE = 015; +const unsigned VALUE_SIZE = 128; +const unsigned short BUFFER_SIZE = 128; + +unsigned long delay = 5000000UL; + +char message[MESSAGE_MAX_LEN+2]; + +volatile bool semaphore_input = false; +volatile bool semaphore_button = false; + +// This set of functions are callbacks -float kp = 0; -float ki = 0; +void on_rx() +{ + static char c; + static unsigned index = 0; + + c = serial.getc(); + message[index] = c; + index++; + message[index] = '\0'; + if ((END_OF_LINE == c) || (MESSAGE_MAX_LEN+1) == index) { + semaphore_input = true; + index = 0; + } +} -char* get_proportional(void) +void on_user_button() +{ + semaphore_button = !semaphore_button; + serial.printf("\r\n%s\r\nOk > ", (semaphore_button? "Paused": "Running")); +} + +char* get_delay(void) { static char value[VALUE_SIZE]; - sprintf(value, "%f", kp); + sprintf(value, "%lu", delay); return value; } -int set_proportional(char* value) -{ - sscanf(value, "%f", &kp); - return MYDSC_KVP_SUCCESS; -} - -char* get_integral(void) +int set_delay(char* value) { - static char value[VALUE_SIZE]; - - sprintf(value, "%f", ki); - return value; -} - -int set_integral(char* value) -{ - sscanf(value, "%f", &ki); - return MYDSC_KVP_SUCCESS; + sscanf(value, "%lu", &delay); + return MYDSC_SUCCESS; } int main() { // Setup - const unsigned short BUFFER_SIZE = 128; - static char command_buffer[BUFFER_SIZE]; - static char key_buffer[BUFFER_SIZE]; - static char value_buffer[BUFFER_SIZE]; + static char command_buffer[BUFFER_SIZE]; + static char key_buffer[BUFFER_SIZE]; + static char value_buffer[BUFFER_SIZE]; + long unsigned delay_count = delay; Serial serial(USBTX, USBRX); serial.baud(9600); - serial.format(8, SerialBase::None, 1); serial.printf("\r\nHello, world!"); + // Setting the key-value list up mydsc_kvp_t kvp; mydsc_kvp_init(&kvp); - mydsc_kvp_append(&kvp, "proportional", &get_proportional, - &set_proportional); - mydsc_kvp_append(&kvp, "integral", &get_integral, &set_integral); - serial.printf("\r\nVersion:\t%s", mydsc_kvp_get_value(&kvp, "version")); + + // Setting the callbacks up + serial.attach(&on_rx, Serial::RxIrq); + button.fall(&on_user_button); + mydsc_kvp_append(&kvp, "delay", &get_delay, &set_delay); + serial.printf("\r\nOK > "); + + led = 1; while(1) { // Loop int result = 0; char* value = ""; - serial.printf("\r\n> "); - - serial.scanf("%s",command_buffer); - if (strcmp(command_buffer, "set") == 0) { - serial.scanf("%s",key_buffer); - serial.scanf("%s",value_buffer); - result = mydsc_kvp_set_value(&kvp, key_buffer, value_buffer); - - if (result == MYDSC_KVP_SUCCESS) - serial.printf("\r\nOk"); - else if (result == MYDSC_KVP_NO_SET) - serial.printf("\r\nError: Value can't be changed"); - else if (result == MYDSC_KVP_NO_KEY) - serial.printf("\r\nError: There isn't such setting"); - else if (result == MYDSC_KVP_WRONG_VALUE) - serial.printf("\r\nError: The value is wrong"); + if (semaphore_input == true) { + semaphore_input = false; + strcpy(command_buffer, strtok(message," \015")); + if (strcmp(command_buffer, "set") == 0) { + strcpy(key_buffer, strtok(NULL," \015")); + strcpy(value_buffer, strtok(NULL, " \015")); + result = mydsc_kvp_set_value(&kvp, key_buffer, value_buffer); + if (result != MYDSC_SUCCESS) + serial.printf("\r\nError: %s", mydsc_error[result]); + } + else if (strcmp(command_buffer,"get") == 0) { + strcpy(key_buffer, strtok(NULL," \015")); + value = mydsc_kvp_get_value(&kvp,key_buffer); + if (NULL != value) + serial.printf("\r\n%s", value); + else + serial.printf("\r\nError: %s", mydsc_error[MYDSC_KVP_NO_KEY]); + } + else if (strcmp(command_buffer,"help") == 0) { + serial.printf("\r\nAvailable commands are: get help set"); + } else - serial.printf("\r\nError: Unknown error"); + serial.printf("\r\nError: %s doesn't exist\r\n", command_buffer); + serial.printf("\r\nOk > "); } - - else if (strcmp(command_buffer,"get") == 0) { - serial.scanf("%s",key_buffer); - value = mydsc_kvp_get_value(&kvp,key_buffer); - if (NULL != value) - serial.printf("\r\n%s", value); - else - serial.printf("\r\nError: There isn't such setting"); - serial.printf("\r\nOk"); + if (!delay_count) { + led = !led; + delay_count = delay; } - else if (strcmp(command_buffer,"help") == 0) { - serial.printf("\r\nAvailable commands are: get help set"); - serial.printf("\r\nOk"); - } - else - serial.printf("\r\nError: %s doesn't exist\r\n", command_buffer); + if (!semaphore_button) + delay_count--; } }
diff -r 584c4d2d10aa -r e315db7c4dea mydsc.lib --- a/mydsc.lib Mon Mar 09 15:56:50 2020 +0000 +++ b/mydsc.lib Sat Mar 28 16:09:30 2020 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/ghsalazar/code/mydsc/#b780db800303 +https://os.mbed.com/users/ghsalazar/code/mydsc/#90c3f1288d41