ThingSetDevice classes

These replace the previous C impementation.
- permits any combination of thingset devices being present
- removes essentially all thingset related #ifdefs outside the actual implementation files.
- Code size is reduced. Code duplication eliminated.

The code is not final in its structure, but works with CAN and Serial.
USBSerial does not work on the F072 (L073 not tested) but this was also
the case for the C implementation.

It is required to apply the changes to the platformio.ini otherwise
the code will not compile.
This commit is contained in:
Hogthrob
2019-08-28 17:37:05 +02:00
committed by Martin Jaeger
parent 01edcbcf07
commit a1098aaf71
8 changed files with 216 additions and 214 deletions
+2 -1
View File
@@ -38,7 +38,8 @@ monitor_speed = 115200
# Compiler settings
build_flags =
-std=gnu++11
-std=gnu++1z
-Wno-register
-fsingle-precision-constant
-Wl,-Map,memory.map
-D TURN_OFF_MBED_DEPRECATED_WARNING
+5 -14
View File
@@ -32,14 +32,9 @@
#include "thingset.h"
#include "thingset_can.h"
extern Serial serial;
#ifndef CAN_SPEED
#define CAN_SPEED 250000 // 250 kHz
#endif
#ifndef CAN_NODE_ID
#define CAN_NODE_ID 10
#endif
class ThingSetCAN_Device
{
@@ -62,9 +57,7 @@ DigitalOut can_disable(PIN_CAN_STB);
extern ThingSet ts;
ThingSetCAN ts_can(CAN_NODE_ID);
ThingSetCAN::ThingSetCAN(uint8_t can_node_id): node_id(can_node_id)
ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c)
{
can_disable = 1; // we disable the transceiver
can.mode(CAN::Normal);
@@ -81,8 +74,8 @@ void ThingSetCAN::enable()
void ThingSetCAN::process_1s()
{
ts_can.pub();
ts_can.process_asap();
pub();
process_asap();
}
bool ThingSetCAN::pub_object(const data_object_t& data_obj)
@@ -107,12 +100,10 @@ bool ThingSetCAN::pub_object(const data_object_t& data_obj)
/**
* returns number of can objects added to queue
*/
extern const int PUB_CHANNEL_CAN;
int ThingSetCAN::pub()
{
int retval = 0;
ts_pub_channel_t* can_chan = ts.get_pub_channel(PUB_CHANNEL_CAN);
ts_pub_channel_t* can_chan = ts.get_pub_channel(channel);
if (can_chan != NULL)
{
@@ -121,7 +112,7 @@ int ThingSetCAN::pub()
const data_object_t *data_obj = ts.get_data_object(can_chan->object_ids[element]);
if (data_obj != NULL && data_obj->access & TS_ACCESS_READ)
{
if (ts_can.pub_object(*data_obj))
if (pub_object(*data_obj))
{
retval++;
}
+7 -13
View File
@@ -42,7 +42,7 @@
#include "thingset_serial.h" // UART or USB serial communication
#include "thingset_can.h" // CAN bus communication
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200);
Dcdc dcdc = {};
DcBus hv_bus = {}; // high voltage side (solar for typical MPPT)
@@ -102,8 +102,6 @@ void system_control()
*/
int main()
{
serial.baud(115200);
leds_init();
battery_conf_init(&bat_conf, BATTERY_TYPE, BATTERY_NUM_CELLS, BATTERY_CAPACITY);
@@ -134,14 +132,8 @@ int main()
calibrate_current_sensors();
// Communication interfaces
#ifdef UART_SERIAL_ENABLED
thingset_serial_init(&serial);
#endif
#ifdef CAN_ENABLED
ts_can.enable();
#endif
ts_devices.enable();
uext_init();
init_watchdog(10); // 10s should be enough for communication ports
@@ -172,11 +164,13 @@ int main()
control_timer_start(CONTROL_FREQUENCY);
wait(0.1); // necessary to prevent MCU from randomly getting stuck here if PV panel is connected before battery
sleep_manager_lock_deep_sleep();
// the main loop is suitable for slow tasks like communication (even blocking wait allowed)
time_t last_call = timestamp;
while (1) {
thingset_serial_process_asap();
ts_devices.process_asap();
uext_process_asap();
time_t now = timestamp;
@@ -194,7 +188,7 @@ int main()
leds_update_soc(charger.soc, load.switch_state == LOAD_STATE_OFF_LOW_SOC);
uext_process_1s();
thingset_serial_process_1s();
ts_devices.process_1s();
last_call = now;
}
+6 -5
View File
@@ -16,14 +16,17 @@
#ifndef _THINGSET_CAN_H_
#define _THINGSET_CAN_H_
#include "config.h"
#ifdef CAN_ENABLED
#include "data_objects.h"
#include "can_msg_queue.h"
#include "thingset_device.h"
class ThingSetCAN
class ThingSetCAN: public ThingSetDevice
{
public:
ThingSetCAN(uint8_t can_node_id);
ThingSetCAN(uint8_t can_node_id, const unsigned int c);
void process_asap();
void process_1s();
@@ -38,9 +41,7 @@ class ThingSetCAN
CANMsgQueue tx_queue;
// CANMsgQueue rx_queue;
uint8_t node_id;
const unsigned int channel;
};
extern ThingSetCAN ts_can;
#endif /* CAN_ENABLED */
#endif
+89
View File
@@ -0,0 +1,89 @@
/* 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 UNIT_TEST
#include "config.h"
#include "mbed.h"
#include "thingset.h"
#include "thingset_device.h"
/*
* Construct all global ThingSetDevices here.
* All of these are added to the list of devices
* for later processing in the normal operation
*/
#ifdef CAN_ENABLED
#include "thingset_can.h"
extern const unsigned int PUB_CHANNEL_CAN;
#ifndef CAN_NODE_ID
#define CAN_NODE_ID 10
#endif
ThingSetCAN ts_can(CAN_NODE_ID, PUB_CHANNEL_CAN);
#endif
#ifdef UART_SERIAL_ENABLED
#include "thingset_serial.h"
extern const int pub_channel_serial;
extern Serial serial;
ThingSetSerial ts_uart(serial, pub_channel_serial);
#endif /* UART_SERIAL_ENABLED */
#ifdef USB_SERIAL_ENABLED
#include "thingset_serial.h"
#include "USBSerial.h"
extern const int pub_channel_serial;
USBSerial ser_usb(0x1f00, 0x2012, 0x0001, false); // connection is not blocked when USB is not plugged in
ThingSetSerial ts_usb(ser_usb, pub_channel_serial);
#endif /* USB_SERIAL_ENABLED */
// we use ifdef here in order to avoid using some dynamically
// allocated list. Only std::vector works with this code if NO devices are enabled
// std::array and also plain C arrays work if at least one array element is there.
std::vector<ThingSetDevice*> ThingSetDeviceManager::devices[] =
{
#ifdef UART_SERIAL_ENABLED
&ts_uart,
#endif
#ifdef USB_SERIAL_ENABLED
&ts_usb,
#endif
#ifdef CAN_ENABLED
&ts_can,
#endif
};
// 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 ThingSetDeviceManager::process_asap() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev){ dev->process_asap(); }); }
void ThingSetDeviceManager::enable() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev){ dev->enable(); }); }
void ThingSetDeviceManager::process_1s()
{
for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev) { dev->process_1s(); });
fflush(stdout);
}
ThingSetDeviceManager ts_devices;
#endif /* UNIT_TEST */
+43
View File
@@ -0,0 +1,43 @@
/* 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 _THINGSET_DEVICE_H_
#define _THINGSET_DEVICE_H_
#include <vector>
class ThingSetDevice
{
public:
virtual void process_asap() {};
virtual void process_1s() {};
virtual void enable() {};
private:
};
class ThingSetDeviceManager: public ThingSetDevice
{
public:
virtual void process_asap();
virtual void process_1s();
virtual void enable();
private:
static std::vector<ThingSetDevice*> devices;
};
extern ThingSetDeviceManager ts_devices;
#endif
+32 -166
View File
@@ -16,197 +16,63 @@
#ifndef UNIT_TEST
#include "config.h"
#include "mbed.h"
#include "thingset.h"
#include "config.h"
#include "thingset_serial.h"
#ifdef USB_SERIAL_ENABLED
#include "USBSerial.h"
USBSerial ser_usb(0x1f00, 0x2012, 0x0001, false); // connection is not blocked when USB is not plugged in
uint8_t buf_req_usb[500];
size_t req_usb_pos = 0;
#endif
#ifdef UART_SERIAL_ENABLED
Serial* ser_uart;
uint8_t buf_req_uart[500];
size_t req_uart_pos = 0;
#endif
static uint8_t buf_resp[1000]; // only one response buffer needed for USB and UART
uint8_t ThingSetStream::buf_resp[1000];
extern ThingSet ts;
extern ts_pub_channel_t pub_channels[];
extern const int pub_channel_serial;
#ifdef CAN_ENABLED
#include "thingset_can.h"
#endif
void thingset_serial_init(Serial* s)
void ThingSetStream::process_1s()
{
#ifdef UART_SERIAL_ENABLED
uart_serial_init(s);
#endif
#ifdef USB_SERIAL_ENABLED
usb_serial_init();
#endif
}
void thingset_serial_process_asap()
{
#ifdef UART_SERIAL_ENABLED
uart_serial_process();
#endif
#ifdef USB_SERIAL_ENABLED
usb_serial_process();
#endif
#ifdef CAN_ENABLED
ts_can.process_asap();
#endif
}
void thingset_serial_process_1s()
{
#ifdef UART_SERIAL_ENABLED
uart_serial_pub();
#endif
#ifdef USB_SERIAL_ENABLED
usb_serial_pub();
#endif
#ifdef CAN_ENABLED
ts_can.process_1s();
#endif
fflush(stdout);
}
#ifdef UART_SERIAL_ENABLED
static bool uart_serial_command_flag = false;
void uart_serial_isr()
{
while (ser_uart->readable() && uart_serial_command_flag == false) {
if (req_uart_pos < sizeof(buf_req_uart)) {
buf_req_uart[req_uart_pos] = ser_uart->getc();
if (buf_req_uart[req_uart_pos] == '\n') {
if (req_uart_pos > 0 && buf_req_uart[req_uart_pos-1] == '\r')
buf_req_uart[req_uart_pos-1] = '\0';
else
buf_req_uart[req_uart_pos] = '\0';
// start processing
uart_serial_command_flag = true;
}
else if (req_uart_pos > 0 && buf_req_uart[req_uart_pos] == '\b') { // backspace
req_uart_pos--;
}
else {
req_uart_pos++;
}
}
}
}
#endif
#ifdef UART_SERIAL_ENABLED
void uart_serial_init(Serial* s)
{
ser_uart = s;
s->attach(uart_serial_isr);
}
#endif
#ifdef UART_SERIAL_ENABLED
extern const int PUB_CHANNEL_SERIAL;
void uart_serial_pub()
{
if (pub_channels[pub_channel_serial].enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), 0);
ser_uart->printf("%s\n", buf_resp);
if (ts.get_pub_channel(channel)->enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), channel);
stream->puts((const char*)buf_resp);
stream->putc('\n');
}
stream->puts(".\n");
}
void uart_serial_process()
void ThingSetStream::process_asap()
{
if (uart_serial_command_flag) {
if (req_uart_pos > 1) {
ser_uart->printf("Received Request (%d bytes): %s\n", strlen((char *)buf_req_uart), buf_req_uart);
ts.process(buf_req_uart, strlen((char *)buf_req_uart), buf_resp, sizeof(buf_resp));
ser_uart->printf("%s\n", buf_resp);
if (command_flag) {
if (req_pos > 1) {
stream->printf("Received Request (%d bytes): %s\n", strlen((char *)buf_req), buf_req);
ts.process(buf_req, strlen((char *)buf_req), buf_resp, sizeof(buf_resp));
stream->puts((const char*)buf_resp);
stream->putc('\n');
}
// start listening for new commands
uart_serial_command_flag = false;
req_uart_pos = 0;
command_flag = false;
req_pos = 0;
}
}
#endif /* UART_SERIAL_ENABLED */
#ifdef USB_SERIAL_ENABLED
static bool usb_serial_command_flag = false;
void usb_serial_isr()
void ThingSetStream::process_input()
{
while (ser_usb.readable() && usb_serial_command_flag == false) {
if (req_usb_pos < 100) {
buf_req_usb[req_usb_pos] = ser_usb.getc();
//ser_usb->putc(req_usb.data[req_usb_pos]); // echo back the character
if (buf_req_usb[req_usb_pos] == '\n') {
if (req_usb_pos > 0 && buf_req_usb[req_usb_pos-1] == '\r')
buf_req_usb[req_usb_pos-1] = '\0';
while (stream->readable() && command_flag == false) {
if (req_pos < sizeof(buf_req)) {
buf_req[req_pos] = stream->getc();
if (buf_req[req_pos] == '\n') {
if (req_pos > 0 && buf_req[req_pos-1] == '\r')
buf_req[req_pos-1] = '\0';
else
buf_req_usb[req_usb_pos] = '\0';
req_usb_pos = 0;
usb_serial_command_flag = true;
buf_req[req_pos] = '\0';
// start processing
command_flag = true;
}
else if (req_usb_pos > 0 && buf_req_usb[req_usb_pos] == '\b') { // backspace
req_usb_pos--;
else if (req_pos > 0 && buf_req[req_pos] == '\b') { // backspace
req_pos--;
}
else {
req_usb_pos++;
req_pos++;
}
}
}
}
void usb_serial_init()
{
ser_usb.attach(usb_serial_isr);
// buf_req_usb = buf_req_uart;
}
void usb_serial_process()
{
if (usb_serial_command_flag) {
//ser->printf("Received Command: %s\n", buf_serial);
//int len_req = sizeof(req_usb.data);
if (req_usb_pos > 1) {
ser_usb.printf("Received Request (%d bytes): %s\n", strlen((char *)buf_req_usb), buf_req_usb);
ts.process(buf_req_usb, strlen((char *)buf_req_usb), buf_resp, sizeof(buf_resp));
ser_usb.printf("%s\n", buf_resp);
}
usb_serial_command_flag = false;
req_usb_pos = 0;
}
}
void usb_serial_pub() {
if (pub_channels[pub_channel_serial].enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), pub_channel_serial);
ser_usb.printf("%s\n", buf_resp);
}
}
#endif /* USB_SERIAL_ENABLED */
#endif /* UNIT_TEST */
+32 -15
View File
@@ -24,24 +24,41 @@
*/
#include "mbed.h"
#include "thingset_device.h"
/** UART serial interface (either in UEXT connector or from additional SWD serial)
*/
void thingset_serial_init(Serial* s);
void thingset_serial_process_asap();
void thingset_serial_process_1s();
class ThingSetStream: public ThingSetDevice
{
public:
ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {};
virtual void process_asap();
virtual void process_1s();
/** UART serial interface (either in UEXT connector or from additional SWD serial)
*/
void uart_serial_init(Serial* s);
void uart_serial_process();
void uart_serial_pub();
protected:
virtual void process_input(); // this is called from the ISR typically
const unsigned int channel;
/** Serial interface via USB CDC device class (currently only supported with STM32F0)
*/
void usb_serial_init();
void usb_serial_process();
void usb_serial_pub();
private:
Stream* stream;
static uint8_t buf_resp[1000]; // only one response buffer needed for all objects
uint8_t buf_req[500];
size_t req_pos = 0;
bool command_flag = false;
};
template<typename T> class ThingSetSerial: public ThingSetStream
{
public:
ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(&s) {}
virtual void enable() { {
Callback<void()> cb([this]() -> void { this->process_input();});
// ser->attach(cb);
}
}
private:
T* ser;
};
#endif /* THINGSET_SERIAL_H */