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.
ASerial.h
00001 // Aaron Birenboim, 26jul15 00002 00003 // Add some common Serial methods used in Arduino sketches 00004 // 00005 // Serial inherits from stream, for which I have no docuemntation. 00006 // examples seem to indicate that stream has (at least) printf 00007 // 00008 // I had trouble doing this with proper inheritance. 00009 // I gave up and just kept a refrence pointer 00010 00011 //#include "Serial.h" 00012 00013 #define EOLN "\r\n" 00014 00015 // singleton serial command byte buffer 00016 #define CMDBUFLEN 64 00017 int cbuf[CMDBUFLEN]; 00018 static int bufIn=0; 00019 static int bufOut=0; 00020 void gotChar() { 00021 int c = CmdSerial.getc(); 00022 cbuf[bufIn % CMDBUFLEN]=c; 00023 bufIn++; 00024 // prevent byte count overflow 00025 // WARNING: if there is a race and this thread resets counts... 00026 // counts could look funny for a bit, but since bufOut 00027 // decrements before bufIn, one hopes that the glitch will not be problematic. 00028 // indication of char in the buffer remains correce, and one hopes that by the next 00029 // time they check, both counts are accurate. 00030 if (bufIn > 401*CMDBUFLEN) { 00031 bufOut -= 400*CMDBUFLEN; 00032 bufIn -= 400*CMDBUFLEN; 00033 } 00034 } 00035 00036 00037 00038 class ASerial 00039 { 00040 public: 00041 Serial *_serial; 00042 ASerial(Serial &s) : _serial(&s) 00043 { 00044 //_serial->attach(&gotChar); 00045 } 00046 00047 void print(const char *s) { _serial->puts(s); } 00048 void print(const int i ) { _serial->printf("%d",i); } 00049 void print(const float f) { _serial->printf("%.3f",f); } 00050 00051 void println(const char *s) { print(s); _serial->puts(EOLN); } 00052 void println(const float f) { print(f); _serial->puts(EOLN); } 00053 00054 void baud(const int i) { _serial->baud(i); } 00055 00056 int cread() 00057 { 00058 /* didn't work. not sure why. something about this not being the pc serial? 00059 int n = _serial->readable(); 00060 if (n) 00061 { 00062 CmdSerial.printf(" %d readable\t",n); 00063 char c = _serial->getc(); 00064 CmdSerial.printf(" got '%c'(%d)\r\n",c,c); 00065 return((int)c); 00066 } 00067 return(-1); 00068 */ 00069 00070 if(bufOut < bufIn) { 00071 int c = cbuf[bufOut % CMDBUFLEN]; 00072 bufOut++; 00073 //DiagSerial.printf("\r\n\t%d got %d(%c)\r\n",bufOut-1,c,c); 00074 00075 00076 int n = bufIn-bufOut; 00077 if ((n > CMDBUFLEN-3) && (n < 800*CMDBUFLEN)) { 00078 DiagSerial.printf("\t\r\tCommand Buffer Overflow RESET!\r\n"); 00079 bufIn=bufOut=0; 00080 } 00081 return(c); 00082 } else { return(-1); } 00083 } 00084 };
Generated on Sat Jul 23 2022 07:33:03 by
