Skip to content

Commit

Permalink
feat(base_peripheral): make BasePeripheral a template on size of regi…
Browse files Browse the repository at this point in the history
…ster address (#151)

* feat(base_peripheral): make BasePeripheral a template on size of register addresses
* Update base peripheral component to be a template class whith template argument being the type for the register address (default uint8_t)
* Updated all subclasses to include the required <> for using the default type of uint8_t

* added missing template

* feat(base_peripheral): update how multi-byte registers are handled
* Update how multi-byte registers are handled in base_peripheral and add additional logging for help debugging
* Update gt911 and st25dv to subclass with register size 16 bit

* feat(base_peripheral): add set_address and probe public methods

* feat(st25dv): udpate to use new set_address function to simplify implementation

* doc:rebuild

* update logging

* minor update to comment

* feat(i2c): add logging functions

* fix(st25dv): read DATA address space for IT_STS
* IT_STS is in DATA address space not SYST address space
* Change IT_STS from class to bitfield struct so that we can use it when returning the interrupt status
* Added priting and comparison for IT_STS
* Update example to decrease logging in ST25, use new IT_STS logging, and update clock speed to 1MHz

* feat(base_peripheral): add public methods for updating config
* Added methods for updating specific members of the config or the config as a whole

* update gt911 example to read every 50ms instead of 100ms

* doc: rebuild
  • Loading branch information
finger563 authored Feb 13, 2024
1 parent bd2353f commit b4e301f
Show file tree
Hide file tree
Showing 112 changed files with 3,906 additions and 534 deletions.
2 changes: 1 addition & 1 deletion components/ads1x15/include/ads1x15.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace espp {
* \section ads1x15_ex1 ADS1X15 Example
* \snippet ads1x15_example.cpp ads1x15 example
*/
class Ads1x15 : public espp::BasePeripheral {
class Ads1x15 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = (0x48); ///< I2C address of the ADS1x15 chips.

Expand Down
2 changes: 1 addition & 1 deletion components/ads7138/include/ads7138.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace espp {
* @section ads7138_ex1 ADS7138 Example
* @snippet ads7138_example.cpp ads7138 example
*/
class Ads7138 : public BasePeripheral {
class Ads7138 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS =
(0x10); ///< Default I2C address of the device (when both R1 and R2 are DNP) (see data sheet
Expand Down
2 changes: 1 addition & 1 deletion components/as5600/include/as5600.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace espp {
* \section as5600_ex1 As5600 Example
* \snippet as5600_example.cpp as5600 example
*/
class As5600 : public BasePeripheral {
class As5600 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = (0b0110110); ///< I2C address of the AS5600

Expand Down
2 changes: 1 addition & 1 deletion components/aw9523/include/aw9523.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace espp {
* \section aw9523_ex1 AW9523 Example
* \snippet aw9523_example.cpp aw9523 example
*/
class Aw9523 : public BasePeripheral {
class Aw9523 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = 0x58; ///< Lower 2 bits are AD1, AD0 pins on the chip.

Expand Down
209 changes: 189 additions & 20 deletions components/base_peripheral/include/base_peripheral.hpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/bm8563/include/bm8563.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace espp {
///
/// \section Example
/// \snippet bm8563_example.cpp bm8563 example
class Bm8563 : public BasePeripheral {
class Bm8563 : public BasePeripheral<> {
public:
/// @brief The default I2C address for the BM8563.
static constexpr uint8_t DEFAULT_ADDRESS = (0x51);
Expand Down
2 changes: 1 addition & 1 deletion components/drv2605/include/drv2605.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace espp {
* \section drv2605_ex1 DRV2605 Example
* \snippet drv2605_example.cpp drv2605 example
*/
class Drv2605 : public BasePeripheral {
class Drv2605 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = (0x5A);

Expand Down
2 changes: 1 addition & 1 deletion components/ft5x06/include/ft5x06.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace espp {
///
/// \section Example
/// \snippet ft5x06_example.cpp ft5x06 example
class Ft5x06 : public BasePeripheral {
class Ft5x06 : public BasePeripheral<> {
public:
/// @brief The default I2C address for the FT5x06.
static constexpr uint8_t DEFAULT_ADDRESS = (0x38);
Expand Down
2 changes: 1 addition & 1 deletion components/gt911/example/main/gt911_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" void app_main(void) {
// task is being stopped / destroyed
{
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, 100ms);
cv.wait_for(lk, 50ms);
}
return false; // don't stop the task
};
Expand Down
26 changes: 5 additions & 21 deletions components/gt911/include/gt911.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace espp {
///
/// \section Example
/// \snippet gt911_example.cpp gt911 example
class Gt911 : public BasePeripheral {
class Gt911 : public BasePeripheral<std::uint16_t> {
public:
/// Default address for the GT911 chip
static constexpr uint8_t DEFAULT_ADDRESS_1 = 0x5D;
Expand Down Expand Up @@ -38,14 +38,15 @@ class Gt911 : public BasePeripheral {
bool update(std::error_code &ec) {
static constexpr size_t DATA_LEN = CONTACT_SIZE * MAX_CONTACTS;
static uint8_t data[DATA_LEN];
read_register(Registers::POINT_INFO, data, 1, ec);
read_many_from_register((uint16_t)Registers::POINT_INFO, data, 1, ec);
if (ec) {
return false;
}
num_touch_points_ = data[0] & 0x0f;
logger_.debug("Got {} touch points", num_touch_points_);
if (num_touch_points_ > 0) {
read_register(Registers::POINTS, data, CONTACT_SIZE * num_touch_points_, ec);
read_many_from_register((uint16_t)Registers::POINTS, data, CONTACT_SIZE * num_touch_points_,
ec);
if (ec) {
return false;
}
Expand All @@ -55,7 +56,7 @@ class Gt911 : public BasePeripheral {
y_ = point->y;
logger_.debug("Touch at ({}, {})", x_, y_);
}
write_register(Registers::POINT_INFO, 0x00, ec); // sync signal
write_u8_to_register((uint16_t)Registers::POINT_INFO, 0x00, ec); // sync signal
if (ec) {
return false;
}
Expand Down Expand Up @@ -227,23 +228,6 @@ class Gt911 : public BasePeripheral {
GTKeyConfig keys;
} __attribute__((packed));

// we use write_then_read since we have to write the u16 register address and
// then read the data
void read_register(Registers reg, uint8_t *data, size_t len, std::error_code &ec) {
uint16_t reg16 = static_cast<uint16_t>(reg);
uint8_t reg_buf[2] = {static_cast<uint8_t>(reg16 >> 8), static_cast<uint8_t>(reg16 & 0xff)};
write_then_read(reg_buf, 2, data, len, ec);
}

void write_register(Registers reg, const uint8_t value, std::error_code &ec) {
uint16_t reg16 = static_cast<uint16_t>(reg);
uint8_t write_buf[3];
write_buf[0] = static_cast<uint8_t>(reg16 >> 8);
write_buf[1] = static_cast<uint8_t>(reg16 & 0xff);
write_buf[2] = value;
write_many(write_buf, 3, ec);
}

std::atomic<bool> home_button_pressed_{false};
std::atomic<uint8_t> num_touch_points_;
std::atomic<uint16_t> x_;
Expand Down
32 changes: 31 additions & 1 deletion components/i2c/include/i2c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class I2c : public espp::BaseComponent {

/// Construct I2C driver
/// \param config Configuration for I2C
explicit I2c(const Config &config) : BaseComponent("I2C", config.log_level), config_(config) {
explicit I2c(const Config &config)
: BaseComponent("I2C", config.log_level)
, config_(config) {
if (config.auto_init) {
std::error_code ec;
init(ec);
Expand All @@ -60,6 +62,8 @@ class I2c : public espp::BaseComponent {
return;
}

logger_.debug("Initializing I2C with config: {}", config_);

std::lock_guard<std::mutex> lock(mutex_);
i2c_config_t i2c_cfg;
memset(&i2c_cfg, 0, sizeof(i2c_cfg));
Expand Down Expand Up @@ -275,3 +279,29 @@ class I2c : public espp::BaseComponent {
std::mutex mutex_;
};
} // namespace espp

// for printing the I2C::Config using fmt
template <> struct fmt::formatter<espp::I2c::Config> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext> auto format(const espp::I2c::Config &c, FormatContext &ctx) {
// print the clock speed in khz
auto clk_speed_khz = c.clk_speed / 1000;
// if it's MHz, print it as such
if (clk_speed_khz >= 1000) {
clk_speed_khz = c.clk_speed / 1000000;
return fmt::format_to(
ctx.out(),
"I2c::Config{{I2C port: {}, SDA: {}, SCL: {}, SDA pullup: {}, SCL pullup: {}, "
"timeout: {}ms, clock speed: {}MHz}}",
c.port, c.sda_io_num, c.scl_io_num, c.sda_pullup_en, c.scl_pullup_en, c.timeout_ms,
clk_speed_khz);
}
return fmt::format_to(
ctx.out(),
"I2c::Config{{I2C port: {}, SDA: {}, SCL: {}, SDA pullup: {}, SCL pullup: {}, "
"timeout: {}ms, clock speed: {}kHz}}",
c.port, c.sda_io_num, c.scl_io_num, c.sda_pullup_en, c.scl_pullup_en, c.timeout_ms,
clk_speed_khz);
}
};
25 changes: 25 additions & 0 deletions components/i2c/include/i2c_format_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,28 @@ template <> struct fmt::formatter<i2c_port_t> {
}
}
};

// for printing gpio_num_t with libfmt
template <> struct fmt::formatter<gpio_num_t> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext> auto format(const gpio_num_t &g, FormatContext &ctx) {
return fmt::format_to(ctx.out(), "{:d}", (int)g);
}
};

// for printing gpio_pullup_t with libfmt
template <> struct fmt::formatter<gpio_pullup_t> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext> auto format(const gpio_pullup_t &p, FormatContext &ctx) {
switch (p) {
case GPIO_PULLUP_DISABLE:
return fmt::format_to(ctx.out(), "GPIO_PULLUP_DISABLE");
case GPIO_PULLUP_ENABLE:
return fmt::format_to(ctx.out(), "GPIO_PULLUP_ENABLE");
default:
return fmt::format_to(ctx.out(), "Unknown");
}
}
};
2 changes: 1 addition & 1 deletion components/max1704x/include/max1704x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace espp {
* @section max1704x_ex1 MAX1704X Example
* @snippet max1704x_example.cpp max1704x example
*/
class Max1704x : public BasePeripheral {
class Max1704x : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = 0x36; ///< Default address of the MAX1704x.

Expand Down
2 changes: 1 addition & 1 deletion components/mcp23x17/include/mcp23x17.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace espp {
* \section mcp23x17_ex1 MCP23x17 Example
* \snippet mcp23x17_example.cpp mcp23x17 example
*/
class Mcp23x17 : public BasePeripheral {
class Mcp23x17 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS =
0b0100000; ///< Lower 3 bits are A2, A2, A0 pins on the chip.
Expand Down
2 changes: 1 addition & 1 deletion components/mt6701/include/mt6701.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace espp {
* \section mt6701_ex1 Mt6701 Example
* \snippet mt6701_example.cpp mt6701 example
*/
class Mt6701 : public BasePeripheral {
class Mt6701 : public BasePeripheral<> {
public:
static constexpr uint8_t DEFAULT_ADDRESS = (0b0000110); ///< I2C address of the MT6701

Expand Down
2 changes: 1 addition & 1 deletion components/qwiicnes/include/qwiicnes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace espp {
///
/// \section Example
/// \snippet qwiicnes_example.cpp qwiicnes example
class QwiicNes : public BasePeripheral {
class QwiicNes : public BasePeripheral<> {
public:
/// @brief The default I2C address of the device.
static constexpr uint8_t DEFAULT_ADDRESS = (0x54);
Expand Down
5 changes: 3 additions & 2 deletions components/st25dv/example/main/st25dv_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ extern "C" void app_main(void) {
.port = I2C_NUM_0,
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
.clk_speed = 1000 * 1000,
});

// now make the st25dv which decodes the data
espp::St25dv st25dv({.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &i2c, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.log_level = espp::Logger::Verbosity::DEBUG});
.log_level = espp::Logger::Verbosity::INFO});

std::array<uint8_t, 50> programmed_data;
std::error_code ec;
Expand Down Expand Up @@ -145,7 +146,7 @@ extern "C" void app_main(void) {
}
static auto last_it_sts = it_sts;
if (it_sts != last_it_sts) {
fmt::print("[{:.3f}] IT STS: {:02x}\n", elapsed(), it_sts);
fmt::print("[{:.3f}] IT STS: {}\n", elapsed(), it_sts);
}
last_it_sts = it_sts;
// we don't want to stop the task, so return false
Expand Down
Loading

0 comments on commit b4e301f

Please sign in to comment.