mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-03-31 03:55:46 +00:00
* 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:
66
src/helpers/ArduinoSerialInterface.cpp
Normal file
66
src/helpers/ArduinoSerialInterface.cpp
Normal 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;
|
||||
}
|
||||
29
src/helpers/ArduinoSerialInterface.h
Normal file
29
src/helpers/ArduinoSerialInterface.h
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user