clang-format: configmanager.cpp, globals.h, gpsread.cpp

This commit is contained in:
Klaus K Wilting 2018-06-12 19:48:21 +02:00
parent 0cbfb1c354
commit 4466889a18
3 changed files with 228 additions and 205 deletions

View File

@ -13,130 +13,151 @@ esp_err_t err;
// defined in antenna.cpp // defined in antenna.cpp
#ifdef HAS_ANTENNA_SWITCH #ifdef HAS_ANTENNA_SWITCH
void antenna_select(const uint8_t _ant); void antenna_select(const uint8_t _ant);
#endif #endif
// populate cfg vars with factory settings // populate cfg vars with factory settings
void defaultConfig() { void defaultConfig() {
cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in paxcounter.conf cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora sf, see pacounter.conf
cfg.txpower = 15; // 2-15, lora tx power cfg.txpower = 15; // 2-15, lora tx power
cfg.adrmode = 1; // 0=disabled, 1=enabled cfg.adrmode = 1; // 0=disabled, 1=enabled
cfg.screensaver = 0; // 0=disabled, 1=enabled cfg.screensaver = 0; // 0=disabled, 1=enabled
cfg.screenon = 1; // 0=disabled, 1=enabled cfg.screenon = 1; // 0=disabled, 1=enabled
cfg.countermode = 0; // 0=cyclic, 1=cumulative, 2=cyclic confirmed cfg.countermode = 0; // 0=cyclic, 1=cumulative, 2=cyclic confirmed
cfg.rssilimit = 0; // threshold for rssilimiter, negative value! cfg.rssilimit = 0; // threshold for rssilimiter, negative value!
cfg.sendcycle = SEND_SECS; // payload send cycle [seconds/2] cfg.sendcycle = SEND_SECS; // payload send cycle [seconds/2]
cfg.wifichancycle = WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] cfg.wifichancycle =
cfg.blescantime = BLESCANINTERVAL / 10; // BT channel scan cycle duration [seconds/100], default 1 (= 10ms) WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100]
cfg.blescan = 1; // 0=disabled, 1=enabled cfg.blescantime =
cfg.wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) BLESCANINTERVAL /
cfg.vendorfilter = 1; // 0=disabled, 1=enabled 10; // BT channel scan cycle [seconds/100], default 1 (= 10ms)
cfg.rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%) cfg.blescan = 1; // 0=disabled, 1=enabled
cfg.gpsmode = 1; // 0=disabled, 1=enabled cfg.wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4)
cfg.vendorfilter = 1; // 0=disabled, 1=enabled
cfg.rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%)
cfg.gpsmode = 1; // 0=disabled, 1=enabled
strncpy( cfg.version, PROGVERSION, sizeof(cfg.version)-1 ); strncpy(cfg.version, PROGVERSION, sizeof(cfg.version) - 1);
} }
void open_storage() { void open_storage() {
err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init(); err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) { }
// NVS partition was truncated and needs to be erased ESP_ERROR_CHECK(err);
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
// Open // Open
ESP_LOGI(TAG, "Opening NVS"); ESP_LOGI(TAG, "Opening NVS");
err = nvs_open("config", NVS_READWRITE, &my_handle); err = nvs_open("config", NVS_READWRITE, &my_handle);
if (err != ESP_OK) if (err != ESP_OK)
ESP_LOGI(TAG, "Error (%d) opening NVS handle", err); ESP_LOGI(TAG, "Error (%d) opening NVS handle", err);
else else
ESP_LOGI(TAG, "Done"); ESP_LOGI(TAG, "Done");
} }
// erase all keys and values in NVRAM // erase all keys and values in NVRAM
void eraseConfig() { void eraseConfig() {
ESP_LOGI(TAG, "Clearing settings in NVS"); ESP_LOGI(TAG, "Clearing settings in NVS");
open_storage(); open_storage();
if (err == ESP_OK) { if (err == ESP_OK) {
nvs_erase_all(my_handle); nvs_erase_all(my_handle);
nvs_commit(my_handle); nvs_commit(my_handle);
nvs_close(my_handle); nvs_close(my_handle);
ESP_LOGI(TAG, "Done");} ESP_LOGI(TAG, "Done");
else { } else {
ESP_LOGW(TAG, "NVS erase failed"); } ESP_LOGW(TAG, "NVS erase failed");
}
} }
// save current configuration from RAM to NVRAM // save current configuration from RAM to NVRAM
void saveConfig() { void saveConfig() {
ESP_LOGI(TAG, "Storing settings in NVS"); ESP_LOGI(TAG, "Storing settings in NVS");
open_storage(); open_storage();
if (err == ESP_OK) { if (err == ESP_OK) {
int8_t flash8 = 0; int8_t flash8 = 0;
int16_t flash16 = 0; int16_t flash16 = 0;
size_t required_size; size_t required_size;
char storedversion[10]; char storedversion[10];
if( nvs_get_str(my_handle, "version", storedversion, &required_size) != ESP_OK || strcmp(storedversion, cfg.version) != 0 ) if (nvs_get_str(my_handle, "version", storedversion, &required_size) !=
nvs_set_str(my_handle, "version", cfg.version); ESP_OK ||
strcmp(storedversion, cfg.version) != 0)
nvs_set_str(my_handle, "version", cfg.version);
if( nvs_get_i8(my_handle, "lorasf", &flash8) != ESP_OK || flash8 != cfg.lorasf ) if (nvs_get_i8(my_handle, "lorasf", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "lorasf", cfg.lorasf); flash8 != cfg.lorasf)
nvs_set_i8(my_handle, "lorasf", cfg.lorasf);
if( nvs_get_i8(my_handle, "txpower", &flash8) != ESP_OK || flash8 != cfg.txpower ) if (nvs_get_i8(my_handle, "txpower", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "txpower", cfg.txpower); flash8 != cfg.txpower)
nvs_set_i8(my_handle, "txpower", cfg.txpower);
if( nvs_get_i8(my_handle, "adrmode", &flash8) != ESP_OK || flash8 != cfg.adrmode ) if (nvs_get_i8(my_handle, "adrmode", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "adrmode", cfg.adrmode); flash8 != cfg.adrmode)
nvs_set_i8(my_handle, "adrmode", cfg.adrmode);
if( nvs_get_i8(my_handle, "screensaver", &flash8) != ESP_OK || flash8 != cfg.screensaver ) if (nvs_get_i8(my_handle, "screensaver", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "screensaver", cfg.screensaver); flash8 != cfg.screensaver)
nvs_set_i8(my_handle, "screensaver", cfg.screensaver);
if( nvs_get_i8(my_handle, "screenon", &flash8) != ESP_OK || flash8 != cfg.screenon ) if (nvs_get_i8(my_handle, "screenon", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "screenon", cfg.screenon); flash8 != cfg.screenon)
nvs_set_i8(my_handle, "screenon", cfg.screenon);
if( nvs_get_i8(my_handle, "countermode", &flash8) != ESP_OK || flash8 != cfg.countermode ) if (nvs_get_i8(my_handle, "countermode", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "countermode", cfg.countermode); flash8 != cfg.countermode)
nvs_set_i8(my_handle, "countermode", cfg.countermode);
if( nvs_get_i8(my_handle, "sendcycle", &flash8) != ESP_OK || flash8 != cfg.sendcycle ) if (nvs_get_i8(my_handle, "sendcycle", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "sendcycle", cfg.sendcycle); flash8 != cfg.sendcycle)
nvs_set_i8(my_handle, "sendcycle", cfg.sendcycle);
if( nvs_get_i8(my_handle, "wifichancycle", &flash8) != ESP_OK || flash8 != cfg.wifichancycle ) if (nvs_get_i8(my_handle, "wifichancycle", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "wifichancycle", cfg.wifichancycle); flash8 != cfg.wifichancycle)
nvs_set_i8(my_handle, "wifichancycle", cfg.wifichancycle);
if( nvs_get_i8(my_handle, "blescantime", &flash8) != ESP_OK || flash8 != cfg.blescantime ) if (nvs_get_i8(my_handle, "blescantime", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "blescantime", cfg.blescantime); flash8 != cfg.blescantime)
nvs_set_i8(my_handle, "blescantime", cfg.blescantime);
if( nvs_get_i8(my_handle, "blescanmode", &flash8) != ESP_OK || flash8 != cfg.blescan ) if (nvs_get_i8(my_handle, "blescanmode", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "blescanmode", cfg.blescan); flash8 != cfg.blescan)
nvs_set_i8(my_handle, "blescanmode", cfg.blescan);
if( nvs_get_i8(my_handle, "wifiant", &flash8) != ESP_OK || flash8 != cfg.wifiant ) if (nvs_get_i8(my_handle, "wifiant", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "wifiant", cfg.wifiant); flash8 != cfg.wifiant)
nvs_set_i8(my_handle, "wifiant", cfg.wifiant);
if( nvs_get_i8(my_handle, "vendorfilter", &flash8) != ESP_OK || flash8 != cfg.vendorfilter ) if (nvs_get_i8(my_handle, "vendorfilter", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "vendorfilter", cfg.vendorfilter); flash8 != cfg.vendorfilter)
nvs_set_i8(my_handle, "vendorfilter", cfg.vendorfilter);
if( nvs_get_i8(my_handle, "rgblum", &flash8) != ESP_OK || flash8 != cfg.rgblum ) if (nvs_get_i8(my_handle, "rgblum", &flash8) != ESP_OK ||
nvs_set_i8(my_handle, "rgblum", cfg.rgblum); flash8 != cfg.rgblum)
nvs_set_i8(my_handle, "rgblum", cfg.rgblum);
if( nvs_get_i8(my_handle, "gpsmode", &flash8) != ESP_OK || flash8 != cfg.gpsmode ) if (nvs_get_i8(my_handle, "gpsmode", &flash8) != ESP_OK ||
flash8 != cfg.gpsmode)
nvs_set_i8(my_handle, "gpsmode", cfg.gpsmode); nvs_set_i8(my_handle, "gpsmode", cfg.gpsmode);
if( nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK || flash16 != cfg.rssilimit ) if (nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK ||
nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit); flash16 != cfg.rssilimit)
nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit);
err = nvs_commit(my_handle); err = nvs_commit(my_handle);
nvs_close(my_handle); nvs_close(my_handle);
if ( err == ESP_OK ) { if (err == ESP_OK) {
ESP_LOGI(TAG, "Done"); ESP_LOGI(TAG, "Done");
} else {
ESP_LOGW(TAG, "NVS config write failed");
}
} else { } else {
ESP_LOGW(TAG, "Error (%d) opening NVS handle", err); ESP_LOGW(TAG, "NVS config write failed");
} }
} else {
ESP_LOGW(TAG, "Error (%d) opening NVS handle", err);
}
} }
// set and save cfg.version // set and save cfg.version
@ -152,31 +173,33 @@ void loadConfig() {
ESP_LOGI(TAG, "Reading settings from NVS"); ESP_LOGI(TAG, "Reading settings from NVS");
open_storage(); open_storage();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGW(TAG,"Error (%d) opening NVS handle, storing defaults", err); ESP_LOGW(TAG, "Error (%d) opening NVS handle, storing defaults", err);
saveConfig(); } // saves factory settings to NVRAM saveConfig();
} // saves factory settings to NVRAM
else { else {
int8_t flash8 = 0; int8_t flash8 = 0;
int16_t flash16 = 0; int16_t flash16 = 0;
size_t required_size; size_t required_size;
// check if configuration stored in NVRAM matches PROGVERSION // check if configuration stored in NVRAM matches PROGVERSION
if( nvs_get_str(my_handle, "version", NULL, &required_size) == ESP_OK ) { if (nvs_get_str(my_handle, "version", NULL, &required_size) == ESP_OK) {
nvs_get_str(my_handle, "version", cfg.version, &required_size); nvs_get_str(my_handle, "version", cfg.version, &required_size);
ESP_LOGI(TAG, "NVRAM settings version = %s", cfg.version); ESP_LOGI(TAG, "NVRAM settings version = %s", cfg.version);
if (strcmp(cfg.version, PROGVERSION)) { if (strcmp(cfg.version, PROGVERSION)) {
ESP_LOGI(TAG, "migrating NVRAM settings to new version %s", PROGVERSION); ESP_LOGI(TAG, "migrating NVRAM settings to new version %s",
PROGVERSION);
nvs_close(my_handle); nvs_close(my_handle);
migrateVersion(); migrateVersion();
} }
} else { } else {
ESP_LOGI(TAG, "new version %s, deleting NVRAM settings", PROGVERSION); ESP_LOGI(TAG, "new version %s, deleting NVRAM settings", PROGVERSION);
nvs_close(my_handle); nvs_close(my_handle);
eraseConfig(); eraseConfig();
migrateVersion(); migrateVersion();
} }
// overwrite defaults with valid values from NVRAM // overwrite defaults with valid values from NVRAM
if( nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK) {
cfg.lorasf = flash8; cfg.lorasf = flash8;
ESP_LOGI(TAG, "lorasf = %d", flash8); ESP_LOGI(TAG, "lorasf = %d", flash8);
} else { } else {
@ -184,7 +207,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "txpower", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "txpower", &flash8) == ESP_OK) {
cfg.txpower = flash8; cfg.txpower = flash8;
ESP_LOGI(TAG, "txpower = %d", flash8); ESP_LOGI(TAG, "txpower = %d", flash8);
} else { } else {
@ -192,7 +215,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "adrmode", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "adrmode", &flash8) == ESP_OK) {
cfg.adrmode = flash8; cfg.adrmode = flash8;
ESP_LOGI(TAG, "adrmode = %d", flash8); ESP_LOGI(TAG, "adrmode = %d", flash8);
} else { } else {
@ -200,7 +223,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "screensaver", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "screensaver", &flash8) == ESP_OK) {
cfg.screensaver = flash8; cfg.screensaver = flash8;
ESP_LOGI(TAG, "screensaver = %d", flash8); ESP_LOGI(TAG, "screensaver = %d", flash8);
} else { } else {
@ -208,7 +231,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "screenon", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "screenon", &flash8) == ESP_OK) {
cfg.screenon = flash8; cfg.screenon = flash8;
ESP_LOGI(TAG, "screenon = %d", flash8); ESP_LOGI(TAG, "screenon = %d", flash8);
} else { } else {
@ -216,7 +239,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "countermode", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "countermode", &flash8) == ESP_OK) {
cfg.countermode = flash8; cfg.countermode = flash8;
ESP_LOGI(TAG, "countermode = %d", flash8); ESP_LOGI(TAG, "countermode = %d", flash8);
} else { } else {
@ -224,7 +247,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "sendcycle", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "sendcycle", &flash8) == ESP_OK) {
cfg.sendcycle = flash8; cfg.sendcycle = flash8;
ESP_LOGI(TAG, "sendcycle = %d", flash8); ESP_LOGI(TAG, "sendcycle = %d", flash8);
} else { } else {
@ -232,7 +255,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "wifichancycle", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "wifichancycle", &flash8) == ESP_OK) {
cfg.wifichancycle = flash8; cfg.wifichancycle = flash8;
ESP_LOGI(TAG, "wifichancycle = %d", flash8); ESP_LOGI(TAG, "wifichancycle = %d", flash8);
} else { } else {
@ -240,7 +263,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "wifiant", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "wifiant", &flash8) == ESP_OK) {
cfg.wifiant = flash8; cfg.wifiant = flash8;
ESP_LOGI(TAG, "wifiantenna = %d", flash8); ESP_LOGI(TAG, "wifiantenna = %d", flash8);
} else { } else {
@ -248,7 +271,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "vendorfilter", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "vendorfilter", &flash8) == ESP_OK) {
cfg.vendorfilter = flash8; cfg.vendorfilter = flash8;
ESP_LOGI(TAG, "vendorfilter = %d", flash8); ESP_LOGI(TAG, "vendorfilter = %d", flash8);
} else { } else {
@ -256,7 +279,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "rgblum", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "rgblum", &flash8) == ESP_OK) {
cfg.rgblum = flash8; cfg.rgblum = flash8;
ESP_LOGI(TAG, "rgbluminosity = %d", flash8); ESP_LOGI(TAG, "rgbluminosity = %d", flash8);
} else { } else {
@ -264,7 +287,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "blescantime", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "blescantime", &flash8) == ESP_OK) {
cfg.blescantime = flash8; cfg.blescantime = flash8;
ESP_LOGI(TAG, "blescantime = %d", flash8); ESP_LOGI(TAG, "blescantime = %d", flash8);
} else { } else {
@ -272,7 +295,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "blescanmode", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "blescanmode", &flash8) == ESP_OK) {
cfg.blescan = flash8; cfg.blescan = flash8;
ESP_LOGI(TAG, "BLEscanmode = %d", flash8); ESP_LOGI(TAG, "BLEscanmode = %d", flash8);
} else { } else {
@ -280,7 +303,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i16(my_handle, "rssilimit", &flash16) == ESP_OK ) { if (nvs_get_i16(my_handle, "rssilimit", &flash16) == ESP_OK) {
cfg.rssilimit = flash16; cfg.rssilimit = flash16;
ESP_LOGI(TAG, "rssilimit = %d", flash16); ESP_LOGI(TAG, "rssilimit = %d", flash16);
} else { } else {
@ -288,7 +311,7 @@ void loadConfig() {
saveConfig(); saveConfig();
} }
if( nvs_get_i8(my_handle, "gpsmode", &flash8) == ESP_OK ) { if (nvs_get_i8(my_handle, "gpsmode", &flash8) == ESP_OK) {
cfg.gpsmode = flash8; cfg.gpsmode = flash8;
ESP_LOGI(TAG, "GPSmode = %d", flash8); ESP_LOGI(TAG, "GPSmode = %d", flash8);
} else { } else {
@ -300,9 +323,9 @@ void loadConfig() {
ESP_LOGI(TAG, "Done"); ESP_LOGI(TAG, "Done");
// put actions to be triggered after config loaded here // put actions to be triggered after config loaded here
#ifdef HAS_ANTENNA_SWITCH // set antenna type, if device has one #ifdef HAS_ANTENNA_SWITCH // set antenna type, if device has one
antenna_select(cfg.wifiant); antenna_select(cfg.wifiant);
#endif #endif
} }
} }

View File

@ -8,12 +8,12 @@
// OLED Display // OLED Display
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
#include <U8x8lib.h> #include <U8x8lib.h>
#endif #endif
//GPS // GPS
#ifdef HAS_GPS #ifdef HAS_GPS
#include <TinyGPS++.h> #include <TinyGPS++.h>
#endif #endif
// LMIC-Arduino LoRaWAN Stack // LMIC-Arduino LoRaWAN Stack
@ -22,7 +22,7 @@
// LED controls // LED controls
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
#include <SmartLeds.h> #include <SmartLeds.h>
#endif #endif
#include "rgb_led.h" #include "rgb_led.h"
@ -31,34 +31,34 @@
// Struct holding devices's runtime configuration // Struct holding devices's runtime configuration
typedef struct { typedef struct {
uint8_t lorasf; // 7-12, lora spreadfactor uint8_t lorasf; // 7-12, lora spreadfactor
uint8_t txpower; // 2-15, lora tx power uint8_t txpower; // 2-15, lora tx power
uint8_t adrmode; // 0=disabled, 1=enabled uint8_t adrmode; // 0=disabled, 1=enabled
uint8_t screensaver; // 0=disabled, 1=enabled uint8_t screensaver; // 0=disabled, 1=enabled
uint8_t screenon; // 0=disabled, 1=enabled uint8_t screenon; // 0=disabled, 1=enabled
uint8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed uint8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed
int16_t rssilimit; // threshold for rssilimiter, negative value! int16_t rssilimit; // threshold for rssilimiter, negative value!
uint8_t sendcycle; // payload send cycle [seconds/2] uint8_t sendcycle; // payload send cycle [seconds/2]
uint8_t wifichancycle; // wifi channel switch cycle [seconds/100] uint8_t wifichancycle; // wifi channel switch cycle [seconds/100]
uint8_t blescantime; // BLE scan cycle duration [seconds] uint8_t blescantime; // BLE scan cycle duration [seconds]
uint8_t blescan; // 0=disabled, 1=enabled uint8_t blescan; // 0=disabled, 1=enabled
uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4)
uint8_t vendorfilter; // 0=disabled, 1=enabled uint8_t vendorfilter; // 0=disabled, 1=enabled
uint8_t rgblum; // RGB Led luminosity (0..100%) uint8_t rgblum; // RGB Led luminosity (0..100%)
uint8_t gpsmode; // 0=disabled, 1=enabled uint8_t gpsmode; // 0=disabled, 1=enabled
char version[10]; // Firmware version char version[10]; // Firmware version
} configData_t; } configData_t;
#ifdef HAS_GPS #ifdef HAS_GPS
typedef struct { typedef struct {
uint32_t latitude; uint32_t latitude;
uint32_t longitude; uint32_t longitude;
uint8_t satellites; uint8_t satellites;
uint16_t hdop; uint16_t hdop;
uint16_t altitude; uint16_t altitude;
} gpsStatus_t; } gpsStatus_t;
extern gpsStatus_t gps_status; // struct for storing gps data extern gpsStatus_t gps_status; // struct for storing gps data
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
#endif #endif
extern configData_t cfg; extern configData_t cfg;
@ -68,6 +68,7 @@ extern char display_lora[], display_lmic[];
extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim;
extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters
extern std::set<uint16_t> macs; extern std::set<uint16_t> macs;
extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching extern hw_timer_t
extern xref2u1_t rcmd_data; // buffer for rcommand results size *channelSwitch; // hardware timer used for wifi channel switching
extern u1_t rcmd_data_size; // buffer for rcommand results size extern xref2u1_t rcmd_data; // buffer for rcommand results size
extern u1_t rcmd_data_size; // buffer for rcommand results size

View File

@ -7,74 +7,73 @@ static const char TAG[] = "main";
// read GPS data and cast to global struct // read GPS data and cast to global struct
void gps_read() { void gps_read() {
gps_status.latitude = (uint32_t) (gps.location.lat() * 1000000); gps_status.latitude = (uint32_t)(gps.location.lat() * 1000000);
gps_status.longitude = (uint32_t) (gps.location.lng() * 1000000); gps_status.longitude = (uint32_t)(gps.location.lng() * 1000000);
gps_status.satellites = (uint8_t) gps.satellites.value(); gps_status.satellites = (uint8_t)gps.satellites.value();
gps_status.hdop = (uint16_t) gps.hdop.value(); gps_status.hdop = (uint16_t)gps.hdop.value();
gps_status.altitude = (uint16_t) gps.altitude.meters(); gps_status.altitude = (uint16_t)gps.altitude.meters();
} }
// GPS serial feed FreeRTos Task // GPS serial feed FreeRTos Task
void gps_loop(void * pvParameters) { void gps_loop(void *pvParameters) {
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
// initialize and, if needed, configure, GPS // initialize and, if needed, configure, GPS
#if defined GPS_SERIAL #if defined GPS_SERIAL
HardwareSerial GPS_Serial(1); HardwareSerial GPS_Serial(1);
#elif defined GPS_I2C #elif defined GPS_I2C
// to be done // to be done
#endif #endif
while(1) { while (1) {
if (cfg.gpsmode) if (cfg.gpsmode) {
{ #if defined GPS_SERIAL
#if defined GPS_SERIAL
// serial connect to GPS device
GPS_Serial.begin(GPS_SERIAL);
while(cfg.gpsmode) {
// feed GPS decoder with serial NMEA data from GPS device
while (GPS_Serial.available()) {
gps.encode(GPS_Serial.read());
}
vTaskDelay(1/portTICK_PERIOD_MS); // reset watchdog
}
// after GPS function was disabled, close connect to GPS device
GPS_Serial.end();
#elif defined GPS_I2C // serial connect to GPS device
GPS_Serial.begin(GPS_SERIAL);
// I2C connect to GPS device with 100 kHz
Wire.begin(GPS_I2C_PINS, 100000);
Wire.beginTransmission(GPS_I2C_ADDRESS_WRITE);
Wire.write(0x00);
i2c_ret == Wire.beginTransmission(GPS_I2C_ADDRESS_READ); while (cfg.gpsmode) {
if (i2c_ret == 0) { // check if device seen on i2c bus // feed GPS decoder with serial NMEA data from GPS device
while(cfg.gpsmode) { while (GPS_Serial.available()) {
// feed GPS decoder with serial NMEA data from GPS device gps.encode(GPS_Serial.read());
while (Wire.available()) {
Wire.requestFrom(GPS_I2C_ADDRESS_READ, 255);
gps.encode(Wire.read());
vTaskDelay(1/portTICK_PERIOD_MS); // reset watchdog
}
}
// after GPS function was disabled, close connect to GPS device
Wire.endTransmission();
Wire.setClock(400000); // Set back to 400KHz to speed up OLED
}
#endif
} }
vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog
vTaskDelay(1/portTICK_PERIOD_MS); // reset watchdog }
// after GPS function was disabled, close connect to GPS device
GPS_Serial.end();
} // end of infinite loop #elif defined GPS_I2C
// I2C connect to GPS device with 100 kHz
Wire.begin(GPS_I2C_PINS, 100000);
Wire.beginTransmission(GPS_I2C_ADDRESS_WRITE);
Wire.write(0x00);
i2c_ret == Wire.beginTransmission(GPS_I2C_ADDRESS_READ);
if (i2c_ret == 0) { // check if device seen on i2c bus
while (cfg.gpsmode) {
// feed GPS decoder with serial NMEA data from GPS device
while (Wire.available()) {
Wire.requestFrom(GPS_I2C_ADDRESS_READ, 255);
gps.encode(Wire.read());
vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog
}
}
// after GPS function was disabled, close connect to GPS device
Wire.endTransmission();
Wire.setClock(400000); // Set back to 400KHz to speed up OLED
}
#endif
}
vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog
} // end of infinite loop
} // gps_loop() } // gps_loop()
#endif // HAS_GPS #endif // HAS_GPS