Skip to content

Commit

Permalink
Merge pull request #230 from kivancsikert/i2c/fail-better-when-i2c-bu…
Browse files Browse the repository at this point in the history
…ses-exhausted

Fail better when ran out of I2C buses
  • Loading branch information
lptr authored Oct 13, 2024
2 parents d1827f6 + 87d47c6 commit 2bbd8f4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion main/kernel/I2CManager.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <exception>
#include <map>
#include <utility>
#include <exception>

#include <Arduino.h>
#include <Wire.h>
Expand Down Expand Up @@ -39,6 +39,9 @@ class I2CManager {
return *(it->second);
} else {
Log.trace("Creating new I2C bus for SDA: %d, SCL: %d", sda, scl);
if (nextBus >= 2) {
throw std::runtime_error("Maximum number of I2C buses reached");
}
TwoWire* wire = new TwoWire(nextBus++);
if (!wire->begin(sda, scl)) {
throw std::runtime_error(
Expand Down
3 changes: 2 additions & 1 deletion main/peripherals/light_sensor/Bh1750.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Bh1750Component
// TODO Make mode configurable
// TODO What's the difference between one-time and continuous mode here?
// Can we save some battery by using one-time mode? Are we losing anything by doing so?
if (!sensor.begin(BH1750::CONTINUOUS_LOW_RES_MODE, config.address, &i2c.getWireFor(config))) {
TwoWire& wire = i2c.getWireFor(config);
if (!sensor.begin(BH1750::CONTINUOUS_LOW_RES_MODE, config.address, &wire)) {
throw PeripheralCreationException("Failed to initialize BH1750 light sensor");
}

Expand Down

0 comments on commit 2bbd8f4

Please sign in to comment.