Skip to content

Commit 6d69234

Browse files
committed
Added functionality from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140 which gives the ability to check if an address is in use.
Method added: Wire.checkAddress(address)
1 parent 49d70eb commit 6d69234

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

Wire/Wire.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ void TwoWire::onRequest( void (*function)(void) )
256256
user_onRequest = function;
257257
}
258258

259+
// used to check an address - from/see from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/2#2
260+
// modded by lstoll@lstoll.net (wasn't returning result)
261+
bool TwoWire::checkAddress(uint8_t twiAddress)
262+
{
263+
uint8_t *txBuffer;
264+
twi_writeTo(twiAddress, txBuffer, 0, 1);
265+
return twi_isFound();
266+
}
267+
268+
259269
// Preinstantiate Objects //////////////////////////////////////////////////////
260270

261271
TwoWire Wire = TwoWire();

Wire/Wire.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ class TwoWire
5959
uint8_t receive(void);
6060
void onReceive( void (*)(int) );
6161
void onRequest( void (*)(void) );
62+
// function to check address - from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/2#2
63+
bool checkAddress(uint8_t);
6264
};
6365

6466
extern TwoWire Wire;
6567

68+
#define WIRE_LIB_SCAN_MOD // notes the check address function has been added. ref http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/3#3
69+
6670
#endif
6771

Wire/utility/twi.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,25 @@ void twi_releaseBus(void)
310310
twi_state = TWI_READY;
311311
}
312312

313+
/*
314+
* Function twi_isFound
315+
* Desc method to see if a device was found after writing to an adddress
316+
* Input none
317+
* Output true(1) if a device was found, false(0) if not
318+
* From: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/1#1
319+
*/
320+
int foundDevice = 0;
321+
int twi_isFound(void)
322+
{
323+
if (foundDevice)
324+
{
325+
foundDevice = 0;
326+
return 1;
327+
}
328+
329+
return 0;
330+
}
331+
313332
SIGNAL(SIG_2WIRE_SERIAL)
314333
{
315334
switch(TW_STATUS){
@@ -323,6 +342,11 @@ SIGNAL(SIG_2WIRE_SERIAL)
323342

324343
// Master Transmitter
325344
case TW_MT_SLA_ACK: // slave receiver acked address
345+
// set found device to true if a device acknowledged the message
346+
// from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/1#1
347+
{
348+
foundDevice = 1;
349+
}
326350
case TW_MT_DATA_ACK: // slave receiver acked data
327351
// if there is data to send, send it, otherwise stop
328352
if(twi_masterBufferIndex < twi_masterBufferLength){

Wire/utility/twi.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#ifndef twi_h
21-
#define twi_h
20+
#ifndef twi_h
21+
#define twi_h
2222

2323
#include <inttypes.h>
2424

2525
//#define ATMEGA8
2626

27-
#ifndef CPU_FREQ
27+
#ifndef CPU_FREQ
2828
#define CPU_FREQ 16000000L
2929
#endif
3030

31-
#ifndef TWI_FREQ
31+
#ifndef TWI_FREQ
3232
#define TWI_FREQ 100000L
3333
#endif
3434

@@ -51,7 +51,9 @@
5151
void twi_attachSlaveTxEvent( void (*)(void) );
5252
void twi_reply(uint8_t);
5353
void twi_stop(void);
54-
void twi_releaseBus(void);
55-
54+
void twi_releaseBus(void);
55+
// dec for is found method from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192228140/1#1
56+
int twi_isFound(void);
57+
5658
#endif
5759

0 commit comments

Comments
 (0)