Multiple UExt objects at same time (#68)

The change permits use of multiple UExt objects at the same time,
given that these use different pins.
The current UExt code is safe to be used in combination
(OLED display and ESP32 wifi).
Future extension may require some checks to be put in place to
ensure correct configuration.
Could be achieved using preprocessor macros easily.

Uses same objectification concept we used for the ThingSet devices.
Could be merged into one if desired.
This commit is contained in:
hogthrob
2019-11-04 09:23:37 +01:00
committed by Martin Jäger
parent bec02cb25b
commit bc8f74aa45
9 changed files with 279 additions and 40 deletions
+3 -3
View File
@@ -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;
+68
View File
@@ -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 <algorithm>
// 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<UExtInterface*>* 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<UExtInterface*>;
}
}
void UExtInterfaceManager::add_ext(UExtInterface* member) {
check_list();
interfaces->push_back(member);
}
UExtInterfaceManager::UExtInterfaceManager() {
check_list();
}
UExtInterface::UExtInterface() {
uext.add_ext(this);
}
UExtInterfaceManager uext;
+54 -16
View File
@@ -17,29 +17,67 @@
#ifndef UEXT_H
#define UEXT_H
#include <vector>
/** @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<UExtInterface*>* 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 */
+8 -6
View File
@@ -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);
+39
View File
@@ -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
+20 -9
View File
@@ -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
}
+39
View File
@@ -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
+9 -6
View File
@@ -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 <inttypes.h>
@@ -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;
+39
View File
@@ -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