@@ -88,8 +88,9 @@ class TwiMasterOrSlave : public TwiMaster
88
88
uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
89
89
volatile int twi_rxBufferIndex = 0 ;
90
90
91
- void (*twi_onSlaveTransmit)(void );
92
- void (*twi_onSlaveReceive)(uint8_t *, size_t );
91
+ void * twi_SlaveTargetObject;
92
+ void (*twi_onSlaveTransmit)(void *);
93
+ void (*twi_onSlaveReceive)(uint8_t *, size_t , void *);
93
94
94
95
// ETS queue/timer interfaces
95
96
enum { EVENTTASK_QUEUE_SIZE = 1 , EVENTTASK_QUEUE_PRIO = 2 };
@@ -103,9 +104,9 @@ class TwiMasterOrSlave : public TwiMaster
103
104
static void eventTask (ETSEvent *e);
104
105
static void ICACHE_RAM_ATTR onTimer (void *unused);
105
106
106
- // Allow not linking in the slave code if there is no call to setAddress
107
+ // Allow not linking in the slave code if there is no call to enableSlave
107
108
bool _slaveEnabled = false ;
108
-
109
+
109
110
// Internal use functions
110
111
void ICACHE_RAM_ATTR onTwipEvent (uint8_t status);
111
112
@@ -115,11 +116,11 @@ class TwiMasterOrSlave : public TwiMaster
115
116
116
117
void setAddress (uint8_t address);
117
118
uint8_t transmit (const uint8_t * data, uint8_t length);
118
- void attachSlaveRxEvent (void (*function)(uint8_t *, size_t ));
119
- void attachSlaveTxEvent (void (*function)(void ));
119
+ void attachSlaveRxEvent (void (*function)(uint8_t *, size_t , void * ));
120
+ void attachSlaveTxEvent (void (*function)(void * ));
120
121
void ICACHE_RAM_ATTR reply (uint8_t ack);
121
122
void ICACHE_RAM_ATTR releaseBus (void );
122
- void enableSlave ();
123
+ void enableSlave (void * targetObject );
123
124
};
124
125
125
126
static TwiMasterOrSlave twi;
@@ -211,10 +212,11 @@ void TwiMasterOrSlave::setAddress(uint8_t address)
211
212
twi_addr = address << 1 ;
212
213
}
213
214
214
- void TwiMasterOrSlave::enableSlave ()
215
+ void TwiMasterOrSlave::enableSlave (void * targetObject )
215
216
{
216
217
if (!_slaveEnabled)
217
218
{
219
+ twi_SlaveTargetObject = targetObject;
218
220
attachInterrupt (twi_scl, onSclChange, CHANGE);
219
221
attachInterrupt (twi_sda, onSdaChange, CHANGE);
220
222
_slaveEnabled = true ;
@@ -453,12 +455,12 @@ uint8_t TwiMasterOrSlave::transmit(const uint8_t* data, uint8_t length)
453
455
return 0 ;
454
456
}
455
457
456
- void TwiMasterOrSlave::attachSlaveRxEvent (void (*function)(uint8_t *, size_t ))
458
+ void TwiMasterOrSlave::attachSlaveRxEvent (void (*function)(uint8_t *, size_t , void * ))
457
459
{
458
460
twi_onSlaveReceive = function;
459
461
}
460
462
461
- void TwiMasterOrSlave::attachSlaveTxEvent (void (*function)(void ))
463
+ void TwiMasterOrSlave::attachSlaveTxEvent (void (*function)(void * ))
462
464
{
463
465
twi_onSlaveTransmit = function;
464
466
}
@@ -624,7 +626,7 @@ void TwiMasterOrSlave::eventTask(ETSEvent *e)
624
626
switch (e->sig )
625
627
{
626
628
case TWI_SIG_TX:
627
- twi.twi_onSlaveTransmit ();
629
+ twi.twi_onSlaveTransmit (twi. twi_SlaveTargetObject );
628
630
629
631
// if they didn't change buffer & length, initialize it
630
632
if (twi.twi_txBufferLength == 0 )
@@ -641,7 +643,7 @@ void TwiMasterOrSlave::eventTask(ETSEvent *e)
641
643
case TWI_SIG_RX:
642
644
// ack future responses and leave slave receiver state
643
645
twi.releaseBus ();
644
- twi.twi_onSlaveReceive (twi.twi_rxBuffer , e->par );
646
+ twi.twi_onSlaveReceive (twi.twi_rxBuffer , e->par , twi. twi_SlaveTargetObject );
645
647
break ;
646
648
}
647
649
}
@@ -990,16 +992,17 @@ extern "C" {
990
992
return twi.transmit (buf, len);
991
993
}
992
994
993
- void twi_attachSlaveRxEvent (void (*cb)(uint8_t *, size_t ))
995
+ void twi_attachSlaveRxEventWithTarget (void (*cb)(uint8_t *, size_t , void * ))
994
996
{
995
997
twi.attachSlaveRxEvent (cb);
996
998
}
997
999
998
- void twi_attachSlaveTxEvent (void (*cb)(void ))
1000
+ void twi_attachSlaveTxEventWithTarget (void (*cb)(void * ))
999
1001
{
1000
1002
twi.attachSlaveTxEvent (cb);
1001
1003
}
1002
1004
1005
+
1003
1006
void twi_reply (uint8_t r)
1004
1007
{
1005
1008
twi.reply (r);
@@ -1010,9 +1013,9 @@ extern "C" {
1010
1013
twi.releaseBus ();
1011
1014
}
1012
1015
1013
- void twi_enableSlaveMode (void )
1016
+ void twi_enableSlaveModeWithTarget (void * targetObject )
1014
1017
{
1015
- twi.enableSlave ();
1018
+ twi.enableSlave (targetObject );
1016
1019
}
1017
1020
1018
1021
};
0 commit comments