initial commit
This commit is contained in:
68
include/dali_comm.hpp
Normal file
68
include/dali_comm.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
// Lightweight communicator for DALI gateway type 1 (USB UART new frame format).
|
||||
// Frames:
|
||||
// - Send: [0x10, addr, cmd]
|
||||
// - Ext: [0x11, addr, cmd] (sent twice with a small delay)
|
||||
// - Query: [0x12, addr, cmd] -> expects [0xFF, data] on success
|
||||
// Callers provide UART callbacks; this class only builds frames and parses basic responses.
|
||||
class DaliComm {
|
||||
public:
|
||||
using SendCallback = std::function<bool(const uint8_t* data, size_t len)>;
|
||||
using ReadCallback = std::function<std::vector<uint8_t>(size_t len, uint32_t timeout_ms)>;
|
||||
using TransactCallback = std::function<std::vector<uint8_t>(const uint8_t* data, size_t len)>;
|
||||
using DelayCallback = std::function<void(uint32_t ms)>;
|
||||
|
||||
explicit DaliComm(SendCallback send_cb,
|
||||
ReadCallback read_cb = nullptr,
|
||||
TransactCallback transact_cb = nullptr,
|
||||
DelayCallback delay_cb = nullptr);
|
||||
|
||||
void setSendCallback(SendCallback cb);
|
||||
void setReadCallback(ReadCallback cb);
|
||||
void setTransactCallback(TransactCallback cb);
|
||||
void setDelayCallback(DelayCallback cb);
|
||||
|
||||
// Methods mirroring lib/dali/comm.dart naming where practical.
|
||||
static std::vector<uint8_t> checksum(const std::vector<uint8_t>& data);
|
||||
bool write(const std::vector<uint8_t>& data) const;
|
||||
std::vector<uint8_t> read(size_t len, uint32_t timeout_ms = 100) const;
|
||||
int checkGatewayType(int gateway) const;
|
||||
void flush() const;
|
||||
bool resetBus() const;
|
||||
bool sendRaw(uint8_t addr, uint8_t cmd) const;
|
||||
bool sendRawNew(uint8_t addr, uint8_t cmd, bool needVerify = false) const;
|
||||
bool sendExtRaw(uint8_t addr, uint8_t cmd) const;
|
||||
bool sendExtRawNew(uint8_t addr, uint8_t cmd) const;
|
||||
std::optional<uint8_t> queryRaw(uint8_t addr, uint8_t cmd) const;
|
||||
std::optional<uint8_t> queryRawNew(uint8_t addr, uint8_t cmd) const;
|
||||
bool send(int dec_addr, uint8_t cmd) const;
|
||||
std::optional<uint8_t> query(int dec_addr, uint8_t cmd) const;
|
||||
bool getBusStatus() const;
|
||||
|
||||
// Send standard command frame (0x10).
|
||||
bool sendCmd(uint8_t addr, uint8_t cmd) const;
|
||||
// Send extended command frame (0x11).
|
||||
bool sendExtCmd(uint8_t addr, uint8_t cmd) const;
|
||||
// Send query frame (0x12) and parse single-byte response. Returns nullopt on no/invalid response.
|
||||
std::optional<uint8_t> queryCmd(uint8_t addr, uint8_t cmd) const;
|
||||
|
||||
// Helpers to mirror Dart address conversion (DEC short address -> DALI odd/even encoded).
|
||||
static uint8_t toCmdAddr(int dec_addr); // odd address for commands
|
||||
static uint8_t toArcAddr(int dec_addr); // even address for direct arc
|
||||
|
||||
private:
|
||||
SendCallback send_;
|
||||
ReadCallback read_;
|
||||
TransactCallback transact_;
|
||||
DelayCallback delay_;
|
||||
|
||||
bool writeFrame(const std::vector<uint8_t>& frame) const;
|
||||
void sleepMs(uint32_t ms) const;
|
||||
};
|
||||
Reference in New Issue
Block a user