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.
Dependencies: mbed-os
Fork of z_compare_commands by
main.cpp
- Committer:
- eawhite
- Date:
- 2018-01-05
- Revision:
- 2:690dd6bdb9a7
- Parent:
- 1:81f52f1beb21
- Child:
- 4:d03967db9991
File content as of revision 2:690dd6bdb9a7:
//http://www.cplusplus.com/reference/cstring/strcmp/ #include "mbed.h" #include <iostream> #include <string> #include <stdio.h> #include <ctype.h> //Digital outputs DigitalOut onBoardLED(LED2); DigitalOut redLED(PE_15); DigitalOut yellowLED(PB_10); DigitalOut greenLED(PB_11); char rxBuffer[80]; char i = 0; char c = 0; string key = "apple"; RawSerial pc(USBTX, USBRX, 9600); Thread serialRX(osPriorityNormal); // Rx Interupt routine void Rx_interrupt(){ pc.attach(NULL, Serial::RxIrq); // Disable Rx interrupt serialRX.signal_set(0xA); // Set signal for Rx thread } // Read received chars from UART void rx_thread(){ while (true) { Thread::signal_wait(0xA); Thread::signal_clr(0xA); //memset(rxBuffer, 0, sizeof(rxBuffer)); while (pc.readable()) { c = pc.getc(); rxBuffer[i] = c; i = i + 1; yellowLED = !yellowLED; } pc.attach(&Rx_interrupt); // Enable Rx interrupt redLED = !redLED; if (c == '\r') { greenLED = !greenLED; string rxBufferSTR = rxBuffer; int c = rxBufferSTR.length(); rxBuffer[c-1] = NULL; // CR character. Probably not needed //string str(rxBuffer); //printf("rxBuffer: %s\n", rxBuffer); //printf("rxBuffer is %u characters long\n", (unsigned)strlen(rxBuffer)); if (rxBufferSTR.find(key) != string::npos) { pc.printf("Found"); } /* if (strcmp (key,rxBuffer) == 0) { puts ("Apple is delicious!\n"); pc.printf ("The command entered is %u characters long.\n",(unsigned)strlen(key)); int i =0; int keysize = (unsigned)strlen(key); int endfor = keysize; //check for number here for(i=0; i<10; i++) { pc.printf("%c", rxBuffer[i]); } } else { puts ("try again loser!\n"); } */ memset(rxBuffer, 0, sizeof(rxBuffer)); //only reset after strcmp i = 0; } } } int main() { serialRX.start(rx_thread); //start serial interrupt thread pc.attach(&Rx_interrupt); //attach interrupt function that triggers when data is available while(1) { Thread::wait(2500); } }