mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-15 00:27:40 +00:00
I2C device manager (#2282)
* message on dev list change * dc detect * added sht3x sensor. * separete environment data from light * max17055 moved to i2c dev * sht fix, goterror detection fix * fix ext sensor app display for a lot of devices. * added bh1750 driver * autoscan on main view * added devlist mutex * better timing * fix h2 sw8 on poweron by usb
This commit is contained in:
@@ -1,151 +1,24 @@
|
||||
#include "battery.hpp"
|
||||
#include "event_m0.hpp"
|
||||
#include "portapack.hpp"
|
||||
#include "ads1110.hpp"
|
||||
#include "max17055.hpp"
|
||||
|
||||
// uncomment if you want to emulate batt management system
|
||||
// #define USE_BATT_EMULATOR
|
||||
|
||||
extern I2C portapack::i2c0;
|
||||
|
||||
#include "i2cdevmanager.hpp"
|
||||
namespace battery {
|
||||
|
||||
constexpr uint32_t BATTERY_UPDATE_INTERVAL = 20000;
|
||||
BatteryManagement::BatteryModules BatteryManagement::detected_ = BatteryManagement::BATT_NONE;
|
||||
|
||||
ads1110::ADS1110 battery_ads1110{portapack::i2c0, 0x48};
|
||||
max17055::MAX17055 battery_max17055{portapack::i2c0, 0x36};
|
||||
|
||||
Thread* BatteryManagement::thread = nullptr;
|
||||
bool BatteryManagement::calcOverride = false;
|
||||
|
||||
void BatteryManagement::detect() {
|
||||
// try to detect supported modules
|
||||
detected_ = BATT_NONE;
|
||||
if (battery_max17055.detect()) {
|
||||
battery_max17055.init();
|
||||
detected_ = BATT_MAX17055;
|
||||
return;
|
||||
}
|
||||
if (battery_ads1110.detect()) {
|
||||
battery_ads1110.init();
|
||||
detected_ = BATT_ADS1110;
|
||||
return;
|
||||
}
|
||||
|
||||
// add new supported module detect + init here
|
||||
|
||||
#ifdef USE_BATT_EMULATOR
|
||||
if (detected_ == BATT_NONE) {
|
||||
detected_ = BATT_EMULATOR;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void BatteryManagement::init(bool override) {
|
||||
calcOverride = override;
|
||||
detect();
|
||||
// sets timer to query and broadcats this info
|
||||
create_thread();
|
||||
}
|
||||
|
||||
bool BatteryManagement::reset_learned() {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return battery_max17055.reset_learned();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// set if the default percentage calculation should be overrided by voltage based one
|
||||
void BatteryManagement::set_calc_override(bool override) {
|
||||
calcOverride = override;
|
||||
}
|
||||
|
||||
// sets the values, it the currend module supports it.
|
||||
void BatteryManagement::getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current) {
|
||||
if (detected_ == BATT_NONE) {
|
||||
valid_mask = BATT_VALID_NONE;
|
||||
return;
|
||||
} else if (detected_ == BATT_ADS1110) {
|
||||
battery_ads1110.getBatteryInfo(valid_mask, voltage);
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
return;
|
||||
} else if (detected_ == BATT_MAX17055) {
|
||||
battery_max17055.getBatteryInfo(valid_mask, batteryPercentage, voltage, current);
|
||||
if (calcOverride) {
|
||||
valid_mask &= ~BATT_VALID_PERCENT; // indicate it is voltage based
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// add new module query here
|
||||
|
||||
#ifdef USE_BATT_EMULATOR
|
||||
if (detected_ == BATT_EMULATOR) {
|
||||
batteryPercentage += 5; // %
|
||||
if (batteryPercentage > 100) batteryPercentage = 0;
|
||||
voltage = rand() % 1000 + 3000; // mV
|
||||
current = rand() % 150; // mA
|
||||
isCharging = rand() % 2;
|
||||
valid_mask = 7;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
(void)current;
|
||||
}
|
||||
|
||||
uint16_t BatteryManagement::get_cycles() {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return (uint16_t)battery_max17055.getValue("Cycles");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
float BatteryManagement::get_tte() {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return battery_max17055.getValue("TTE");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
float BatteryManagement::get_ttf() {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return battery_max17055.getValue("TTF");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t BatteryManagement::read_register(const uint8_t reg) {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return battery_max17055.read_register(reg);
|
||||
}
|
||||
return 0xFFFF;
|
||||
}
|
||||
|
||||
bool BatteryManagement::write_register(const uint8_t reg, const uint16_t value) {
|
||||
if (detected_ == BATT_MAX17055) {
|
||||
return battery_max17055.write_register(reg, value);
|
||||
}
|
||||
// Helper function to checkif there is ANY battery management ic present.
|
||||
bool BatteryManagement::isDetected() {
|
||||
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
||||
if (dev) return true;
|
||||
dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_ADS1110);
|
||||
if (dev) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t BatteryManagement::getPercent() {
|
||||
if (detected_ == BATT_NONE) return 102;
|
||||
uint8_t validity = 0;
|
||||
uint8_t batteryPercentage = 0;
|
||||
uint16_t voltage = 0;
|
||||
int32_t current = 0;
|
||||
getBatteryInfo(validity, batteryPercentage, voltage, current);
|
||||
if ((validity & BATT_VALID_VOLTAGE) != BATT_VALID_VOLTAGE) return 102;
|
||||
if (calcOverride || ((validity & BATT_VALID_PERCENT) != BATT_VALID_PERCENT)) {
|
||||
validity &= ~BATT_VALID_PERCENT; // indicate it is voltage based
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
}
|
||||
return batteryPercentage;
|
||||
}
|
||||
|
||||
uint8_t BatteryManagement::calc_percent_voltage(uint16_t voltage) {
|
||||
// Calculate the remaining battery percentage
|
||||
uint8_t batteryPercentage = (float)(voltage - BATTERY_MIN_VOLTAGE) / (float)(BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * 100.0;
|
||||
@@ -154,42 +27,4 @@ uint8_t BatteryManagement::calc_percent_voltage(uint16_t voltage) {
|
||||
return batteryPercentage;
|
||||
}
|
||||
|
||||
uint16_t BatteryManagement::getVoltage() {
|
||||
if (detected_ == BATT_NONE) return 0;
|
||||
uint8_t validity = 0;
|
||||
uint8_t batteryPercentage = 0;
|
||||
uint16_t voltage = 0;
|
||||
int32_t current = 0;
|
||||
getBatteryInfo(validity, batteryPercentage, voltage, current);
|
||||
if ((validity & BATT_VALID_VOLTAGE) != BATT_VALID_VOLTAGE) return 0;
|
||||
return voltage;
|
||||
}
|
||||
|
||||
msg_t BatteryManagement::timer_fn(void* arg) {
|
||||
(void)arg;
|
||||
uint8_t validity = 0;
|
||||
uint8_t batteryPercentage = 102;
|
||||
uint16_t voltage = 0;
|
||||
int32_t current = 0;
|
||||
chThdSleepMilliseconds(1000); // wait ui for fully load
|
||||
while (1) {
|
||||
if (!detected_) {
|
||||
detect(); // try to detect it again, it maybe disconnected while pp was powered up
|
||||
chThdSleepMilliseconds(500);
|
||||
}
|
||||
if (detected_) {
|
||||
BatteryManagement::getBatteryInfo(validity, batteryPercentage, voltage, current);
|
||||
// send local message
|
||||
BatteryStateMessage msg{validity, batteryPercentage, current >= 0, voltage};
|
||||
EventDispatcher::send_message(msg);
|
||||
}
|
||||
chThdSleepMilliseconds(BATTERY_UPDATE_INTERVAL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void BatteryManagement::create_thread() {
|
||||
thread = chThdCreateFromHeap(NULL, 512, NORMALPRIO, BatteryManagement::timer_fn, nullptr);
|
||||
}
|
||||
|
||||
} // namespace battery
|
Reference in New Issue
Block a user