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
Diff: main.cpp
- Revision:
- 2:690dd6bdb9a7
- Parent:
- 1:81f52f1beb21
- Child:
- 4:d03967db9991
--- a/main.cpp Fri Jan 05 16:53:06 2018 +0000 +++ b/main.cpp Fri Jan 05 17:47:00 2018 +0000 @@ -1,58 +1,99 @@ //http://www.cplusplus.com/reference/cstring/strcmp/ #include "mbed.h" -#include <string> +#include <iostream> +#include <string> +#include <stdio.h> +#include <ctype.h> -Serial pc(USBTX, USBRX); +//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"; -DigitalOut myled(LED1); +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 +} -//THIS CODE DOESN'T WORK YET. -//This may help http://www.cplusplus.com/reference/string/string/compare/ +// 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() { - char key[] = "apple"; - char key2[] = "pear"; - char puttyBuffer[20]; - char numBuffer[20]; + + serialRX.start(rx_thread); //start serial interrupt thread + pc.attach(&Rx_interrupt); //attach interrupt function that triggers when data is available while(1) { - - myled = 1; // LED is ON - wait(1.0); // 200 ms - myled = 0; // LED is OFF - wait(1.0); // 1 sec - - //pc.printf("What is the password?\n"); - pc.printf ("Please choose a fruit?\n"); - fflush (stdout); - scanf ("%79s",puttyBuffer); - //scanf ("%79s",numBuffer); - - if (strcmp (key,puttyBuffer) == 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<endfor; i++) - { - pc.printf("%c", puttyBuffer[i]); - } - */ - } - else if (strcmp (key2,puttyBuffer) == 0){ - puts ("Eww, pear is icky.\n"); - } - else{ - puts ("try again loser!\n"); - } + Thread::wait(2500); } }