From 3bd6ad39aa58a35d6c90f3b297dffc3ee2b6aa7c Mon Sep 17 00:00:00 2001
From: Darrell
Date: Fri, 8 Jul 2022 19:06:11 -0400
Subject: [PATCH] Refactor i2c scanner into sensor #526 (#538)
---
src/I2CScanner.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++
src/I2CScanner.h | 13 ++++++
src/globals.h | 1 -
src/main.cpp | 101 ++-------------------------------------
src/main.h | 1 +
5 files changed, 131 insertions(+), 99 deletions(-)
create mode 100644 src/I2CScanner.cpp
create mode 100644 src/I2CScanner.h
diff --git a/src/I2CScanner.cpp b/src/I2CScanner.cpp
new file mode 100644
index 0000000..8d06e46
--- /dev/null
+++ b/src/I2CScanner.cpp
@@ -0,0 +1,114 @@
+#ifdef SENSORS
+
+#include "I2CScanner.h"
+
+#include "globals.h"
+#include "mqtt.h"
+#include "defaults.h"
+#include
+#include "string_utils.h"
+
+#include
+
+namespace I2CScanner
+{
+ bool enabled = false;
+
+ void Setup()
+ {
+ }
+
+ void ConnectToWifi()
+ {
+ enabled = WiFiSettings.checkbox("enabled", false, "Debug I2C addreses. Look at the serial log to get the correct address");
+ }
+
+ void SerialReport()
+ {
+ }
+
+ void Loop()
+ {
+ if (!I2C_Bus_1_Enabled && !I2C_Bus_2_Enabled) return;
+ if (!enabled) return;
+ enabled = false;
+
+ byte error, address;
+ int nDevices;
+ nDevices = 0;
+
+ if (I2C_Bus_1_Enabled)
+ {
+ Serial.println("Scanning I2C for devices on Bus 1...");
+ for (address = 1; address < 127; address++)
+ {
+ Wire.beginTransmission(address);
+ error = Wire.endTransmission();
+ if (error == 0)
+ {
+ Serial.print("I2C device found on bus 1 at address 0x");
+
+ if (address < 16)
+ {
+ Serial.print("0");
+ }
+
+ Serial.println(address, HEX);
+ nDevices++;
+ }
+ else if (error == 4)
+ {
+ Serial.print("Unknow error on bus 1 at address 0x");
+ if (address < 16)
+ {
+ Serial.print("0");
+ }
+ Serial.println(address, HEX);
+ }
+ }
+ }
+
+ if (I2C_Bus_2_Enabled)
+ {
+ Serial.println("Scanning I2C for devices on Bus 2...");
+
+ for (address = 1; address < 127; address++)
+ {
+ Wire1.beginTransmission(address);
+ error = Wire1.endTransmission();
+ if (error == 0)
+ {
+ Serial.print("I2C device found on bus 2 at address 0x");
+
+ if (address < 16)
+ {
+ Serial.print("0");
+ }
+
+ Serial.println(address, HEX);
+ nDevices++;
+ }
+ else if (error == 4)
+ {
+ Serial.print("Unknow error on bus 2 at address 0x");
+ if (address < 16)
+ {
+ Serial.print("0");
+ }
+ Serial.println(address, HEX);
+ }
+ }
+ }
+
+ if (nDevices == 0)
+ {
+ Serial.println("No I2C devices found\n");
+ }
+ }
+
+ bool SendDiscovery()
+ {
+ }
+}
+
+#endif
diff --git a/src/I2CScanner.h b/src/I2CScanner.h
new file mode 100644
index 0000000..0f6859f
--- /dev/null
+++ b/src/I2CScanner.h
@@ -0,0 +1,13 @@
+#pragma once
+#ifdef SENSORS
+#include
+
+namespace I2CScanner {
+ void Setup();
+ void ConnectToWifi();
+ void SerialReport();
+ void Loop();
+ bool SendDiscovery();
+}
+
+#endif
diff --git a/src/globals.h b/src/globals.h
index efa4778..ebb8417 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -36,6 +36,5 @@ _DECL int I2C_Bus_1_SDA;
_DECL int I2C_Bus_1_SCL;
_DECL int I2C_Bus_2_SDA;
_DECL int I2C_Bus_2_SCL;
-_DECL bool I2CDebug;
_DECL bool I2C_Bus_1_Enabled;
_DECL bool I2C_Bus_2_Enabled;
diff --git a/src/main.cpp b/src/main.cpp
index 1225647..1393ce6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -201,8 +201,6 @@ void setupNetwork()
WiFiSettings.heading("I2C Settings ℹ️", false);
- I2CDebug = WiFiSettings.checkbox("I2CDebug", false, "Debug I2C addreses. Look at the serial log to get the correct address");
-
WiFiSettings.html("h4", "Bus 1:");
I2C_Bus_1_SDA = WiFiSettings.integer("I2C_Bus_1_SDA", 0, 39, DEFAULT_I2C_BUS_1_SDA, "SDA pin (0 to disable)");
I2C_Bus_1_SCL = WiFiSettings.integer("I2C_Bus_1_SCL", 0, 39, DEFAULT_I2C_BUS_1_SCL, "SCL pin (0 to disable)");
@@ -212,6 +210,8 @@ void setupNetwork()
I2C_Bus_2_SDA = WiFiSettings.integer("I2C_Bus_2_SDA", 0, "SDA pin (0 to disable)");
I2C_Bus_2_SCL = WiFiSettings.integer("I2C_Bus_2_SCL", 0, "SCL pin (0 to disable)");
+ I2CScanner::ConnectToWifi();
+
WiFiSettings.heading("I2C Sensors ℹ️", false);
BH1750::ConnectToWifi();
@@ -568,24 +568,6 @@ void setup()
#endif
#ifdef SENSORS
DHT::Setup();
-
- if (I2C_Bus_1_SDA != 0 && I2C_Bus_1_SDA != 0) {
- Wire.begin(I2C_Bus_1_SDA, I2C_Bus_1_SCL);
- I2C_Bus_1_Enabled = true;
- Serial.println("\nInitialized I2C Bus 1");
- }
-
- if (I2C_Bus_2_SDA != 0 && I2C_Bus_2_SDA != 0) {
- Wire1.begin(I2C_Bus_2_SDA, I2C_Bus_2_SCL);
- I2C_Bus_2_Enabled = true;
- Serial.println("\nInitialized I2C Bus 2");
- }
-
- if (I2CDebug)
- {
- Serial.println("\nI2C Scanner");
- }
-
BH1750::Setup();
BME280::Setup();
TSL2561::Setup();
@@ -601,83 +583,6 @@ void setup()
//non blocking ambient sensor
-void l2cScanner()
-{
- if (I2C_Bus_1_Enabled || I2C_Bus_2_Enabled) {
-
- if (!I2CDebug) return;
-
- byte error, address;
- int nDevices;
- Serial.println("Scanning I2C device...");
- nDevices = 0;
-
- for (address = 1; address < 127; address++)
- {
- Wire.beginTransmission(address);
- error = Wire.endTransmission();
- if (error == 0)
- {
- Serial.print("I2C device found on bus 1 at address 0x");
-
- if (address < 16)
- {
- Serial.print("0");
- }
-
- Serial.println(address, HEX);
- nDevices++;
- }
- else if (error == 4)
- {
- Serial.print("Unknow error on bus 1 at address 0x");
- if (address < 16)
- {
- Serial.print("0");
- }
- Serial.println(address, HEX);
- }
- }
-
- for (address = 1; address < 127; address++)
- {
- Wire1.beginTransmission(address);
- error = Wire1.endTransmission();
- if (error == 0)
- {
- Serial.print("I2C device found on bus 2 at address 0x");
-
- if (address < 16)
- {
- Serial.print("0");
- }
-
- Serial.println(address, HEX);
- nDevices++;
- }
- else if (error == 4)
- {
- Serial.print("Unknow error on bus 2 at address 0x");
- if (address < 16)
- {
- Serial.print("0");
- }
- Serial.println(address, HEX);
- }
- }
-
- if (nDevices == 0)
- {
- Serial.println("No I2C devices found\n");
- }
- else
- {
- Serial.println("done\n");
- I2CDebug = false;
- }
- delay(5000);
- }
-}
#endif
void loop()
@@ -694,7 +599,7 @@ void loop()
BME280::Loop();
TSL2561::Loop();
HX711::Loop();
- l2cScanner();
+ I2CScanner::Loop();
#endif
WiFiSettings.httpLoop();
}
diff --git a/src/main.h b/src/main.h
index b908527..352b28e 100644
--- a/src/main.h
+++ b/src/main.h
@@ -25,6 +25,7 @@
#include "Network.h"
#include "MotionSensors.h"
+#include "I2CScanner.h"
#ifdef SENSORS
#include