Skip to content

Commit 433fc52

Browse files
committed
feat: update peripherals
* Updated espp submodule to have latest version which contains new I2C class and updated versions of the touch peripherals * Updated box-emu-hal component to simplify the i2c code to use new espp::I2c class * Updated associated input / main code using i2c peripherals to use new i2c classes and to simplify it.
1 parent b0d2034 commit 433fc52

File tree

19 files changed

+57
-758
lines changed

19 files changed

+57
-758
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(
22
INCLUDE_DIRS "include"
33
SRC_DIRS "src"
4-
REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "spi_flash" "nvs_flash" "codec" "display" "display_drivers" "controller" "mcp23x17" "ads1x15" "qwiicnes" "input_drivers" "ft5x06" "tt21100" "drv2605" "event_manager"
4+
REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "spi_flash" "nvs_flash" "codec" "display" "display_drivers" "mcp23x17" "input_drivers" "tt21100" "drv2605" "event_manager" "i2c"
55
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
#include "i2c.hpp"
6+
7+
extern std::shared_ptr<espp::I2c> internal_i2c;
8+
extern std::shared_ptr<espp::I2c> external_i2c;
9+
10+
void i2c_init();

components/box-emu-hal/include/i2c.hpp

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "hal_i2c.hpp"
2+
3+
// Only used for the touchpad and the imu
4+
static constexpr i2c_port_t I2C_INTERNAL = I2C_NUM_0;
5+
// used for our peripherals (external to the ESP S3 BOX)
6+
static constexpr i2c_port_t I2C_EXTERNAL = I2C_NUM_1;
7+
static constexpr int I2C_FREQ_HZ = (400*1000);
8+
static constexpr int I2C_TIMEOUT_MS = 10;
9+
10+
std::shared_ptr<espp::I2c> internal_i2c = nullptr;
11+
std::shared_ptr<espp::I2c> external_i2c = nullptr;
12+
13+
static bool initialized = false;
14+
15+
void i2c_init() {
16+
if (initialized) return;
17+
internal_i2c = std::make_shared<espp::I2c>(espp::I2c::Config{
18+
.port = I2C_INTERNAL,
19+
.sda_io_num = GPIO_NUM_8,
20+
.scl_io_num = GPIO_NUM_18,
21+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
22+
.scl_pullup_en = GPIO_PULLUP_ENABLE});
23+
external_i2c = std::make_shared<espp::I2c>(espp::I2c::Config{
24+
.port = I2C_EXTERNAL,
25+
.sda_io_num = GPIO_NUM_41,
26+
.scl_io_num = GPIO_NUM_40,
27+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
28+
.scl_pullup_en = GPIO_PULLUP_ENABLE});
29+
initialized = true;
30+
}

components/box-emu-hal/src/i2c.cpp

Lines changed: 0 additions & 103 deletions
This file was deleted.

components/box-emu-hal/src/input.cpp

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,46 @@
11
#include "input.h"
22

3-
#include "i2c.hpp"
3+
#include "hal_i2c.hpp"
44

55
#include "task.hpp"
66

77
#include "mcp23x17.hpp"
88

99
#include "touchpad_input.hpp"
10-
#define USE_FT5X06 0
11-
#if USE_FT5X06
12-
#include "ft5x06.hpp"
13-
#else
1410
#include "tt21100.hpp"
15-
#endif
1611

1712
using namespace std::chrono_literals;
1813

1914
static std::shared_ptr<espp::Mcp23x17> mcp23x17;
20-
21-
#if USE_FT5X06
22-
static std::shared_ptr<Ft5x06> ft5x06;
23-
#else
24-
static std::shared_ptr<Tt21100> tt21100;
25-
#endif
15+
static std::shared_ptr<espp::Tt21100> tt21100;
2616
static std::shared_ptr<espp::TouchpadInput> touchpad;
2717

2818
/**
2919
* Touch Controller configuration
3020
*/
31-
#if USE_FT5X06
32-
void ft5x06_write(uint8_t reg_addr, uint8_t value) {
33-
uint8_t write_buf[] = {reg_addr, value};
34-
i2c_write_internal_bus(Ft5x06::ADDRESS, write_buf, 2);
35-
}
36-
37-
void ft5x06_read(uint8_t reg_addr, uint8_t *read_data, size_t read_len) {
38-
i2c_read_internal_bus(Ft5x06::ADDRESS, reg_addr, read_data, read_len);
39-
}
40-
#else
41-
void tt21100_write(uint8_t reg_addr, uint8_t value) {
42-
uint8_t write_buf[] = {reg_addr, value};
43-
i2c_write_internal_bus(Tt21100::ADDRESS, write_buf, 2);
44-
}
45-
46-
void tt21100_read(uint8_t *read_data, size_t read_len) {
47-
i2c_read_internal_bus(Tt21100::ADDRESS, read_data, read_len);
48-
}
49-
#endif
50-
5121
void touchpad_read(uint8_t* num_touch_points, uint16_t* x, uint16_t* y, uint8_t* btn_state) {
52-
// NOTE: ft5x06 does not have button support, so data->btn_val cannot be set
53-
#if USE_FT5X06
54-
ft5x06->get_touch_point(num_touch_points, x, y);
55-
// TODO: how to handle quit condition if FT5x06?
56-
#else
5722
// get the latest data from the device
58-
while (!tt21100->read())
59-
;
23+
std::error_code ec;
24+
tt21100->update(ec);
25+
if (ec) {
26+
fmt::print("error updating tt21100: {}\n", ec.message());
27+
return;
28+
}
6029
// now hand it off
6130
tt21100->get_touch_point(num_touch_points, x, y);
6231
*btn_state = tt21100->get_home_button_state();
63-
#endif
6432
}
6533

6634
static std::atomic<bool> initialized = false;
6735
void init_input() {
6836
if (initialized) return;
6937
fmt::print("Initializing input drivers...\n");
7038

71-
#if USE_FT5X06
72-
fmt::print("Initializing ft5x06\n");
73-
ft5x06 = std::make_shared<Ft5x06>(Ft5x06::Config{
74-
.write = ft5x06_write,
75-
.read = ft5x06_read,
76-
.log_level = espp::Logger::Verbosity::WARN
77-
});
78-
#else
7939
fmt::print("Initializing Tt21100\n");
80-
tt21100 = std::make_shared<Tt21100>(Tt21100::Config{
81-
.read = tt21100_read,
40+
tt21100 = std::make_shared<espp::Tt21100>(espp::Tt21100::Config{
41+
.read = std::bind(&espp::I2c::read, internal_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3),
8242
.log_level = espp::Logger::Verbosity::WARN
8343
});
84-
#endif
8544

8645
fmt::print("Initializing touchpad\n");
8746
touchpad = std::make_shared<espp::TouchpadInput>(espp::TouchpadInput::Config{
@@ -98,8 +57,8 @@ void init_input() {
9857
.port_a_interrupt_mask = 0x00,
9958
.port_b_direction_mask = 0xFF, // D-pad B0-B3, ABXY B4-B7
10059
.port_b_interrupt_mask = 0x00,
101-
.write = i2c_write_external_bus,
102-
.read = i2c_read_external_bus,
60+
.write = std::bind(&espp::I2c::write, external_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3),
61+
.read = std::bind(&espp::I2c::read_at_register, external_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
10362
.log_level = espp::Logger::Verbosity::WARN
10463
});
10564

components/espp

Submodule espp updated 132 files

components/ft5x06/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)