ESPresense/src/I2C.cpp

139 lines
4.0 KiB
C++
Raw Normal View History

#ifdef SENSORS
2022-07-09 19:02:43 +02:00
#include "I2C.h"
#include "globals.h"
#include "mqtt.h"
#include "defaults.h"
2022-07-31 05:54:47 +02:00
#include <AsyncWiFiSettings.h>
#include "string_utils.h"
#include <Adafruit_BME280.h>
2022-07-09 19:02:43 +02:00
namespace I2C
{
2022-07-09 19:02:43 +02:00
bool I2CDebug = false;
2022-07-09 19:02:43 +02:00
void ConnectToWifi()
{
2022-07-31 05:54:47 +02:00
AsyncWiFiSettings.heading("I2C Settings <a href='https://espresense.com/configuration/settings#i2c-settings' target='_blank'></a>", false);
2022-07-09 19:02:43 +02:00
2022-07-31 05:54:47 +02:00
AsyncWiFiSettings.html("h4", "Bus 1:");
2022-07-11 05:28:22 +02:00
2022-07-31 05:54:47 +02:00
I2C_Bus_1_SDA = AsyncWiFiSettings.integer("I2C_Bus_1_SDA", 0, 39, DEFAULT_I2C_BUS_1_SDA, "SDA pin (-1 to disable)");
I2C_Bus_1_SCL = AsyncWiFiSettings.integer("I2C_Bus_1_SCL", 0, 39, DEFAULT_I2C_BUS_1_SCL, "SCL pin (-1 to disable)");
2022-07-09 19:02:43 +02:00
2022-07-31 05:54:47 +02:00
AsyncWiFiSettings.html("h4", "Bus 2:");
2022-07-09 19:02:43 +02:00
2022-07-31 05:54:47 +02:00
I2C_Bus_2_SDA = AsyncWiFiSettings.integer("I2C_Bus_2_SDA", -1, "SDA pin (-1 to disable)");
I2C_Bus_2_SCL = AsyncWiFiSettings.integer("I2C_Bus_2_SCL", -1, "SCL pin (-1 to disable)");
2022-07-09 19:02:43 +02:00
2022-07-31 05:54:47 +02:00
I2CDebug = AsyncWiFiSettings.checkbox("I2CDebug", false, "Debug I2C addreses. Look at the serial log to get the correct address");
}
2022-07-09 19:02:43 +02:00
void SerialReport()
{
}
2022-07-09 19:02:43 +02:00
void Setup()
{
2022-07-09 19:02:43 +02:00
if (I2C_Bus_1_SDA != -1 && I2C_Bus_1_SDA != -1) {
Wire.begin(I2C_Bus_1_SDA, I2C_Bus_1_SCL);
I2C_Bus_1_Enabled = true;
2022-07-11 05:28:22 +02:00
Serial.println("Initialized I2C Bus 1 (SDA: " + String(I2C_Bus_1_SDA) + ", SCL: " + String(I2C_Bus_1_SCL) + ")");
2022-07-09 19:02:43 +02:00
}
if (I2C_Bus_2_SDA != -1 && I2C_Bus_2_SDA != -1) {
Wire1.begin(I2C_Bus_2_SDA, I2C_Bus_2_SCL);
I2C_Bus_2_Enabled = true;
2022-07-11 05:28:22 +02:00
Serial.println("Initialized I2C Bus 2 (SDA: " + String(I2C_Bus_2_SDA) + ", SCL: " + String(I2C_Bus_2_SCL) + ")");
2022-07-09 19:02:43 +02:00
}
}
void Loop()
{
if (!I2C_Bus_1_Enabled && !I2C_Bus_2_Enabled) return;
2022-07-09 19:02:43 +02:00
if (!I2CDebug) return;
I2CDebug = 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)
{
2022-07-09 19:02:43 +02:00
Serial.print("Unknown 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)
{
2022-07-09 19:02:43 +02:00
Serial.print("Unknown 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()
{
2022-07-09 19:02:43 +02:00
return true;
}
}
#endif