From 3f89943edf4d9b8fc880b624a5fa037e66655869 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Sun, 24 Nov 2019 13:52:14 +0100 Subject: [PATCH] ThingSet interfaces merged with Ext interfaces --- src/ext/{thingset_can.cpp => can.cpp} | 49 ++++++++- src/ext/{thingset_serial.cpp => serial.cpp} | 74 +++++++++++++- src/ext/thingset_can.h | 57 ----------- src/ext/thingset_interface.cpp | 108 -------------------- src/ext/thingset_interface.h | 44 -------- src/ext/thingset_serial.h | 76 -------------- src/main_mbed.cpp | 9 +- test/main.cpp | 1 - 8 files changed, 119 insertions(+), 299 deletions(-) rename src/ext/{thingset_can.cpp => can.cpp} (89%) rename src/ext/{thingset_serial.cpp => serial.cpp} (61%) delete mode 100644 src/ext/thingset_can.h delete mode 100644 src/ext/thingset_interface.cpp delete mode 100644 src/ext/thingset_interface.h delete mode 100644 src/ext/thingset_serial.h diff --git a/src/ext/thingset_can.cpp b/src/ext/can.cpp similarity index 89% rename from src/ext/thingset_can.cpp rename to src/ext/can.cpp index 5bbbceb..9f9cd76 100644 --- a/src/ext/thingset_can.cpp +++ b/src/ext/can.cpp @@ -14,7 +14,9 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#ifdef __MBED__ + +#include "mbed.h" #include "config.h" @@ -25,17 +27,56 @@ #error The hardware does not support CAN, please undefine CAN_ENABLED #endif -#include "mbed.h" #include "can_msg_queue.h" //#include "can_iso_tp.h" #include "thingset.h" -#include "thingset_can.h" #ifndef CAN_SPEED #define CAN_SPEED 250000 // 250 kHz #endif +#include "data_objects.h" +#include "can_msg_queue.h" +#include "ext.h" + +class ThingSetCAN: public ExtInterface +{ + public: + ThingSetCAN(uint8_t can_node_id, const unsigned int c); + + void process_asap(); + void process_1s(); + + void enable(); + + private: + bool pub_object(const data_object_t& data_obj); + int pub(); + void process_outbox(); + + #if defined(CAN_RECEIVE) + void process_inbox(); + void process_input(); + void send_object_name(int data_obj_id, uint8_t can_dest_id); + CANMsgQueue rx_queue; + #endif + + CANMsgQueue tx_queue; + uint8_t node_id; + const unsigned int channel; + CAN can; + DigitalOut can_disable; +}; + + +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); + + class ThingSetCAN_Device { CAN can; @@ -263,4 +304,4 @@ void ThingSetCAN::process_input() #endif /* CAN_RECEIVE */ #endif /* CAN_ENABLED */ -#endif /* UNIT_TEST */ +#endif /* __MBED__ */ diff --git a/src/ext/thingset_serial.cpp b/src/ext/serial.cpp similarity index 61% rename from src/ext/thingset_serial.cpp rename to src/ext/serial.cpp index 3fb07e8..1be77e0 100644 --- a/src/ext/thingset_serial.cpp +++ b/src/ext/serial.cpp @@ -16,10 +16,80 @@ #ifdef __MBED__ -#include "thingset_serial.h" - +#include "mbed.h" #include "config.h" #include "thingset.h" +#include "ext.h" + +class ThingSetStream: public ExtInterface +{ + public: + ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {}; + + virtual void process_asap(); + virtual void process_1s(); + + protected: + virtual void process_input(); // this is called from the ISR typically + const unsigned int channel; + + virtual bool readable() + { + return stream->readable(); + } + + private: + Stream* stream; + + static char buf_resp[1000]; // only one response buffer needed for all objects + char buf_req[500]; + size_t req_pos = 0; + bool command_flag = false; +}; + +template class ThingSetSerial: public ThingSetStream +{ + public: + ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(s) {} + + void enable() { + Callback cb([this]() -> void { this->process_input();}); + ser.attach(cb); + } + + private: + bool readable() + { + return ser.readable(); + } + + private: + T& ser; +}; + + +/* + * Construct all global ExtInterfaces here. + * All of these are added to the list of devices + * for later processing in the normal operation + */ + +#ifdef UART_SERIAL_ENABLED + 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 "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 */ char ThingSetStream::buf_resp[1000]; diff --git a/src/ext/thingset_can.h b/src/ext/thingset_can.h deleted file mode 100644 index 5861e95..0000000 --- a/src/ext/thingset_can.h +++ /dev/null @@ -1,57 +0,0 @@ -/* 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_CAN_H -#define THINGSET_CAN_H - -#include "config.h" - -#ifdef CAN_ENABLED -#include "data_objects.h" -#include "can_msg_queue.h" -#include "thingset_interface.h" - -class ThingSetCAN: public ThingSetInterface -{ - public: - ThingSetCAN(uint8_t can_node_id, const unsigned int c); - - void process_asap(); - void process_1s(); - - void enable(); - - private: - bool pub_object(const data_object_t& data_obj); - int pub(); - void process_outbox(); - - #if defined(CAN_RECEIVE) - void process_inbox(); - void process_input(); - void send_object_name(int data_obj_id, uint8_t can_dest_id); - CANMsgQueue rx_queue; - #endif - - CANMsgQueue tx_queue; - uint8_t node_id; - const unsigned int channel; - CAN can; - DigitalOut can_disable; -}; - -#endif /* CAN_ENABLED */ -#endif diff --git a/src/ext/thingset_interface.cpp b/src/ext/thingset_interface.cpp deleted file mode 100644 index e92f88f..0000000 --- a/src/ext/thingset_interface.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* 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 "thingset_interface.h" - -#include // for for_each loop - -#ifdef __MBED__ -#include "mbed.h" -#endif - -#include "config.h" -#include "thingset.h" - -/* - * Construct all global ThingSetInterfaces 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 ThingSetInterfaceManager::interfaces = -{ -#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 ThingSetInterfaceManager::process_asap() -{ - for_each(std::begin(interfaces),std::end(interfaces), [](ThingSetInterface* tsif) { - tsif->process_asap(); - }); -} - -void ThingSetInterfaceManager::enable() -{ - for_each(std::begin(interfaces),std::end(interfaces), [](ThingSetInterface* tsif) { - tsif->enable(); - }); -} - -void ThingSetInterfaceManager::process_1s() -{ - for_each(std::begin(interfaces),std::end(interfaces), [](ThingSetInterface* tsif) { - tsif->process_1s(); - }); -} - -ThingSetInterfaceManager ts_interfaces; - -#endif /* UNIT_TEST */ diff --git a/src/ext/thingset_interface.h b/src/ext/thingset_interface.h deleted file mode 100644 index 54a6ad4..0000000 --- a/src/ext/thingset_interface.h +++ /dev/null @@ -1,44 +0,0 @@ -/* 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_INTERFACE_H -#define THINGSET_INTERFACE_H - -#include - -class ThingSetInterface -{ - public: - virtual void process_asap() {}; - virtual void process_1s() {}; - virtual void enable() {}; - private: - -}; - -class ThingSetInterfaceManager: public ThingSetInterface -{ - public: - virtual void process_asap(); - virtual void process_1s(); - virtual void enable(); - - private: - static std::vector interfaces; -}; - -extern ThingSetInterfaceManager ts_interfaces; -#endif diff --git a/src/ext/thingset_serial.h b/src/ext/thingset_serial.h deleted file mode 100644 index 9efa481..0000000 --- a/src/ext/thingset_serial.h +++ /dev/null @@ -1,76 +0,0 @@ -/* 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_SERIAL_H -#define THINGSET_SERIAL_H - -/** @file - * - * @brief - * ThingSet protocol based communication via UART or USB serial port - */ - -#include "mbed.h" - -#include "thingset_interface.h" - -class ThingSetStream: public ThingSetInterface -{ - public: - ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {}; - - virtual void process_asap(); - virtual void process_1s(); - - protected: - virtual void process_input(); // this is called from the ISR typically - const unsigned int channel; - - virtual bool readable() - { - return stream->readable(); - } - - private: - Stream* stream; - - static char buf_resp[1000]; // only one response buffer needed for all objects - char buf_req[500]; - size_t req_pos = 0; - bool command_flag = false; -}; - -template class ThingSetSerial: public ThingSetStream -{ - public: - ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(s) {} - - void enable() { - Callback cb([this]() -> void { this->process_input();}); - ser.attach(cb); - } - - private: - bool readable() - { - return ser.readable(); - } - - private: - T& ser; -}; - -#endif /* THINGSET_SERIAL_H */ diff --git a/src/main_mbed.cpp b/src/main_mbed.cpp index e41a04c..70d885b 100644 --- a/src/main_mbed.cpp +++ b/src/main_mbed.cpp @@ -39,8 +39,6 @@ #include "leds.h" // LED switching using charlieplexing #include "device_status.h" // log data (error memory, min/max measurements, etc.) #include "data_objects.h" // for access to internal data via ThingSet -#include "ext/thingset_serial.h" // UART or USB serial communication -#include "ext/thingset_can.h" // CAN bus communication #ifdef BOOTLOADER_ENABLED #include "bl_support.h" // Bootloader support from the application side @@ -113,10 +111,9 @@ int main() update_measurements(); calibrate_current_sensors(); - // Communication interfaces - ts_interfaces.enable(); - + // initialize all extensions and external communication interfaces ext_mgr.enable_all(); + init_watchdog(10); // 10s should be enough for communication ports solar_terminal.init_solar(); @@ -141,7 +138,6 @@ int main() time_t last_call = timestamp; while (1) { - ts_interfaces.process_asap(); ext_mgr.process_asap(); time_t now = timestamp; @@ -164,7 +160,6 @@ int main() leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC)); ext_mgr.process_1s(); - ts_interfaces.process_1s(); last_call = now; } diff --git a/test/main.cpp b/test/main.cpp index 194f3e1..dad90f3 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -8,7 +8,6 @@ #include "pwm_switch.h" // PWM charge controller #include "bat_charger.h" // battery settings and charger state machine #include "adc_dma.h" // ADC using DMA and conversion to measurement values -#include "ext/uext.h" // communication interfaces, displays, etc. in UEXT connector #include "eeprom.h" // external I2C EEPROM #include "load.h" // load and USB output management #include "leds.h" // LED switching using charlieplexing