-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.c
354 lines (330 loc) · 11.5 KB
/
telemetry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
This project is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Deviation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Deviation. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "music.h"
#include "config/model.h"
#include "config/tx.h"
#include "telemetry.h"
static void _get_volt_str(char *str, u32 value);
static void _get_temp_str(char *str, int value);
#include "telemetry/telem_devo.c"
#include "telemetry/telem_dsm.c"
#include "telemetry/telem_frsky.c"
#define CAP_DSM 1
#define CAP_FRSKY 2
#define CAP_TYPEMASK 0x03
struct Telemetry Telemetry;
static u32 alarm_duration[TELEM_NUM_ALARMS] = {0, 0, 0, 0, 0, 0};
static u8 telem_idx = 0;
static u8 alarm = 0;
static u32 alarm_time = 0;
static u8 last_updated[TELEM_UPDATE_SIZE];
static u32 last_time;
#define CHECK_DURATION 500
#define MUSIC_INTERVAL 2000 // DON'T need to play music in every 100ms
void _get_volt_str(char *str, u32 value)
{
sprintf(str, "%d.%dV", (int)value /10, (int)value % 10);
}
void _get_temp_str(char *str, int value)
{
if (value == 0) {
strcpy(str, "----");
} else {
if (Transmitter.telem & TELEMUNIT_FAREN) {
sprintf(str, "%dF", ((int)value * 9 + 160)/ 5);
} else {
sprintf(str, "%dC", (int)value);
}
}
}
u32 TELEMETRY_IsUpdated(int val)
{
if (val == 0xff) {
for(int i = 0; i < TELEM_UPDATE_SIZE; i++) {
if (last_updated[i] | Telemetry.updated[i])
return 1;
}
return 0;
}
return (last_updated[val / 8] | Telemetry.updated[val / 8]) & (1 << val % 8);
}
s32 TELEMETRY_GetValue(int idx)
{
return _TELEMETRY_GetValue(&Telemetry, idx);
}
s32 _TELEMETRY_GetValue(struct Telemetry *t, int idx)
{
switch (idx) {
case TELEM_GPS_LONG:
return t->gps.longitude;
case TELEM_GPS_LAT:
return t->gps.latitude;
case TELEM_GPS_ALT:
return t->gps.altitude;
case TELEM_GPS_SPEED:
return t->gps.velocity;
case TELEM_GPS_TIME:
return t->gps.time;
}
if (TELEMETRY_Type() == TELEM_DEVO)
return _devo_value(t, idx);
if (TELEMETRY_Type() == TELEM_DSM)
return _dsm_value(t, idx);
return _frsky_value(t, idx);
}
const char * TELEMETRY_GetValueStrByValue(char *str, unsigned telem, s32 value)
{
int h, m, s, ss;
char letter = ' ';
int unit = 0; // rBE-OPT: Optimizing string usage, saves some bytes
switch(telem) {
case TELEM_GPS_LONG:
// allowed values: +/-180° = +/- 180*60*60*1000; W if value<0, E if value>=0; -180° = 180°
if (value < 0) {
letter = 'W';
value = -value;
} else
letter = 'E';
h = value / 1000 / 60 / 60;
m = (value - h * 1000 * 60 * 60) / 1000 / 60;
s = (value - h * 1000 * 60 * 60 - m * 1000 * 60) / 1000;
ss = value % 1000;
sprintf(str, "%c %3d° %02d' %02d.%03d\"", letter, h, m, s, ss);
break;
case TELEM_GPS_LAT:
// allowed values: +/-90° = +/- 90*60*60*1000; S if value<0, N if value>=0
if (value < 0) {
letter = 'S';
value = -value;
} else
letter = 'N';
h = value / 1000 / 60 / 60;
m = (value - h * 1000 * 60 * 60) / 1000 / 60;
s = (value - h * 1000 * 60 * 60 - m * 1000 * 60) / 1000;
ss = value % 1000;
sprintf(str, "%c %3d° %02d' %02d.%03d\"", letter, h, m, s, ss);
break;
case TELEM_GPS_ALT:
if (Transmitter.telem & TELEMUNIT_FEET) {
value = value * 328 / 100;
if (value < 0) {
letter = '-';
value =-value;
}
unit = 1;
}
sprintf(str, "%c%d.%03d%s", letter, (int)value / 1000, (int)value % 1000, unit ? "ft" : "m");
break;
case TELEM_GPS_SPEED:
if (Transmitter.telem & TELEMUNIT_FEET) {
value = value * 2237 / 1000;
unit = 1;
}
sprintf(str, "%d.%03d%s", (int)value / 1000, (int)value % 1000, unit ? "mph" : "m/s");
break;
case TELEM_GPS_TIME:
{
int year = 2000 + (((u32)Telemetry.gps.time >> 26) & 0x3F);
int month = ((u32)Telemetry.gps.time >> 22) & 0x0F;
int day = ((u32)Telemetry.gps.time >> 17) & 0x1F;
int hour = ((u32)Telemetry.gps.time >> 12) & 0x1F;
int min = ((u32)Telemetry.gps.time >> 6) & 0x3F;
int sec = ((u32)Telemetry.gps.time >> 0) & 0x3F;
sprintf(str, "%2d:%02d:%02d %4d-%02d-%02d",
hour, min, sec, year, month, day);
break;
}
default:
if (TELEMETRY_Type() == TELEM_DEVO)
return _devo_str_by_value(str, telem, value);
if (TELEMETRY_Type() == TELEM_DSM)
return _dsm_str_by_value(str, telem, value);
return _frsky_str_by_value(str, telem, value);
}
return str;
}
const char * TELEMETRY_GetValueStr(char *str, unsigned telem)
{
s32 value = TELEMETRY_GetValue(telem);
return TELEMETRY_GetValueStrByValue(str, telem, value);
}
const char * TELEMETRY_Name(char *str, unsigned telem)
{
if (TELEMETRY_Type() == TELEM_DEVO)
return _devo_name(str, telem);
if (TELEMETRY_Type() == TELEM_DSM)
return _dsm_name(str, telem);
return _frsky_name(str, telem);
}
const char * TELEMETRY_ShortName(char *str, unsigned telem)
{
switch(telem) {
case TELEM_GPS_LONG: strcpy(str, _tr("Longitude")); break;
case TELEM_GPS_LAT: strcpy(str, _tr("Latitude")); break;
case TELEM_GPS_ALT: strcpy(str, _tr("Altitude")); break;
case TELEM_GPS_SPEED: strcpy(str, _tr("Speed")); break;
case TELEM_GPS_TIME: strcpy(str, _tr("Time")); break;
default:
if (TELEMETRY_Type() == TELEM_DEVO)
return _devo_short_name(str, telem);
if (TELEMETRY_Type() == TELEM_DSM)
return _dsm_short_name(str, telem);
return _frsky_short_name(str, telem);
}
return str;
}
s32 TELEMETRY_GetMaxValue(unsigned telem)
{
if (TELEMETRY_Type() == TELEM_DEVO)
return _devo_get_max_value(telem);
if (TELEMETRY_Type() == TELEM_DSM)
return _dsm_get_max_value(telem);
return _frsky_get_max_value(telem);
}
void TELEMETRY_SetUpdated(int telem)
{
Telemetry.updated[telem/8] |= (1 << telem % 8);
}
int TELEMETRY_Type()
{
if (Telemetry.capabilities & CAP_DSM)
return TELEM_DSM;
if (Telemetry.capabilities & CAP_FRSKY)
return TELEM_FRSKY;
return TELEM_DEVO;
}
int TELEMETRY_GetNumTelemSrc()
{
if (TELEMETRY_Type() == TELEM_DEVO)
return NUM_DEVO_TELEM;
if (TELEMETRY_Type() == TELEM_DSM)
return NUM_DSM_TELEM;
return NUM_FRSKY_TELEM;
}
int TELEMETRY_GetNumTelem()
{
return TELEMETRY_Type() == TELEM_DEVO ? TELEM_DEVO_LAST-1 : TELEM_DSM_LAST-1;
}
void TELEMETRY_SetTypeByProtocol(enum Protocols protocol)
{
if (protocol == PROTOCOL_DSM2 || protocol == PROTOCOL_DSMX)
TELEMETRY_SetType(TELEM_DSM);
else if (protocol == PROTOCOL_FRSKY2WAY)
TELEMETRY_SetType(TELEM_FRSKY);
else
TELEMETRY_SetType(TELEM_DEVO);
}
void TELEMETRY_SetType(int type)
{
unsigned cap = Telemetry.capabilities & ~CAP_TYPEMASK;
if (type == TELEM_DSM)
cap |= CAP_DSM;
if (type == TELEM_FRSKY)
cap |= CAP_FRSKY;
Telemetry.capabilities = cap;
}
//#define DEBUG_TELEMALARM
void TELEMETRY_Alarm()
{
//Update 'updated' state every time we get here
u32 current_time = CLOCK_getms();
if (current_time - last_time > TELEM_ERROR_TIME) {
last_time = current_time;
for(int i = 0; i < TELEM_UPDATE_SIZE; i++) {
last_updated[i] = Telemetry.updated[i];
Telemetry.updated[i] = 0;
}
}
// don't need to check all the 6 telem-configs at one time, this is not a critical and urgent task
// instead, check 1 of them at a time
telem_idx = (telem_idx + 1) % TELEM_NUM_ALARMS;
if(! Model.telem_alarm[telem_idx]) {
alarm &= ~(1 << telem_idx); // clear this set
return;
}
unsigned idx = Model.telem_alarm[telem_idx];
s32 value = TELEMETRY_GetValue(idx);
if (value == 0) {
alarm &= ~(1 << telem_idx); // clear this set
return;
}
if (! TELEMETRY_IsUpdated(0xff)) {
// bug fix: do not alarm when no telem packet is received, it might caused by RX is powered off
alarm &= ~(1 << telem_idx); // clear this set
return;
}
if (Model.telem_flags & (1 << telem_idx)) {
if (! (alarm & (1 << telem_idx)) && (value <= Model.telem_alarm_val[telem_idx])) {
if (alarm_duration[telem_idx] == 0) {
alarm_duration[telem_idx] = current_time;
} else if (current_time - alarm_duration[telem_idx] > CHECK_DURATION) {
alarm_duration[telem_idx] = 0;
alarm |= 1 << telem_idx;
#ifdef DEBUG_TELEMALARM
printf("set: 0x%x\n\n", alarm);
#endif
}
} else if ((alarm & (1 << telem_idx)) && (value > (s32)Model.telem_alarm_val[telem_idx])) {
if (alarm_duration[telem_idx] == 0) {
alarm_duration[telem_idx] = current_time;
} else if (current_time - alarm_duration[telem_idx] > CHECK_DURATION) {
alarm_duration[telem_idx] = 0;
alarm &= ~(1 << telem_idx);
#ifdef DEBUG_TELEMALARM
printf("clear: 0x%x\n\n", alarm);
#endif
}
} else
alarm_duration[telem_idx] = 0;
} else {
if (! (alarm & (1 << telem_idx)) && (value >= Model.telem_alarm_val[telem_idx])) {
if (alarm_duration[telem_idx] == 0) {
alarm_duration[telem_idx] = current_time;
} else if (current_time - alarm_duration[telem_idx] > CHECK_DURATION) {
alarm_duration[telem_idx] = 0;
alarm |= 1 << telem_idx;
#ifdef DEBUG_TELEMALARM
printf("set: 0x%x\n\n", alarm);
#endif
}
} else if ((alarm & (1 << telem_idx)) && (value < (s32)Model.telem_alarm_val[telem_idx])) {
if (alarm_duration[telem_idx] == 0) {
alarm_duration[telem_idx] = current_time;
} else if (current_time - alarm_duration[telem_idx] > CHECK_DURATION) {
alarm_duration[telem_idx] = 0;
alarm &= ~(1 << telem_idx);
#ifdef DEBUG_TELEMALARM
printf("clear: 0x%x\n\n", alarm);
#endif
}
} else
alarm_duration[telem_idx] = 0;
}
if ((alarm & (1 << telem_idx))) {
if (current_time >= alarm_time + MUSIC_INTERVAL) {
alarm_time = current_time;
#ifdef DEBUG_TELEMALARM
printf("beep: %d\n\n", telem_idx);
#endif
MUSIC_Play(MUSIC_TELEMALARM1 + telem_idx);
}
}
}
int TELEMETRY_HasAlarm(int src)
{
for(int i = 0; i < TELEM_NUM_ALARMS; i++)
if(Model.telem_alarm[i] == src && (alarm & (1 << i)))
return 1;
return 0;
}