Flying Silicon Sensor
ptvBleInterface.cpp
Go to the documentation of this file.
1
23#include "ptvBleInterface.h"
24
25#include <Arduino.h> // Added by Arduino in build process
26
27#include "debug.h"
28#include "bleInterface.h"
29
31#define SEND_VOLT 0
32
34#define CHUNK_SZ BLE_ATT_LEN
35
37#define CHUNK_DELAY_MS 0
38
39
46
47 poll();
48
49 if (command.length() > 0) {
50 cmd = command;
51 command = "";
52 return 1;
53 }
54 return 0;
55}
56
63
64 // bytewise read the BLE UART Adafruit FIFO,
65 // which is hard configured with item size 1
66 uint8_t ch = '\0';
67 while (bleuart.read(& ch, 1)) {
68 if (ch == '\n') {
69 // command completed, save it and clear buffer
71 clrBuffer();
72 break;
73 }
74 else if (ch != '\r' && ch != '\0') {
75 if (pos < sizeof(rcvBuffer)) {
76 // concatenate as long as buffer is not full
77 rcvBuffer[pos] = ch;
78 pos++;
79 }
80 else {
81 // buffer full, still no line end found
82 // forcefully complete / split
84 clrBuffer();
85 break;
86 }
87 }
88 }
89 if (! isConnected()) {
90 command = "";
91 clrBuffer();
92 }
93}
94
99void PtvBleInterface::sendChunked(String & msg) {
100
101 // send data in multiple chunks as required for old BLE versions
102 // after each chunk a small delay is needed to avoid overflow of the FIFO buffer
103
104 // append checksum
105 byte cksum = 0;
106 for (unsigned int i = 1; i < msg.length(); i++) // skip the dollar sign
107 cksum ^= (byte) msg[i]; // bitwise exclusive OR
108 char nmea[8 + 1];
109 sprintf(nmea, "%02x", cksum);
110
111 msg += "*";
112 msg += nmea;
113 // use LOGL macro without the \n to get proper terminal output
114 LOGL(msg)
115 msg += "\n";
116
117 unsigned int msglen = msg.length();
118 unsigned int chunks = msglen / CHUNK_SZ;
119 unsigned int remain = msglen - (chunks * CHUNK_SZ);
120
121 // write the complete chunks
122 String part;
123 int p = 0;
124 for (unsigned int i = 0; i < chunks; i++) {
125 part = msg.substring(p, p + CHUNK_SZ);
126 p += CHUNK_SZ;
127
128 sendData(part.c_str(), part.length());
129 delay(CHUNK_DELAY_MS);
130 }
131
132 // write the remainder
133 if (remain > 0) {
134 part = msg.substring(p, p + remain);
135
136 sendData(part.c_str(), part.length());
137 delay(CHUNK_DELAY_MS);
138 }
139}
140
147void PtvBleInterface::sendMetaMsg(const String &mna, const String &mmo, const String &msn) {
148
149 String msg = "$PTVSOAR";
150 msg += ",MNA," + mna;
151 msg += ",MMO," + mmo;
152 msg += ",MSN," + msn;
153
154 sendChunked(msg);
155}
156
164 // $PTVSOAR,<type>,<value>,<type>,<value>,...*<checksum>\n
165 // 11111111111111111111222222222222222222223333333333333333333344444444444444444444555555555555555555556666666666 chunk
166 // 0 0 0 0 0 0 0 0 0 0 1 byte index
167 // 0 1 2 3 4 5 6 7 8 9 0 byte index
168 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 byte index
169 // $PTVSOAR,MNA,Flying Silicon,MMO,FS-SP4,MSN,23-001,PIT,-00.02,PRS,0987.654,OAT,+24.7,OAH,058,PCT,100,CHG,1*FF\n
170 // $PTVSOAR,PIT,-00.02,PRS,0987.654,OAT,+24.7,OAH,058,PCT,100,CHG,1*FF\n
171
172 String msg;
173
174 msg = "$PTVSOAR";
175 // optionally the full meta information can be sent
176 // msg = META_HEADER;
177
178 char buf[20];
179
180 msg += ",PIT,";
181 sprintf(buf, "%06.2f", data.diffPa); // Pitot Differential Pressure 999.99
182 msg += buf;
183
184 msg += ",PRS,";
185 sprintf(buf, "%08.3f", data.absHpa); // Atmospheric Pressure 9999.999
186 msg += buf;
187
188 msg += ",OAT,";
189 sprintf(buf, "%+05.1f", data.tempC); // Air Temperature -99.0 .. +99.0
190 msg += buf;
191
192 msg += ",OAH,";
193 sprintf(buf, "%03.0f", data.humPct); // Air Relative Humidity 000 - 100
194 msg += buf;
195
196 msg += ",PCT,";
197 sprintf(buf, "%03.0f", data.batPct); // Battery State of Charge % 000 - 100
198 msg += buf;
199
200 msg += ",CHG,";
201 sprintf(buf, "%d", data.isCharging); // Battery Charging Flag
202 msg += buf;
203
204 if (SEND_VOLT) {
205 msg += ",VOL,";
206 sprintf(buf, "%4.2f", data.batVolt);
207 msg += buf;
208 }
209
210 // append checksum
211 byte cksum = 0;
212 for (unsigned int i = 1; i < msg.length(); i++) // skip the dollar sign
213 cksum ^= (byte) msg.charAt(i); // bitwise exclusive OR
214 char nmea[8 + 1];
215 sprintf(nmea, "%02x", cksum);
216
217 msg += "*";
218 msg += nmea;
219
220 // use LOGL macro without the \n to get proper terminal output
221 LOGL(msg)
222
223 msg += "\n";
224 sendChunked(msg);
225}
226
234 // $PTV,<pitotPa>,<baroHpa>,<tempC>,<humidPct>,<battPct>,<battChg>*<checksum>\n
235 // 1111111111111111111122222222222222222222 chunk
236 // 0 1 2 3 byte index
237 // 0123456789012345678901234567890123456789 byte index
238 // $PTV,000.00,0987.105,+20.5,058,100,1*FF\n
239
240 String msg = "$PTV";
241
242 char buf[100]; // enough
243 sprintf(buf,
244 ",%06.2f"
245 ",%08.3f"
246 ",%+05.1f"
247 ",%03.0f"
248 ",%03.0f"
249 ",%d"
250 , data.diffPa // Pitot Differential Pressure 999.99
251 , data.absHpa // Atmospheric Pressure 9999.999
252 , data.tempC // Air Temperature -99.9 .. +99.9
253 , data.humPct // Air Relative Humidity 000 - 100
254 , data.batPct // Battery State of Charge % 000 - 100
255 , data.isCharging // Battery Charging Flag
256 );
257 msg += buf;
258
259 if (SEND_VOLT) {
260 sprintf(buf, ",%4.2f", data.batVolt);
261 msg += buf;
262 }
263
264 sendChunked(msg);
265}
266
Provides data transmission over BLE (encapsulating BLE details)
int isConnected(void)
void sendData(const char *buf, int len)
FsUart bleuart
Customized UART service.
void clrBuffer()
Clear internal receive buffer.
String command
Holds last received text line / command.
void sendMetaMsg(const String &mna, const String &mmo, const String &msn)
void sendShortMsg(const SensorData &data)
char rcvBuffer[FIFO_SZ]
Receive data buffer, same size as UART FIFO.
void sendChunked(String &msg)
void sendLongMsg(const SensorData &data)
int getReceived(String &cmd)
unsigned int pos
Write position for next byte.
Debug log macros.
#define LOGL(s)
Debug log inactive.
Definition debug.h:39
#define CHUNK_SZ
Size of data chunks (bytes) sent via BLE interface.
#define SEND_VOLT
Optionally append battery voltage to BLE messages.
#define CHUNK_DELAY_MS
Time ms to wait after sending one chunk.
Extension of BleInterface providing the PTV / PTVSOAR protocol formats.
Sensor data structure.
int isCharging
Charging state.
float batPct
Battery charge %.
float batVolt
Battery Voltage.
float diffPa
SDP31 differential pressure Pa.
float humPct
SHT humidity %.
float tempC
SHT temperature °C.
float absHpa
DPS310 barometric pressure hPa.