* added helpers/ArduinoSerialInterface, for the 'companion radio'

* .ini, target envs: Heltec_v3_companion_radio_usb &  Heltec_v3_companion_radio_ble
This commit is contained in:
Scott Powell
2025-01-28 23:26:55 +11:00
parent d9dc76f197
commit cd81878e92
4 changed files with 125 additions and 16 deletions

View File

@@ -100,7 +100,7 @@ static uint32_t _atoi(const char* sp) {
#define RESP_CODE_SELF_INFO 5 // reply to CMD_APP_START
#define RESP_CODE_SENT 6 // reply to CMD_SEND_TXT_MSG
#define RESP_CODE_CONTACT_MSG_RECV 7 // a reply to CMD_SYNC_NEXT_MESSAGE
#define RESP_CODE_GROUP_MSG_RECV 8 // a reply to CMD_SYNC_NEXT_MESSAGE
#define RESP_CODE_CHANNEL_MSG_RECV 8 // a reply to CMD_SYNC_NEXT_MESSAGE
// these are _pushed_ to client app at any time
#define PUSH_CODE_ADVERT 0x80
@@ -110,7 +110,7 @@ static uint32_t _atoi(const char* sp) {
/* -------------------------------------------------------------------------------------- */
class MyMesh : public BaseChatMesh, ContactVisitor {
class MyMesh : public BaseChatMesh {
FILESYSTEM* _fs;
uint32_t expected_ack_crc; // TODO: keep table of expected ACKs
mesh::GroupChannel* _public;
@@ -293,7 +293,7 @@ protected:
void onChannelMessageRecv(const mesh::GroupChannel& channel, int in_path_len, uint32_t timestamp, const char *text) override {
int i = 0;
out_frame[i++] = RESP_CODE_GROUP_MSG_RECV;
out_frame[i++] = RESP_CODE_CHANNEL_MSG_RECV;
out_frame[i++] = in_path_len < 0 ? 0xFF : in_path_len;
out_frame[i++] = TXT_TYPE_PLAIN;
memcpy(&out_frame[i], &timestamp, 4); i += 4;
@@ -351,15 +351,6 @@ public:
_public = addChannel(PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
}
// ContactVisitor
void onContactVisit(const ContactInfo& contact) override {
Serial.printf(" %s - ", contact.name);
char tmp[40];
int32_t secs = contact.last_advert_timestamp - getRTCClock()->getCurrentTime();
AdvertTimeHelper::formatRelativeTimeDiff(tmp, secs, false);
Serial.println(tmp);
}
void handleCmdFrame(size_t len) {
if (cmd_frame[0] == CMD_APP_START && len >= 8) { // sent when app establishes connection, respond with node ID
uint8_t app_ver = cmd_frame[1];
@@ -536,10 +527,15 @@ public:
};
#ifdef ESP32
#include <helpers/esp32/SerialBLEInterface.h>
SerialBLEInterface serial_interface;
#ifdef BLE_PIN_CODE
#include <helpers/esp32/SerialBLEInterface.h>
SerialBLEInterface serial_interface;
#else
#include <helpers/ArduinoSerialInterface.h>
ArduinoSerialInterface serial_interface;
#endif
#else
#error "need to define a serial interface"
#error "need to define a serial interface"
#endif
#if defined(NRF52_PLATFORM)
@@ -600,7 +596,11 @@ void setup() {
#elif defined(ESP32)
SPIFFS.begin(true);
#ifdef BLE_PIN_CODE
serial_interface.begin("MeshCore", BLE_PIN_CODE);
#else
serial_interface.begin(Serial);
#endif
serial_interface.enable();
the_mesh.begin(SPIFFS, serial_interface);

View File

@@ -106,7 +106,21 @@ lib_deps =
adafruit/RTClib @ ^2.1.3
densaugeo/base64 @ ~1.4.0
[env:Heltec_v3_companion_radio]
[env:Heltec_v3_companion_radio_usb]
extends = Heltec_lora32_v3
build_flags =
${Heltec_lora32_v3.build_flags}
-D MAX_CONTACTS=100
-D MAX_GROUP_CHANNELS=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<../examples/companion_radio/main.cpp>
lib_deps =
${Heltec_lora32_v3.lib_deps}
adafruit/RTClib @ ^2.1.3
densaugeo/base64 @ ~1.4.0
[env:Heltec_v3_companion_radio_ble]
extends = Heltec_lora32_v3
build_flags =
${Heltec_lora32_v3.build_flags}

View File

@@ -0,0 +1,66 @@
#include "ArduinoSerialInterface.h"
#define RECV_STATE_IDLE 0
#define RECV_STATE_HDR_FOUND 1
#define RECV_STATE_LEN_FOUND 2
void ArduinoSerialInterface::enable() {
_isEnabled = true;
_state = RECV_STATE_IDLE;
}
void ArduinoSerialInterface::disable() {
_isEnabled = false;
}
bool ArduinoSerialInterface::isConnected() const {
return true; // no way of knowing, so assume yes
}
bool ArduinoSerialInterface::isWriteBusy() const {
return false;
}
size_t ArduinoSerialInterface::writeFrame(const uint8_t src[], size_t len) {
if (len > MAX_FRAME_SIZE) {
// frame is too big!
return 0;
}
uint8_t hdr[2];
hdr[0] = '>';
hdr[1] = len;
_serial->write(hdr, 2);
return _serial->write(src, len);
}
size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
while (_serial->available()) {
int c = _serial->read();
if (c < 0) break;
switch (_state) {
case RECV_STATE_IDLE:
if (c == '<') {
_state = RECV_STATE_HDR_FOUND;
}
break;
case RECV_STATE_HDR_FOUND:
_frame_len = (uint8_t)c;
rx_len = 0;
_state = _frame_len > 0 ? RECV_STATE_LEN_FOUND : RECV_STATE_IDLE;
break;
default:
if (rx_len < MAX_FRAME_SIZE) {
rx_buf[rx_len] = (uint8_t)c; // rest of frame will be discarded if > MAX
}
rx_len++;
if (rx_len >= _frame_len) { // received a complete frame?
if (_frame_len > MAX_FRAME_SIZE) _frame_len = MAX_FRAME_SIZE; // truncate
memcpy(dest, rx_buf, _frame_len);
_state = RECV_STATE_IDLE; // reset state, for next frame
return _frame_len;
}
}
}
return 0;
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include "BaseSerialInterface.h"
#include <Arduino.h>
class ArduinoSerialInterface : public BaseSerialInterface {
bool _isEnabled;
uint8_t _state;
uint8_t _frame_len;
uint8_t rx_len;
HardwareSerial* _serial;
uint8_t rx_buf[MAX_FRAME_SIZE];
public:
ArduinoSerialInterface() { _isEnabled = false; _state = 0; }
void begin(HardwareSerial& serial) { _serial = &serial; }
// BaseSerialInterface methods
void enable() override;
void disable() override;
bool isEnabled() const override { return _isEnabled; }
bool isConnected() const override;
bool isWriteBusy() const override;
size_t writeFrame(const uint8_t src[], size_t len) override;
size_t checkRecvFrame(uint8_t dest[]) override;
};