A simple wrapper for the Raw serial class to transmit double variables.
Dependents: EquatorStrutDigitalMonitor
LightWeightSerialTransmit.cpp@0:ff597e1c059d, 2014-08-22 (annotated)
- Committer:
- pyrostew
- Date:
- Fri Aug 22 06:37:08 2014 +0000
- Revision:
- 0:ff597e1c059d
Lightweight library to simplify serial transmits using the RawSerial class. extracted code from EquatorStrutDigitalMonitorAlpesh.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pyrostew | 0:ff597e1c059d | 1 | #include "LightWeightSerialTransmit.h" |
pyrostew | 0:ff597e1c059d | 2 | |
pyrostew | 0:ff597e1c059d | 3 | LWSerialTX::LWSerialTX(int baudRate) |
pyrostew | 0:ff597e1c059d | 4 | { |
pyrostew | 0:ff597e1c059d | 5 | SerialConnection = new RawSerial(P1_27, P1_26); |
pyrostew | 0:ff597e1c059d | 6 | (*SerialConnection).baud(baudRate); |
pyrostew | 0:ff597e1c059d | 7 | } |
pyrostew | 0:ff597e1c059d | 8 | |
pyrostew | 0:ff597e1c059d | 9 | void LWSerialTX::NewFile() |
pyrostew | 0:ff597e1c059d | 10 | { |
pyrostew | 0:ff597e1c059d | 11 | (*SerialConnection).putc(28); |
pyrostew | 0:ff597e1c059d | 12 | (*SerialConnection).putc(10); |
pyrostew | 0:ff597e1c059d | 13 | (*SerialConnection).putc(13); |
pyrostew | 0:ff597e1c059d | 14 | } |
pyrostew | 0:ff597e1c059d | 15 | |
pyrostew | 0:ff597e1c059d | 16 | void LWSerialTX::NewLine() |
pyrostew | 0:ff597e1c059d | 17 | { |
pyrostew | 0:ff597e1c059d | 18 | (*SerialConnection).putc(10); |
pyrostew | 0:ff597e1c059d | 19 | (*SerialConnection).putc(13); |
pyrostew | 0:ff597e1c059d | 20 | } |
pyrostew | 0:ff597e1c059d | 21 | |
pyrostew | 0:ff597e1c059d | 22 | void LWSerialTX::Delimiter(Delimiters delimiter) |
pyrostew | 0:ff597e1c059d | 23 | { |
pyrostew | 0:ff597e1c059d | 24 | (*SerialConnection).putc(delimiter); |
pyrostew | 0:ff597e1c059d | 25 | } |
pyrostew | 0:ff597e1c059d | 26 | |
pyrostew | 0:ff597e1c059d | 27 | void LWSerialTX::Transmit(double output) |
pyrostew | 0:ff597e1c059d | 28 | { |
pyrostew | 0:ff597e1c059d | 29 | int outChar = 0; |
pyrostew | 0:ff597e1c059d | 30 | |
pyrostew | 0:ff597e1c059d | 31 | if (output < 0.0) |
pyrostew | 0:ff597e1c059d | 32 | { |
pyrostew | 0:ff597e1c059d | 33 | (*SerialConnection).putc('-'); |
pyrostew | 0:ff597e1c059d | 34 | output *= -1.0; |
pyrostew | 0:ff597e1c059d | 35 | } |
pyrostew | 0:ff597e1c059d | 36 | if (output >= 1000.0) |
pyrostew | 0:ff597e1c059d | 37 | { |
pyrostew | 0:ff597e1c059d | 38 | outChar = output / 1000; |
pyrostew | 0:ff597e1c059d | 39 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 40 | output -= outChar * 1000.0; |
pyrostew | 0:ff597e1c059d | 41 | } |
pyrostew | 0:ff597e1c059d | 42 | if (output >= 100.0) |
pyrostew | 0:ff597e1c059d | 43 | { |
pyrostew | 0:ff597e1c059d | 44 | outChar = output / 100; |
pyrostew | 0:ff597e1c059d | 45 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 46 | output -= outChar * 100.0; |
pyrostew | 0:ff597e1c059d | 47 | } |
pyrostew | 0:ff597e1c059d | 48 | else if(outChar > 0) |
pyrostew | 0:ff597e1c059d | 49 | { |
pyrostew | 0:ff597e1c059d | 50 | (*SerialConnection).putc('0'); |
pyrostew | 0:ff597e1c059d | 51 | } |
pyrostew | 0:ff597e1c059d | 52 | if (output >= 10.0) |
pyrostew | 0:ff597e1c059d | 53 | { |
pyrostew | 0:ff597e1c059d | 54 | outChar = output / 10; |
pyrostew | 0:ff597e1c059d | 55 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 56 | output -= outChar * 10.0; |
pyrostew | 0:ff597e1c059d | 57 | } |
pyrostew | 0:ff597e1c059d | 58 | else if(outChar > 0) |
pyrostew | 0:ff597e1c059d | 59 | { |
pyrostew | 0:ff597e1c059d | 60 | (*SerialConnection).putc('0'); |
pyrostew | 0:ff597e1c059d | 61 | } |
pyrostew | 0:ff597e1c059d | 62 | if (output >= 1.0) |
pyrostew | 0:ff597e1c059d | 63 | { |
pyrostew | 0:ff597e1c059d | 64 | outChar = output; |
pyrostew | 0:ff597e1c059d | 65 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 66 | output -= outChar; |
pyrostew | 0:ff597e1c059d | 67 | } |
pyrostew | 0:ff597e1c059d | 68 | else |
pyrostew | 0:ff597e1c059d | 69 | { |
pyrostew | 0:ff597e1c059d | 70 | (*SerialConnection).putc('0'); |
pyrostew | 0:ff597e1c059d | 71 | } |
pyrostew | 0:ff597e1c059d | 72 | if (output >= 0.1) |
pyrostew | 0:ff597e1c059d | 73 | { |
pyrostew | 0:ff597e1c059d | 74 | (*SerialConnection).putc('.'); |
pyrostew | 0:ff597e1c059d | 75 | outChar = output * 10; |
pyrostew | 0:ff597e1c059d | 76 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 77 | output -= (double)outChar / 10.0; |
pyrostew | 0:ff597e1c059d | 78 | } |
pyrostew | 0:ff597e1c059d | 79 | else |
pyrostew | 0:ff597e1c059d | 80 | { |
pyrostew | 0:ff597e1c059d | 81 | (*SerialConnection).putc('.'); |
pyrostew | 0:ff597e1c059d | 82 | (*SerialConnection).putc('0'); |
pyrostew | 0:ff597e1c059d | 83 | } |
pyrostew | 0:ff597e1c059d | 84 | if (output >= 0.01) |
pyrostew | 0:ff597e1c059d | 85 | { |
pyrostew | 0:ff597e1c059d | 86 | outChar = output * 100; |
pyrostew | 0:ff597e1c059d | 87 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 88 | output -= (double)outChar / 100.0; |
pyrostew | 0:ff597e1c059d | 89 | } |
pyrostew | 0:ff597e1c059d | 90 | else |
pyrostew | 0:ff597e1c059d | 91 | { |
pyrostew | 0:ff597e1c059d | 92 | (*SerialConnection).putc('0'); |
pyrostew | 0:ff597e1c059d | 93 | } |
pyrostew | 0:ff597e1c059d | 94 | if (output >= 0.001) |
pyrostew | 0:ff597e1c059d | 95 | { |
pyrostew | 0:ff597e1c059d | 96 | outChar= output * 1000; |
pyrostew | 0:ff597e1c059d | 97 | (*SerialConnection).putc(outChar + 48); |
pyrostew | 0:ff597e1c059d | 98 | } |
pyrostew | 0:ff597e1c059d | 99 | } |