From 4e454331d469bfcd531e72034a5c2c5e446acde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 20 Dec 2017 17:55:09 +0100 Subject: [PATCH] Improved I2C auto-discover avoiding collissions --- code/espurna/config/prototypes.h | 4 ++- code/espurna/config/sensors.h | 4 +-- code/espurna/i2c.ino | 46 ++++++++++++++++++++++-- code/espurna/sensors/BMX280Sensor.h | 16 +++------ code/espurna/sensors/BaseSensor.h | 32 +++++++++++++++++ code/espurna/sensors/EmonADC121Sensor.h | 11 ++---- code/espurna/sensors/EmonADS1X15Sensor.h | 11 ++---- code/espurna/sensors/SI7021Sensor.h | 14 +++----- 8 files changed, 95 insertions(+), 43 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index a3018c43..b6bfb588 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -51,7 +51,9 @@ template String getSetting(const String& key, unsigned int index, T // ----------------------------------------------------------------------------- // I2C // ----------------------------------------------------------------------------- -unsigned char i2cFindFirst(size_t size, unsigned char * addresses); +unsigned char i2cFindAndLock(size_t size, unsigned char * addresses); +bool i2cGetLock(unsigned char address); +bool i2cReleaseLock(unsigned char address); // ----------------------------------------------------------------------------- // Debug diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index fa0e6a35..e25d0302 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -93,7 +93,7 @@ //-------------------------------------------------------------------------------- #ifndef BMX280_SUPPORT -#define BMX280_SUPPORT 0 +#define BMX280_SUPPORT 1 #endif #ifndef BMX280_ADDRESS @@ -283,7 +283,7 @@ //-------------------------------------------------------------------------------- #ifndef SI7021_SUPPORT -#define SI7021_SUPPORT 0 +#define SI7021_SUPPORT 1 #endif #ifndef SI7021_ADDRESS diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index e914b35d..77a83e63 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -8,6 +8,9 @@ Copyright (C) 2017 by Xose PĂ©rez #if I2C_SUPPORT +#include +std::vector _i2c_locked; + #if I2C_USE_BRZO #include "brzo_i2c.h" unsigned long _i2c_scl_frequency = 0; @@ -123,13 +126,50 @@ bool i2cCheck(unsigned char address) { #endif } -unsigned char i2cFindFirst(size_t size, unsigned char * addresses) { - for (unsigned char i=0; i