mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-03-28 17:42:59 +01:00
Add Serial1
This commit is contained in:
@@ -7,7 +7,19 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
Comms *Comms0;
|
||||
Comms *HostComms;
|
||||
Comms *Comms0 = NULL;
|
||||
Comms *Comms1 = NULL;
|
||||
|
||||
void _onReceiveUsb(size_t len)
|
||||
{
|
||||
if (HostComms == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HostComms->_onReceive();
|
||||
}
|
||||
|
||||
void _onReceive0()
|
||||
{
|
||||
@@ -19,54 +31,82 @@ void _onReceive0()
|
||||
Comms0->_onReceive();
|
||||
}
|
||||
|
||||
void _onReceive1()
|
||||
{
|
||||
if (Comms1 == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Comms1->_onReceive();
|
||||
}
|
||||
|
||||
void _onUsbEvent0(void *arg, esp_event_base_t event_base, int32_t event_id,
|
||||
void *event_data)
|
||||
{
|
||||
if (event_base == ARDUINO_HW_CDC_EVENTS)
|
||||
{
|
||||
// arduino_hw_cdc_event_data_t *data = (arduino_hw_cdc_event_data_t *)event_data;
|
||||
arduino_hw_cdc_event_data_t *data = (arduino_hw_cdc_event_data_t *)event_data;
|
||||
if (event_id == ARDUINO_HW_CDC_RX_EVENT)
|
||||
{
|
||||
_onReceive0(/*data->rx.len*/);
|
||||
_onReceiveUsb(data->rx.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Comms::initComms(Config &c)
|
||||
{
|
||||
bool fine = false;
|
||||
if (c.listen_on_usb.equalsIgnoreCase("readline"))
|
||||
{
|
||||
// comms using readline plaintext protocol
|
||||
Comms0 = new ReadlineComms(Serial);
|
||||
HostComms = new ReadlineComms("Host", Serial);
|
||||
Serial.onEvent(ARDUINO_HW_CDC_RX_EVENT, _onUsbEvent0);
|
||||
Serial.begin();
|
||||
|
||||
Serial.println("Initialized communications on Serial using readline protocol");
|
||||
|
||||
return true;
|
||||
fine = true;
|
||||
}
|
||||
else if (c.listen_on_serial0.equalsIgnoreCase("readline"))
|
||||
|
||||
if (c.listen_on_serial0.equalsIgnoreCase("readline"))
|
||||
{
|
||||
// comms using readline plaintext protocol
|
||||
Comms0 = new ReadlineComms(Serial0);
|
||||
Comms0 = new ReadlineComms("UART0", Serial0);
|
||||
Serial0.onReceive(_onReceive0, false);
|
||||
Serial0.begin(115200);
|
||||
|
||||
Serial.println("Initialized communications on Serial0 using readline protocol");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (c.listen_on_serial0.equalsIgnoreCase("none"))
|
||||
else
|
||||
{
|
||||
Comms0 = new NoopComms();
|
||||
|
||||
Serial.println("Configured none - Initialized no communications");
|
||||
return false;
|
||||
Serial.println("Configured none - Initialized no communications on Serial0");
|
||||
}
|
||||
|
||||
Comms0 = new NoopComms();
|
||||
Serial.println("Nothing is configured - initialized no communications");
|
||||
return false;
|
||||
if (c.listen_on_serial1.equalsIgnoreCase("readline"))
|
||||
{
|
||||
// comms using readline plaintext protocol
|
||||
Comms1 = new ReadlineComms("UART1", Serial1);
|
||||
Serial1.onReceive(_onReceive1, false);
|
||||
Serial1.begin(115200);
|
||||
|
||||
Serial.println("Initialized communications on Serial1 using readline protocol");
|
||||
}
|
||||
else
|
||||
{
|
||||
Comms1 = new NoopComms();
|
||||
|
||||
Serial.println("Configured none - Initialized no communications on Serial1");
|
||||
}
|
||||
|
||||
if (!fine)
|
||||
{
|
||||
HostComms = new NoopComms();
|
||||
Serial.println("Nothing is configured - initialized no communications");
|
||||
}
|
||||
return fine;
|
||||
}
|
||||
|
||||
size_t Comms::available() { return received_pos; }
|
||||
@@ -177,6 +217,10 @@ void ReadlineComms::_onReceive()
|
||||
delete m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(name + ": discarding > " + pack);
|
||||
}
|
||||
partialPacket = partialPacket.substring(i + 1);
|
||||
i = partialPacket.indexOf('\n');
|
||||
}
|
||||
@@ -191,6 +235,7 @@ bool ReadlineComms::send(Message &m)
|
||||
{
|
||||
case MessageType::SCAN:
|
||||
p = _scan_str(m.payload.scan);
|
||||
Serial.println(name + ": the message is: " + p);
|
||||
break;
|
||||
case MessageType::SCAN_RESULT:
|
||||
p = _scan_result_str(m.payload.dump);
|
||||
@@ -267,8 +312,6 @@ Message *_parsePacket(String p)
|
||||
m->payload.scan.delay = _intParam(p, -1);
|
||||
return m;
|
||||
}
|
||||
|
||||
Serial.println("ignoring unknown message " + p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ struct Message
|
||||
|
||||
struct Comms
|
||||
{
|
||||
String name;
|
||||
Stream &serial;
|
||||
Message **received;
|
||||
size_t received_sz;
|
||||
@@ -52,8 +53,9 @@ struct Comms
|
||||
|
||||
Message *wrap;
|
||||
|
||||
Comms(Stream &serial)
|
||||
: serial(serial), received(NULL), received_sz(0), received_pos(0), wrap(NULL) {};
|
||||
Comms(String name, Stream &serial)
|
||||
: name(name), serial(serial), received(NULL), received_sz(0), received_pos(0),
|
||||
wrap(NULL) {};
|
||||
|
||||
virtual size_t available();
|
||||
virtual bool send(Message &) = 0;
|
||||
@@ -67,7 +69,7 @@ struct Comms
|
||||
|
||||
struct NoopComms : Comms
|
||||
{
|
||||
NoopComms() : Comms(Serial0) {};
|
||||
NoopComms() : Comms("no-op", Serial0) {};
|
||||
|
||||
virtual bool send(Message &) { return true; };
|
||||
virtual void _onReceive() {};
|
||||
@@ -77,14 +79,19 @@ struct ReadlineComms : Comms
|
||||
{
|
||||
String partialPacket;
|
||||
|
||||
ReadlineComms(Stream &serial) : Comms(serial), partialPacket("") {};
|
||||
ReadlineComms(String name, Stream &serial)
|
||||
: Comms(name, serial), partialPacket("") {};
|
||||
|
||||
virtual bool send(Message &) override;
|
||||
|
||||
virtual void _onReceive() override;
|
||||
};
|
||||
|
||||
extern Comms *HostComms;
|
||||
|
||||
extern Comms *Comms0;
|
||||
|
||||
extern Comms *Comms1;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -112,6 +112,12 @@ Config Config::init()
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r.key.equalsIgnoreCase("listen_on_serial1"))
|
||||
{
|
||||
c.listen_on_serial1 = r.value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r.key.equalsIgnoreCase("listen_on_usb"))
|
||||
{
|
||||
c.listen_on_serial0 = r.value;
|
||||
@@ -136,6 +142,7 @@ bool Config::write_config(const char *path)
|
||||
f.println("print_profile_time = " + String(print_profile_time ? "true" : "false"));
|
||||
f.println("log_data_json_interval = " + String(log_data_json_interval));
|
||||
f.println("listen_on_serial0 = " + listen_on_serial0);
|
||||
f.println("listen_on_serial1 = " + listen_on_serial1);
|
||||
f.println("listen_on_usb = " + listen_on_usb);
|
||||
|
||||
f.close();
|
||||
|
||||
@@ -10,12 +10,13 @@ struct Config
|
||||
bool print_profile_time;
|
||||
int log_data_json_interval;
|
||||
String listen_on_serial0;
|
||||
String listen_on_serial1;
|
||||
String listen_on_usb;
|
||||
|
||||
Config()
|
||||
: create_missing_config(CREATE_MISSING_CONFIG), print_profile_time(false),
|
||||
log_data_json_interval(1000), listen_on_serial0(String("none")),
|
||||
listen_on_usb("readline") {};
|
||||
listen_on_serial1(String("readline")), listen_on_usb("readline") {};
|
||||
bool write_config(const char *path);
|
||||
|
||||
static Config init();
|
||||
|
||||
@@ -133,6 +133,8 @@ int main(int argc, char** argv)
|
||||
lines--;
|
||||
write(1, buffer, pos);
|
||||
} else if (!is_wrap) {
|
||||
write(1, "> ", 2);
|
||||
write(1, buffer, pos);
|
||||
errors++;
|
||||
}
|
||||
|
||||
|
||||
64
src/main.cpp
64
src/main.cpp
@@ -686,6 +686,8 @@ ScanTask report_scans = ScanTask{
|
||||
delay : 0 // 0 => as and when it happens; > 0 => at least once that many ms
|
||||
};
|
||||
|
||||
bool requested_host = true;
|
||||
|
||||
void dumpToCommsTask(void *parameter)
|
||||
{
|
||||
uint64_t last_epoch = frequency_scan_result.last_epoch;
|
||||
@@ -712,7 +714,17 @@ void dumpToCommsTask(void *parameter)
|
||||
Message m;
|
||||
m.type = MessageType::SCAN_RESULT;
|
||||
m.payload.dump = frequency_scan_result.dump;
|
||||
Comms0->send(m);
|
||||
if (requested_host)
|
||||
{
|
||||
HostComms->send(m);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Comms0 != NULL)
|
||||
Comms0->send(m);
|
||||
if (Comms1 != NULL)
|
||||
Comms1->send(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1274,9 +1286,9 @@ void check_ranges()
|
||||
#ifdef SERIAL_OUT
|
||||
void checkComms()
|
||||
{
|
||||
while (Comms0->available() > 0)
|
||||
while (HostComms->available() > 0)
|
||||
{
|
||||
Message *m = Comms0->receive();
|
||||
Message *m = HostComms->receive();
|
||||
if (m == NULL)
|
||||
continue;
|
||||
|
||||
@@ -1284,6 +1296,52 @@ void checkComms()
|
||||
{
|
||||
case MessageType::SCAN:
|
||||
report_scans = m->payload.scan;
|
||||
requested_host = true;
|
||||
Serial.println("Host: forwarding message SCAN to peer");
|
||||
Comms0->send(*m); // forward to peer
|
||||
Comms1->send(*m); // forward to peer
|
||||
break;
|
||||
}
|
||||
delete m;
|
||||
}
|
||||
|
||||
while (Comms0->available() > 0)
|
||||
{
|
||||
Message *m = Comms0->receive();
|
||||
Serial.println("Comms0: was available, but didn't receive");
|
||||
if (m == NULL)
|
||||
continue;
|
||||
|
||||
switch (m->type)
|
||||
{
|
||||
case MessageType::SCAN:
|
||||
report_scans = m->payload.scan; // receive from peer
|
||||
requested_host = false;
|
||||
break;
|
||||
|
||||
case MessageType::SCAN_RESULT:
|
||||
HostComms->send(*m); // forward from peer
|
||||
break;
|
||||
}
|
||||
delete m;
|
||||
}
|
||||
|
||||
while (Comms1->available() > 0)
|
||||
{
|
||||
Message *m = Comms1->receive();
|
||||
Serial.println("Comms1: was available, but didn't receive");
|
||||
if (m == NULL)
|
||||
continue;
|
||||
|
||||
switch (m->type)
|
||||
{
|
||||
case MessageType::SCAN:
|
||||
report_scans = m->payload.scan; // receive from peer
|
||||
requested_host = false;
|
||||
break;
|
||||
|
||||
case MessageType::SCAN_RESULT:
|
||||
HostComms->send(*m); // forward from peer
|
||||
break;
|
||||
}
|
||||
delete m;
|
||||
|
||||
Reference in New Issue
Block a user