Merge pull request #4606 from thinkyhead/rc_more_i2c_slave
Add an i2c request handler
This commit is contained in:
commit
11027a56b6
|
@ -837,8 +837,17 @@ void servo_init() {
|
|||
|
||||
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
|
||||
|
||||
void i2c_listener(int bytes) {
|
||||
i2c.receive(bytes); // just echo all bytes received to serial
|
||||
void i2c_on_receive(int bytes) { // just echo all bytes received to serial
|
||||
i2c.receive(bytes);
|
||||
}
|
||||
|
||||
void i2c_on_request() { // just send dummy data for now
|
||||
static const char *msg = "Hello World!\n";
|
||||
const char *adr = msg;
|
||||
char c;
|
||||
i2c.reset();
|
||||
while (c = *adr++) i2c.addbyte(c);
|
||||
i2c.send();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -991,7 +1000,8 @@ void setup() {
|
|||
#endif
|
||||
|
||||
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
|
||||
i2c.onReceive(i2c_listener);
|
||||
i2c.onReceive(i2c_on_receive);
|
||||
i2c.onRequest(i2c_on_request);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,8 @@
|
|||
// Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
|
||||
//#define DEBUG_TWIBUS
|
||||
|
||||
typedef void (*twiSlaveFunc_t)(int bytes);
|
||||
typedef void (*twiReceiveFunc_t)(int bytes);
|
||||
typedef void (*twiRequestFunc_t)();
|
||||
|
||||
/**
|
||||
* TWIBUS class
|
||||
|
@ -143,13 +144,20 @@ class TWIBus {
|
|||
inline void receive(uint8_t bytes) { relaydata(bytes); }
|
||||
|
||||
/**
|
||||
* @brief Register a slave handler
|
||||
* @details Set a handler to receive data from the bus,
|
||||
* so we can act as a slave.
|
||||
* @brief Register a slave receive handler
|
||||
* @details Set a handler to receive data addressed to us.
|
||||
*
|
||||
* @param handler A function to handle receiving bytes
|
||||
*/
|
||||
inline void onReceive(const twiSlaveFunc_t handler) { Wire.onReceive(handler); }
|
||||
inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
|
||||
|
||||
/**
|
||||
* @brief Register a slave request handler
|
||||
* @details Set a handler to send data requested from us.
|
||||
*
|
||||
* @param handler A function to handle receiving bytes
|
||||
*/
|
||||
inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in a new issue