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.
Revision 1:44cb74be8b67, committed 2019-01-26
- Comitter:
- JamesStockton
- Date:
- Sat Jan 26 15:40:39 2019 +0000
- Parent:
- 0:279c73db549d
- Commit message:
- Added comments
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 279c73db549d -r 44cb74be8b67 main.cpp --- a/main.cpp Sat Jan 26 15:24:01 2019 +0000 +++ b/main.cpp Sat Jan 26 15:40:39 2019 +0000 @@ -1,20 +1,22 @@ #include "mbed.h" #include "USBSerial.h" + +//USB Serial / malloc functions void mallocRead(void); void serCallback(void); void dumpSerial(void); USBSerial ser; #define BUFLENG 16 -char *charbuf; -char *bufstart; +char *charbuf; //malloc pointer +char *bufstart; //malloc start bool bufOverun = false; int main() { - charbuf = (char *)malloc(BUFLENG); - bufstart = charbuf; - memset(bufstart, 'A', BUFLENG); + charbuf = (char *)malloc(BUFLENG); //sets up malloc + bufstart = charbuf; //sets pointer start variable + memset(bufstart, 'A', BUFLENG); //fills buffer with A /* for(int i=0; i<BUFLENG; i++){ *charbuf = (char)'A'; @@ -23,46 +25,47 @@ //charbuf = bufstart; wait_ms(1000); - ser.attach(serCallback); - ser.printf("Serial USB begin with malloc buffer \n\n"); + ser.attach(serCallback); //attaches USB Serial callback function + ser.printf("Serial USB begin with malloc buffer \n\n"); //startup message while(1) { - mallocRead(); + mallocRead(); //reads the malloc once every second wait_ms(1000); } } +//serial void serCallback(void) { - int bufin = ser.available(); - if(bufin <= (BUFLENG - (charbuf - bufstart))) { - for(int i=0; i<bufin; i++){ - *charbuf = (char)ser._getc(); - charbuf++; + int bufin = ser.available(); //gets the length of the serial to read + if(bufin <= (BUFLENG - (charbuf - bufstart))) { //checks to see if the buffer is long enough + for(int i=0; i<bufin; i++){ //collects each char to read + *charbuf = (char)ser._getc(); //adds char to malloc + charbuf++; //increments pointer } } else { - dumpSerial(); + dumpSerial(); //if the malloc is not big enough the serial data will be dumped } } void mallocRead(void) { - for(int i=0; i<BUFLENG; i++){ + for(int i=0; i<BUFLENG; i++){ //increments through the whole buffer, printing the chars ser.printf("%c", bufstart[i]); } if(bufOverun) { - ser.printf("\tDATA LOST buffer over ran"); - bufOverun = false; + ser.printf("\tDATA LOST buffer over ran"); //lets the user know that the buffer was exceded + bufOverun = false; //resets buffer over run boolean } ser.printf("\n"); - charbuf = bufstart; + charbuf = bufstart; //sets the pointer back to the start of the buffer } void dumpSerial(void) { while(ser.readable()){ - ser._getc(); + ser._getc(); //dumps the buffer one char at a time while the buffer is full } bufOverun = true; }