Skip to content

Instantly share code, notes, and snippets.

@sticilface
Created July 29, 2018 17:18
Show Gist options
  • Save sticilface/0827bf7e0e5054d34206f66f80765fca to your computer and use it in GitHub Desktop.
Save sticilface/0827bf7e0e5054d34206f66f80765fca to your computer and use it in GitHub Desktop.
Working surrogate
/* This sketch lets a RemoteWorker device connected through Ethernet TCP act
as if it is present on the SoftwareBitBang bus connected to this Surrogate
device.
The RemoteWorker sketch can the run on a device not capable of SWBB but
with Ethernet support, like a PC or a Raspberry PI.
Surrogate and RemoteWorker examples contributed by Fred Larsen. */
#define PJON_PACKET_MAX_LENGTH 50
#define PJON_MAX_PACKETS 3
#define SERIAL_METHOD ThroughAsyncSerial // ThroughSerial or ThroughAsyncSerial
#define PJON_INCLUDE_TAS
#define PJON_INCLUDE_TS
#define PJON_INCLUDE_SWBB
#include <PJON.h>
const uint8_t DEVICE_ID = 155;
// SWBB Device ID for this device and the RemoteWorker
// <Strategy name> bus(selected device id)
PJON<SoftwareBitBang> busA(DEVICE_ID);
PJON<SERIAL_METHOD> busB(1);
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;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
busA.strategy.set_pin(2);
busA.set_receiver(receiver_functionA);
busA.set_error(error_handler);
busA.begin();
busB.strategy.set_serial(&Serial1);
busB.set_router(true);
busB.set_error(error_handler);
busB.set_receiver(receiver_functionB);
busB.begin();
pinMode(ERROR_LED_PIN, OUTPUT);
pinMode(SWBB_LED_PIN, OUTPUT);
pinMode(SERIAL_LED_PIN, OUTPUT);
digitalWrite(ERROR_LED_PIN, LOW);
digitalWrite(SWBB_LED_PIN, LOW);
digitalWrite(SERIAL_LED_PIN, LOW);
pinMode(7,OUTPUT); // on send_response
digitalWrite(7,LOW);
pinMode(8,OUTPUT); // on receive_response
digitalWrite(8,LOW);
Serial.println("SURROGATE SERIAL READY");
}
void error_handler(uint8_t code, uint16_t data, void *custom_pointer) {
digitalWrite(ERROR_LED_PIN, HIGH);
error_on_time = millis();
}
void receiver_functionA(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
// Forward packet to RemoteWorker on bus B, preserving the original sender id
busB.send_from_id(
packet_info.sender_id,
packet_info.sender_bus_id,
DEVICE_ID,
busB.localhost,
(char *)payload,
length,
packet_info.header,
packet_info.id,
packet_info.port
);
light_led(0);
}
void receiver_functionB(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
// All packets sent by the RemoteWorker is delivered to this device, when in the
// single_initiate_direction listening mode.
// Forward packet to specified target device on bus A
busA.send_packet_blocking(
packet_info.receiver_id,
packet_info.receiver_bus_id,
(char *)payload,
length,
packet_info.header,
packet_info.id,
packet_info.port
);
light_led(1);
}
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);
}
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 loop() {
busA.receive();
busB.update();
busB.receive();
busA.update();
check_leds();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment