ELEC350 - Team Q / Mbed OS z_compare_commands_NEW_METHOD

Dependencies:   mbed-os

Fork of z_compare_commands by ELEC350 - Team Q

Committer:
eawhite
Date:
Fri Jan 05 17:47:00 2018 +0000
Revision:
2:690dd6bdb9a7
Parent:
1:81f52f1beb21
Child:
4:d03967db9991
new method of reading in PuTTY strings;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmith2 0:f46e16e62b0c 1 //http://www.cplusplus.com/reference/cstring/strcmp/
osmith2 0:f46e16e62b0c 2
osmith2 0:f46e16e62b0c 3 #include "mbed.h"
eawhite 2:690dd6bdb9a7 4 #include <iostream>
eawhite 2:690dd6bdb9a7 5 #include <string>
eawhite 2:690dd6bdb9a7 6 #include <stdio.h>
eawhite 2:690dd6bdb9a7 7 #include <ctype.h>
osmith2 0:f46e16e62b0c 8
eawhite 2:690dd6bdb9a7 9 //Digital outputs
eawhite 2:690dd6bdb9a7 10 DigitalOut onBoardLED(LED2);
eawhite 2:690dd6bdb9a7 11 DigitalOut redLED(PE_15);
eawhite 2:690dd6bdb9a7 12 DigitalOut yellowLED(PB_10);
eawhite 2:690dd6bdb9a7 13 DigitalOut greenLED(PB_11);
eawhite 2:690dd6bdb9a7 14
eawhite 2:690dd6bdb9a7 15 char rxBuffer[80];
eawhite 2:690dd6bdb9a7 16
eawhite 2:690dd6bdb9a7 17 char i = 0;
eawhite 2:690dd6bdb9a7 18 char c = 0;
eawhite 2:690dd6bdb9a7 19
eawhite 2:690dd6bdb9a7 20 string key = "apple";
osmith2 0:f46e16e62b0c 21
osmith2 0:f46e16e62b0c 22
eawhite 2:690dd6bdb9a7 23 RawSerial pc(USBTX, USBRX, 9600);
eawhite 2:690dd6bdb9a7 24 Thread serialRX(osPriorityNormal);
eawhite 2:690dd6bdb9a7 25
eawhite 2:690dd6bdb9a7 26 // Rx Interupt routine
eawhite 2:690dd6bdb9a7 27 void Rx_interrupt(){
eawhite 2:690dd6bdb9a7 28 pc.attach(NULL, Serial::RxIrq); // Disable Rx interrupt
eawhite 2:690dd6bdb9a7 29 serialRX.signal_set(0xA); // Set signal for Rx thread
eawhite 2:690dd6bdb9a7 30 }
osmith2 0:f46e16e62b0c 31
eawhite 2:690dd6bdb9a7 32 // Read received chars from UART
eawhite 2:690dd6bdb9a7 33 void rx_thread(){
eawhite 2:690dd6bdb9a7 34 while (true) {
eawhite 2:690dd6bdb9a7 35 Thread::signal_wait(0xA);
eawhite 2:690dd6bdb9a7 36 Thread::signal_clr(0xA);
eawhite 2:690dd6bdb9a7 37 //memset(rxBuffer, 0, sizeof(rxBuffer));
eawhite 2:690dd6bdb9a7 38 while (pc.readable()) {
eawhite 2:690dd6bdb9a7 39 c = pc.getc();
eawhite 2:690dd6bdb9a7 40 rxBuffer[i] = c;
eawhite 2:690dd6bdb9a7 41 i = i + 1;
eawhite 2:690dd6bdb9a7 42 yellowLED = !yellowLED;
eawhite 2:690dd6bdb9a7 43 }
eawhite 2:690dd6bdb9a7 44
eawhite 2:690dd6bdb9a7 45 pc.attach(&Rx_interrupt); // Enable Rx interrupt
eawhite 2:690dd6bdb9a7 46 redLED = !redLED;
eawhite 2:690dd6bdb9a7 47
eawhite 2:690dd6bdb9a7 48
eawhite 2:690dd6bdb9a7 49
eawhite 2:690dd6bdb9a7 50 if (c == '\r') {
eawhite 2:690dd6bdb9a7 51 greenLED = !greenLED;
eawhite 2:690dd6bdb9a7 52 string rxBufferSTR = rxBuffer;
eawhite 2:690dd6bdb9a7 53 int c = rxBufferSTR.length();
eawhite 2:690dd6bdb9a7 54 rxBuffer[c-1] = NULL; // CR character. Probably not needed
eawhite 2:690dd6bdb9a7 55 //string str(rxBuffer);
eawhite 2:690dd6bdb9a7 56 //printf("rxBuffer: %s\n", rxBuffer);
eawhite 2:690dd6bdb9a7 57 //printf("rxBuffer is %u characters long\n", (unsigned)strlen(rxBuffer));
eawhite 2:690dd6bdb9a7 58
eawhite 2:690dd6bdb9a7 59 if (rxBufferSTR.find(key) != string::npos) {
eawhite 2:690dd6bdb9a7 60 pc.printf("Found");
eawhite 2:690dd6bdb9a7 61 }
eawhite 2:690dd6bdb9a7 62 /*
eawhite 2:690dd6bdb9a7 63 if (strcmp (key,rxBuffer) == 0) {
eawhite 2:690dd6bdb9a7 64 puts ("Apple is delicious!\n");
eawhite 2:690dd6bdb9a7 65
eawhite 2:690dd6bdb9a7 66 pc.printf ("The command entered is %u characters long.\n",(unsigned)strlen(key));
eawhite 2:690dd6bdb9a7 67 int i =0;
eawhite 2:690dd6bdb9a7 68 int keysize = (unsigned)strlen(key);
eawhite 2:690dd6bdb9a7 69 int endfor = keysize;
eawhite 2:690dd6bdb9a7 70
eawhite 2:690dd6bdb9a7 71 //check for number here
eawhite 2:690dd6bdb9a7 72
eawhite 2:690dd6bdb9a7 73 for(i=0; i<10; i++) {
eawhite 2:690dd6bdb9a7 74 pc.printf("%c", rxBuffer[i]);
eawhite 2:690dd6bdb9a7 75 }
eawhite 2:690dd6bdb9a7 76
eawhite 2:690dd6bdb9a7 77 } else {
eawhite 2:690dd6bdb9a7 78 puts ("try again loser!\n");
eawhite 2:690dd6bdb9a7 79 }
eawhite 2:690dd6bdb9a7 80 */
eawhite 2:690dd6bdb9a7 81
eawhite 2:690dd6bdb9a7 82 memset(rxBuffer, 0, sizeof(rxBuffer)); //only reset after strcmp
eawhite 2:690dd6bdb9a7 83 i = 0;
eawhite 2:690dd6bdb9a7 84 }
eawhite 2:690dd6bdb9a7 85 }
eawhite 2:690dd6bdb9a7 86
eawhite 2:690dd6bdb9a7 87 }
osmith2 0:f46e16e62b0c 88
osmith2 0:f46e16e62b0c 89
osmith2 0:f46e16e62b0c 90 int main() {
eawhite 2:690dd6bdb9a7 91
eawhite 2:690dd6bdb9a7 92 serialRX.start(rx_thread); //start serial interrupt thread
eawhite 2:690dd6bdb9a7 93 pc.attach(&Rx_interrupt); //attach interrupt function that triggers when data is available
osmith2 0:f46e16e62b0c 94
osmith2 0:f46e16e62b0c 95 while(1) {
eawhite 2:690dd6bdb9a7 96 Thread::wait(2500);
osmith2 0:f46e16e62b0c 97 }
osmith2 0:f46e16e62b0c 98
osmith2 0:f46e16e62b0c 99 }