A serial parser for a specific project
Fork of Serial_HelloWorld_Mbed by
main.cpp
- Committer:
- philipgoosen
- Date:
- 2015-07-14
- Revision:
- 7:d25708589910
- Parent:
- 6:da101b92bc2d
File content as of revision 7:d25708589910:
#include "mbed.h" // Data parser by Philip Goosen /*#define TX USBTX #define RX USBRX*/ #define TX PA_2 #define RX PA_3 #define VERSION 1 Serial pc(TX, RX); // tx, rx DigitalOut led(LED1); using namespace std; int isDigit(char digit) { if (digit >= '0' or digit <= '9') { return 1; } else { return 0; } } int isLetter(char let) // Only works for upper case { if (let >= 'A' or let <= 'Z') { return 1; } else { return 0; } } int isAlpha(char c) { if( isDigit(c) or isLetter(c) ) { return 1; } else { return 0; } } int toDigit(char c) { return (c - '0'); } int main() { int motor = 0; int direction = 0; int speed = 0; led=1; wait(0.5); led=0; pc.baud(115200); pc.printf("Philip's program VERSION"); while(1) { //pc.putc(pc.getc() + 1); if (pc.getc() == '$' && pc.getc() == '#') { printf("\nRecieved input...\n"); /*led=1; wait(1); led=0; led=1; wait(1); led=0;*/ char c = pc.getc();// Gets a character from the computer that should be a digit from 0 - 9 //int count = toDigit(c); int x = 0; bool work = true; while (x < 3) { char cur = pc.getc(); char next = pc.getc(); // Add alpha checks to these if (cur == 'M' && x==0 && isDigit(next)) { motor = toDigit(next); } else if (cur == 'D' && x==1 && isDigit(next)) { direction = toDigit(next); } else if (cur == 'S' && x==2 && isDigit(next)) { char readSpeed[3] = {' ', ' ', '\0'}; readSpeed[0] = next; //readSpeed[2] = '\0'; cur = pc.getc(); if ( isDigit(cur) ) { readSpeed[1] = cur; } else { readSpeed[1] = '\0'; } speed = atoi(readSpeed); } else { work = false; printf("Invalid format"); } x++; } if (work) { //set values to servo library pc.printf("\nMotor %d Direction %d Speed %d", motor, speed, direction); } } } }