ThingSet interfaces merged with Ext interfaces

This commit is contained in:
Martin Jaeger
2019-12-20 10:43:51 +01:00
parent 4326d74fe2
commit 3f89943edf
8 changed files with 119 additions and 299 deletions
+45 -4
View File
@@ -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__ */
@@ -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<typename T> class ThingSetSerial: public ThingSetStream
{
public:
ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(s) {}
void enable() {
Callback<void()> 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];
-57
View File
@@ -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
-108
View File
@@ -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 <algorithm> // 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<ThingSetInterface*> 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 */
-44
View File
@@ -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 <vector>
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<ThingSetInterface*> interfaces;
};
extern ThingSetInterfaceManager ts_interfaces;
#endif
-76
View File
@@ -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<typename T> class ThingSetSerial: public ThingSetStream
{
public:
ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(s) {}
void enable() {
Callback<void()> cb([this]() -> void { this->process_input();});
ser.attach(cb);
}
private:
bool readable()
{
return ser.readable();
}
private:
T& ser;
};
#endif /* THINGSET_SERIAL_H */
+2 -7
View File
@@ -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;
}
-1
View File
@@ -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