Skip to content

Commit ef94b18

Browse files
committed
testing
1 parent ff9394c commit ef94b18

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

lib/src/multimotor/serial/lx_servo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ LXServo::LXServo(uint8_t id, SerialDriveManager* bus)
3232

3333
bool LXServo::send(uint8_t cmd, const uint8_t* params, int param_cnt, uint8_t id) {
3434
if (param_cnt < 0 || param_cnt > 4) return false;
35-
auto serial = (bus_? bus_->getInterface() : nullptr);
36-
if (!serial) return false;
35+
if (!bus_) return false;
3736

3837
uint8_t txbuf[32] = {0};
3938
int buflen = 6 + param_cnt;
@@ -51,7 +50,7 @@ bool LXServo::send(uint8_t cmd, const uint8_t* params, int param_cnt, uint8_t id
5150
cksum += txbuf[i];
5251
txbuf[buflen - 1] = ~cksum;
5352

54-
serial->write(txbuf, buflen);
53+
bus_->write(txbuf, buflen);
5554
return true;
5655
}
5756

lib/src/multimotor/serial/serial_drive_manager.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
#include "../debugprint.h"
66
#include "lx_servo.h"
77

8+
void SerialDriveManager::beginSinglePin(SerialInterface* port, int txRxPin) {
9+
interface_ = port;
10+
uartSinglePin_ = txRxPin;
11+
if (!interface_)
12+
return;
13+
#ifdef ARDUINO_ARCH_ESP32
14+
auto serial = (HardwareSerial*)interface_;
15+
serial->begin(115200, SERIAL_8N1, txRxPin, txRxPin);
16+
pinMode(txRxPin, OUTPUT | PULLUP);
17+
#endif
18+
delay(3);
19+
while (interface_->available())
20+
interface_->read();
21+
}
22+
23+
void SerialDriveManager::beginDualPins(SerialInterface* port, int txPin, int rxPin) {
24+
interface_ = port;
25+
if (!interface_)
26+
return;
27+
uartSinglePin_ = GPIO_NUM_NC;
28+
#ifdef ARDUINO
29+
auto serial = (HardwareSerial*)interface_;
30+
serial->begin(115200, SERIAL_8N1, txPin, rxPin);
31+
#endif
32+
pinMode(txPin, OUTPUT | PULLUP);
33+
pinMode(rxPin, INPUT | PULLDOWN);
34+
}
35+
836
void SerialDriveManager::addDrive(MotorDrive* drive) {
937
if (driveCount_ < MAX_DRIVES) {
1038
drives_[driveCount_++] = drive;
@@ -48,6 +76,13 @@ void SerialDriveManager::handleIncoming(uint32_t id, uint8_t const* indata, uint
4876
}
4977

5078
void SerialDriveManager::iterate(uint32_t now) {
79+
if (uartSinglePin_ != GPIO_NUM_NC) {
80+
if (interface_->availableForWrite() < 1) { //finished write
81+
pinMode(uartSinglePin_, INPUT | PULLUP);
82+
delayMicroseconds(10);
83+
}
84+
}
85+
5186
uint8_t buf[INBUF_LEN];
5287
int len = 0;
5388
while (interface_->available() && (len < INBUF_LEN)) {
@@ -57,3 +92,13 @@ void SerialDriveManager::iterate(uint32_t now) {
5792
handleIncoming(0, buf, len, now);
5893
}
5994
}
95+
96+
void SerialDriveManager::write(uint8_t const* data, uint8_t len) {
97+
if (!interface_ || len == 0) return;
98+
#if defined ARDUINO_ARCH_ESP32
99+
if (uartSinglePin_ != GPIO_NUM_NC)
100+
pinMode(uartSinglePin_, OUTPUT | PULLUP);
101+
delayMicroseconds(10);
102+
#endif
103+
interface_->write(data, len);
104+
}

lib/src/multimotor/serial/serial_drive_manager.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
class SerialDriveManager : public DriveManager {
66
public:
7-
SerialDriveManager(SerialInterface* serialInterface) : interface_(serialInterface) {}
8-
SerialInterface* getInterface() const { return interface_; }
7+
SerialDriveManager() { }
8+
// SerialInterface* getInterface() const { return interface_; }
9+
void beginSinglePin(SerialInterface* port, int txRxPin);
10+
void beginDualPins(SerialInterface* port, int txPin, int rxPin);
911

1012
virtual void addDrive(MotorDrive* drive) override;
1113
virtual MotorDrive* getDrive(uint8_t id) override;
@@ -15,10 +17,13 @@ class SerialDriveManager : public DriveManager {
1517
virtual void handleIncoming(uint32_t id, uint8_t const* data, uint8_t len, uint32_t now) override;
1618
virtual void iterate(uint32_t now) override;
1719

20+
void write(uint8_t const* data, uint8_t len);
21+
1822
protected:
1923
static constexpr uint8_t MAX_DRIVES = 16;
2024
MotorDrive* drives_[MAX_DRIVES] = {nullptr};
2125
uint8_t driveCount_ = 0;
26+
int uartSinglePin_ = -1;
2227
SerialInterface* interface_ = nullptr;
2328

2429
static constexpr uint8_t INBUF_LEN = 32;

0 commit comments

Comments
 (0)