Procházet zdrojové kódy

Add an i2c request handler

Scott Lahteine před 8 roky
rodič
revize
67f119d18b
2 změnil soubory, kde provedl 26 přidání a 8 odebrání
  1. 13
    3
      Marlin/Marlin_main.cpp
  2. 13
    5
      Marlin/twibus.h

+ 13
- 3
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -837,8 +837,17 @@ void servo_init() {
837 837
 
838 838
 #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
839 839
 
840
-  void i2c_listener(int bytes) {
841
-    i2c.receive(bytes);        // just echo all bytes received to serial
840
+  void i2c_on_receive(int bytes) { // just echo all bytes received to serial
841
+    i2c.receive(bytes);
842
+  }
843
+
844
+  void i2c_on_request() {          // just send dummy data for now
845
+    static const char *msg = "Hello World!\n";
846
+    const char *adr = msg;
847
+    char c;
848
+    i2c.reset();
849
+    while (c = *adr++) i2c.addbyte(c);
850
+    i2c.send();
842 851
   }
843 852
 
844 853
 #endif
@@ -991,7 +1000,8 @@ void setup() {
991 1000
   #endif
992 1001
 
993 1002
   #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
994
-    i2c.onReceive(i2c_listener);
1003
+    i2c.onReceive(i2c_on_receive);
1004
+    i2c.onRequest(i2c_on_request);
995 1005
   #endif
996 1006
 }
997 1007
 

+ 13
- 5
Marlin/twibus.h Zobrazit soubor

@@ -30,7 +30,8 @@
30 30
 // Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
31 31
 //#define DEBUG_TWIBUS
32 32
 
33
-typedef void (*twiSlaveFunc_t)(int bytes);
33
+typedef void (*twiReceiveFunc_t)(int bytes);
34
+typedef void (*twiRequestFunc_t)();
34 35
 
35 36
 /**
36 37
  * TWIBUS class
@@ -143,13 +144,20 @@ class TWIBus {
143 144
       inline void receive(uint8_t bytes) { relaydata(bytes); }
144 145
 
145 146
       /**
146
-       * @brief Register a slave handler
147
-       * @details Set a handler to receive data from the bus,
148
-       *          so we can act as a slave.
147
+       * @brief Register a slave receive handler
148
+       * @details Set a handler to receive data addressed to us.
149 149
        *
150 150
        * @param handler A function to handle receiving bytes
151 151
        */
152
-      inline void onReceive(const twiSlaveFunc_t handler) { Wire.onReceive(handler); }
152
+      inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
153
+
154
+      /**
155
+       * @brief Register a slave request handler
156
+       * @details Set a handler to send data requested from us.
157
+       *
158
+       * @param handler A function to handle receiving bytes
159
+       */
160
+      inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
153 161
 
154 162
     #endif
155 163
 

Loading…
Zrušit
Uložit