diff --git a/src/main.cpp b/src/main.cpp index bfca733..2f43c44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,7 +116,7 @@ int main() // Communication interfaces ts_interfaces.enable(); - uext_init(); + uext.enable(); init_watchdog(10); // 10s should be enough for communication ports solar_terminal.init_solar(); @@ -146,7 +146,7 @@ int main() while (1) { ts_interfaces.process_asap(); - uext_process_asap(); + uext.process_asap(); time_t now = timestamp; if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere) @@ -170,7 +170,7 @@ int main() leds_update_1s(); leds_update_soc(charger.soc, load.state == LOAD_STATE_OFF_LOW_SOC); - uext_process_1s(); + uext.process_1s(); ts_interfaces.process_1s(); last_call = now; diff --git a/src/uext.cpp b/src/uext.cpp new file mode 100644 index 0000000..f650dd1 --- /dev/null +++ b/src/uext.cpp @@ -0,0 +1,68 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "uext.h" +#include + +// we use code to self register objects at construction time. +// It relies on the fact that initially this pointer will be initialized +// with NULL ! This is only true for global variables or static members of classes +// so this must remain a static class member +std::vector* UExtInterfaceManager::interfaces; + +// run the respective function on all objects in the "devices" list +// use the c++11 lambda expressions here for the for_each loop, keeps things compact + +void UExtInterfaceManager::process_asap() { + for_each(std::begin(*interfaces),std::end(*interfaces), [](UExtInterface* tsif) { + tsif->process_asap(); + }); +} + +void UExtInterfaceManager::enable() { + for_each(std::begin(*interfaces),std::end(*interfaces), [](UExtInterface* tsif) { + tsif->enable(); + }); +} + +void UExtInterfaceManager::process_1s() { + for_each(std::begin(*interfaces),std::end(*interfaces), [](UExtInterface* tsif) { + tsif->process_1s(); + }); +} + +void UExtInterfaceManager::check_list() { + if (UExtInterfaceManager::interfaces == NULL) + { + UExtInterfaceManager::interfaces = new std::vector; + } +} + +void UExtInterfaceManager::add_ext(UExtInterface* member) { + check_list(); + interfaces->push_back(member); +} + +UExtInterfaceManager::UExtInterfaceManager() { + check_list(); +} + +UExtInterface::UExtInterface() { + uext.add_ext(this); +} + +UExtInterfaceManager uext; + diff --git a/src/uext.h b/src/uext.h index a95d608..e46523e 100644 --- a/src/uext.h +++ b/src/uext.h @@ -17,29 +17,67 @@ #ifndef UEXT_H #define UEXT_H +#include + /** @file * * @brief * Communication interfaces connected to UEXT port */ -/* UEXT interface initialization - * - * This function is called only once at startup. - */ -void uext_init(); +class UExtInterface +{ + public: + virtual void process_asap() {}; + virtual void process_1s() {}; + virtual void enable() {}; -/** UEXT interface process function - * - * This function is called in each main loop, as soon as all other tasks finished. - */ -void uext_process_asap(); + UExtInterface(); + private: +}; -/** UEXT interface process function - * - * This function is called every second, if no other task was blocking for a longer time. - * It should be used for state machines, etc. - */ -void uext_process_1s(); +class UExtInterfaceManager +{ + public: + /** UEXT interface process function + * + * This function is called in each main loop, as soon as all other tasks finished. + */ + virtual void process_asap(); + + /** UEXT interface process function + * + * This function is called every second, if no other task was blocking for a longer time. + * It should be used for state machines, etc. + */ + virtual void process_1s(); + + /* UEXT interface initialization + * + * This function is called only once at startup. + */ + virtual void enable(); + + /** + * Adds an UExtInterface object to the list of managed extensions, + * automatically called by UExtInterface objects during construction + */ + static void add_ext(UExtInterface*); + + UExtInterfaceManager(); + + private: + /** + * Stores list of all UExtInterface objects + */ + static std::vector* interfaces; + + /** + * Makes sure there is an internal interfacts vector created, does nothing if it exists + */ + static void check_list(); +}; + +extern UExtInterfaceManager uext; #endif /* UEXT_H */ diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 46c2648..d5647de 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -20,11 +20,13 @@ #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size -#include "uext.h" +#include "uext_oled.h" #include "pcb.h" #include "Adafruit_SSD1306.h" +static UExtOled uext_oled; // local instance, will self register itself + const unsigned char bmp_load [] = { 0x20, 0x22, 0x04, 0x70, 0x88, 0x8B, 0x88, 0x70, 0x04, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -47,18 +49,18 @@ const unsigned char bmp_disconnected [] = { I2C i2c(PIN_UEXT_SDA, PIN_UEXT_SCL); Adafruit_SSD1306_I2c oled(i2c, PIN_UEXT_SSEL, 0x78, 64, 128); -void uext_init() -{ +UExtOled::UExtOled() {} + +void UExtOled::enable() { #ifdef PIN_UEXT_DIS DigitalOut uext_dis(PIN_UEXT_DIS); uext_dis = 0; #endif } -void uext_process_asap() {;} +void UExtOled::process_asap() {} -void uext_process_1s() -{ +void UExtOled::process_1s() { oled.clearDisplay(); oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1); diff --git a/src/uext/uext_oled.h b/src/uext/uext_oled.h new file mode 100644 index 0000000..252ac0c --- /dev/null +++ b/src/uext/uext_oled.h @@ -0,0 +1,39 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UEXT_OLED_H +#define UEXT_OLED_H + +#include "config.h" + +#ifdef OLED_ENABLED +#include "uext.h" + +class UExtOled: public UExtInterface +{ + public: + UExtOled(); + + void process_asap(); + void process_1s(); + + void enable(); + + private: +}; + +#endif /* OLED_ENABLED */ +#endif diff --git a/src/uext/uext_template.cpp b/src/uext/uext_template.cpp index 0730f66..a4ce20b 100644 --- a/src/uext/uext_template.cpp +++ b/src/uext/uext_template.cpp @@ -29,23 +29,34 @@ // Only compile if this UEXT interface is enabled in config.h #ifdef UEXT_DUMMY_ENABLED -void uext_init(void) -{ - #ifdef PIN_UEXT_DIS + + +#include "uext_template.h" + +static UExtDummy uext_dummy; // local instance, will self register itself + +/** + * Constructor, place basic initialization here, if necessary + */ +UExtDummy::UExtDummy() {} + + +/** + * Enable operation, place start of use code here, if necessary + */ +void UExtDummy::enable() { +#ifdef PIN_UEXT_DIS DigitalOut uext_dis(PIN_UEXT_DIS); uext_dis = 0; - #endif - +#endif // add you init functions here } -void uext_process_asap(void) -{ +void UExtDummy::process_asap(void) { // add functions to be called as soon as possible during each loop in main function } -void uext_process_1s(void) -{ +void UExtDummy::process_1s(void) { // add functions to be called every second here } diff --git a/src/uext/uext_template.h b/src/uext/uext_template.h new file mode 100644 index 0000000..b633674 --- /dev/null +++ b/src/uext/uext_template.h @@ -0,0 +1,39 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UEX_TEMPLATE_H +#define UEXT_TEMPLATE_H + +#include "config.h" + +#ifdef UEXT_DUMMY_ENABLED +#include "uext.h" + +class UExtDummy: public UExtInterface +{ + public: + UExtDummy(); + + void process_asap(); + void process_1s(); + + void enable(); + + private: +}; + +#endif /* UEXT_DUMMY_ENABLED */ +#endif diff --git a/src/uext/uext_wifi.cpp b/src/uext/uext_wifi.cpp index d5fc351..8da0677 100644 --- a/src/uext/uext_wifi.cpp +++ b/src/uext/uext_wifi.cpp @@ -1,11 +1,12 @@ #ifndef UNIT_TEST -#include "uext.h" #include "config.h" #ifdef WIFI_ENABLED +#include "uext/uext_wifi.h" + #include "load.h" #include "thingset.h" #include @@ -159,18 +160,20 @@ int wifi_reset(void) return res; } -void uext_init() -{ +static UExtWifi uext_wifi; // local instance, will self register itself + +UExtWifi::UExtWifi() {} + +void UExtWifi::enable() { #ifdef PIN_UEXT_DIS DigitalOut uext_dis(PIN_UEXT_DIS); uext_dis = 0; #endif } -void uext_process_asap(void) {;} +void UExtWifi::process_asap(void) {} -void uext_process_1s() -{ +void UExtWifi::process_1s() { static WifiState state = STATE_WIFI_RESET; static int error_counter = 0; diff --git a/src/uext/uext_wifi.h b/src/uext/uext_wifi.h new file mode 100644 index 0000000..e34b0fb --- /dev/null +++ b/src/uext/uext_wifi.h @@ -0,0 +1,39 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UEXT_WIFI_H +#define UEXT_WIFI_H + +#include "config.h" + +#ifdef WIFI_ENABLED +#include "uext.h" + +class UExtWifi: public UExtInterface +{ + public: + UExtWifi(); + + void process_asap(); + void process_1s(); + + void enable(); + + private: +}; + +#endif /* WIFI_ENABLED */ +#endif