Skip to content
Open
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
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ jobs:
target: esp32s3
- path: 'components/st25dv/example'
target: esp32s3
- path: 'components/st7123touch/example'
target: esp32s3
- path: 'components/state_machine/example'
target: esp32
- path: 'components/t-deck/example'
Expand All @@ -205,6 +207,8 @@ jobs:
target: esp32
- path: 'components/timer/example'
target: esp32
- path: 'components/touch/example'
target: esp32
- path: 'components/tla2528/example'
target: esp32
- path: 'components/tt21100/example'
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/upload_components.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ jobs:
components/socket
components/spi
components/st25dv
components/st7123touch
components/state_machine
components/t_keyboard
components/t-deck
Expand All @@ -123,6 +124,7 @@ jobs:
components/task
components/thermistor
components/timer
components/touch
components/tla2528
components/tt21100
components/utils
Expand Down
2 changes: 1 addition & 1 deletion components/chsc6x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES "base_peripheral"
REQUIRES "base_peripheral" "touch"
)
1 change: 1 addition & 0 deletions components/chsc6x/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ dependencies:
idf:
version: '>=5.0'
espp/base_peripheral: '>=1.0'
espp/touch: '>=1.0'
42 changes: 24 additions & 18 deletions components/chsc6x/include/chsc6x.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#pragma once

#include <atomic>
#include <functional>

#include "base_peripheral.hpp"
#include "touch.hpp"

namespace espp {
/// @brief Driver for the Chsc6x touch controller
///
/// \section chsc6x_ex1 Example
/// \snippet chsc6x_example.cpp chsc6x example
class Chsc6x : public BasePeripheral<> {
class Chsc6x : public BasePeripheral<>, public ITouchDevice {
public:
/// Default address for the CHSC6X chip
static constexpr uint8_t DEFAULT_ADDRESS = 0x2E;
Expand All @@ -34,6 +34,7 @@ class Chsc6x : public BasePeripheral<> {
/// @param ec Error code to set if an error occurs
/// @return True if the CHSC6X has new data, false otherwise
bool update(std::error_code &ec) {
TouchState state{};
static constexpr size_t DATA_LEN = 5;
static uint8_t data[DATA_LEN];
read_many_from_register(0, data, DATA_LEN, ec);
Expand All @@ -42,39 +43,44 @@ class Chsc6x : public BasePeripheral<> {

// first byte is non-zero when touched, 3rd byte is x, 5th byte is y
if (data[0] == 0) {
x_ = 0;
y_ = 0;
num_touch_points_ = 0;
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
touch_state_ = state;
return true;
}
x_ = data[2];
y_ = data[4];
num_touch_points_ = 1;
logger_.debug("Touch at ({}, {})", x_, y_);
state.num_touch_points = 1;
state.points[0] = {.x = data[2], .y = data[4]};
logger_.debug("Touch at ({}, {})", state.points[0].x, state.points[0].y);
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
touch_state_ = state;
return true;
}

/// @brief Get the cached touch state.
/// @return The cached touch state as of the last update() call.
TouchState touch_state() const override {
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
return touch_state_;
}

/// @brief Get the number of touch points
/// @return The number of touch points as of the last update
/// @note This is a cached value from the last update() call
uint8_t get_num_touch_points() const { return num_touch_points_; }
uint8_t get_num_touch_points() const { return touch_state().num_touch_points; }

/// @brief Get the touch point data
/// @param num_touch_points The number of touch points as of the last update
/// @param x The x coordinate of the touch point
/// @param y The y coordinate of the touch point
/// @note This is a cached value from the last update() call
void get_touch_point(uint8_t *num_touch_points, uint16_t *x, uint16_t *y) const {
*num_touch_points = get_num_touch_points();
if (*num_touch_points != 0) {
*x = x_;
*y = y_;
}
auto state = touch_state();
auto point = state.primary_point();
*num_touch_points = state.num_touch_points;
*x = point.x;
*y = point.y;
}

protected:
std::atomic<uint8_t> num_touch_points_;
std::atomic<uint16_t> x_;
std::atomic<uint16_t> y_;
TouchState touch_state_;
}; // class Chsc6x
} // namespace espp
2 changes: 1 addition & 1 deletion components/cst816/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES "base_peripheral"
REQUIRES "base_peripheral" "touch"
)
1 change: 1 addition & 0 deletions components/cst816/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ dependencies:
idf:
version: '>=5.0'
espp/base_peripheral: '>=1.0'
espp/touch: '>=1.0'
40 changes: 23 additions & 17 deletions components/cst816/include/cst816.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <atomic>
#include <functional>

#include "base_peripheral.hpp"
#include "touch.hpp"

namespace espp {
/// @brief Driver for the CST816 touch controller
Expand All @@ -18,7 +18,7 @@ namespace espp {
///
/// \section cst816_ex1 Example
/// \snippet cst816_example.cpp cst816 example
class Cst816 : public BasePeripheral<std::uint8_t> {
class Cst816 : public BasePeripheral<std::uint8_t>, public ITouchDevice {
public:
/// Default address for the CST816 chip
static constexpr uint8_t DEFAULT_ADDRESS = 0x15;
Expand All @@ -42,43 +42,52 @@ class Cst816 : public BasePeripheral<std::uint8_t> {
/// @param ec Error code to set if an error occurs
/// @return True if the CST816 has new data, false otherwise
bool update(std::error_code &ec) {
TouchState state{};
bool new_data = false;
Data data{};
read_many_from_register(static_cast<uint8_t>(Registers::DATA_START),
reinterpret_cast<uint8_t *>(&data), sizeof(data), ec);
if (ec)
return false;

num_touch_points_ = data.num;
x_ = (data.x_h << 8) | data.x_l;
y_ = (data.y_h << 8) | data.y_l;
home_button_pressed_ = false;
state.num_touch_points = data.num;
state.points[0] = {.x = static_cast<uint16_t>((data.x_h << 8) | data.x_l),
.y = static_cast<uint16_t>((data.y_h << 8) | data.y_l)};
new_data = true;
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
touch_state_ = state;
return new_data;
}

/// @brief Get the cached touch state.
/// @return The cached touch state as of the last update() call.
TouchState touch_state() const override {
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
return touch_state_;
}

/// @brief Get the number of touch points
/// @return The number of touch points as of the last update
/// @note This is a cached value from the last update() call
uint8_t get_num_touch_points() const { return num_touch_points_; }
uint8_t get_num_touch_points() const { return touch_state().num_touch_points; }

/// @brief Get the touch point data
/// @param num_touch_points The number of touch points as of the last update
/// @param x The x coordinate of the touch point
/// @param y The y coordinate of the touch point
/// @note This is a cached value from the last update() call
void get_touch_point(uint8_t *num_touch_points, uint16_t *x, uint16_t *y) const {
*num_touch_points = get_num_touch_points();
if (*num_touch_points != 0) {
*x = x_;
*y = y_;
}
auto state = touch_state();
auto point = state.primary_point();
*num_touch_points = state.num_touch_points;
*x = point.x;
*y = point.y;
}

/// @brief Get the home button state
/// @return True if the home button is pressed, false otherwise
/// @note This is a cached value from the last update() call
bool get_home_button_state() const { return home_button_pressed_; }
bool get_home_button_state() const { return touch_state().btn_state; }

protected:
static constexpr int MAX_CONTACTS = 1;
Expand All @@ -98,9 +107,6 @@ class Cst816 : public BasePeripheral<std::uint8_t> {
CHIP_ID = 0xA7,
};

std::atomic<bool> home_button_pressed_{false};
std::atomic<uint8_t> num_touch_points_;
std::atomic<uint16_t> x_;
std::atomic<uint16_t> y_;
TouchState touch_state_;
};
} // namespace espp
2 changes: 1 addition & 1 deletion components/esp-box/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
idf_component_register(
INCLUDE_DIRS "include"
SRC_DIRS "src"
REQUIRES driver esp_driver_i2s base_component codec display display_drivers i2c input_drivers interrupt gt911 task tt21100 icm42607
REQUIRES driver esp_driver_i2s base_component codec display display_drivers i2c input_drivers interrupt gt911 task touch tt21100 icm42607
REQUIRED_IDF_TARGETS "esp32s3"
)
1 change: 1 addition & 0 deletions components/esp-box/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
espp/interrupt: '>=1.0'
espp/gt911: '>=1.0'
espp/task: '>=1.0'
espp/touch: '>=1.0'
espp/tt21100: '>=1.0'
espp/icm42607: '>=1.0'
targets:
Expand Down
6 changes: 2 additions & 4 deletions components/esp-box/include/esp-box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "icm42607.hpp"
#include "interrupt.hpp"
#include "st7789.hpp"
#include "touch.hpp"
#include "touchpad_input.hpp"
#include "tt21100.hpp"

Expand Down Expand Up @@ -359,8 +360,6 @@ class EspBox : public BaseComponent {
bool initialize_codec();
bool initialize_i2s(uint32_t default_audio_rate);
bool update_touch();
bool update_gt911();
bool update_tt21100();
void update_volume_output();
bool audio_task_callback(std::mutex &m, std::condition_variable &cv, bool &task_notified);
void lcd_wait_lines();
Expand Down Expand Up @@ -509,8 +508,7 @@ class EspBox : public BaseComponent {
button_callback_t mute_button_callback_{nullptr};

// touch
std::shared_ptr<Gt911> gt911_; // only used on ESP32-S3-BOX-3
std::shared_ptr<Tt21100> tt21100_; // only used on ESP32-S3-BOX
std::shared_ptr<ITouchDevice> touch_driver_;
std::shared_ptr<TouchpadInput> touchpad_input_;
std::recursive_mutex touchpad_data_mutex_;
TouchpadData touchpad_data_;
Expand Down
61 changes: 8 additions & 53 deletions components/esp-box/src/touchpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ using namespace espp;
////////////////////////

bool EspBox::initialize_touch(const EspBox::touch_callback_t &callback) {
if (gt911_ || tt21100_) {
if (touch_driver_) {
logger_.warn("Touch already initialized, not initializing again!");
return false;
}

switch (box_type_) {
case BoxType::BOX3:
logger_.info("Initializing GT911");
gt911_ = std::make_unique<espp::Gt911>(espp::Gt911::Config{
touch_driver_ = std::make_shared<espp::Gt911>(espp::Gt911::Config{
.write = std::bind(&espp::I2c::write, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &internal_i2c_, std::placeholders::_1,
Expand All @@ -24,7 +24,7 @@ bool EspBox::initialize_touch(const EspBox::touch_callback_t &callback) {
break;
case BoxType::BOX:
logger_.info("Initializing TT21100");
tt21100_ = std::make_unique<espp::Tt21100>(espp::Tt21100::Config{
touch_driver_ = std::make_shared<espp::Tt21100>(espp::Tt21100::Config{
.write = std::bind(&espp::I2c::write, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &internal_i2c_, std::placeholders::_1,
Expand All @@ -44,71 +44,26 @@ bool EspBox::initialize_touch(const EspBox::touch_callback_t &callback) {
return true;
}

bool EspBox::update_gt911() {
// ensure the gt911 is initialized
if (!gt911_) {
return false;
}
// get the latest data from the device
std::error_code ec;
bool new_data = gt911_->update(ec);
if (ec) {
logger_.error("could not update gt911: {}\n", ec.message());
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = {};
return false;
}
if (!new_data) {
return false;
}
// get the latest data from the touchpad
TouchpadData temp_data;
gt911_->get_touch_point(&temp_data.num_touch_points, &temp_data.x, &temp_data.y);
temp_data.btn_state = gt911_->get_home_button_state();
// update the touchpad data
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = temp_data;
return true;
}

bool EspBox::update_tt21100() {
// ensure the tt21100 is initialized
if (!tt21100_) {
bool EspBox::update_touch() {
if (!touch_driver_) {
return false;
}
// get the latest data from the device
std::error_code ec;
bool new_data = tt21100_->update(ec);
bool new_data = touch_driver_->update(ec);
if (ec) {
logger_.error("could not update tt21100: {}\n", ec.message());
logger_.error("could not update touch driver: {}\n", ec.message());
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = {};
return false;
}
if (!new_data) {
return false;
}
// get the latest data from the touchpad
TouchpadData temp_data;
tt21100_->get_touch_point(&temp_data.num_touch_points, &temp_data.x, &temp_data.y);
temp_data.btn_state = tt21100_->get_home_button_state();
// update the touchpad data
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = temp_data;
touchpad_data_ = touch_driver_->touchpad_data();
return true;
}

bool EspBox::update_touch() {
switch (box_type_) {
case BoxType::BOX3:
return update_gt911();
case BoxType::BOX:
return update_tt21100();
default:
return false;
}
}

void EspBox::touchpad_read(uint8_t *num_touch_points, uint16_t *x, uint16_t *y,
uint8_t *btn_state) {
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
Expand Down
2 changes: 1 addition & 1 deletion components/ft5x06/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES "base_peripheral"
REQUIRES "base_peripheral" "touch"
)
Loading
Loading