--I am trying to reuse > I2CScanner.ino -- I2C bus scanner for Arduino
- 2009,2014, Tod E. Kurt, and incorporate into .h and .cpp file.
--I am stuck on how to assign the argument in the main program of , method:
look_for_I2C.scanI2CBus(look_for_I2C.start_address, look_for_I2C.end_address,
scanFunc);
--do not known what to put in place to scanFunc to make it works ?
-- what C++ code principle this involved in solving this ?
--This program will be incorporated later into another codes that will use the first I2C device address found and to read data from it. The foundDeviceFlag is to indicate a device is found and the address will be used.
Both foundDeviceFlag and addr to be used by main program.
scan form address 8 to 119.
/* ***** Borrowed and based of:
I2CScanner.ino -- I2C bus scanner for Arduino
* 2009,2014, Tod E. Kurt, http://todbot.com/blog/
*/
#include <Wire.h>
#include "firstChkI2c.h"
#define WIRE Wire
class I2CclassNew look_for_I2C;
// ------------------------------------------------
void setup() {
Serial.begin(9600);
delay(100);
WIRE.begin();
Serial.println("\nI2CScanner ready!");
Serial.print("starting scanning of I2C bus from addr DEC : ");
Serial.print(look_for_I2C.start_address, DEC);
Serial.print(" to ");
Serial.print(look_for_I2C.end_address, DEC);
Serial.println("......");
look_for_I2C.scanI2CBus(look_for_I2C.start_address, look_for_I2C.end_address, scanFunc);
// do not known what to put in place to scanFunc to make it works ?
}
void loop() {}
====================================================
firstChkI2C.h >>>> below
#include <Arduino.h>
#include <Wire.h>
extern "C" {
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
#define WIRE Wire
extern "C" {
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
#ifndef FIRSTCHKI2C_H
#define FIRSTCHKI2C_H
class I2CclassNew {
public:
byte addr, result;
byte start_address = 8; // lower addresses are reserved to prevent conflicts with other protocols
byte end_address = 119; // higher addresses unlock other modes, like 10-bit addressing
static int foundDeviceFlag; //** static
void scanI2CBus(byte from_addr, byte to_addr,
void (*callback)(byte address, byte result)) // pointer to function ??
{
Serial.println("Scanning...");
byte rc;
byte data = 0; // not used, just an address to feed to twi_writeTo()
for (byte addr = from_addr; addr <= to_addr; addr++) {
rc = twi_writeTo(addr, &data, 0, 1, 0);
callback(addr, rc);
delay(100);
}
}
};
#endif
FirstChkI2c.cpp file below >>
include <Arduino.h>
#include <Wire.h>
#include "firstChkI2c.h"
// Called when address is found in scanI2CBus()
void *scanFunc(byte addr, byte result) {
Serial.print("addr 0x: ");
Serial.print(addr, HEX);
I2CclassNew ::foundDeviceFlag = 0;
Serial.print((result == 0) ? " found!" : " ");
Serial.print((addr % 4) ? "\t" : "\n");
if (result == 0) {
I2CclassNew ::foundDeviceFlag = 1;
Serial.print(" foundDeviceFlag = ");
Serial.println(I2CclassNew ::foundDeviceFlag);
Serial.print((addr % 4) ? "\t" : "\n");
}
}