-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathchsc6x_example.cpp
More file actions
86 lines (78 loc) · 2.85 KB
/
Copy pathchsc6x_example.cpp
File metadata and controls
86 lines (78 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <chrono>
#include <sdkconfig.h>
#include <vector>
#include "chsc6x.hpp"
#include "i2c.hpp"
#include "task.hpp"
using namespace std::chrono_literals;
extern "C" void app_main(void) {
{
fmt::print("Starting chsc6x example\n");
//! [chsc6x example]
// make the I2C that we'll use to communicate
espp::I2c i2c({
.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,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.timeout_ms = 100,
.clk_speed = 400 * 1000,
});
bool has_chsc6x = i2c.probe_device(espp::Chsc6x::DEFAULT_ADDRESS);
fmt::print("Touchpad probe: {}\n", has_chsc6x);
std::error_code ec;
auto chsc6x_device =
i2c.add_device<uint8_t>({.device_address = espp::Chsc6x::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!chsc6x_device) {
fmt::print("CHSC6X I2C device initialization failed: {}\n", ec.message());
return;
}
// now make the chsc6x which decodes the data
espp::Chsc6x chsc6x({.write = espp::make_i2c_addressed_write(chsc6x_device),
.read = espp::make_i2c_addressed_read(chsc6x_device),
.log_level = espp::Logger::Verbosity::WARN});
// and finally, make the task to periodically poll the chsc6x and print
// the state
auto task_fn = [&chsc6x](std::mutex &m, std::condition_variable &cv) {
std::error_code ec;
// update the state
bool new_data = chsc6x.update(ec);
if (ec) {
fmt::print("Could not update state\n");
return false;
}
if (!new_data) {
return false; // don't stop the task
}
// get the state
uint8_t num_touch_points = 0;
uint16_t x = 0, y = 0;
chsc6x.get_touch_point(&num_touch_points, &x, &y);
fmt::print("num_touch_points: {}, x: {}, y: {}\n", num_touch_points, x, y);
// NOTE: sleeping in this way allows the sleep to exit early when the
// task is being stopped / destroyed
{
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, 50ms);
}
return false; // don't stop the task
};
auto task = espp::Task({.callback = task_fn,
.task_config = {.name = "Chsc6x Task"},
.log_level = espp::Logger::Verbosity::WARN});
task.start();
//! [chsc6x example]
while (true) {
std::this_thread::sleep_for(100ms);
}
}
fmt::print("Chsc6x example complete!\n");
while (true) {
std::this_thread::sleep_for(1s);
}
}