Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail better when ran out of I2C buses #230

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading