Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX, USBRX); 00004 00005 static char serial_buffer[1024] = { 0 }; 00006 static uint32_t serial_ix = 0; 00007 static bool serial_line_ready = false; 00008 static bool button_pressed = false; 00009 static uint32_t button_count = 0; 00010 00011 static DigitalOut led(LED1); 00012 static InterruptIn btn(BUTTON1); 00013 00014 void rx_irq() { 00015 char c = pc.getc(); 00016 00017 if (c == '\r') { 00018 serial_line_ready = true; 00019 } 00020 else if (c == '\n') { 00021 return; 00022 } 00023 else { 00024 serial_buffer[serial_ix] = c; 00025 00026 serial_ix++; 00027 } 00028 00029 if (serial_ix >= 1023) { 00030 memset(serial_buffer, 0, 1024); 00031 serial_ix = 0; 00032 } 00033 } 00034 00035 void btn_irq() { 00036 button_pressed = true; 00037 button_count++; 00038 } 00039 00040 int main() { 00041 pc.baud(115200); 00042 pc.printf("!Hello from proprietary serial device #1\r\n"); 00043 00044 pc.attach(callback(&rx_irq)); 00045 btn.fall(callback(&btn_irq)); 00046 00047 while (1) { 00048 if (serial_line_ready) { 00049 if (strcmp(serial_buffer, "+LED1") == 0) { 00050 led = 1; 00051 printf(">%s\r\n!LED1 is now on\r\n", serial_buffer); 00052 } 00053 else if (strcmp(serial_buffer, "-LED1") == 0) { 00054 led = 0; 00055 printf(">%s\r\n!LED1 is now off\r\n", serial_buffer); 00056 } 00057 else { 00058 printf(">%s\r\n!Unknown command\r\n", serial_buffer); 00059 } 00060 00061 serial_line_ready = false; 00062 memset(serial_buffer, 0, 1024); 00063 serial_ix = 0; 00064 } 00065 00066 if (button_pressed) { 00067 printf("<BTN=%lu\r\n", button_count); 00068 button_pressed = false; 00069 } 00070 00071 wait_ms(1); 00072 } 00073 }
Generated on Fri Jul 15 2022 18:29:22 by
1.7.2