Browse Source

Merge pull request #4606 from thinkyhead/rc_more_i2c_slave

Add an i2c request handler
pull/1/head
Scott Lahteine 8 years ago
committed by GitHub
parent
commit
11027a56b6
  1. 16
      Marlin/Marlin_main.cpp
  2. 18
      Marlin/twibus.h

16
Marlin/Marlin_main.cpp

@ -837,8 +837,17 @@ void servo_init() {
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0 #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
void i2c_listener(int bytes) { void i2c_on_receive(int bytes) { // just echo all bytes received to serial
i2c.receive(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 #endif
@ -991,7 +1000,8 @@ void setup() {
#endif #endif
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0 #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
i2c.onReceive(i2c_listener); i2c.onReceive(i2c_on_receive);
i2c.onRequest(i2c_on_request);
#endif #endif
} }

18
Marlin/twibus.h

@ -30,7 +30,8 @@
// Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM) // Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
//#define DEBUG_TWIBUS //#define DEBUG_TWIBUS
typedef void (*twiSlaveFunc_t)(int bytes); typedef void (*twiReceiveFunc_t)(int bytes);
typedef void (*twiRequestFunc_t)();
/** /**
* TWIBUS class * TWIBUS class
@ -143,13 +144,20 @@ class TWIBus {
inline void receive(uint8_t bytes) { relaydata(bytes); } inline void receive(uint8_t bytes) { relaydata(bytes); }
/** /**
* @brief Register a slave handler * @brief Register a slave receive handler
* @details Set a handler to receive data from the bus, * @details Set a handler to receive data addressed to us.
* so we can act as a slave.
* *
* @param handler A function to handle receiving bytes * @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 #endif

Loading…
Cancel
Save