Skip to content

Commit

Permalink
Refactor libraries and test
Browse files Browse the repository at this point in the history
  • Loading branch information
GOB52 committed Jun 11, 2024
1 parent b7b11cb commit e39a856
Show file tree
Hide file tree
Showing 23 changed files with 2,725 additions and 320 deletions.
2,600 changes: 2,600 additions & 0 deletions lib/M5PaHub/docs/Doxyfile

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions lib/M5PaHub/docs/doxy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Please execute on repositry root

## Get version from library.properties
## Get git rev of HEAD
LIB_VERSION="$(pcregrep -o1 "^\s*version\s*=\s*(\*|\d+(\.\d+){0,3}(\.\*)?)" library.properties)"
#echo ${DOXYGEN_PROJECT_NUMBER}
DOXYGEN_PROJECT_NUMBER="${LIB_VERSION} git rev:$(git rev-parse --short HEAD)" doxygen docs/Doxyfile


42 changes: 0 additions & 42 deletions lib/M5PaHub/src/unit/unit_PaHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,6 @@ using namespace m5::utility::mmh3;
namespace m5 {
namespace unit {

#if 0
// For children
class PaHubWireImpl : public Adapter::Impl {
public:
explicit PaHubWireImpl(TwoWire& wire, const uint8_t addr)
: Adapter::Impl(addr), _wire(&wire) {
}

virtual m5::hal::error::error_t read(uint8_t* data,
const size_t len) override {
if (data) {
if (_wire->requestFrom(_addr, len)) {
auto count = std::min(len, (size_t)_wire->available());
for (size_t i = 0; i < count; ++i) {
data[i] = (uint8_t)_wire->read();
}
return (count == len) ? m5::hal::error::error_t::OK
: m5::hal::error::error_t::UNKNOWN_ERROR;
}
}
return m5::hal::error::error_t::UNKNOWN_ERROR;
}
virtual m5::hal::error::error_t write(const uint8_t* data,
const size_t len) override {
}

protected:
TwoWire* _wire{};
};

class PaHubAdapter : public Adapter {
public:
PaHubAdapter(TwoWire& wire, const uint8_t addr) {
}
PaHubAdapter(m5::hal::bus::Bus* bus, const uint8_t addr) {
}
PaHubAdapter(m5::hal::bus::Bus& bus, const uint8_t addr)
: PaHubAdapter(&bus, addr) {
}
};
#endif

// class UnitPaHub
const char UnitPaHub::name[] = "UnitPaHub";
const types::uid_t UnitPaHub::uid{"UnitPaHub"_mmh3};
Expand Down
16 changes: 2 additions & 14 deletions lib/M5PaHub/src/unit/unit_PaHub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ namespace unit {
@brief PaHub unit (PCA 9548AP)
*/
class UnitPaHub : public Component {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitPaHub, 0x70);

public:
constexpr static uint8_t DEFAULT_ADDRESS{0x70};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];
constexpr static uint8_t MAX_CHANNEL = 6;

explicit UnitPaHub(const uint8_t addr = DEFAULT_ADDRESS);
Expand All @@ -38,15 +36,6 @@ class UnitPaHub : public Component {
}

protected:
inline virtual const char* unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}
virtual Adapter* ensure_adapter(const uint8_t ch) override;
virtual m5::hal::error::error_t select_channel(
const uint8_t ch = 8) override;
Expand All @@ -59,5 +48,4 @@ class UnitPaHub : public Component {

} // namespace unit
} // namespace m5

#endif
17 changes: 2 additions & 15 deletions lib/M5Unit-ENV/src/unit/unit_ENV3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ namespace unit {
@details This unit itself has no I/O, but holds SHT30 and QMP6988
*/
class UnitENV3 : public Component {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitENV3, 0xFF);

public:
UnitSHT30 sht30; //!< @brief SHT30 instance
UnitQMP6988 qmp6988; //!< @brief QMP6988 instance

// Not access, UnitENV3 is just a container
constexpr static uint8_t DEFAULT_ADDRESS{0x00};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];

explicit UnitENV3(const uint8_t addr = DEFAULT_ADDRESS);
virtual ~UnitENV3() {
}
Expand All @@ -42,15 +38,6 @@ class UnitENV3 : public Component {
}

protected:
inline virtual const char* unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}
virtual Adapter* ensure_adapter(const uint8_t ch) override;

private:
Expand Down
2 changes: 1 addition & 1 deletion lib/M5Unit-ENV/src/unit/unit_QMP6988.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ bool UnitQMP6988::reset() {
uint8_t v{0xE6}; // When inputting "E6h", a soft-reset will be occurred
auto ret = writeRegister8(RESET, v);
(void)ret;
// TODO / WARNING
// TODO / WARNING (HAL)
// Reset causes a NO ACK error, but ignore it.
m5::utility::delay(10); // Need delay

Expand Down
30 changes: 9 additions & 21 deletions lib/M5Unit-ENV/src/unit/unit_QMP6988.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ enum class Average : uint8_t {
@brief Operation mode setting
*/
enum class PowerMode : uint8_t {
Sleep =
0, //!< @brief Minimal power consumption, but no measurements are taken
Force =
1, //!< @brief Energise the circuit for measurement only when measuring
Normal = 3, //!< @brief Normally energized (periodic measurement)
//! @brief Minimal power consumption, but no measurements are taken
Sleep = 0,
//! @brief Energise the circuit for measurement only when measuring
Force = 1,
//! @brief Normally energized (periodic measurement)
Normal = 3,
};

/*!
Expand All @@ -60,6 +61,7 @@ struct CtrlMeasurement {
return static_cast<PowerMode>(value & 0x03);
}
///@}

///@name Setter
///@{
void temperatureAvg(const Average a) {
Expand Down Expand Up @@ -149,12 +151,9 @@ struct Calibration {
estimation
*/
class UnitQMP6988 : public Component {
public:
constexpr static uint8_t DEFAULT_ADDRESS{0x70};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];
M5_UNIT_COMPONENT_HPP_BUILDER(UnitQMP6988, 0x70);

public:
/*!
@struct config_t
@brief Settings
Expand Down Expand Up @@ -227,7 +226,6 @@ class UnitQMP6988 : public Component {
inline qmp6988::Average pressureAverage() const {
return _pressureAvg;
}

///@}

// API
Expand Down Expand Up @@ -355,16 +353,6 @@ class UnitQMP6988 : public Component {
bool getStatus(qmp6988::Status& s);

protected:
inline virtual const char* unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}

bool get_measurement_condition(uint8_t& cond);
bool set_measurement_condition(const uint8_t cond);
bool get_io_setup(uint8_t& s);
Expand Down
36 changes: 4 additions & 32 deletions lib/M5Unit-ENV/src/unit/unit_SCD4x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ namespace unit {
@brief CO2 sensor unit
*/
class UnitSCD40 : public Component {
public:
constexpr static uint8_t DEFAULT_ADDRESS{0x62};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];
M5_UNIT_COMPONENT_HPP_BUILDER(UnitSCD40, 0x62);

public:
/*!
@struct config_t
@brief Settings
Expand Down Expand Up @@ -296,19 +293,8 @@ class UnitSCD40 : public Component {
///@}

protected:
inline virtual const char *unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}

bool read_measurement(const bool all = true);


protected:
constexpr static unsigned long SIGNAL_INTERVAL_MS{5000};
constexpr static unsigned long SIGNAL_INTERVAL_LOW_MS{30 * 1000};
Expand All @@ -330,12 +316,9 @@ class UnitSCD40 : public Component {
@brief CO2 sensor unit
*/
class UnitSCD41 : public UnitSCD40 {
public:
constexpr static uint8_t DEFAULT_ADDRESS{0x62};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];
M5_UNIT_COMPONENT_HPP_BUILDER(UnitSCD41, 0xFF);

public:
explicit UnitSCD41(const uint8_t addr = DEFAULT_ADDRESS) : UnitSCD40(addr) {
}

Expand All @@ -361,17 +344,6 @@ class UnitSCD41 : public UnitSCD40 {
*/
bool measureSingleShotRHTOnly(void);
///@}

protected:
inline virtual const char *unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}
};

///@cond
Expand Down
32 changes: 19 additions & 13 deletions lib/M5Unit-ENV/src/unit/unit_SHT3x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct Temperature {
}
};

// After sending a command to the sensor a minimalwaiting time of 1ms is needed
// before another commandcan be received by the sensor.
bool delay1() {
m5::utility::delay(1);
return true;
Expand Down Expand Up @@ -97,14 +99,12 @@ bool UnitSHT30::measurementSingleShot(const sht3x::Repeatability rep,

uint32_t idx = m5::stl::to_underlying(rep) + (stretch ? 0 : 3);
if (idx >= m5::stl::size(cmd)) {
M5_LIB_LOGE("Internal error");
M5_LIB_LOGE("Invalid arg : %u", (int)rep);
return false;
}

if (writeRegister(cmd[idx])) {
if (!stretch) {
m5::utility::delay(ms[m5::stl::to_underlying(rep)]);
}
m5::utility::delay(stretch ? 1 : ms[m5::stl::to_underlying(rep)]);
return read_measurement();
}
return false;
Expand Down Expand Up @@ -151,16 +151,18 @@ bool UnitSHT30::startPeriodicMeasurement(const sht3x::MPS mps,
m5::stl::to_underlying(rep)]);
if (_periodic) {
_interval = interval_table[m5::stl::to_underlying(mps)];
m5::utility::delay(15);
m5::utility::delay(16);
return true;
}
return _periodic;
}

bool UnitSHT30::stopPeriodicMeasurement() {
if (writeRegister(STOP_PERIODIC_MEASUREMENT)) {
_periodic = false;
m5::utility::delay(15);
return true;
// Upon reception of the break command the sensor will abort the ongoing
// measurement and enter the single shot mode. This takes 1ms
return delay1();
}
return false;
}
Expand All @@ -181,7 +183,7 @@ bool UnitSHT30::readMeasurement() {
bool UnitSHT30::accelerateResponseTime() {
if (writeRegister(ACCELERATED_RESPONSE_TIME)) {
_interval = 1000 / 4; // 4mps
m5::utility::delay(15);
m5::utility::delay(16);
return true;
}
return false;
Expand All @@ -205,15 +207,17 @@ bool UnitSHT30::clearStatus() {

bool UnitSHT30::softReset() {
if (inPeriodic()) {
M5_LIB_LOGD("Periodic measurements are running");
M5_LIB_LOGE("Periodic measurements are running");
return false;
}

if (!clearStatus() || !writeRegister(SOFT_RESET)) {
return false;
if (writeRegister(SOFT_RESET)) {
// Max 1.5 ms
// Time between ACK of soft reset command and sensor entering idle state
m5::utility::delay(2);
return true;
}
m5::utility::delay(2);
return true;
return false;
}

bool UnitSHT30::generalReset() {
Expand All @@ -225,6 +229,8 @@ bool UnitSHT30::generalReset() {
// Reset does not return ACK, which is an error, but should be ignored
generalCall(&cmd, 1);

m5::utility::delay(1);

auto timeout_at = m5::utility::millis() + 10;
bool done{};
do {
Expand Down
17 changes: 2 additions & 15 deletions lib/M5Unit-ENV/src/unit/unit_SHT3x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,9 @@ struct Status {
@brief Temperature and humidity, sensor unit
*/
class UnitSHT30 : public Component {
public:
constexpr static uint8_t DEFAULT_ADDRESS{0x44};
static const types::uid_t uid;
static const types::attr_t attr;
static const char name[];
M5_UNIT_COMPONENT_HPP_BUILDER(UnitSHT30, 0x44);

public:
/*!
@struct config_t
@brief Settings
Expand Down Expand Up @@ -272,16 +269,6 @@ class UnitSHT30 : public Component {
///@}

protected:
inline virtual const char* unit_device_name() const override {
return name;
}
inline virtual types::uid_t unit_identifier() const override {
return uid;
}
inline virtual types::attr_t unit_attribute() const override {
return attr;
}

bool read_measurement();

protected:
Expand Down
Loading

0 comments on commit e39a856

Please sign in to comment.