|
|
@ -62,7 +62,7 @@ class TWIBus {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Number of bytes on buffer
|
|
|
|
* @brief Number of bytes on buffer
|
|
|
|
* @description Number of bytes in the buffer waiting to be flushed to the bus.
|
|
|
|
* @description Number of bytes in the buffer waiting to be flushed to the bus
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
uint8_t buffer_s = 0;
|
|
|
|
uint8_t buffer_s = 0;
|
|
|
|
|
|
|
|
|
|
|
@ -94,7 +94,7 @@ class TWIBus {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Send the buffer data to the bus
|
|
|
|
* @brief Send the buffer data to the bus
|
|
|
|
* @details Flush the buffer to the bus at the target address.
|
|
|
|
* @details Flush the buffer to the target address
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void send();
|
|
|
|
void send();
|
|
|
|
|
|
|
|
|
|
|
@ -107,45 +107,57 @@ class TWIBus {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void addbyte(const char c);
|
|
|
|
void addbyte(const char c);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @brief Add some bytes to the buffer
|
|
|
|
|
|
|
|
* @details Add bytes to the end of the buffer.
|
|
|
|
|
|
|
|
* Concatenates at the buffer size.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param src source data address
|
|
|
|
|
|
|
|
* @param bytes the number of bytes to add
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void addbytes(char src[], uint8_t bytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @brief Add a null-terminated string to the buffer
|
|
|
|
|
|
|
|
* @details Add bytes to the end of the buffer up to a nul.
|
|
|
|
|
|
|
|
* Concatenates at the buffer size.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param str source string address
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void addstring(char str[]);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Set the target slave address
|
|
|
|
* @brief Set the target slave address
|
|
|
|
* @details The target slave address for sending the full packet.
|
|
|
|
* @details The target slave address for sending the full packet
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param addr 7-bit integer address
|
|
|
|
* @param adr 7-bit integer address
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void address(const uint8_t addr);
|
|
|
|
void address(const uint8_t adr);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Request data from the slave device
|
|
|
|
* @brief Echo data on the bus to serial
|
|
|
|
* @details Request a number of bytes from a slave device.
|
|
|
|
* @details Echo some number of bytes from the bus
|
|
|
|
* This implementation simply sends the data to serial
|
|
|
|
* to serial in a parser-friendly format.
|
|
|
|
* in a parser-friendly format.
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void reqbytes(const uint8_t bytes);
|
|
|
|
static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Relay data from the slave device to serial
|
|
|
|
* @brief Request data from the slave device
|
|
|
|
* @details Relay a number of bytes from the bus to
|
|
|
|
* @details Request a number of bytes from a slave device.
|
|
|
|
* serial in a parser-friendly format.
|
|
|
|
* This implementation simply sends the data to serial
|
|
|
|
|
|
|
|
* in a parser-friendly format.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void relaydata(uint8_t bytes);
|
|
|
|
void reqbytes(const uint8_t bytes);
|
|
|
|
|
|
|
|
|
|
|
|
#if I2C_SLAVE_ADDRESS > 0
|
|
|
|
#if I2C_SLAVE_ADDRESS > 0
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @brief Receive bytes (passively)
|
|
|
|
|
|
|
|
* @details Receive bytes sent to our slave address.
|
|
|
|
|
|
|
|
* and simply echo them to serial.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline void receive(uint8_t bytes) { relaydata(bytes); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Register a slave receive handler
|
|
|
|
* @brief Register a slave receive handler
|
|
|
|
* @details Set a handler to receive data addressed to us.
|
|
|
|
* @details Set a handler to receive data addressed to us
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -153,12 +165,25 @@ class TWIBus {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Register a slave request handler
|
|
|
|
* @brief Register a slave request handler
|
|
|
|
* @details Set a handler to send data requested from us.
|
|
|
|
* @details Set a handler to send data requested from us
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
|
|
|
|
inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @brief Default handler to receive
|
|
|
|
|
|
|
|
* @details Receive bytes sent to our slave address
|
|
|
|
|
|
|
|
* and simply echo them to serial.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void receive(uint8_t bytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @brief Send a reply to the bus
|
|
|
|
|
|
|
|
* @details Send the buffer and clear it.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void reply(char str[]=NULL);
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
@ -167,7 +192,11 @@ class TWIBus {
|
|
|
|
* @brief Prints a debug message
|
|
|
|
* @brief Prints a debug message
|
|
|
|
* @details Prints a simple debug message "TWIBus::function: value"
|
|
|
|
* @details Prints a simple debug message "TWIBus::function: value"
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static void debug(const char func[], int32_t val = -1);
|
|
|
|
static void prefix(const char func[]);
|
|
|
|
|
|
|
|
static void debug(const char func[], uint32_t adr);
|
|
|
|
|
|
|
|
static void debug(const char func[], char c);
|
|
|
|
|
|
|
|
static void debug(const char func[], char adr[]);
|
|
|
|
|
|
|
|
static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
};
|
|
|
|