Skip to content

Commit

Permalink
less redundancy, beautified and define-switch added
Browse files Browse the repository at this point in the history
  • Loading branch information
asteinh committed Oct 8, 2019
1 parent 715c882 commit 1c3395e
Show file tree
Hide file tree
Showing 17 changed files with 1,101 additions and 1,191 deletions.
81 changes: 44 additions & 37 deletions Arduino/libraries/microOS/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,54 @@
#include "microOS.h"

Channel::Channel(Stream *port, ProtocolInterface *protocol) :
_active(false), _port(port), _protocol(protocol)
_active(false), _port(port), _protocol(protocol)
{
//do nothing
//do nothing
}

void Channel::start()
{
if((_port!=NULL) && (_protocol!=NULL))
_active = true;
void Channel::start() {
if((_port!=NULL) && (_protocol!=NULL))
_active = true;
}

void Channel::stop()
{
_active = false;
void Channel::stop() {
_active = false;
}

bool Channel::receive()
{
if(_active){
bool msg_flag = false;
while((_port->available())&&(!msg_flag)){ //keep on receiving bytes until we find a message or all bytes are read
msg_flag = _protocol->decode(_port->read());//set the message flag to received status
}
return msg_flag;
} else {
return false;
}
}

bool Channel::send(void *msg)
{
if(_active){
uint8_t buffer[CHANNEL_BUFFER_SIZE];
uint16_t length = _protocol->encode(msg, buffer);
return (_port->write(buffer, length) == length);
} else {
return false;
}
}

bool Channel::getActive(){ return _active; }
Stream* Channel::getPort(){ return _port; }
ProtocolInterface* Channel::getProtocol(){ return _protocol; }
void* Channel::getMessage(){ return _protocol->getMessage(); }
bool Channel::receive() {
if(_active) {
bool msg_flag = false;
while((_port->available()) && (!msg_flag)) { //keep on receiving bytes until we find a message or all bytes are read
msg_flag = _protocol->decode(_port->read());//set the message flag to received status
}
return msg_flag;
} else {
return false;
}
}

bool Channel::send(void *msg) {
if(_active) {
uint8_t buffer[CHANNEL_BUFFER_SIZE];
uint16_t length = _protocol->encode(msg, buffer);
return (_port->write(buffer, length) == length);
} else {
return false;
}
}

bool Channel::getActive() {
return _active;
}

Stream* Channel::getPort() {
return _port;
}

ProtocolInterface* Channel::getProtocol() {
return _protocol;
}

void* Channel::getMessage() {
return _protocol->getMessage();
}
35 changes: 17 additions & 18 deletions Arduino/libraries/microOS/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// include for teensy
#define USB_SERIAL
#define CHANNEL_BUFFER_SIZE 64
#define CHANNEL_BUFFER_SIZE 64

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
Expand All @@ -13,26 +13,25 @@

#include "protocol_interface.h"

class Channel
{
class Channel {
private:
bool _active;
Stream *_port;
ProtocolInterface *_protocol;
bool _active;

Stream *_port;
ProtocolInterface *_protocol;

public:
Channel(Stream *port, ProtocolInterface* protocol);
void start();
void stop();
bool receive();
bool send(void *msg);
bool getActive();
Stream* getPort();
ProtocolInterface* getProtocol();
void* getMessage();
Channel(Stream *port, ProtocolInterface* protocol);

void start();
void stop();
bool receive();
bool send(void *msg);

bool getActive();
Stream* getPort();
ProtocolInterface* getProtocol();
void* getMessage();
};

#endif //CHANNEL_H
34 changes: 18 additions & 16 deletions Arduino/libraries/microOS/communicator_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@

#include <inttypes.h>

class CommunicatorInterface
{
class CommunicatorInterface {
public:
virtual void init();
virtual void init();

virtual void receive();
virtual void transmit();
virtual void receive();
virtual void transmit();

virtual void sendHeartbeat();
virtual void sendThreadInfo(uint8_t ID, uint8_t priority,
uint32_t duration, uint32_t latency,
uint32_t total_duration, uint32_t total_latency, uint32_t number_of_executions);
virtual void sendGPIO();
virtual void sendGPIOX();
virtual void sendEvent(uint16_t event);
virtual void sendPrint(const char *text);
virtual void sendIntParam(const String& name, const uint16_t offset, const int32_t value);
virtual void sendFloatParam(const String& name, const uint16_t offset, const float value);
//virtual void sendHwinfo()
virtual void sendHeartbeat();
virtual void sendThreadInfo(uint8_t ID, uint8_t priority, uint32_t duration,
uint32_t latency, uint32_t total_duration,
uint32_t total_latency,
uint32_t number_of_executions);

virtual void sendGPIO();
virtual void sendEvent(uint16_t event);
virtual void sendPrint(const char *text);
virtual void sendIntParam(const String& name, const uint16_t offset,
const int32_t value);
virtual void sendFloatParam(const String& name, const uint16_t offset,
const float value);
//virtual void sendHwinfo()
};

#endif //COMMUNICATOR_INTERFACE_H
143 changes: 71 additions & 72 deletions Arduino/libraries/microOS/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,116 @@
#define FORWARD_LIST_H

template<class T> struct forward_list_node {
T data;
forward_list_node<T> *next;
T data;
forward_list_node<T> *next;
};

template<typename T> class forward_iterator {
private:
forward_list_node<T> *_next;
private:
forward_list_node<T> *_next;

public:
forward_iterator(forward_list_node<T> *next) : _next(next){}
public:
forward_iterator(forward_list_node<T> *next) : _next(next) { }

bool has_next();
const T& next();
const T& peek_next();
bool has_next();
const T& next();
const T& peek_next();
};

template<typename T> bool forward_iterator<T>::has_next() {
return _next;
return _next;
}

template<typename T> const T& forward_iterator<T>::next() {
forward_list_node<T> *current = _next;
_next = _next->next;
return (current->data);
forward_list_node<T> *current = _next;
_next = _next->next;
return (current->data);
}

template<typename T> const T& forward_iterator<T>::peek_next() {
return (_next->data);
return (_next->data);
}

template<typename T> class forward_list {
private:
forward_list_node<T> *_begin = NULL;
forward_list_node<T> *_end = NULL;

void push_first(T v);

public:
~forward_list() {
while(_begin != NULL)
pop_front();
}

void push_front(T v);
void push_back(T v);
void pop_front();
forward_iterator<T> begin();
size_t size();
bool empty();

private:
forward_list_node<T> *_begin = NULL;
forward_list_node<T> *_end = NULL;

void push_first(T v);

public:
~forward_list() {
while(_begin != NULL)
pop_front();
}

void push_front(T v);
void push_back(T v);
void pop_front();
forward_iterator<T> begin();
size_t size();
bool empty();
};

template<typename T> void forward_list<T>::push_first(T v) {
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = NULL;
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = NULL;

_begin = n;
_end = n;
_begin = n;
_end = n;
}

template<typename T> void forward_list<T>::push_front(T v) {
if(!empty()) {
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = _begin;
_begin = n;
} else {
push_first(v);
}
if(!empty()) {
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = _begin;
_begin = n;
} else {
push_first(v);
}
}

template<typename T> void forward_list<T>::push_back(T v) {
if(!empty()) {
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = NULL;
_end->next = n;
_end = n;
} else {
push_first(v);
}
if(!empty()) {
forward_list_node<T> *n = new forward_list_node<T>;
n->data = v;
n->next = NULL;
_end->next = n;
_end = n;
} else {
push_first(v);
}
}

template<typename T> void forward_list<T>::pop_front() {
if(_begin != NULL) {
forward_list_node<T> *n = _begin;
_begin = _begin->next;
delete n;
} else {
_end = _begin;
}
if(_begin != NULL) {
forward_list_node<T> *n = _begin;
_begin = _begin->next;
delete n;
} else {
_end = _begin;
}
}

template<typename T> forward_iterator<T> forward_list<T>::begin() {
return forward_iterator<T>(_begin);
return forward_iterator<T>(_begin);
}

template<typename T> size_t forward_list<T>::size() {
forward_iterator<T> i = begin();
size_t n = 0;
while(i.has_next()) {
n++;
i.next();
}
return n;
forward_iterator<T> i = begin();

size_t n = 0;
while(i.has_next()) {
n++;
i.next();
}
return n;
}

template<typename T> bool forward_list<T>::empty() {
return !_begin;
return !_begin;
}

#endif // FORWARD_LIST
Loading

0 comments on commit 1c3395e

Please sign in to comment.