CoreS3 refactored & remove M5 libraries (#143)
Remove dependencies M5Unified and M5GFX because: - Sometimes the release doesn't even compile - The amount of functions is too close to the limit (I recently had to remove some code/dependency to be able to make a build at all) - Graphics performance issue since last update - Touch screen performance issues (not perfect yet, but better) - Compatibility issues with Tactility SPI/I2C versus M5Unified/M5GFX variants.
This commit is contained in:
committed by
GitHub
parent
3214923425
commit
737c0f7447
@@ -0,0 +1,74 @@
|
||||
#include "Axp2101.h"
|
||||
#include "Log.h"
|
||||
|
||||
bool Axp2101::readRegister12(uint8_t reg, float& out) const {
|
||||
std::uint8_t data[2] = {0};
|
||||
if (tt::hal::i2c::masterRead(port, DEFAULT_ADDRESS, reg, data, 2, DEFAULT_TIMEOUT) == ESP_OK) {
|
||||
out = (data[0] & 0x0F) << 8 | data[1];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Axp2101::readRegister14(uint8_t reg, float& out) const {
|
||||
std::uint8_t data[2] = {0};
|
||||
if (tt::hal::i2c::masterRead(port, DEFAULT_ADDRESS, reg, data, 2, DEFAULT_TIMEOUT) == ESP_OK) {
|
||||
out = (data[0] & 0x3F) << 8 | data[1];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Axp2101::readRegister16(uint8_t reg, uint16_t& out) const {
|
||||
std::uint8_t data[2] = {0};
|
||||
if (tt::hal::i2c::masterRead(port, DEFAULT_ADDRESS, reg, data, 2, DEFAULT_TIMEOUT) == ESP_OK) {
|
||||
out = data[0] << 8 | data[1];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Axp2101::readRegister8(uint8_t reg, uint8_t& result) const {
|
||||
return tt::hal::i2c::masterWriteRead(port, DEFAULT_ADDRESS, ®, 1, &result, 1, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
bool Axp2101::writeRegister8(uint8_t reg, uint8_t value) const {
|
||||
return tt::hal::i2c::masterWrite(port, DEFAULT_ADDRESS, reg, &value, 1, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
bool Axp2101::getBatteryVoltage(float& vbatMillis) const {
|
||||
return readRegister14(0x34, vbatMillis);
|
||||
}
|
||||
|
||||
bool Axp2101::getChargeStatus(ChargeStatus& status) const {
|
||||
uint8_t value;
|
||||
if (readRegister8(0x01, value)) {
|
||||
value = (value >> 5) & 0b11;
|
||||
status = (value == 1) ? CHARGE_STATUS_CHARGING : ((value == 2) ? CHARGE_STATUS_DISCHARGING : CHARGE_STATUS_STANDBY);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Axp2101::isChargingEnabled(bool& enabled) const {
|
||||
uint8_t value;
|
||||
if (readRegister8(0x18, value)) {
|
||||
enabled = value & 0b10;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Axp2101::setChargingEnabled(bool enabled) const {
|
||||
uint8_t value;
|
||||
if (readRegister8(0x18, value)) {
|
||||
return writeRegister8(0x18, (value & 0xFD) | (enabled << 1));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "hal/i2c/I2c.h"
|
||||
|
||||
/**
|
||||
* References:
|
||||
* - https://github.com/m5stack/M5Unified/blob/master/src/utility/AXP2101_Class.cpp
|
||||
* - http://file.whycan.com/files/members/6736/AXP2101_Datasheet_V1.0_en_3832.pdf
|
||||
*/
|
||||
class Axp2101 {
|
||||
|
||||
i2c_port_t port;
|
||||
|
||||
static constexpr uint8_t DEFAULT_ADDRESS = 0x34;
|
||||
static constexpr TickType_t DEFAULT_TIMEOUT = 1000 / portTICK_PERIOD_MS;
|
||||
|
||||
bool readRegister8(uint8_t reg, uint8_t& result) const;
|
||||
bool writeRegister8(uint8_t reg, uint8_t value) const;
|
||||
bool readRegister12(uint8_t reg, float& out) const;
|
||||
bool readRegister14(uint8_t reg, float& out) const;
|
||||
bool readRegister16(uint8_t reg, uint16_t& out) const;
|
||||
|
||||
public:
|
||||
|
||||
enum ChargeStatus {
|
||||
CHARGE_STATUS_CHARGING = 0b01,
|
||||
CHARGE_STATUS_DISCHARGING = 0b10,
|
||||
CHARGE_STATUS_STANDBY = 0b00
|
||||
};
|
||||
|
||||
Axp2101(i2c_port_t port) : port(port) {}
|
||||
|
||||
bool getBatteryVoltage(float& vbatMillis) const;
|
||||
bool getChargeStatus(ChargeStatus& status) const;
|
||||
bool isChargingEnabled(bool& enabled) const;
|
||||
bool setChargingEnabled(bool enabled) const;
|
||||
};
|
||||
Reference in New Issue
Block a user