Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions src/WiThrottle.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "CommandStation.h"
#include "Watchdog.h"
#include "dcc/power_event.hpp"
#include "event_bus.hpp"

#include <WiFi.h>
#include <WiFiServer.h>
Expand All @@ -32,7 +32,7 @@
#endif


class WiThrottleServer: public dcc::PowerObserver {
class WiThrottleServer: public etl::message_router<WiThrottleServer, PowerEventMsg> {
public:

constexpr static uint16_t DEF_PORT = 4444;
Expand All @@ -45,10 +45,13 @@ class WiThrottleServer: public dcc::PowerObserver {
server.end();
}

void notification(const dcc::PowerEvent &event) override {
notifyPowerStatus();
void on_receive(const PowerEventMsg &msg) {
//if(msg.event.channel == CS.getMainTrack())
notifyPowerStatus();
}

void on_receive_unknown(const etl::imessage& msg) {}

void notifyPowerStatus(AsyncClient *c=nullptr) {
bool v = CS.getPowerState();
powerStatus = v ? '1' : '0';
Expand Down
24 changes: 24 additions & 0 deletions src/event_bus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <etl/message_bus.h>

enum {
PowerEventId = 0
};

class PowerEventMsg: public etl::message<PowerEventId> {
public:
dcc::PowerEvent event;
PowerEventMsg(const dcc::PowerEvent &e): event{e} {}
};

/** Main event bus for application.
* Passes through all messages like power changes, users connecting/disconnecting, etc.
* Interested parties can subscribe to relevant events.
*/
inline etl::message_bus<20> event_bus;


inline etl::imessage_bus &get_event_bus() {
return event_bus;
}
10 changes: 8 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "WiThrottle.h"

#include "event_bus.hpp"

#include <LocoNetStream.h>

#include <WiFi.h>
Expand All @@ -21,6 +23,8 @@

#include <Arduino.h>

#include <etl/message.h>

#include <etl/callback_timer_atomic.h>
#include <stdio.h>
#include <atomic>
Expand Down Expand Up @@ -85,6 +89,7 @@ TimerType timerController;
etl::timer::id::type ledTimer;
etl::timer::id::type checkCurrentTimer;


class PowerStatusObserver: public dcc::PowerObserver {
void notification(const dcc::PowerEvent &event) override {
if(!event.state && event.reason == dcc::PowerEvent::Reason::Overcurrent) {
Expand All @@ -95,11 +100,12 @@ class PowerStatusObserver: public dcc::PowerObserver {

Serial.println("Overcurrent on prog");
}

}
event_bus.receive(PowerEventMsg{event});
}
} powerStatusObserver;


void setup() {

Serial.begin(115200);
Expand Down Expand Up @@ -212,7 +218,7 @@ void setup() {
MDNS.setInstanceName(CS_NAME);
lbServer.begin();
withrottleServer.begin();
dccMain.add_observer(withrottleServer); // withrottle doesn't need prog channel
get_event_bus().subscribe(withrottleServer); // withrottle doesn't need prog channel

#endif

Expand Down