Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sticilface/7537ee844a7cbb3a3342bc1a9322667d to your computer and use it in GitHub Desktop.
Save sticilface/7537ee844a7cbb3a3342bc1a9322667d to your computer and use it in GitHub Desktop.
PJON_router sketch
#define SERIAL_METHOD ThroughSerial // ThroughSerial or ThroughAsyncSerial
#define PJON_MAX_PACKETS 3
#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;
PJONAny busSWBB(&link_swbb, PJON_NOT_ASSIGNED, 1000);
PJONAny busSerial(&link_serial, PJON_NOT_ASSIGNED, 1000);
PJONInteractiveRouter<PJONVirtualBusRouter<PJONSwitch>> router(2, (PJONAny*[2]){&busSWBB, &busSerial});
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.set_virtual_bus(0); // Enable virtual bus
router.begin();
pinMode(ERROR_LED_PIN, OUTPUT);
pinMode(SWBB_LED_PIN, OUTPUT);
pinMode(SERIAL_LED_PIN, OUTPUT);
Serial.println("Router Ready");
// pinMode(7,OUTPUT); // on sending ACK
// digitalWrite(7,LOW);
//
// pinMode(8,OUTPUT); //
// digitalWrite(8,LOW);
}
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