Basic tank-style drive robot control firmware for Freescale FRDM-K64F. Controls motors on a Dual-Full-H-Bridge with EN, like DBH-1x series, from Bluetooth serial commands
Diff: ASerial.h
- Revision:
- 2:54d27fdcbe5c
- Parent:
- 0:41ca27337c2b
- Child:
- 3:502f90649834
--- a/ASerial.h Fri Jul 31 17:37:52 2015 +0000 +++ b/ASerial.h Sun Aug 02 18:34:12 2015 +0000 @@ -4,35 +4,80 @@ // // Serial inherits from stream, for which I have no docuemntation. // examples seem to indicate that stream has (at least) printf +// +// I had trouble doing this with proper inheritance. +// I gave up and just kept a refrence pointer //#include "Serial.h" +#define EOLN "\r\n" + +// singleton serial command byte buffer +#define CMDBUFLEN 16 +int cbuf[CMDBUFLEN]; +static int bufIn=0; +static int bufOut=0; +void gotChar() { + int c = CmdSerial.getc(); + cbuf[bufIn % CMDBUFLEN]=c; + bufIn++; + // prevent byte count overflow + // WARNING: if there is a race and this thread resets counts... + // counts could look funny for a bit, but since bufOut + // decrements before bufIn, one hopes that the glitch will not be problematic. + // indication of char in the buffer remains correce, and one hopes that by the next + // time they check, both counts are accurate. + if (bufIn > 1001*CMDBUFLEN) { + bufOut -= 1000*CMDBUFLEN; + bufIn -= 1000*CMDBUFLEN; + } +} + + + class ASerial { public: Serial *_serial; ASerial(Serial &s) : _serial(&s) { - + //_serial->attach(&gotChar); } void print(const char *s) { _serial->puts(s); } void print(const int i ) { _serial->printf("%d",i); } void print(const float f) { _serial->printf("%.3f",f); } - void println(const char *s) { print(s); _serial->putc('\n'); } - void println(const float f) { print(f); _serial->putc('\n'); } + void println(const char *s) { print(s); _serial->puts(EOLN); } + void println(const float f) { print(f); _serial->puts(EOLN); } void baud(const int i) { _serial->baud(i); } - int readc() + int cread() { - if (_serial->readable()) +/* didn't work. not sure why. something about this not being the pc serial? + int n = _serial->readable(); + if (n) { +CmdSerial.printf(" %d readable\t",n); char c = _serial->getc(); +CmdSerial.printf(" got '%c'(%d)\r\n",c,c); return((int)c); } return(-1); +*/ + + if(bufOut < bufIn) { + int c = cbuf[bufOut % CMDBUFLEN]; + bufOut++; + _serial->printf("\r\n\t%d got %d(%c)\r\n",bufOut-1,c,c); + + + int n = bufIn-bufOut; + if ((n > CMDBUFLEN-3) && (n < 800*CMDBUFLEN)) { + _serial->printf("\t\r\tCommand Buffer Overflow Warning!\r\n"); + } + return(c); + } else { return(-1); } } }; - \ No newline at end of file