// The device class. You'll derive your from this. Override init() and update();
classI2cDev{
public:
virtual~I2cDev(){};
virtualboolinit(uint8_taddr);// returns true if it is that that device we are looking for.
virtualvoidupdate()=0;// override this, and you'll be able to query your device and broadcast the result to the system
voidset_update_interval(uint8_tinterval);// sets the device's update interval in sec. if you change it, don't forget to change back to it's original value after you finished!
uint8_tget_update_interval();// gets the device's update interval in sec
booli2c_read(uint8_t*reg,uint8_treg_size,uint8_t*data,uint8_tbytes);// if want to read without register addr, just set reg_size to 0. this way can read 8, or 16 or 32 bit registers too. reg_size in bytes! returns true on succes. handles the errcnt automatically!
booli2c_write(uint8_t*reg,uint8_treg_size,uint8_t*data,uint8_tbytes);// if want to write without register addr, just set reg_size to 0. this way can read 8, or 16 or 32 bit registers too. reg_size in bytes! returns true on succes. handles the errcnt automatically!
// helpers for easier i2c communication
uint8_tread8_1(uint8_treg);
boolwrite8_1(uint8_treg,uint8_tdata);
uint16_tread16_1(uint8_treg);
int16_treadS16_1(uint8_treg);
uint16_tread16_LE_1(uint8_treg);
int16_treadS16_LE_1(uint8_treg);
uint32_tread24_1(uint8_treg);
boolneed_del=false;// device can self destruct, and re-init when new scan discovers it
I2C_DEVMDLmodel=I2CDEVMDL_NOTSET;// overwrite it in the init()!!!
uint8_tquery_interval=5;// in seconds. can be overriden in init() if necessary
protected:
voidgot_error();// i2c communication will call this when communication was not ok. you can call it from any part of your code too.
voidgot_success();// i2c communication will call this when the communication was ok. you can call it from any part of your code too.
uint8_taddr=0;// some devices can have different addresses, so we store what was it wound with
uint8_terrcnt=0;// error count during communication. if it reaches a threshold set need_del to remove itself from the device list
};
// store for the devices. may not have a driver if not supported
classI2DevListElement{
public:
uint8_taddr=0;// i2c addr of the device
std::unique_ptr<I2cDev>dev=nullptr;// device driver if any
};
classI2CDevManager{
public:
staticvoidinit();// creates the thread, and sets an one time full scan
staticvoidmanual_scan();// it'll init a forced device scan in the thread's next cycle. (1sec max)
staticvoidset_autoscan_interval(uint16_tinterval);// 0 no auto scan, other values: seconds
staticuint16_tget_autoscan_interval();
staticI2cDev*get_dev_by_addr(uint8_taddr);// caller function needs to cast to the specific device!
staticI2cDev*get_dev_by_model(I2C_DEVMDLmodel);// caller function needs to cast to the specific device!
staticstd::vector<I2C_DEVMDL>get_dev_list_by_model();// returns the currently discovered
staticstd::vector<uint8_t>get_gev_list_by_addr();// returns the currently discovered