Updated modules to new inheritance model
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "custom_types.h"
|
#include "custom_types.h"
|
||||||
#include "modules/logger/system_logger.h"
|
#include "modules/logger/system_logger.h"
|
||||||
|
#include "base/task.h"
|
||||||
|
#include "base/ring_buffer.h"
|
||||||
|
#include "base/module_base.h"
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
class cmd {
|
class cmd: public module_base{
|
||||||
private:
|
private:
|
||||||
HardwareSerial *_data_stream;
|
HardwareSerial *_data_stream;
|
||||||
unsigned long _baud_rate = 115200;
|
unsigned long _baud_rate = 115200;
|
||||||
@@ -12,8 +15,9 @@ private:
|
|||||||
unsigned int _idx = 0;
|
unsigned int _idx = 0;
|
||||||
bool _buf_open = false;
|
bool _buf_open = false;
|
||||||
int try_parse();
|
int try_parse();
|
||||||
|
ring_buffer<Task, 16> _queue;
|
||||||
public:
|
public:
|
||||||
|
int push(const Task& task) override;
|
||||||
cmd(HardwareSerial *data_stream);
|
cmd(HardwareSerial *data_stream);
|
||||||
cmd(HardwareSerial *data_stream, system_logger *logger);
|
cmd(HardwareSerial *data_stream, system_logger *logger);
|
||||||
~cmd();
|
~cmd();
|
||||||
@@ -41,6 +45,10 @@ int cmd::try_parse() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cmd::push(const Task& task) {
|
||||||
|
return _queue.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
cmd::cmd(HardwareSerial *data_stream)
|
cmd::cmd(HardwareSerial *data_stream)
|
||||||
: _data_stream(data_stream), _logger(nullptr) {}
|
: _data_stream(data_stream), _logger(nullptr) {}
|
||||||
|
|
||||||
|
|||||||
@@ -2,28 +2,50 @@
|
|||||||
#include "custom_types.h"
|
#include "custom_types.h"
|
||||||
#include "flags.h"
|
#include "flags.h"
|
||||||
#include "modules/logger/system_logger.h"
|
#include "modules/logger/system_logger.h"
|
||||||
|
#include "base/task.h"
|
||||||
|
#include "base/ring_buffer.h"
|
||||||
|
#include "base/module_base.h"
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
class config {
|
class config : public module_base{
|
||||||
private:
|
private:
|
||||||
vehicle_config _config;
|
vehicle_config _config;
|
||||||
system_logger *_logger;
|
system_logger *_logger;
|
||||||
bool _valid_config;
|
bool _valid_config;
|
||||||
track_data _loaded_track;
|
track_data _loaded_track;
|
||||||
|
ring_buffer<Task, 16> _queue;
|
||||||
|
int read_cfg();
|
||||||
|
int write_cfg();
|
||||||
|
int write_cfg(const vehicle_config& new_config);
|
||||||
public:
|
public:
|
||||||
|
int push(const Task& task) override;
|
||||||
config();
|
config();
|
||||||
config(system_logger *logger);
|
config(system_logger *logger);
|
||||||
~config();
|
~config();
|
||||||
int auto_init();
|
int auto_init();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int config::push(const Task& task) {
|
||||||
|
return _queue.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
config::config() : _logger(nullptr), _valid_config(true) {}
|
config::config() : _logger(nullptr), _valid_config(true) {}
|
||||||
config::config(system_logger *logger) : _logger(logger), _valid_config(true) {}
|
config::config(system_logger *logger) : _logger(logger), _valid_config(true) {}
|
||||||
|
|
||||||
config::~config() {}
|
config::~config() {}
|
||||||
|
|
||||||
int config::auto_init() {
|
int config::read_cfg() {
|
||||||
EEPROM.get(0, _config);
|
EEPROM.get(0, _config);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int config::write_cfg(const vehicle_config& new_config) {
|
||||||
|
EEPROM.put(0, new_config);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int config::auto_init() {
|
||||||
|
this->read_cfg();
|
||||||
if (_config.magic != CONFIG_MAGIC) {
|
if (_config.magic != CONFIG_MAGIC) {
|
||||||
#ifdef WARN
|
#ifdef WARN
|
||||||
if (_logger != nullptr) {
|
if (_logger != nullptr) {
|
||||||
@@ -31,8 +53,8 @@ int config::auto_init() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
vehicle_config clean_config;
|
vehicle_config clean_config;
|
||||||
EEPROM.put(0, clean_config);
|
this->write_cfg(clean_config);
|
||||||
EEPROM.get(0, _config);
|
this->read_cfg();
|
||||||
if (_config.magic != CONFIG_MAGIC) {
|
if (_config.magic != CONFIG_MAGIC) {
|
||||||
#ifdef ERROR
|
#ifdef ERROR
|
||||||
if (_logger != nullptr) {
|
if (_logger != nullptr) {
|
||||||
|
|||||||
@@ -3,25 +3,33 @@
|
|||||||
#include "TinyGPSPlus.h"
|
#include "TinyGPSPlus.h"
|
||||||
#include "flags.h"
|
#include "flags.h"
|
||||||
#include "modules/logger/system_logger.h"
|
#include "modules/logger/system_logger.h"
|
||||||
|
#include "base/task.h"
|
||||||
|
#include "base/ring_buffer.h"
|
||||||
|
#include "base/module_base.h"
|
||||||
|
#include "data/gps_store.h"
|
||||||
#define MOD "modules/gps/gps.h"
|
#define MOD "modules/gps/gps.h"
|
||||||
|
|
||||||
class gps {
|
class gps : public module_base{
|
||||||
private:
|
private:
|
||||||
TinyGPSPlus *_gps;
|
TinyGPSPlus *_gps;
|
||||||
HardwareSerial *_data_stream;
|
HardwareSerial *_data_stream;
|
||||||
system_logger *_logger;
|
system_logger *_logger;
|
||||||
|
ring_buffer<Task, 16> _queue;
|
||||||
public:
|
public:
|
||||||
|
int push(const Task& task) override;
|
||||||
gps(HardwareSerial *data_stream);
|
gps(HardwareSerial *data_stream);
|
||||||
gps(HardwareSerial *data_stream, system_logger *logger);
|
gps(HardwareSerial *data_stream, system_logger *logger);
|
||||||
~gps();
|
~gps();
|
||||||
int parse_task(unsigned long timeout_ms = 500);
|
int loop(unsigned long timeout_ms = 500);
|
||||||
int init();
|
int init();
|
||||||
gps_data get_data();
|
gps_data get_data();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int gps::push(const Task& task) {
|
||||||
|
return _queue.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
gps::gps(HardwareSerial *data_stream)
|
gps::gps(HardwareSerial *data_stream)
|
||||||
: _data_stream(data_stream), _logger(nullptr) {
|
: _data_stream(data_stream), _logger(nullptr) {
|
||||||
_data_stream = data_stream;
|
_data_stream = data_stream;
|
||||||
@@ -43,27 +51,14 @@ int gps::init() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gps::parse_task(unsigned long timeout_ms) {
|
int gps::loop(unsigned long timeout_ms) {
|
||||||
|
|
||||||
#ifdef DEEP_DEBUG
|
|
||||||
if (_logger != nullptr) {
|
|
||||||
_logger->deep_debug(String(MOD) + ": parse_task: Begin");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
unsigned long timeout = millis() + timeout_ms;
|
unsigned long timeout = millis() + timeout_ms;
|
||||||
#ifdef DEEP_DEBUG
|
|
||||||
if (_logger != nullptr) {
|
|
||||||
_logger->deep_debug(String(MOD) + ": parse_task: Entering Parse Loop");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
while (_data_stream->available() > 0) {
|
while (_data_stream->available() > 0) {
|
||||||
_gps->encode(_data_stream->read());
|
if (_gps->encode(_data_stream->read())) {
|
||||||
if (millis() > timeout) {
|
gps_global_write(this->get_data());
|
||||||
#ifdef DEEP_DEBUG
|
|
||||||
if (_logger != nullptr) {
|
|
||||||
_logger->deep_debug(String(MOD) + ": parse_task: Parse Loop Timed Out");
|
|
||||||
}
|
}
|
||||||
#endif
|
if (millis() > timeout) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
#include "modules/logger/system_logger.h"
|
#include "modules/logger/system_logger.h"
|
||||||
#include "modules/gps/gps.h"
|
#include "modules/gps/gps.h"
|
||||||
#include <LiquidCrystal_I2C.h>
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
#include "base/task.h"
|
||||||
|
#include "base/ring_buffer.h"
|
||||||
|
#include "base/module_base.h"
|
||||||
#define MOD "modules/lcd/lcd.h"
|
#define MOD "modules/lcd/lcd.h"
|
||||||
|
|
||||||
namespace screen
|
namespace screen
|
||||||
@@ -14,7 +17,7 @@ typedef enum lcd_screen {
|
|||||||
};
|
};
|
||||||
} // namespace screen
|
} // namespace screen
|
||||||
|
|
||||||
class lcd {
|
class lcd : public module_base{
|
||||||
private:
|
private:
|
||||||
LiquidCrystal_I2C *_display;
|
LiquidCrystal_I2C *_display;
|
||||||
bool _dispaly_cleared;
|
bool _dispaly_cleared;
|
||||||
@@ -22,7 +25,7 @@ private:
|
|||||||
screen::lcd_screen _screen;
|
screen::lcd_screen _screen;
|
||||||
unsigned long _last_render;
|
unsigned long _last_render;
|
||||||
unsigned long _frame_duration;
|
unsigned long _frame_duration;
|
||||||
gps *_gps;
|
ring_buffer<Task, 16> _queue;
|
||||||
void clear();
|
void clear();
|
||||||
void print(const String& msg);
|
void print(const String& msg);
|
||||||
void print(char);
|
void print(char);
|
||||||
@@ -32,15 +35,16 @@ private:
|
|||||||
void print(long val, int base);
|
void print(long val, int base);
|
||||||
void print(unsigned int val, int base);
|
void print(unsigned int val, int base);
|
||||||
void print(int val, int base);
|
void print(int val, int base);
|
||||||
|
|
||||||
|
int render_gps_debug();
|
||||||
public:
|
public:
|
||||||
|
int push(const Task& task) override;
|
||||||
lcd();
|
lcd();
|
||||||
lcd(system_logger *logger);
|
lcd(system_logger *logger);
|
||||||
~lcd();
|
~lcd();
|
||||||
int init();
|
int init();
|
||||||
int set_gps(gps *gps);
|
|
||||||
int print_message(String message);
|
int print_message(String message);
|
||||||
int switch_screen(screen::lcd_screen new_screen);
|
int loop(unsigned long timeout_ms=500);
|
||||||
int render_task();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void lcd::clear() {
|
void lcd::clear() {
|
||||||
@@ -90,6 +94,49 @@ void lcd::print(int i, int base=10) {
|
|||||||
_dispaly_cleared = false;
|
_dispaly_cleared = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lcd::render_gps_debug() {
|
||||||
|
|
||||||
|
this->clear();
|
||||||
|
gps_data data;
|
||||||
|
gps_global_read(data);
|
||||||
|
|
||||||
|
_display->setCursor(0,0);
|
||||||
|
this->print("Alt: ");
|
||||||
|
if (data.altitude.valid) {
|
||||||
|
this->print(data.altitude.value, 5);
|
||||||
|
} else {
|
||||||
|
this->print("not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
_display->setCursor(0,1);
|
||||||
|
this->print("Lat: ");
|
||||||
|
if (data.lat.valid) {
|
||||||
|
this->print(data.lat.value, 5);
|
||||||
|
} else {
|
||||||
|
this->print("not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
_display->setCursor(0,2);
|
||||||
|
this->print("Lng: ");
|
||||||
|
if (data.lng.valid) {
|
||||||
|
this->print(data.lng.value, 5);
|
||||||
|
} else {
|
||||||
|
this->print("not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
_display->setCursor(0,3);
|
||||||
|
this->print("Spd: ");
|
||||||
|
if (data.speed.valid) {
|
||||||
|
this->print(data.speed.value, 5);
|
||||||
|
} else {
|
||||||
|
this->print("not valid");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int lcd::push(const Task& task) {
|
||||||
|
return _queue.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
lcd::lcd(): _logger(nullptr), _screen(screen::blank), _last_render(0), _frame_duration(500), _dispaly_cleared(false), _gps(nullptr) { _display = new LiquidCrystal_I2C(0x27, 20, 4); }
|
lcd::lcd(): _logger(nullptr), _screen(screen::blank), _last_render(0), _frame_duration(500), _dispaly_cleared(false), _gps(nullptr) { _display = new LiquidCrystal_I2C(0x27, 20, 4); }
|
||||||
|
|
||||||
lcd::lcd(system_logger *logger): _logger(logger), _screen(screen::blank), _last_render(0), _frame_duration(500), _dispaly_cleared(false), _gps(nullptr) {
|
lcd::lcd(system_logger *logger): _logger(logger), _screen(screen::blank), _last_render(0), _frame_duration(500), _dispaly_cleared(false), _gps(nullptr) {
|
||||||
@@ -196,13 +243,32 @@ int lcd::print_message(String message) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lcd::switch_screen(screen::lcd_screen new_screen) {
|
int lcd::loop(unsigned long timeout_ms) {
|
||||||
_screen = new_screen;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int lcd::render_task() {
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
unsigned long task_timeout = now + timeout_ms;
|
||||||
|
while (_queue.size() > 0) {
|
||||||
|
Task next_task;
|
||||||
|
int res = _queue.pop(next_task);
|
||||||
|
if (res != 0 ) {
|
||||||
|
if (millis() > task_timeout) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
switch (next_task.type)
|
||||||
|
{
|
||||||
|
case TASK_DISPLAY_GPS_DEBUG:
|
||||||
|
_screen = screen::gps_debug;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (millis() > task_timeout) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if (now < _last_render + _frame_duration) {
|
if (now < _last_render + _frame_duration) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -213,44 +279,7 @@ int lcd::render_task() {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case screen::gps_debug:
|
case screen::gps_debug:
|
||||||
this->clear();
|
this->render_gps_debug();
|
||||||
if (_gps == nullptr) {
|
|
||||||
this->print_message("No GPS Found");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
gps_data data = _gps->get_data();
|
|
||||||
|
|
||||||
_display->setCursor(0,0);
|
|
||||||
this->print("Alt: ");
|
|
||||||
if (data.altitude.valid) {
|
|
||||||
this->print(data.altitude.value, 5);
|
|
||||||
} else {
|
|
||||||
this->print("not valid");
|
|
||||||
}
|
|
||||||
|
|
||||||
_display->setCursor(0,1);
|
|
||||||
this->print("Lat: ");
|
|
||||||
if (data.lat.valid) {
|
|
||||||
this->print(data.lat.value, 5);
|
|
||||||
} else {
|
|
||||||
this->print("not valid");
|
|
||||||
}
|
|
||||||
|
|
||||||
_display->setCursor(0,2);
|
|
||||||
this->print("Lng: ");
|
|
||||||
if (data.lng.valid) {
|
|
||||||
this->print(data.lng.value, 5);
|
|
||||||
} else {
|
|
||||||
this->print("not valid");
|
|
||||||
}
|
|
||||||
|
|
||||||
_display->setCursor(0,3);
|
|
||||||
this->print("Spd: ");
|
|
||||||
if (data.speed.valid) {
|
|
||||||
this->print(data.speed.value, 5);
|
|
||||||
} else {
|
|
||||||
this->print("not valid");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "flags.h"
|
#include "flags.h"
|
||||||
|
#include "base/ring_buffer.h"
|
||||||
|
#include "base/task.h"
|
||||||
|
#include "base/module_base.h"
|
||||||
|
|
||||||
class system_logger
|
class system_logger
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user