#pragma once #include #include #include #include #include // 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; using ReadCallback = std::function(size_t len, uint32_t timeout_ms)>; using TransactCallback = std::function(const uint8_t* data, size_t len)>; using DelayCallback = std::function; 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 checksum(const std::vector& data); bool write(const std::vector& data) const; std::vector 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 queryRaw(uint8_t addr, uint8_t cmd) const; std::optional queryRawNew(uint8_t addr, uint8_t cmd) const; bool send(int dec_addr, uint8_t cmd) const; std::optional 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 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& frame) const; void sleepMs(uint32_t ms) const; };