Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ class BridgeClass {

struct k_mutex read_mutex;
struct k_mutex write_mutex;

k_tid_t upd_tid;
k_thread_stack_t *upd_stack_area;
struct k_thread upd_thread_data;

bool started = false;

public:

BridgeClass(HardwareSerial& serial) {
serial_ptr = &serial;
transport = new SerialTransport(serial);
}

operator bool() const {
return started;
}

// Initialize the bridge
bool begin(unsigned long baud=DEFAULT_SERIAL_BAUD) {
serial_ptr->begin(baud);
Expand All @@ -67,7 +73,11 @@ class BridgeClass {
UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT);

bool res;
return call(RESET_METHOD, res);
call(RESET_METHOD, res);
if (res) {
started = true;
}
return res;
}

template<typename F>
Expand Down Expand Up @@ -212,8 +222,10 @@ class BridgeClass {

class BridgeClassUpdater {
public:
static void safeUpdate(BridgeClass& bridge) {
bridge.update_safe(); // access private method
static void safeUpdate(BridgeClass* bridge) {
if (*bridge) {
bridge->update_safe();
}
}

private:
Expand All @@ -223,14 +235,16 @@ class BridgeClassUpdater {
BridgeClass Bridge(Serial1);

void updateEntryPoint(void *, void *, void *){
while(1){
Bridge.update();
while (1) {
if (Bridge) {
Bridge.update();
}
k_msleep(1);
}
}

static void safeUpdate(){
BridgeClassUpdater::safeUpdate(Bridge);
BridgeClassUpdater::safeUpdate(&Bridge);
}

void __loopHook(){
Expand Down
20 changes: 12 additions & 8 deletions src/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@ template<size_t BufferSize=DEFAULT_MONITOR_BUF_SIZE>
class BridgeMonitor: public Stream {

private:
BridgeClass& bridge;
BridgeClass* bridge;
RingBufferN<BufferSize> temp_buffer;
struct k_mutex monitor_mutex;
bool is_connected = false;

public:
BridgeMonitor(BridgeClass& bridge): bridge(bridge) {}
BridgeMonitor(BridgeClass& bridge): bridge(&bridge) {}

bool begin() {
return bridge.call(MON_CONNECTED_METHOD, is_connected);
k_mutex_init(&monitor_mutex);
if (!bridge) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think bridge should be dereferenced first:
(!(*bridge))
otherwise the bool operator is not invoked

bridge->begin();
}
return bridge->call(MON_CONNECTED_METHOD, is_connected);
}

bool isConnected() const {
operator bool() const {
return is_connected;
}

Expand Down Expand Up @@ -73,6 +76,7 @@ class BridgeMonitor: public Stream {
int peek() override {
k_mutex_lock(&monitor_mutex, K_FOREVER);
if (temp_buffer.available()) {
k_mutex_unlock(&monitor_mutex);
return temp_buffer.peek();
}
k_mutex_unlock(&monitor_mutex);
Expand All @@ -95,7 +99,7 @@ class BridgeMonitor: public Stream {
}

size_t written;
bool ret = bridge.call(MON_WRITE_METHOD, written, send_buffer);
bool ret = bridge->call(MON_WRITE_METHOD, written, send_buffer);
if (ret) {
return written;
}
Expand All @@ -105,7 +109,7 @@ class BridgeMonitor: public Stream {

bool reset() {
bool res;
bool ok = bridge.call(MON_RESET_METHOD, res);
bool ok = bridge->call(MON_RESET_METHOD, res);
if (ok && res) {
is_connected = false;
}
Expand All @@ -114,7 +118,7 @@ class BridgeMonitor: public Stream {

size_t write(String message) {
size_t size;
bool ok = bridge.call(MON_WRITE_METHOD, size, message);
bool ok = bridge->call(MON_WRITE_METHOD, size, message);

if (!ok) return 0;

Expand All @@ -126,7 +130,7 @@ class BridgeMonitor: public Stream {
if (size == 0) return 0;

MsgPack::arr_t<uint8_t> message;
bool ret = bridge.call(MON_READ_METHOD, message, size);
bool ret = bridge->call(MON_READ_METHOD, message, size);

k_mutex_lock(&monitor_mutex, K_FOREVER);
if (ret) {
Expand Down