JuReceiver/system_sensors/gnss.h
2023-09-10 15:33:34 +02:00

200 lines
4.5 KiB
C

#ifndef _J_SYS_SENS_GNSS_H
#define _J_SYS_SENS_GNSS_H
#define jType_S_GNSS 0x23
#define jCommand_GNSS_location 0
#define jCommand_GNSS_date 1
#define jCommand_GNSS_time 2
#define jCommand_GNSS_speed 3
#define jCommand_GNSS_course 4
#define jCommand_GNSS_altitude 5
#define jCommand_GNSS_satellites 6
#define jCommand_GNSS_hdop 7
#define jCommand_GNSS_fail 0xff
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_location;
byte isValid;
double lat;
double lng;
} gnssLocData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_date;
byte isValid;
uint32_t value;
uint16_t year;
uint8_t month;
uint8_t day;
} gnssDateData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_time;
byte isValid;
uint32_t value;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t centisecond;
} gnssTimeData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_speed;
byte isValid;
double mps;
} gnssSpeedData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_course;
byte isValid;
double deg;
} gnssCourseData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_altitude;
byte isValid;
double m;
} gnssAltData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_satellites;
byte isValid;
uint32_t n;
} gnssSatData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_hdop;
byte isValid;
double hdop;
} gnssHdopData;
typedef struct {
byte type = jType_S_GNSS;
byte command = jCommand_GNSS_fail;
} gnssFailData;
#ifdef jGNSS
#include <TinyGPS++.h> //TinyGPSPlus
TinyGPSPlus gps;
void initGNSS() { Serial2.begin(9600, SERIAL_8N1, 16, 17); }
void GNSSLoop() {
while (Serial2.available() > 0) {
int readd = Serial2.read();
gps.encode(readd);
}
}
void GNSSradioTask() {
switch (jRCbuff[1]) {
case jCommand_GNSS_location: {
gnssLocData data;
data.isValid =
gps.location.isValid() | (gps.location.isUpdated() << 1);
data.lat = gps.location.lat();
data.lng = gps.location.lng();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_date: {
gnssDateData data;
data.isValid = gps.date.isValid() | (gps.date.isUpdated() << 1);
data.value = gps.date.value();
data.year = gps.date.year();
data.month = gps.date.month();
data.day = gps.date.day();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_time: {
gnssTimeData data;
data.isValid = gps.time.isValid() | (gps.time.isUpdated() << 1);
data.value = gps.time.value();
data.hour = gps.time.hour();
data.minute = gps.time.minute();
data.second = gps.time.second();
data.centisecond = gps.time.centisecond();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_speed: {
gnssSpeedData data;
data.isValid = gps.speed.isValid() | (gps.speed.isUpdated() << 1);
data.mps = gps.speed.mps();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_course: {
gnssCourseData data;
data.isValid = gps.course.isValid() | (gps.course.isUpdated() << 1);
data.deg = gps.course.deg();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_altitude: {
gnssAltData data;
data.isValid =
gps.altitude.isValid() | (gps.altitude.isUpdated() << 1);
data.m = gps.altitude.meters();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_satellites: {
gnssSatData data;
data.isValid =
gps.satellites.isValid() | (gps.satellites.isUpdated() << 1);
data.n = gps.satellites.value();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_GNSS_hdop: {
gnssHdopData data;
data.isValid = gps.hdop.isValid() | (gps.hdop.isUpdated() << 1);
data.hdop = gps.hdop.hdop();
jSendNRF(&data, sizeof(data));
} break;
}
}
#else
void initGNSS() {}
void GNSSLoop() {}
void GNSSradioTask() {
gnssFailData data;
jSendNRF(&data, sizeof(data));
}
#endif
#endif