A simple wrapper for the Raw serial class to transmit double variables.
Dependents: EquatorStrutDigitalMonitor
Revision 0:ff597e1c059d, committed 2014-08-22
- Comitter:
- pyrostew
- Date:
- Fri Aug 22 06:37:08 2014 +0000
- Commit message:
- Lightweight library to simplify serial transmits using the RawSerial class. extracted code from EquatorStrutDigitalMonitorAlpesh.
Changed in this revision
LightWeightSerialTransmit.cpp | Show annotated file Show diff for this revision Revisions of this file |
LightWeightSerialTransmit.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r ff597e1c059d LightWeightSerialTransmit.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightWeightSerialTransmit.cpp Fri Aug 22 06:37:08 2014 +0000 @@ -0,0 +1,99 @@ +#include "LightWeightSerialTransmit.h" + +LWSerialTX::LWSerialTX(int baudRate) +{ + SerialConnection = new RawSerial(P1_27, P1_26); + (*SerialConnection).baud(baudRate); +} + +void LWSerialTX::NewFile() +{ + (*SerialConnection).putc(28); + (*SerialConnection).putc(10); + (*SerialConnection).putc(13); +} + +void LWSerialTX::NewLine() +{ + (*SerialConnection).putc(10); + (*SerialConnection).putc(13); +} + +void LWSerialTX::Delimiter(Delimiters delimiter) +{ + (*SerialConnection).putc(delimiter); +} + +void LWSerialTX::Transmit(double output) +{ + int outChar = 0; + + if (output < 0.0) + { + (*SerialConnection).putc('-'); + output *= -1.0; + } + if (output >= 1000.0) + { + outChar = output / 1000; + (*SerialConnection).putc(outChar + 48); + output -= outChar * 1000.0; + } + if (output >= 100.0) + { + outChar = output / 100; + (*SerialConnection).putc(outChar + 48); + output -= outChar * 100.0; + } + else if(outChar > 0) + { + (*SerialConnection).putc('0'); + } + if (output >= 10.0) + { + outChar = output / 10; + (*SerialConnection).putc(outChar + 48); + output -= outChar * 10.0; + } + else if(outChar > 0) + { + (*SerialConnection).putc('0'); + } + if (output >= 1.0) + { + outChar = output; + (*SerialConnection).putc(outChar + 48); + output -= outChar; + } + else + { + (*SerialConnection).putc('0'); + } + if (output >= 0.1) + { + (*SerialConnection).putc('.'); + outChar = output * 10; + (*SerialConnection).putc(outChar + 48); + output -= (double)outChar / 10.0; + } + else + { + (*SerialConnection).putc('.'); + (*SerialConnection).putc('0'); + } + if (output >= 0.01) + { + outChar = output * 100; + (*SerialConnection).putc(outChar + 48); + output -= (double)outChar / 100.0; + } + else + { + (*SerialConnection).putc('0'); + } + if (output >= 0.001) + { + outChar= output * 1000; + (*SerialConnection).putc(outChar + 48); + } +} \ No newline at end of file
diff -r 000000000000 -r ff597e1c059d LightWeightSerialTransmit.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightWeightSerialTransmit.h Fri Aug 22 06:37:08 2014 +0000 @@ -0,0 +1,28 @@ +#ifndef LIGHT_WEIGHT_SERIAL_TRANSMIT_H +#define LIGHT_WEIGHT_SERIAL_TRANSMIT_H + +#include "mbed.h" +#include "RawSerial.h" + +class LWSerialTX +{ +public: + LWSerialTX(int baudRate); + + enum Delimiters + { + Tab = 9, + Comma = 44, + Space = 32 + }; + + void Transmit(double output); + void Delimiter(Delimiters delimiter); + void NewLine(); + void NewFile(); + +private: + RawSerial* SerialConnection; +}; + +#endif \ No newline at end of file