Skip to content

Instantly share code, notes, and snippets.

@sticilface
Created July 29, 2018 17:16
Show Gist options
  • Save sticilface/b2ee8a096bc718cac536d3bc38e04e77 to your computer and use it in GitHub Desktop.
Save sticilface/b2ee8a096bc718cac536d3bc38e04e77 to your computer and use it in GitHub Desktop.
Router
#define SERIAL_METHOD ThroughAsyncSerial // ThroughSerial or ThroughAsyncSerial
#define ROUTER_TYPE 2 // 1 = PJONVirtualBusRouter, 2 = segmented switch...
#define PJON_MAX_PACKETS 3
#define BUS_ID 253
#define PJON_BUS_PIN 2
#define PJON_INCLUDE_SWBB
#define PJON_INCLUDE_ANY
#define PJON_INCLUDE_TAS
#define PJON_INCLUDE_TS
#include <Arduino.h>
#include <PJONInteractiveRouter.h>
const int ERROR_LED_PIN = 4, SWBB_LED_PIN = 5, SERIAL_LED_PIN = 6;
const int LED_DURATION = 100; // how long each packet transfer shall be visible (ms)
uint32_t error_on_time = 0, swbb_on_time = 0, serial_on_time = 0;
StrategyLink<SoftwareBitBang> link_swbb;
StrategyLink<SERIAL_METHOD> link_serial;
#if (ROUTER_TYPE == 1)
PJONAny busSWBB(&link_swbb, PJON_NOT_ASSIGNED, 10);
PJONAny busSerial(&link_serial, PJON_NOT_ASSIGNED);
PJONInteractiveRouter<PJONVirtualBusRouter<PJONSwitch>> router(2, (PJONAny*[2]){&busSWBB, &busSerial});
#elif (ROUTER_TYPE == 2)
PJONAny busSWBB(&link_swbb, PJON_NOT_ASSIGNED, 10, 2, 0);
PJONAny busSerial(&link_serial, PJON_NOT_ASSIGNED, 0, 2, 1);
PJONInteractiveRouter<PJONSwitch> router(2, (PJONAny*[2]){&busSWBB, &busSerial});
#endif
String outgoing; // outgoing message
byte msgCount = 0; // count of outgoing messages
byte localAddress = 0xBB; // address of this device
byte destination = 0xFF; // destination to send to
long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends
void setup() {
Serial.begin(115200); // initialize serial
Serial1.begin(115200); //PJON
link_swbb.strategy.set_pin(PJON_BUS_PIN);
link_serial.strategy.set_serial(&Serial1);
//link_serial.strategy.set_timeout(0);
router.set_sendnotification(sendnotification_function);
router.set_error(error_handler);
router.begin();
pinMode(ERROR_LED_PIN, OUTPUT);
pinMode(SWBB_LED_PIN, OUTPUT);
pinMode(SERIAL_LED_PIN, OUTPUT);
Serial.println("Router Ready");
}
static uint32_t timer = 0;
void loop() {
check_leds();
router.loop();
}
void sendnotification_function(const uint8_t * const payload, const uint16_t length, const uint8_t receiver_bus,
const uint8_t sender_bus, const PJON_Packet_Info &packet_info) {
light_led(sender_bus);
}
void error_handler(uint8_t code, uint16_t data, void *custom_ptr) {
digitalWrite(ERROR_LED_PIN, HIGH);
error_on_time = millis();
}
void light_led(uint8_t bus_number) {
switch(bus_number) {
case 0: {
digitalWrite(SWBB_LED_PIN, HIGH);
swbb_on_time = millis();
};
case 1: {
digitalWrite(SERIAL_LED_PIN, HIGH);
serial_on_time = millis();
};
}
}
void check_led(uint8_t pin, uint32_t &on_time) {
if (on_time != 0 && (uint32_t)(millis() - on_time) > LED_DURATION) {
digitalWrite(pin, LOW);
on_time = 0;
}
}
void check_leds() {
check_led(ERROR_LED_PIN, error_on_time);
check_led(SWBB_LED_PIN, swbb_on_time);
check_led(SERIAL_LED_PIN, serial_on_time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment