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