From 29e186624ce51d02e21606004a54b0d96557b4b0 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Thu, 17 Oct 2019 10:36:48 +0200 Subject: [PATCH] ThingSet device renamed to interface Reason: According to ThingSet spec, a device is a complete physical hardware device like a charge controller. Within one device, we can have different communication interfaces that use the ThingSet protocol. --- src/main.cpp | 6 +-- src/{interface_can.cpp => thingset_can.cpp} | 22 ++++++----- src/thingset_can.h | 16 ++++---- ...gset_device.cpp => thingset_interface.cpp} | 37 ++++++++++++++----- ...thingset_device.h => thingset_interface.h} | 17 +++++---- src/thingset_serial.h | 10 ++--- 6 files changed, 65 insertions(+), 43 deletions(-) rename src/{interface_can.cpp => thingset_can.cpp} (96%) rename src/{thingset_device.cpp => thingset_interface.cpp} (72%) rename src/{thingset_device.h => thingset_interface.h} (79%) diff --git a/src/main.cpp b/src/main.cpp index b990b1e..bf670f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -134,7 +134,7 @@ int main() calibrate_current_sensors(); // Communication interfaces - ts_devices.enable(); + ts_interfaces.enable(); uext_init(); init_watchdog(10); // 10s should be enough for communication ports @@ -181,7 +181,7 @@ int main() time_t last_call = timestamp; while (1) { - ts_devices.process_asap(); + ts_interfaces.process_asap(); uext_process_asap(); time_t now = timestamp; @@ -202,7 +202,7 @@ int main() leds_update_soc(charger.soc, load.switch_state == LOAD_STATE_OFF_LOW_SOC); uext_process_1s(); - ts_devices.process_1s(); + ts_interfaces.process_1s(); last_call = now; } diff --git a/src/interface_can.cpp b/src/thingset_can.cpp similarity index 96% rename from src/interface_can.cpp rename to src/thingset_can.cpp index 9884636..5bbbceb 100644 --- a/src/interface_can.cpp +++ b/src/thingset_can.cpp @@ -45,7 +45,6 @@ class ThingSetCAN_Device }; - //---------------------------------------------------------------------------- // preliminary simple CAN functions to send data to the bus for logging // Data format based on CBOR specification (except for first byte, which uses @@ -56,8 +55,11 @@ class ThingSetCAN_Device extern ThingSet ts; -ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c), can(PIN_CAN_RX, PIN_CAN_TX, CAN_SPEED), -can_disable(PIN_CAN_STB) +ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): + node_id(can_node_id), + channel(c), + can(PIN_CAN_RX, PIN_CAN_TX, CAN_SPEED), + can_disable(PIN_CAN_STB) { can_disable = 1; // we disable the transceiver can.mode(CAN::Normal); @@ -70,7 +72,7 @@ void ThingSetCAN::enable() { can_disable = 0; // we enable the transceiver #if defined(CAN_RECEIVE) - can.attach([this](){ this->process_input(); } ); + can.attach([this](){ this->process_input(); } ); #endif } @@ -97,8 +99,6 @@ bool ThingSetCAN::pub_object(const data_object_t& data_obj) } - - /** * returns number of can objects added to queue */ @@ -117,7 +117,7 @@ int ThingSetCAN::pub() if (pub_object(*data_obj)) { retval++; - } + } } } } @@ -160,7 +160,7 @@ void ThingSetCAN::send_object_name(int data_obj_id, uint8_t can_dest_id) msg.id = msg_priority << 26 | function_id << 16 |(can_dest_id << 8)| node_id; // TODO: add destination node ID const data_object_t *dop = ts.get_data_object(data_obj_id); - + if (dop != NULL) { if (dop->access & TS_ACCESS_READ) { msg.data[2] = TS_T_STRING; @@ -236,7 +236,7 @@ void ThingSetCAN::process_inbox() } } break; - } + } case TS_NAME: data_obj_id = msg.data[1] + (msg.data[2] << 8); send_object_name(data_obj_id, can_dest_id); @@ -247,7 +247,9 @@ void ThingSetCAN::process_inbox() max_attempts--; } } -void ThingSetCAN::process_input() { + +void ThingSetCAN::process_input() +{ CANMessage msg; while (can.read(msg)) { if (!rx_queue.full()) { diff --git a/src/thingset_can.h b/src/thingset_can.h index b885275..5861e95 100644 --- a/src/thingset_can.h +++ b/src/thingset_can.h @@ -13,17 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _THINGSET_CAN_H_ -#define _THINGSET_CAN_H_ + +#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" +#include "thingset_interface.h" -class ThingSetCAN: public ThingSetDevice +class ThingSetCAN: public ThingSetInterface { public: ThingSetCAN(uint8_t can_node_id, const unsigned int c); @@ -37,7 +38,7 @@ class ThingSetCAN: public ThingSetDevice bool pub_object(const data_object_t& data_obj); int pub(); void process_outbox(); - + #if defined(CAN_RECEIVE) void process_inbox(); void process_input(); @@ -46,10 +47,11 @@ class ThingSetCAN: public ThingSetDevice #endif CANMsgQueue tx_queue; - uint8_t node_id; + uint8_t node_id; const unsigned int channel; CAN can; DigitalOut can_disable; }; + #endif /* CAN_ENABLED */ -#endif \ No newline at end of file +#endif diff --git a/src/thingset_device.cpp b/src/thingset_interface.cpp similarity index 72% rename from src/thingset_device.cpp rename to src/thingset_interface.cpp index 4214a95..fdd5410 100644 --- a/src/thingset_device.cpp +++ b/src/thingset_interface.cpp @@ -19,10 +19,10 @@ #include "config.h" #include "mbed.h" #include "thingset.h" -#include "thingset_device.h" +#include "thingset_interface.h" /* - * Construct all global ThingSetDevices here. + * Construct all global ThingSetInterfaces here. * All of these are added to the list of devices * for later processing in the normal operation */ @@ -56,11 +56,11 @@ #endif /* USB_SERIAL_ENABLED */ -// we use ifdef here in order to avoid using some dynamically +// 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 ThingSetDeviceManager::devices = + +std::vector ThingSetInterfaceManager::interfaces = { #ifdef UART_SERIAL_ENABLED &ts_uart, @@ -74,12 +74,29 @@ std::vector ThingSetDeviceManager::devices = }; // 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 +// 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(); }); } +void ThingSetInterfaceManager::process_asap() +{ + for_each(std::begin(interfaces),std::end(interfaces), [](ThingSetInterface* tsif) { + tsif->process_asap(); + }); +} -ThingSetDeviceManager ts_devices; +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/thingset_device.h b/src/thingset_interface.h similarity index 79% rename from src/thingset_device.h rename to src/thingset_interface.h index 9718c55..54a6ad4 100644 --- a/src/thingset_device.h +++ b/src/thingset_interface.h @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef _THINGSET_DEVICE_H_ -#define _THINGSET_DEVICE_H_ + +#ifndef THINGSET_INTERFACE_H +#define THINGSET_INTERFACE_H #include -class ThingSetDevice +class ThingSetInterface { public: virtual void process_asap() {}; @@ -28,16 +29,16 @@ class ThingSetDevice }; -class ThingSetDeviceManager: public ThingSetDevice +class ThingSetInterfaceManager: public ThingSetInterface { public: virtual void process_asap(); virtual void process_1s(); - virtual void enable(); + private: - static std::vector devices; + static std::vector interfaces; }; -extern ThingSetDeviceManager ts_devices; -#endif \ No newline at end of file +extern ThingSetInterfaceManager ts_interfaces; +#endif diff --git a/src/thingset_serial.h b/src/thingset_serial.h index a489a07..d87d657 100644 --- a/src/thingset_serial.h +++ b/src/thingset_serial.h @@ -24,9 +24,9 @@ */ #include "mbed.h" -#include "thingset_device.h" +#include "thingset_interface.h" -class ThingSetStream: public ThingSetDevice +class ThingSetStream: public ThingSetInterface { public: ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {}; @@ -34,7 +34,7 @@ class ThingSetStream: public ThingSetDevice virtual void process_asap(); virtual void process_1s(); - protected: + protected: virtual void process_input(); // this is called from the ISR typically const unsigned int channel; @@ -61,13 +61,13 @@ template class ThingSetSerial: public ThingSetStream Callback cb([this]() -> void { this->process_input();}); ser.attach(cb); } - + private: bool readable() { return ser.readable(); } - + private: T& ser; };