Coding style updated/fixed
Indentations of class definitions and preprocessor statements were not uniform throughout the code. This commit cleans up indentations as well as the order of include statements. Style conventions see: https://github.com/LibreSolar/coding-style
This commit is contained in:
+107
-111
@@ -10,136 +10,134 @@
|
||||
|
||||
#if CONFIG_EXT_THINGSET_SERIAL // otherwise don't compile code to reduce firmware size
|
||||
|
||||
#include "thingset.h"
|
||||
#include "ext.h"
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
#define TX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_TX_BUF_SIZE
|
||||
#define RX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_RX_BUF_SIZE
|
||||
#else
|
||||
#define TX_BUF_SIZE 1000
|
||||
#define RX_BUF_SIZE 500
|
||||
#endif
|
||||
|
||||
#ifdef __MBED__
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <sys/printk.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <drivers/uart.h>
|
||||
#endif /* MBED or ZEPHYR */
|
||||
|
||||
#include "thingset.h"
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
#define TX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_TX_BUF_SIZE
|
||||
#define RX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_RX_BUF_SIZE
|
||||
|
||||
#define UART_DEVICE_NAME DT_ALIAS_UART_DBG_LABEL
|
||||
struct device *uart_dev = device_get_binding(UART_DEVICE_NAME);
|
||||
|
||||
#endif /* MBED or ZEPHYR */
|
||||
#else
|
||||
#define TX_BUF_SIZE 1000
|
||||
#define RX_BUF_SIZE 500
|
||||
#endif
|
||||
|
||||
class ThingSetStream: public ExtInterface
|
||||
{
|
||||
public:
|
||||
#ifdef __MBED__
|
||||
ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {};
|
||||
#elif defined(__ZEPHYR__)
|
||||
ThingSetStream(const unsigned int c): channel(c){};
|
||||
#endif
|
||||
public:
|
||||
#ifdef __MBED__
|
||||
ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {};
|
||||
#elif defined(__ZEPHYR__)
|
||||
ThingSetStream(const unsigned int c): channel(c){};
|
||||
#endif
|
||||
|
||||
virtual void process_asap();
|
||||
virtual void process_1s();
|
||||
virtual void process_asap();
|
||||
virtual void process_1s();
|
||||
|
||||
protected:
|
||||
#ifdef __MBED__
|
||||
virtual void process_input(); // this is called from the ISR typically
|
||||
#elif defined (__ZEPHYR__)
|
||||
static void process_input(void*);
|
||||
#endif
|
||||
protected:
|
||||
#ifdef __MBED__
|
||||
virtual void process_input(); // this is called from the ISR typically
|
||||
#elif defined (__ZEPHYR__)
|
||||
static void process_input(void*);
|
||||
#endif
|
||||
|
||||
const unsigned int channel;
|
||||
const unsigned int channel;
|
||||
|
||||
#ifdef __MBED__
|
||||
virtual bool readable()
|
||||
{
|
||||
return stream->readable();
|
||||
#ifdef __MBED__
|
||||
virtual bool readable()
|
||||
{
|
||||
return stream->readable();
|
||||
}
|
||||
#elif defined (__ZEPHYR__)
|
||||
static bool readable()
|
||||
{
|
||||
// Start processing interrupts in ISR.
|
||||
// This function should be called the first thing in the ISR.
|
||||
if (!uart_irq_update(uart_dev)) {
|
||||
// The function output should always be 1.
|
||||
return false;
|
||||
}
|
||||
// Check if data is available to read.
|
||||
return uart_irq_rx_ready(uart_dev);
|
||||
}
|
||||
|
||||
#elif defined (__ZEPHYR__)
|
||||
static bool readable()
|
||||
{
|
||||
// Start processing interrupts in ISR.
|
||||
// This function should be called the first thing in the ISR.
|
||||
if (!uart_irq_update(uart_dev)) {
|
||||
// The function output should always be 1.
|
||||
return false;
|
||||
}
|
||||
// Check if data is available to read.
|
||||
return uart_irq_rx_ready(uart_dev);
|
||||
}
|
||||
typedef struct {
|
||||
char *buf_req_ptr;
|
||||
size_t *req_pos_ptr;
|
||||
bool *command_flag_ptr;
|
||||
} DataStreamStruct;
|
||||
|
||||
typedef struct {
|
||||
char *buf_req_ptr;
|
||||
size_t *req_pos_ptr;
|
||||
bool *command_flag_ptr;
|
||||
} DataStreamStruct;
|
||||
// Data structure used to pass member variables into a static callback ISR.
|
||||
// Callback registration requires the function to be static.
|
||||
// But static functions dont have access to the class members.
|
||||
// Hence these members are passed as a structure of pointers.
|
||||
DataStreamStruct data_stream = {buf_req, &req_pos, &command_flag};
|
||||
#endif /* MBED */
|
||||
|
||||
// Data structure used to pass member variables into a static callback ISR.
|
||||
// Callback registration requires the function to be static.
|
||||
// But static functions dont have access to the class members.
|
||||
// Hence these members are passed as a structure of pointers.
|
||||
DataStreamStruct data_stream = {buf_req, &req_pos, &command_flag};
|
||||
#endif /* MBED */
|
||||
private:
|
||||
#ifdef __MBED__
|
||||
Stream* stream;
|
||||
#endif /* MBED */
|
||||
|
||||
private:
|
||||
#ifdef __MBED__
|
||||
Stream* stream;
|
||||
#endif /* MBED */
|
||||
|
||||
char buf_resp[TX_BUF_SIZE];
|
||||
char buf_req[RX_BUF_SIZE];
|
||||
size_t req_pos = 0;
|
||||
bool command_flag = false;
|
||||
char buf_resp[TX_BUF_SIZE];
|
||||
char buf_req[RX_BUF_SIZE];
|
||||
size_t req_pos = 0;
|
||||
bool command_flag = false;
|
||||
};
|
||||
|
||||
#ifdef __MBED__
|
||||
|
||||
template<typename T> class ThingSetSerial: public ThingSetStream
|
||||
{
|
||||
public:
|
||||
ThingSetSerial(T& s, const unsigned int c): ThingSetStream(s,c), ser(s) {}
|
||||
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);
|
||||
}
|
||||
void enable() {
|
||||
Callback<void()> cb([this]() -> void { this->process_input();});
|
||||
ser.attach(cb);
|
||||
}
|
||||
|
||||
private:
|
||||
bool readable()
|
||||
{
|
||||
return ser.readable();
|
||||
}
|
||||
private:
|
||||
bool readable()
|
||||
{
|
||||
return ser.readable();
|
||||
}
|
||||
|
||||
private:
|
||||
T& ser;
|
||||
private:
|
||||
T& ser;
|
||||
};
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
|
||||
class ThingSetSerial: public ThingSetStream
|
||||
{
|
||||
public:
|
||||
ThingSetSerial(const int c): ThingSetStream(c){}
|
||||
public:
|
||||
ThingSetSerial(const int c): ThingSetStream(c){}
|
||||
|
||||
void enable() {
|
||||
/* Configure UART ISR: Configure a Callback function */
|
||||
// Callback function to be given access to the class members using the below API.
|
||||
// These members are accessed by the ISR using a pointer to the data structure.
|
||||
uart_irq_callback_user_data_set(uart_dev, process_input, (void*)&data_stream);
|
||||
void enable() {
|
||||
/* Configure UART ISR: Configure a Callback function */
|
||||
// Callback function to be given access to the class members using the below API.
|
||||
// These members are accessed by the ISR using a pointer to the data structure.
|
||||
uart_irq_callback_user_data_set(uart_dev, process_input, (void*)&data_stream);
|
||||
|
||||
/* Enable Rx interrupt in the IER */
|
||||
uart_irq_rx_enable(uart_dev);
|
||||
}
|
||||
/* Enable Rx interrupt in the IER */
|
||||
uart_irq_rx_enable(uart_dev);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* MBED or ZEPHYR*/
|
||||
|
||||
/*
|
||||
@@ -149,14 +147,15 @@ class ThingSetSerial: public ThingSetStream
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_EXT_THINGSET_SERIAL
|
||||
extern const int pub_channel_serial;
|
||||
#ifdef __MBED__
|
||||
extern Serial serial;
|
||||
ThingSetSerial ts_uart(serial, pub_channel_serial);
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
ThingSetSerial ts_uart(pub_channel_serial);
|
||||
#endif /* MBED or ZEPHYR */
|
||||
extern const int pub_channel_serial;
|
||||
|
||||
#ifdef __MBED__
|
||||
extern Serial serial;
|
||||
ThingSetSerial ts_uart(serial, pub_channel_serial);
|
||||
#elif defined(__ZEPHYR__)
|
||||
ThingSetSerial ts_uart(pub_channel_serial);
|
||||
#endif /* MBED or ZEPHYR */
|
||||
|
||||
#endif /* CONFIG_EXT_THINGSET_SERIAL */
|
||||
|
||||
@@ -165,19 +164,17 @@ extern ThingSet ts;
|
||||
void ThingSetStream::process_1s()
|
||||
{
|
||||
if (ts.get_pub_channel(channel)->enabled) {
|
||||
#ifdef __MBED__
|
||||
#ifdef __MBED__
|
||||
ts.pub_msg_json(buf_resp, sizeof(buf_resp), channel);
|
||||
stream->puts(buf_resp);
|
||||
stream->putc('\n');
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
#elif defined(__ZEPHYR__)
|
||||
int len = ts.pub_msg_json(buf_resp, sizeof(buf_resp), channel);
|
||||
for (int i = 0; i < len; i++) {
|
||||
uart_poll_out(uart_dev, buf_resp[i]);
|
||||
}
|
||||
uart_poll_out(uart_dev, '\n');
|
||||
|
||||
#endif /* MBED or ZREPHYR */
|
||||
#endif /* MBED or ZREPHYR */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,21 +183,20 @@ void ThingSetStream::process_asap()
|
||||
if (command_flag) {
|
||||
// commands must have 2 or more characters
|
||||
if (req_pos > 1) {
|
||||
#ifdef __MBED__
|
||||
#ifdef __MBED__
|
||||
stream->printf("Received Request (%d bytes): %s\n", strlen(buf_req), buf_req);
|
||||
ts.process((uint8_t *)buf_req, strlen(buf_req), (uint8_t *)buf_resp, sizeof(buf_resp));
|
||||
stream->puts(buf_resp);
|
||||
stream->putc('\n');
|
||||
fflush(*stream);
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
#elif defined(__ZEPHYR__)
|
||||
printf("Received Request (%d bytes): %s\n", strlen(buf_req), buf_req);
|
||||
int len = ts.process((uint8_t *)buf_req, strlen(buf_req), (uint8_t *)buf_resp, sizeof(buf_resp));
|
||||
for (int i = 0; i < len; i++) {
|
||||
uart_poll_out(uart_dev, buf_resp[i]);
|
||||
}
|
||||
uart_poll_out(uart_dev, '\n');
|
||||
#endif /* MBED or ZEPHYR */
|
||||
#endif /* MBED or ZEPHYR */
|
||||
}
|
||||
|
||||
// start listening for new commands
|
||||
@@ -219,20 +215,20 @@ void ThingSetStream::process_input()
|
||||
void ThingSetStream::process_input(void* user_data)
|
||||
#endif /* MBED or ZEPHYR */
|
||||
{
|
||||
#ifdef __ZEPHYR__
|
||||
#ifdef __ZEPHYR__
|
||||
u8_t c;
|
||||
DataStreamStruct *data_stream_ptr = (DataStreamStruct *)user_data;
|
||||
bool command_flag = *(data_stream_ptr->command_flag_ptr);
|
||||
char *buf_req = data_stream_ptr->buf_req_ptr;
|
||||
size_t req_pos = *(data_stream_ptr->req_pos_ptr);
|
||||
#endif /* __ZEPHYR__ */
|
||||
#endif /* __ZEPHYR__ */
|
||||
|
||||
while (readable() && command_flag == false) {
|
||||
#ifdef __MBED__
|
||||
#ifdef __MBED__
|
||||
int c = stream->getc();
|
||||
#elif defined(__ZEPHYR__)
|
||||
#elif defined(__ZEPHYR__)
|
||||
uart_fifo_read(uart_dev, &c, 1);
|
||||
#endif /* MBED */
|
||||
#endif /* MBED */
|
||||
|
||||
// \r\n and \n are markers for line end, i.e. command end
|
||||
// we accept this at any time, even if the buffer is 'full', since
|
||||
@@ -261,10 +257,10 @@ void ThingSetStream::process_input(void* user_data)
|
||||
buf_req[req_pos++] = c;
|
||||
}
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
#ifdef __ZEPHYR__
|
||||
*(data_stream_ptr->command_flag_ptr) = command_flag;
|
||||
*(data_stream_ptr->req_pos_ptr) = req_pos;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user