-
Notifications
You must be signed in to change notification settings - Fork 27
/
gps.c
219 lines (193 loc) · 8.18 KB
/
gps.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
#include "gps_uart.h"
#include "constants.h"
#include <furi.h>
#include <furi_hal_power.h>
#include <gui/gui.h>
#include <string.h>
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
static void render_callback(Canvas* const canvas, void* context) {
furi_assert(context);
GpsUart* gps_uart = context;
furi_mutex_acquire(gps_uart->mutex, FuriWaitForever);
if(!gps_uart->changing_baudrate) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 32, 8, AlignCenter, AlignBottom, "Latitude");
canvas_draw_str_aligned(canvas, 96, 8, AlignCenter, AlignBottom, "Longitude");
canvas_draw_str_aligned(canvas, 21, 30, AlignCenter, AlignBottom, "Course");
canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignBottom, "Speed");
canvas_draw_str_aligned(canvas, 107, 30, AlignCenter, AlignBottom, "Altitude");
canvas_draw_str_aligned(canvas, 32, 52, AlignCenter, AlignBottom, "Satellites");
canvas_draw_str_aligned(canvas, 96, 52, AlignCenter, AlignBottom, "Last Fix");
canvas_set_font(canvas, FontSecondary);
char buffer[64];
snprintf(buffer, 64, "%f", (double)gps_uart->status.latitude);
canvas_draw_str_aligned(canvas, 32, 18, AlignCenter, AlignBottom, buffer);
snprintf(buffer, 64, "%f", (double)gps_uart->status.longitude);
canvas_draw_str_aligned(canvas, 96, 18, AlignCenter, AlignBottom, buffer);
snprintf(buffer, 64, "%.1f", (double)gps_uart->status.course);
canvas_draw_str_aligned(canvas, 21, 40, AlignCenter, AlignBottom, buffer);
switch(gps_uart->speed_units) {
case KPH:
snprintf(buffer, 64, "%.2f kph", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
break;
case MPH:
snprintf(buffer, 64, "%.2f mph", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
break;
case KNOTS:
default:
snprintf(buffer, 64, "%.2f kn", (double)gps_uart->status.speed);
break;
}
canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignBottom, buffer);
snprintf(
buffer,
64,
"%.1f %c",
(double)gps_uart->status.altitude,
tolower(gps_uart->status.altitude_units));
canvas_draw_str_aligned(canvas, 107, 40, AlignCenter, AlignBottom, buffer);
snprintf(buffer, 64, "%d", gps_uart->status.satellites_tracked);
canvas_draw_str_aligned(canvas, 32, 62, AlignCenter, AlignBottom, buffer);
snprintf(
buffer,
64,
"%02d:%02d:%02d UTC",
gps_uart->status.time_hours,
gps_uart->status.time_minutes,
gps_uart->status.time_seconds);
canvas_draw_str_aligned(canvas, 96, 62, AlignCenter, AlignBottom, buffer);
} else {
char buffer[64];
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "Baudrate set to:");
snprintf(buffer, 64, "%ld baud", gps_uart->baudrate);
canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, buffer);
}
furi_mutex_release(gps_uart->mutex);
}
static void input_callback(InputEvent* input_event, void* context) {
furi_assert(context);
FuriMessageQueue* event_queue = context;
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
int32_t gps_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
bool otg_was_enabled = furi_hal_power_is_otg_enabled();
uint8_t attempts = 5;
while(--attempts > 0) {
if(furi_hal_power_enable_otg()) break;
}
if(attempts == 0) {
if(furi_hal_power_get_usb_voltage() < 4.5f) {
FURI_LOG_E(
"GPS",
"Error power otg enable. BQ2589 check otg fault = %d",
furi_hal_power_check_otg_fault() ? 1 : 0);
}
}
GpsUart* gps_uart = gps_uart_enable();
gps_uart->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!gps_uart->mutex) {
FURI_LOG_E("GPS", "cannot create mutex\r\n");
free(gps_uart);
return 255;
}
// set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_callback, gps_uart);
view_port_input_callback_set(view_port, input_callback, event_queue);
// open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
PluginEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
furi_mutex_acquire(gps_uart->mutex, FuriWaitForever);
if(event_status == FuriStatusOk) {
// press events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypeShort) {
switch(event.input.key) {
case InputKeyUp:
case InputKeyDown:
case InputKeyRight:
case InputKeyLeft:
case InputKeyBack:
break;
case InputKeyOk:
if(!gps_uart->backlight_on) {
notification_message_block(
gps_uart->notifications, &sequence_display_backlight_enforce_on);
gps_uart->backlight_on = true;
} else {
notification_message_block(
gps_uart->notifications, &sequence_display_backlight_enforce_auto);
notification_message(
gps_uart->notifications, &sequence_display_backlight_off);
gps_uart->backlight_on = false;
}
break;
default:
break;
}
} else if(event.input.type == InputTypeLong) {
switch(event.input.key) {
case InputKeyUp:
gps_uart_deinit_thread(gps_uart);
const int baudrate_length =
sizeof(gps_baudrates) / sizeof(gps_baudrates[0]);
current_gps_baudrate++;
if(current_gps_baudrate >= baudrate_length) {
current_gps_baudrate = 0;
}
gps_uart->baudrate = gps_baudrates[current_gps_baudrate];
gps_uart_init_thread(gps_uart);
gps_uart->changing_baudrate = true;
furi_mutex_release(gps_uart->mutex);
view_port_update(view_port);
break;
case InputKeyRight:
gps_uart->speed_units++;
if(gps_uart->speed_units == INVALID) {
gps_uart->speed_units = KNOTS;
}
break;
case InputKeyBack:
processing = false;
break;
default:
break;
}
}
}
}
if(!gps_uart->changing_baudrate) {
furi_mutex_release(gps_uart->mutex);
view_port_update(view_port);
} else {
furi_delay_ms(1000);
gps_uart->changing_baudrate = false;
}
}
notification_message_block(gps_uart->notifications, &sequence_display_backlight_enforce_auto);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_mutex_free(gps_uart->mutex);
gps_uart_disable(gps_uart);
if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
furi_hal_power_disable_otg();
}
return 0;
}