This commit is contained in:
RogueMaster
2022-10-28 01:05:39 -04:00
parent 0632548ea5
commit d6a0464d16
8 changed files with 753 additions and 808 deletions

View File

@@ -4,23 +4,19 @@
#include <gui/gui.h>
#include <string.h>
typedef enum
{
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct
{
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
static void render_callback(Canvas* const canvas, void* context)
{
static void render_callback(Canvas* const canvas, void* context) {
const GpsUart* gps_uart = acquire_mutex((ValueMutex*)context, 25);
if (gps_uart == NULL)
{
if(gps_uart == NULL) {
return;
}
@@ -43,27 +39,35 @@ static void render_callback(Canvas* const canvas, void* context)
canvas_draw_str_aligned(canvas, 21, 40, AlignCenter, AlignBottom, buffer);
snprintf(buffer, 64, "%.2f kn", (double)gps_uart->status.speed);
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));
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,
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);
release_mutex((ValueMutex*)context, gps_uart);
}
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue)
{
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
int32_t gps_app(void* p)
{
int32_t gps_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
@@ -71,8 +75,7 @@ int32_t gps_app(void* p)
GpsUart* gps_uart = gps_uart_enable();
ValueMutex gps_uart_mutex;
if (!init_mutex(&gps_uart_mutex, gps_uart, sizeof(GpsUart)))
{
if(!init_mutex(&gps_uart_mutex, gps_uart, sizeof(GpsUart))) {
FURI_LOG_E("GPS", "cannot create mutex\r\n");
free(gps_uart);
return 255;
@@ -88,21 +91,16 @@ int32_t gps_app(void* p)
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
PluginEvent event;
for (bool processing = true; processing;)
{
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
GpsUart* gps_uart = (GpsUart*)acquire_mutex_block(&gps_uart_mutex);
if (event_status == FuriStatusOk)
{
if(event_status == FuriStatusOk) {
// press events
if (event.type == EventTypeKey)
{
if (event.input.type == InputTypePress)
{
switch (event.input.key)
{
if(event.type == EventTypeKey) {
if(event.input.type == InputTypePress) {
switch(event.input.key) {
case InputKeyUp:
case InputKeyDown:
case InputKeyRight:
@@ -115,9 +113,7 @@ int32_t gps_app(void* p)
}
}
}
}
else
{
} else {
FURI_LOG_D("GPS", "FuriMessageQueue: event timeout");
}

View File

@@ -3,48 +3,39 @@
#include "minmea.h"
#include "gps_uart.h"
typedef enum
{
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtRxDone = (1 << 1),
} WorkerEvtFlags;
#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context)
{
static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
GpsUart* gps_uart = (GpsUart*)context;
if (ev == UartIrqEventRXNE)
{
if(ev == UartIrqEventRXNE) {
furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
}
}
static void gps_uart_serial_init(GpsUart* gps_uart)
{
static void gps_uart_serial_init(GpsUart* gps_uart) {
furi_hal_console_disable();
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
furi_hal_uart_set_br(FuriHalUartIdUSART1, GPS_BAUDRATE);
}
static void gps_uart_serial_deinit(GpsUart* gps_uart)
{
static void gps_uart_serial_deinit(GpsUart* gps_uart) {
UNUSED(gps_uart);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
furi_hal_console_enable();
}
static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line)
{
switch (minmea_sentence_id(line, false))
{
case MINMEA_SENTENCE_RMC:
{
static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
switch(minmea_sentence_id(line, false)) {
case MINMEA_SENTENCE_RMC: {
struct minmea_sentence_rmc frame;
if (minmea_parse_rmc(&frame, line))
{
if(minmea_parse_rmc(&frame, line)) {
gps_uart->status.valid = frame.valid;
gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
@@ -58,11 +49,9 @@ static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line)
}
} break;
case MINMEA_SENTENCE_GGA:
{
case MINMEA_SENTENCE_GGA: {
struct minmea_sentence_gga frame;
if (minmea_parse_gga(&frame, line))
{
if(minmea_parse_gga(&frame, line)) {
gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
gps_uart->status.altitude = minmea_tofloat(&frame.altitude);
@@ -82,8 +71,7 @@ static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line)
}
}
static int32_t gps_uart_worker(void* context)
{
static int32_t gps_uart_worker(void* context) {
GpsUart* gps_uart = (GpsUart*)context;
gps_uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
@@ -91,51 +79,43 @@ static int32_t gps_uart_worker(void* context)
gps_uart_serial_init(gps_uart);
while (1)
{
while(1) {
uint32_t events =
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
furi_check((events & FuriFlagError) == 0);
if (events & WorkerEvtStop)
{
if(events & WorkerEvtStop) {
break;
}
if (events & WorkerEvtRxDone)
{
if(events & WorkerEvtRxDone) {
size_t len = 0;
do
{
len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset,
do {
len = furi_stream_buffer_receive(
gps_uart->rx_stream,
gps_uart->rx_buf + rx_offset,
RX_BUF_SIZE - 1 - rx_offset,
0);
if (len > 0)
{
if(len > 0) {
rx_offset += len;
gps_uart->rx_buf[rx_offset] = '\0';
char * line_current = (char *)gps_uart->rx_buf;
while (1)
{
while (*line_current == '\0' && line_current < (char *)gps_uart->rx_buf + rx_offset - 1)
{
char* line_current = (char*)gps_uart->rx_buf;
while(1) {
while(*line_current == '\0' &&
line_current < (char*)gps_uart->rx_buf + rx_offset - 1) {
line_current++;
}
char * newline = strchr(line_current, '\n');
if (newline)
{
char* newline = strchr(line_current, '\n');
if(newline) {
*newline = '\0';
gps_uart_parse_nmea(gps_uart, line_current);
line_current = newline + 1;
}
else
{
if (line_current > (char *)gps_uart->rx_buf)
{
} else {
if(line_current > (char*)gps_uart->rx_buf) {
rx_offset = 0;
while (*line_current)
{
while(*line_current) {
gps_uart->rx_buf[rx_offset++] = *(line_current++);
}
}
@@ -143,8 +123,7 @@ static int32_t gps_uart_worker(void* context)
}
}
}
}
while (len > 0);
} while(len > 0);
}
}
@@ -154,8 +133,7 @@ static int32_t gps_uart_worker(void* context)
return 0;
}
GpsUart* gps_uart_enable()
{
GpsUart* gps_uart_enable() {
GpsUart* gps_uart = malloc(sizeof(GpsUart));
gps_uart->status.valid = false;
@@ -183,8 +161,7 @@ GpsUart* gps_uart_enable()
return gps_uart;
}
void gps_uart_disable(GpsUart* gps_uart)
{
void gps_uart_disable(GpsUart* gps_uart) {
furi_assert(gps_uart);
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtStop);
furi_thread_join(gps_uart->thread);

View File

@@ -6,8 +6,7 @@
#define GPS_BAUDRATE 9600
#define RX_BUF_SIZE 1024
typedef struct
{
typedef struct {
bool valid;
float latitude;
float longitude;
@@ -22,8 +21,7 @@ typedef struct
int time_seconds;
} GpsStatus;
typedef struct
{
typedef struct {
FuriThread* thread;
FuriStreamBuffer* rx_stream;
uint8_t rx_buf[RX_BUF_SIZE];

View File

@@ -14,131 +14,115 @@
#define boolstr(s) ((s) ? "true" : "false")
static int hex2int(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
static int hex2int(char c) {
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
uint8_t minmea_checksum(const char *sentence)
{
uint8_t minmea_checksum(const char* sentence) {
// Support senteces with or without the starting dollar sign.
if (*sentence == '$')
sentence++;
if(*sentence == '$') sentence++;
uint8_t checksum = 0x00;
// The optional checksum is an XOR of all bytes between "$" and "*".
while (*sentence && *sentence != '*')
checksum ^= *sentence++;
while(*sentence && *sentence != '*') checksum ^= *sentence++;
return checksum;
}
bool minmea_check(const char *sentence, bool strict)
{
bool minmea_check(const char* sentence, bool strict) {
uint8_t checksum = 0x00;
// A valid sentence starts with "$".
if (*sentence++ != '$')
return false;
if(*sentence++ != '$') return false;
// The optional checksum is an XOR of all bytes between "$" and "*".
while (*sentence && *sentence != '*' && isprint((unsigned char) *sentence))
while(*sentence && *sentence != '*' && isprint((unsigned char)*sentence))
checksum ^= *sentence++;
// If checksum is present...
if (*sentence == '*') {
if(*sentence == '*') {
// Extract checksum.
sentence++;
int upper = hex2int(*sentence++);
if (upper == -1)
return false;
if(upper == -1) return false;
int lower = hex2int(*sentence++);
if (lower == -1)
return false;
if(lower == -1) return false;
int expected = upper << 4 | lower;
// Check for checksum mismatch.
if (checksum != expected)
return false;
} else if (strict) {
if(checksum != expected) return false;
} else if(strict) {
// Discard non-checksummed frames in strict mode.
return false;
}
// The only stuff allowed at this point is a newline.
while (*sentence == '\r' || *sentence == '\n') {
while(*sentence == '\r' || *sentence == '\n') {
sentence++;
}
if (*sentence) {
if(*sentence) {
return false;
}
return true;
}
bool minmea_scan(const char *sentence, const char *format, ...)
{
bool minmea_scan(const char* sentence, const char* format, ...) {
bool result = false;
bool optional = false;
if (sentence == NULL)
return false;
if(sentence == NULL) return false;
va_list ap;
va_start(ap, format);
const char *field = sentence;
const char* field = sentence;
#define next_field() \
do { \
/* Progress to the next field. */ \
while (minmea_isfield(*sentence)) \
sentence++; \
while(minmea_isfield(*sentence)) sentence++; \
/* Make sure there is a field there. */ \
if (*sentence == ',') { \
if(*sentence == ',') { \
sentence++; \
field = sentence; \
} else { \
field = NULL; \
} \
} while (0)
} while(0)
while (*format) {
while(*format) {
char type = *format++;
if (type == ';') {
if(type == ';') {
// All further fields are optional.
optional = true;
continue;
}
if (!field && !optional) {
if(!field && !optional) {
// Field requested but we ran out if input. Bail out.
goto parse_error;
}
switch (type) {
switch(type) {
case 'c': { // Single character field (char).
char value = '\0';
if (field && minmea_isfield(*field))
value = *field;
if(field && minmea_isfield(*field)) value = *field;
*va_arg(ap, char *) = value;
*va_arg(ap, char*) = value;
} break;
case 'd': { // Single character direction field (int).
int value = 0;
if (field && minmea_isfield(*field)) {
switch (*field) {
if(field && minmea_isfield(*field)) {
switch(*field) {
case 'N':
case 'E':
value = 1;
@@ -152,7 +136,7 @@ bool minmea_scan(const char *sentence, const char *format, ...)
}
}
*va_arg(ap, int *) = value;
*va_arg(ap, int*) = value;
} break;
case 'f': { // Fractional value with scale (struct minmea_float).
@@ -160,19 +144,18 @@ bool minmea_scan(const char *sentence, const char *format, ...)
int_least32_t value = -1;
int_least32_t scale = 0;
if (field) {
while (minmea_isfield(*field)) {
if (*field == '+' && !sign && value == -1) {
if(field) {
while(minmea_isfield(*field)) {
if(*field == '+' && !sign && value == -1) {
sign = 1;
} else if (*field == '-' && !sign && value == -1) {
} else if(*field == '-' && !sign && value == -1) {
sign = -1;
} else if (isdigit((unsigned char) *field)) {
} else if(isdigit((unsigned char)*field)) {
int digit = *field - '0';
if (value == -1)
value = 0;
if (value > (INT_LEAST32_MAX-digit) / 10) {
if(value == -1) value = 0;
if(value > (INT_LEAST32_MAX - digit) / 10) {
/* we ran out of bits, what do we do? */
if (scale) {
if(scale) {
/* truncate extra precision */
break;
} else {
@@ -181,15 +164,13 @@ bool minmea_scan(const char *sentence, const char *format, ...)
}
}
value = (10 * value) + digit;
if (scale)
scale *= 10;
} else if (*field == '.' && scale == 0) {
if(scale) scale *= 10;
} else if(*field == '.' && scale == 0) {
scale = 1;
} else if (*field == ' ') {
} else if(*field == ' ') {
/* Allow spaces at the start of the field. Not NMEA
* conformant, but some modules do this. */
if (sign != 0 || value != -1 || scale != 0)
goto parse_error;
if(sign != 0 || value != -1 || scale != 0) goto parse_error;
} else {
goto parse_error;
}
@@ -197,42 +178,38 @@ bool minmea_scan(const char *sentence, const char *format, ...)
}
}
if ((sign || scale) && value == -1)
goto parse_error;
if((sign || scale) && value == -1) goto parse_error;
if (value == -1) {
if(value == -1) {
/* No digits were scanned. */
value = 0;
scale = 0;
} else if (scale == 0) {
} else if(scale == 0) {
/* No decimal point. */
scale = 1;
}
if (sign)
value *= sign;
if(sign) value *= sign;
*va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale};
*va_arg(ap, struct minmea_float*) = (struct minmea_float){value, scale};
} break;
case 'i': { // Integer value, default 0 (int).
int value = 0;
if (field) {
char *endptr;
if(field) {
char* endptr;
value = strtol(field, &endptr, 10);
if (minmea_isfield(*endptr))
goto parse_error;
if(minmea_isfield(*endptr)) goto parse_error;
}
*va_arg(ap, int *) = value;
*va_arg(ap, int*) = value;
} break;
case 's': { // String value (char *).
char *buf = va_arg(ap, char *);
char* buf = va_arg(ap, char*);
if (field) {
while (minmea_isfield(*field))
*buf++ = *field++;
if(field) {
while(minmea_isfield(*field)) *buf++ = *field++;
}
*buf = '\0';
@@ -240,30 +217,26 @@ bool minmea_scan(const char *sentence, const char *format, ...)
case 't': { // NMEA talker+sentence identifier (char *).
// This field is always mandatory.
if (!field)
goto parse_error;
if(!field) goto parse_error;
if (field[0] != '$')
goto parse_error;
for (int f=0; f<5; f++)
if (!minmea_isfield(field[1+f]))
goto parse_error;
if(field[0] != '$') goto parse_error;
for(int f = 0; f < 5; f++)
if(!minmea_isfield(field[1 + f])) goto parse_error;
char *buf = va_arg(ap, char *);
memcpy(buf, field+1, 5);
char* buf = va_arg(ap, char*);
memcpy(buf, field + 1, 5);
buf[5] = '\0';
} break;
case 'D': { // Date (int, int, int), -1 if empty.
struct minmea_date *date = va_arg(ap, struct minmea_date *);
struct minmea_date* date = va_arg(ap, struct minmea_date*);
int d = -1, m = -1, y = -1;
if (field && minmea_isfield(*field)) {
if(field && minmea_isfield(*field)) {
// Always six digits.
for (int f=0; f<6; f++)
if (!isdigit((unsigned char) field[f]))
goto parse_error;
for(int f = 0; f < 6; f++)
if(!isdigit((unsigned char)field[f])) goto parse_error;
char dArr[] = {field[0], field[1], '\0'};
char mArr[] = {field[2], field[3], '\0'};
@@ -279,15 +252,14 @@ bool minmea_scan(const char *sentence, const char *format, ...)
} break;
case 'T': { // Time (int, int, int, int), -1 if empty.
struct minmea_time *time_ = va_arg(ap, struct minmea_time *);
struct minmea_time* time_ = va_arg(ap, struct minmea_time*);
int h = -1, i = -1, s = -1, u = -1;
if (field && minmea_isfield(*field)) {
if(field && minmea_isfield(*field)) {
// Minimum required: integer time.
for (int f=0; f<6; f++)
if (!isdigit((unsigned char) field[f]))
goto parse_error;
for(int f = 0; f < 6; f++)
if(!isdigit((unsigned char)field[f])) goto parse_error;
char hArr[] = {field[0], field[1], '\0'};
char iArr[] = {field[2], field[3], '\0'};
@@ -298,10 +270,10 @@ bool minmea_scan(const char *sentence, const char *format, ...)
field += 6;
// Extra: fractional time. Saved as microseconds.
if (*field++ == '.') {
if(*field++ == '.') {
uint32_t value = 0;
uint32_t scale = 1000000LU;
while (isdigit((unsigned char) *field) && scale > 1) {
while(isdigit((unsigned char)*field) && scale > 1) {
value = (value * 10) + (*field++ - '0');
scale /= 10;
}
@@ -335,11 +307,9 @@ parse_error:
return result;
}
bool minmea_talker_id(char talker[3], const char *sentence)
{
bool minmea_talker_id(char talker[3], const char* sentence) {
char type[6];
if (!minmea_scan(sentence, "t", type))
return false;
if(!minmea_scan(sentence, "t", type)) return false;
talker[0] = type[0];
talker[1] = type[1];
@@ -348,42 +318,31 @@ bool minmea_talker_id(char talker[3], const char *sentence)
return true;
}
enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict)
{
if (!minmea_check(sentence, strict))
return MINMEA_INVALID;
enum minmea_sentence_id minmea_sentence_id(const char* sentence, bool strict) {
if(!minmea_check(sentence, strict)) return MINMEA_INVALID;
char type[6];
if (!minmea_scan(sentence, "t", type))
return MINMEA_INVALID;
if(!minmea_scan(sentence, "t", type)) return MINMEA_INVALID;
if (!strcmp(type+2, "GBS"))
return MINMEA_SENTENCE_GBS;
if (!strcmp(type+2, "GGA"))
return MINMEA_SENTENCE_GGA;
if (!strcmp(type+2, "GLL"))
return MINMEA_SENTENCE_GLL;
if (!strcmp(type+2, "GSA"))
return MINMEA_SENTENCE_GSA;
if (!strcmp(type+2, "GST"))
return MINMEA_SENTENCE_GST;
if (!strcmp(type+2, "GSV"))
return MINMEA_SENTENCE_GSV;
if (!strcmp(type+2, "RMC"))
return MINMEA_SENTENCE_RMC;
if (!strcmp(type+2, "VTG"))
return MINMEA_SENTENCE_VTG;
if (!strcmp(type+2, "ZDA"))
return MINMEA_SENTENCE_ZDA;
if(!strcmp(type + 2, "GBS")) return MINMEA_SENTENCE_GBS;
if(!strcmp(type + 2, "GGA")) return MINMEA_SENTENCE_GGA;
if(!strcmp(type + 2, "GLL")) return MINMEA_SENTENCE_GLL;
if(!strcmp(type + 2, "GSA")) return MINMEA_SENTENCE_GSA;
if(!strcmp(type + 2, "GST")) return MINMEA_SENTENCE_GST;
if(!strcmp(type + 2, "GSV")) return MINMEA_SENTENCE_GSV;
if(!strcmp(type + 2, "RMC")) return MINMEA_SENTENCE_RMC;
if(!strcmp(type + 2, "VTG")) return MINMEA_SENTENCE_VTG;
if(!strcmp(type + 2, "ZDA")) return MINMEA_SENTENCE_ZDA;
return MINMEA_UNKNOWN;
}
bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence)
{
bool minmea_parse_gbs(struct minmea_sentence_gbs* frame, const char* sentence) {
// $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C
char type[6];
if (!minmea_scan(sentence, "tTfffifff",
if(!minmea_scan(
sentence,
"tTfffifff",
type,
&frame->time,
&frame->err_latitude,
@@ -392,36 +351,37 @@ bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence)
&frame->svid,
&frame->prob,
&frame->bias,
&frame->stddev
))
return false;
if (strcmp(type+2, "GBS"))
&frame->stddev))
return false;
if(strcmp(type + 2, "GBS")) return false;
return true;
}
bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
{
bool minmea_parse_rmc(struct minmea_sentence_rmc* frame, const char* sentence) {
// $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
char type[6];
char validity;
int latitude_direction;
int longitude_direction;
int variation_direction;
if (!minmea_scan(sentence, "tTcfdfdffDfd",
if(!minmea_scan(
sentence,
"tTcfdfdffDfd",
type,
&frame->time,
&validity,
&frame->latitude, &latitude_direction,
&frame->longitude, &longitude_direction,
&frame->latitude,
&latitude_direction,
&frame->longitude,
&longitude_direction,
&frame->speed,
&frame->course,
&frame->date,
&frame->variation, &variation_direction))
return false;
if (strcmp(type+2, "RMC"))
&frame->variation,
&variation_direction))
return false;
if(strcmp(type + 2, "RMC")) return false;
frame->valid = (validity == 'A');
frame->latitude.value *= latitude_direction;
@@ -431,27 +391,31 @@ bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
return true;
}
bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
{
bool minmea_parse_gga(struct minmea_sentence_gga* frame, const char* sentence) {
// $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
char type[6];
int latitude_direction;
int longitude_direction;
if (!minmea_scan(sentence, "tTfdfdiiffcfcf_",
if(!minmea_scan(
sentence,
"tTfdfdiiffcfcf_",
type,
&frame->time,
&frame->latitude, &latitude_direction,
&frame->longitude, &longitude_direction,
&frame->latitude,
&latitude_direction,
&frame->longitude,
&longitude_direction,
&frame->fix_quality,
&frame->satellites_tracked,
&frame->hdop,
&frame->altitude, &frame->altitude_units,
&frame->height, &frame->height_units,
&frame->altitude,
&frame->altitude_units,
&frame->height,
&frame->height_units,
&frame->dgps_age))
return false;
if (strcmp(type+2, "GGA"))
return false;
if(strcmp(type + 2, "GGA")) return false;
frame->latitude.value *= latitude_direction;
frame->longitude.value *= longitude_direction;
@@ -459,12 +423,13 @@ bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
return true;
}
bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
{
bool minmea_parse_gsa(struct minmea_sentence_gsa* frame, const char* sentence) {
// $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
char type[6];
if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff",
if(!minmea_scan(
sentence,
"tciiiiiiiiiiiiifff",
type,
&frame->mode,
&frame->fix_type,
@@ -484,29 +449,30 @@ bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
&frame->hdop,
&frame->vdop))
return false;
if (strcmp(type+2, "GSA"))
return false;
if(strcmp(type + 2, "GSA")) return false;
return true;
}
bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence)
{
bool minmea_parse_gll(struct minmea_sentence_gll* frame, const char* sentence) {
// $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
char type[6];
int latitude_direction;
int longitude_direction;
if (!minmea_scan(sentence, "tfdfdTc;c",
if(!minmea_scan(
sentence,
"tfdfdTc;c",
type,
&frame->latitude, &latitude_direction,
&frame->longitude, &longitude_direction,
&frame->latitude,
&latitude_direction,
&frame->longitude,
&longitude_direction,
&frame->time,
&frame->status,
&frame->mode))
return false;
if (strcmp(type+2, "GLL"))
return false;
if(strcmp(type + 2, "GLL")) return false;
frame->latitude.value *= latitude_direction;
frame->longitude.value *= longitude_direction;
@@ -514,12 +480,13 @@ bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence)
return true;
}
bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
{
bool minmea_parse_gst(struct minmea_sentence_gst* frame, const char* sentence) {
// $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
char type[6];
if (!minmea_scan(sentence, "tTfffffff",
if(!minmea_scan(
sentence,
"tTfffffff",
type,
&frame->time,
&frame->rms_deviation,
@@ -530,14 +497,12 @@ bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
&frame->longitude_error_deviation,
&frame->altitude_error_deviation))
return false;
if (strcmp(type+2, "GST"))
return false;
if(strcmp(type + 2, "GST")) return false;
return true;
}
bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
{
bool minmea_parse_gsv(struct minmea_sentence_gsv* frame, const char* sentence) {
// $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
// $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
// $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75
@@ -545,7 +510,9 @@ bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
// $GPGSV,4,4,13*7B
char type[6];
if (!minmea_scan(sentence, "tiii;iiiiiiiiiiiiiiii",
if(!minmea_scan(
sentence,
"tiii;iiiiiiiiiiiiiiii",
type,
&frame->total_msgs,
&frame->msg_nr,
@@ -565,18 +532,15 @@ bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
&frame->sats[3].nr,
&frame->sats[3].elevation,
&frame->sats[3].azimuth,
&frame->sats[3].snr
)) {
&frame->sats[3].snr)) {
return false;
}
if (strcmp(type+2, "GSV"))
return false;
if(strcmp(type + 2, "GSV")) return false;
return true;
}
bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence)
{
bool minmea_parse_vtg(struct minmea_sentence_vtg* frame, const char* sentence) {
// $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48
// $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41
// $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22
@@ -584,7 +548,9 @@ bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence)
char type[6];
char c_true, c_magnetic, c_knots, c_kph, c_faa_mode;
if (!minmea_scan(sentence, "t;fcfcfcfcc",
if(!minmea_scan(
sentence,
"t;fcfcfcfcc",
type,
&frame->true_track_degrees,
&c_true,
@@ -596,28 +562,24 @@ bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence)
&c_kph,
&c_faa_mode))
return false;
if (strcmp(type+2, "VTG"))
return false;
if(strcmp(type + 2, "VTG")) return false;
// values are only valid with the accompanying characters
if (c_true != 'T')
frame->true_track_degrees.scale = 0;
if (c_magnetic != 'M')
frame->magnetic_track_degrees.scale = 0;
if (c_knots != 'N')
frame->speed_knots.scale = 0;
if (c_kph != 'K')
frame->speed_kph.scale = 0;
if(c_true != 'T') frame->true_track_degrees.scale = 0;
if(c_magnetic != 'M') frame->magnetic_track_degrees.scale = 0;
if(c_knots != 'N') frame->speed_knots.scale = 0;
if(c_kph != 'K') frame->speed_kph.scale = 0;
frame->faa_mode = (enum minmea_faa_mode)c_faa_mode;
return true;
}
bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence)
{
bool minmea_parse_zda(struct minmea_sentence_zda* frame, const char* sentence) {
// $GPZDA,201530.00,04,07,2002,00,00*60
char type[6];
if(!minmea_scan(sentence, "tTiiiii",
if(!minmea_scan(
sentence,
"tTiiiii",
type,
&frame->time,
&frame->date.day,
@@ -626,27 +588,25 @@ bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence)
&frame->hour_offset,
&frame->minute_offset))
return false;
if (strcmp(type+2, "ZDA"))
return false;
if(strcmp(type + 2, "ZDA")) return false;
// check offsets
if (abs(frame->hour_offset) > 13 ||
frame->minute_offset > 59 ||
frame->minute_offset < 0)
if(abs(frame->hour_offset) > 13 || frame->minute_offset > 59 || frame->minute_offset < 0)
return false;
return true;
}
int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_)
{
if (date->year == -1 || time_->hours == -1)
return -1;
int minmea_getdatetime(
struct tm* tm,
const struct minmea_date* date,
const struct minmea_time* time_) {
if(date->year == -1 || time_->hours == -1) return -1;
memset(tm, 0, sizeof(*tm));
if (date->year < 80) {
if(date->year < 80) {
tm->tm_year = 2000 + date->year - 1900; // 2000-2079
} else if (date->year >= 1900) {
} else if(date->year >= 1900) {
tm->tm_year = date->year - 1900; // 4 digit year, use directly
} else {
tm->tm_year = date->year; // 1980-1999
@@ -660,14 +620,15 @@ int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const stru
return 0;
}
int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_)
{
int minmea_gettime(
struct timespec* ts,
const struct minmea_date* date,
const struct minmea_time* time_) {
struct tm tm;
if (minmea_getdatetime(&tm, date, time_))
return -1;
if(minmea_getdatetime(&tm, date, time_)) return -1;
time_t timestamp = mktime(&tm); /* See README.md if your system lacks timegm(). */
if (timestamp != (time_t)-1) {
if(timestamp != (time_t)-1) {
ts->tv_sec = timestamp;
ts->tv_nsec = time_->microseconds * 1000;
return 0;

View File

@@ -87,8 +87,10 @@ struct minmea_sentence_gga {
int fix_quality;
int satellites_tracked;
struct minmea_float hdop;
struct minmea_float altitude; char altitude_units;
struct minmea_float height; char height_units;
struct minmea_float altitude;
char altitude_units;
struct minmea_float height;
char height_units;
struct minmea_float dgps_age;
};
@@ -179,22 +181,22 @@ struct minmea_sentence_zda {
/**
* Calculate raw sentence checksum. Does not check sentence integrity.
*/
uint8_t minmea_checksum(const char *sentence);
uint8_t minmea_checksum(const char* sentence);
/**
* Check sentence validity and checksum. Returns true for valid sentences.
*/
bool minmea_check(const char *sentence, bool strict);
bool minmea_check(const char* sentence, bool strict);
/**
* Determine talker identifier.
*/
bool minmea_talker_id(char talker[3], const char *sentence);
bool minmea_talker_id(char talker[3], const char* sentence);
/**
* Determine sentence identifier.
*/
enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
enum minmea_sentence_id minmea_sentence_id(const char* sentence, bool strict);
/**
* Scanf-like processor for NMEA sentences. Supports the following formats:
@@ -210,72 +212,70 @@ enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
* ; - following fields are optional
* Returns true on success. See library source code for details.
*/
bool minmea_scan(const char *sentence, const char *format, ...);
bool minmea_scan(const char* sentence, const char* format, ...);
/*
* Parse a specific type of sentence. Return true on success.
*/
bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence);
bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence);
bool minmea_parse_gbs(struct minmea_sentence_gbs* frame, const char* sentence);
bool minmea_parse_rmc(struct minmea_sentence_rmc* frame, const char* sentence);
bool minmea_parse_gga(struct minmea_sentence_gga* frame, const char* sentence);
bool minmea_parse_gsa(struct minmea_sentence_gsa* frame, const char* sentence);
bool minmea_parse_gll(struct minmea_sentence_gll* frame, const char* sentence);
bool minmea_parse_gst(struct minmea_sentence_gst* frame, const char* sentence);
bool minmea_parse_gsv(struct minmea_sentence_gsv* frame, const char* sentence);
bool minmea_parse_vtg(struct minmea_sentence_vtg* frame, const char* sentence);
bool minmea_parse_zda(struct minmea_sentence_zda* frame, const char* sentence);
/**
* Convert GPS UTC date/time representation to a UNIX calendar time.
*/
int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_);
int minmea_getdatetime(
struct tm* tm,
const struct minmea_date* date,
const struct minmea_time* time_);
/**
* Convert GPS UTC date/time representation to a UNIX timestamp.
*/
int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
int minmea_gettime(
struct timespec* ts,
const struct minmea_date* date,
const struct minmea_time* time_);
/**
* Rescale a fixed-point value to a different scale. Rounds towards zero.
*/
static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale)
{
if (f->scale == 0)
return 0;
if (f->scale == new_scale)
return f->value;
if (f->scale > new_scale)
return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
static inline int_least32_t minmea_rescale(const struct minmea_float* f, int_least32_t new_scale) {
if(f->scale == 0) return 0;
if(f->scale == new_scale) return f->value;
if(f->scale > new_scale)
return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale / new_scale / 2) /
(f->scale / new_scale);
else
return f->value * (new_scale/f->scale);
return f->value * (new_scale / f->scale);
}
/**
* Convert a fixed-point value to a floating-point value.
* Returns NaN for "unknown" values.
*/
static inline float minmea_tofloat(const struct minmea_float *f)
{
if (f->scale == 0)
return NAN;
return (float) f->value / (float) f->scale;
static inline float minmea_tofloat(const struct minmea_float* f) {
if(f->scale == 0) return NAN;
return (float)f->value / (float)f->scale;
}
/**
* Convert a raw coordinate to a floating point DD.DDD... value.
* Returns NaN for "unknown" values.
*/
static inline float minmea_tocoord(const struct minmea_float *f)
{
if (f->scale == 0)
return NAN;
if (f->scale > (INT_LEAST32_MAX / 100))
return NAN;
if (f->scale < (INT_LEAST32_MIN / 100))
return NAN;
static inline float minmea_tocoord(const struct minmea_float* f) {
if(f->scale == 0) return NAN;
if(f->scale > (INT_LEAST32_MAX / 100)) return NAN;
if(f->scale < (INT_LEAST32_MIN / 100)) return NAN;
int_least32_t degrees = f->value / (f->scale * 100);
int_least32_t minutes = f->value % (f->scale * 100);
return (float) degrees + (float) minutes / (60 * f->scale);
return (float)degrees + (float)minutes / (60 * f->scale);
}
/**
@@ -283,7 +283,7 @@ static inline float minmea_tocoord(const struct minmea_float *f)
* sentence data field.
*/
static inline bool minmea_isfield(char c) {
return isprint((unsigned char) c) && c != ',' && c != '*';
return isprint((unsigned char)c) && c != ',' && c != '*';
}
#ifdef __cplusplus

View File

@@ -38,7 +38,7 @@ static FlipperFormat* snake_game_open_file() {
void snake_game_save_score_to_file(int16_t highscore) {
FlipperFormat* file = snake_game_open_file();
uint32_t temp = highscore;
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE,&temp, 1)){
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) {
snake_game_close_file(file);
return;
}
@@ -105,12 +105,12 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) {
}
furi_string_free(file_type);
uint32_t temp;
snake_state->highscore = (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0;
snake_state->highscore =
(flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0;
flipper_format_rewind(file);
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)){
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) {
snake_game_close_file(file);
return false;
}

View File

@@ -235,14 +235,13 @@ static void snake_game_move_snake(SnakeState* const snake_state, Point const nex
static void snake_game_game_over(SnakeState* const snake_state, NotificationApp* notification) {
snake_state->state = GameStateGameOver;
snake_state->len = snake_state->len -7;
if(snake_state->len > snake_state->highscore){
snake_state->len = snake_state->len - 7;
if(snake_state->len > snake_state->highscore) {
snake_state->isNewHighscore = true;
snake_state->highscore = snake_state->len;
}
notification_message_block(notification, &sequence_fail);
}
static void
@@ -302,8 +301,7 @@ int32_t snake_game_app(void* p) {
SnakeState* snake_state = malloc(sizeof(SnakeState));
snake_state->isNewHighscore = false;
snake_state->highscore = 0;
if(!snake_game_init_game_from_file(snake_state))
snake_game_init_game(snake_state);
if(!snake_game_init_game_from_file(snake_state)) snake_game_init_game(snake_state);
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, snake_state, sizeof(SnakeState))) {
@@ -376,8 +374,7 @@ int32_t snake_game_app(void* p) {
release_mutex(&state_mutex, snake_state);
}
if(snake_state->isNewHighscore)
snake_game_save_score_to_file(snake_state->highscore);
if(snake_state->isNewHighscore) snake_game_save_score_to_file(snake_state->highscore);
// Wait for all notifications to be played and return backlight to normal state
notification_message_block(notification, &sequence_display_backlight_enforce_auto);

View File

@@ -24,7 +24,7 @@
static void render_callback(Canvas* const canvas, void* ctx) {
PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
if (plugin_state != NULL && !plugin_state->changing_scene) {
if(plugin_state != NULL && !plugin_state->changing_scene) {
totp_scene_director_render(canvas, plugin_state);
}
@@ -49,29 +49,43 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
totp_scene_director_init_scenes(plugin_state);
if (plugin_state->crypto_verify_data == NULL) {
if(plugin_state->crypto_verify_data == NULL) {
DialogMessage* message = dialog_message_alloc();
dialog_message_set_buttons(message, "No", NULL, "Yes");
dialog_message_set_text(message, "Would you like to setup PIN?", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
dialog_message_set_text(
message,
"Would you like to setup PIN?",
SCREEN_WIDTH_CENTER,
SCREEN_HEIGHT_CENTER,
AlignCenter,
AlignCenter);
DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs, message);
dialog_message_free(message);
if (dialog_result == DialogMessageButtonRight) {
if(dialog_result == DialogMessageButtonRight) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
} else {
totp_crypto_seed_iv(plugin_state, NULL, 0);
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}
} else if (plugin_state->pin_set) {
} else if(plugin_state->pin_set) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
} else {
totp_crypto_seed_iv(plugin_state, NULL, 0);
if (totp_crypto_verify_key(plugin_state)) {
if(totp_crypto_verify_key(plugin_state)) {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
} else {
FURI_LOG_E(LOGGING_TAG, "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
FURI_LOG_E(
LOGGING_TAG,
"Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
DialogMessage* message = dialog_message_alloc();
dialog_message_set_buttons(message, "Exit", NULL, NULL);
dialog_message_set_text(message, "Digital signature verification failed", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
dialog_message_set_text(
message,
"Digital signature verification failed",
SCREEN_WIDTH_CENTER,
SCREEN_HEIGHT_CENTER,
AlignCenter,
AlignCenter);
dialog_message_show(plugin_state->dialogs, message);
dialog_message_free(message);
return false;
@@ -94,7 +108,7 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
ListNode* node = plugin_state->tokens_list;
ListNode* tmp;
while (node != NULL) {
while(node != NULL) {
tmp = node->next;
TokenInfo* tokenInfo = node->data;
token_info_free(tokenInfo);
@@ -102,7 +116,7 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
node = tmp;
}
if (plugin_state->crypto_verify_data != NULL) {
if(plugin_state->crypto_verify_data != NULL) {
free(plugin_state->crypto_verify_data);
}
free(plugin_state);
@@ -112,7 +126,7 @@ int32_t totp_app() {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
PluginState* plugin_state = malloc(sizeof(PluginState));
if (!totp_plugin_state_init(plugin_state)) {
if(!totp_plugin_state_init(plugin_state)) {
FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
totp_plugin_state_free(plugin_state);
return 254;
@@ -137,18 +151,20 @@ int32_t totp_app() {
bool processing = true;
uint32_t last_user_interaction_time = furi_get_tick();
while(processing) {
if (plugin_state->changing_scene) continue;
if(plugin_state->changing_scene) continue;
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
PluginState* plugin_state = acquire_mutex_block(&state_mutex);
if(event_status == FuriStatusOk) {
if (event.type == EventTypeKey) {
if(event.type == EventTypeKey) {
last_user_interaction_time = furi_get_tick();
}
processing = totp_scene_director_handle_event(&event, plugin_state);
} else if (plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication && furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
} else if(
plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
}