|
| 1 | +/* |
| 2 | + FirmataTransport.cpp |
| 3 | + Copyright (c) 2025 Phil Schatzmann. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | +#ifdef USE_FIRMATA |
| 20 | +#include "FirmataTransport.h" |
| 21 | +#include <chrono> |
| 22 | + |
| 23 | +namespace arduino { |
| 24 | + |
| 25 | +static constexpr uint8_t DIGITAL_MESSAGE = 0x90; |
| 26 | +static constexpr uint8_t ANALOG_MESSAGE = 0xE0; |
| 27 | +static constexpr uint8_t START_SYSEX = 0xF0; |
| 28 | +static constexpr uint8_t END_SYSEX = 0xF7; |
| 29 | + |
| 30 | +FirmataTransport::~FirmataTransport() { end(); } |
| 31 | + |
| 32 | +bool FirmataTransport::begin(Stream &stream) { |
| 33 | + end(); // idempotent |
| 34 | + |
| 35 | + stream_ = &stream; |
| 36 | + running_ = true; |
| 37 | + thread_ = std::thread(&FirmataTransport::readerLoop, this); |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +void FirmataTransport::end() { |
| 42 | + running_ = false; |
| 43 | + if (thread_.joinable()) { |
| 44 | + thread_.join(); |
| 45 | + } |
| 46 | + stream_ = nullptr; |
| 47 | +} |
| 48 | + |
| 49 | +void FirmataTransport::writeBytes(const uint8_t *data, size_t len) { |
| 50 | + std::lock_guard<std::mutex> lock(write_mutex_); |
| 51 | + if (!stream_) return; |
| 52 | + for (size_t i = 0; i < len; ++i) { |
| 53 | + stream_->write(data[i]); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void FirmataTransport::addSysexHandler(uint8_t command, SysexHandler handler) { |
| 58 | + std::lock_guard<std::mutex> lock(sysex_handlers_mutex_); |
| 59 | + sysex_handlers_[command] = std::move(handler); |
| 60 | +} |
| 61 | + |
| 62 | +void FirmataTransport::removeSysexHandler(uint8_t command) { |
| 63 | + std::lock_guard<std::mutex> lock(sysex_handlers_mutex_); |
| 64 | + sysex_handlers_.erase(command); |
| 65 | +} |
| 66 | + |
| 67 | +void FirmataTransport::readerLoop() { |
| 68 | + enum class State { kWaitCommand, kWaitLsb, kWaitMsb, kInSysex }; |
| 69 | + State state = State::kWaitCommand; |
| 70 | + uint8_t command = 0; |
| 71 | + uint8_t channel = 0; |
| 72 | + uint8_t lsb = 0; |
| 73 | + std::vector<uint8_t> sysex_buffer; |
| 74 | + |
| 75 | + while (running_.load()) { |
| 76 | + if (stream_ == nullptr || stream_->available() <= 0) { |
| 77 | + std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + const int value = stream_->read(); |
| 82 | + if (value < 0) continue; |
| 83 | + const uint8_t byte = static_cast<uint8_t>(value); |
| 84 | + |
| 85 | + if (state == State::kInSysex) { |
| 86 | + if (byte == END_SYSEX) { |
| 87 | + state = State::kWaitCommand; |
| 88 | + if (!sysex_buffer.empty()) { |
| 89 | + const uint8_t sysexCommand = sysex_buffer.front(); |
| 90 | + SysexHandler handler; |
| 91 | + { |
| 92 | + std::lock_guard<std::mutex> lock(sysex_handlers_mutex_); |
| 93 | + const auto it = sysex_handlers_.find(sysexCommand); |
| 94 | + if (it != sysex_handlers_.end()) handler = it->second; |
| 95 | + } |
| 96 | + if (handler) { |
| 97 | + FirmataSysexMessage msg; |
| 98 | + msg.command = sysexCommand; |
| 99 | + msg.data.assign(sysex_buffer.begin() + 1, sysex_buffer.end()); |
| 100 | + handler(msg); |
| 101 | + } |
| 102 | + } |
| 103 | + sysex_buffer.clear(); |
| 104 | + } else { |
| 105 | + sysex_buffer.push_back(byte); |
| 106 | + } |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + switch (state) { |
| 111 | + case State::kWaitCommand: |
| 112 | + if (byte == START_SYSEX) { |
| 113 | + state = State::kInSysex; |
| 114 | + sysex_buffer.clear(); |
| 115 | + } else if ((byte & 0xF0) == DIGITAL_MESSAGE) { |
| 116 | + command = DIGITAL_MESSAGE; |
| 117 | + channel = byte & 0x0F; |
| 118 | + state = State::kWaitLsb; |
| 119 | + } else if ((byte & 0xF0) == ANALOG_MESSAGE) { |
| 120 | + command = ANALOG_MESSAGE; |
| 121 | + channel = byte & 0x0F; |
| 122 | + state = State::kWaitLsb; |
| 123 | + } |
| 124 | + // Any other command byte (REPORT_VERSION, SYSTEM_RESET, ...) is |
| 125 | + // outside the subset this client needs and is simply discarded. |
| 126 | + break; |
| 127 | + case State::kWaitLsb: |
| 128 | + lsb = byte & 0x7F; |
| 129 | + state = State::kWaitMsb; |
| 130 | + break; |
| 131 | + case State::kWaitMsb: { |
| 132 | + const uint8_t msb = byte & 0x7F; |
| 133 | + if (command == DIGITAL_MESSAGE) { |
| 134 | + if (digital_handler_) { |
| 135 | + FirmataDigitalMessage msg; |
| 136 | + msg.port = channel; |
| 137 | + msg.value = lsb | (static_cast<uint16_t>(msb) << 7); |
| 138 | + digital_handler_(msg); |
| 139 | + } |
| 140 | + } else { |
| 141 | + if (analog_handler_) { |
| 142 | + FirmataAnalogMessage msg; |
| 143 | + msg.pin = channel; |
| 144 | + msg.value = lsb | (static_cast<int>(msb) << 7); |
| 145 | + analog_handler_(msg); |
| 146 | + } |
| 147 | + } |
| 148 | + state = State::kWaitCommand; |
| 149 | + break; |
| 150 | + } |
| 151 | + case State::kInSysex: |
| 152 | + // handled above |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace arduino |
| 159 | + |
| 160 | +#endif // USE_FIRMATA |
0 commit comments