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.
This commit is contained in:
Martin Jaeger
2019-10-17 10:36:48 +02:00
parent 6c3885e5d0
commit 29e186624c
6 changed files with 65 additions and 43 deletions
+3 -3
View File
@@ -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;
}
+12 -10
View File
@@ -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()) {
+9 -7
View File
@@ -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
#endif
@@ -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<ThingSetDevice*> ThingSetDeviceManager::devices =
std::vector<ThingSetInterface*> ThingSetInterfaceManager::interfaces =
{
#ifdef UART_SERIAL_ENABLED
&ts_uart,
@@ -74,12 +74,29 @@ std::vector<ThingSetDevice*> 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 */
@@ -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 <vector>
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<ThingSetDevice*> devices;
static std::vector<ThingSetInterface*> interfaces;
};
extern ThingSetDeviceManager ts_devices;
#endif
extern ThingSetInterfaceManager ts_interfaces;
#endif
+5 -5
View File
@@ -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<typename T> class ThingSetSerial: public ThingSetStream
Callback<void()> cb([this]() -> void { this->process_input();});
ser.attach(cb);
}
private:
bool readable()
{
return ser.readable();
}
private:
T& ser;
};