From 78ec09c494faadf9e7d06dc7d3e04531c3a34ff7 Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 13 Mar 2023 15:14:32 +1100 Subject: Touchwheel test --- src/drivers/touchwheel.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/drivers/touchwheel.cpp (limited to 'src/drivers/touchwheel.cpp') diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp new file mode 100644 index 00000000..11a115fb --- /dev/null +++ b/src/drivers/touchwheel.cpp @@ -0,0 +1,92 @@ +#include "touchwheel.hpp" + +#include + +#include "assert.h" +#include "driver/i2c.h" +#include "esp_err.h" +#include "esp_log.h" +#include "hal/i2c_types.h" + +#include "i2c.hpp" + +namespace drivers { + +static const char* kTag = "TOUCHWHEEL"; +static const uint8_t kTouchWheelAddress = 0x1C; + +static const uint8_t kWriteMask = 0x80; +static const uint8_t kReadMask = 0xA0; + +double normalise(uint16_t min, uint16_t max, uint16_t value) { + if (value >= max) { + return 1.0; + } + if (value <= min) { + return 0.0; + } + uint16_t range = max - min; + return (double)(value - min) / range; +} + + +auto TouchWheel::create(GpioExpander* expander) + -> cpp::result, Error> { + std::unique_ptr wheel = std::make_unique(expander); + wheel->WriteRegister(Register::SLIDER_OPTIONS, 0xC0); + return wheel; +} + +TouchWheel::TouchWheel(GpioExpander* gpio) { + this->gpio_ = gpio; +}; + +TouchWheel::~TouchWheel(){ +}; + +void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { + // uint8_t maskedReg = reg | kWriteMask; + uint8_t maskedReg = reg; + I2CTransaction transaction; + transaction.start() + .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) + .write_ack(maskedReg, val) + .stop(); + ESP_ERROR_CHECK(transaction.Execute()); +} + +void TouchWheel::ReadRegister(uint8_t reg, uint8_t* data, uint8_t count) { + // uint8_t maskedReg = reg | kReadMask; + uint8_t maskedReg = reg; + + if (count <= 0) { + return; + } + + I2CTransaction transaction; + transaction.start() + .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) + .write_ack(maskedReg) + .stop() + .start() + .write_addr(kTouchWheelAddress, I2C_MASTER_READ) + .read(data, I2C_MASTER_NACK) + .stop(); + + // TODO: Handle errors here. + ESP_ERROR_CHECK(transaction.Execute()); +} + +void TouchWheel::Update() { + // Read data from device into member struct + uint8_t position; + this->ReadRegister(Register::SLIDER_POSITION, &position, 1); + data_.wheel_position = position; +} + +TouchWheelData TouchWheel::GetTouchWheelData() const { + return data_; +} + + +} // namespace drivers -- cgit v1.2.3 From 1b245316fe282035f094a3656f717d419e8bd526 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 14:48:35 +1000 Subject: fix up touchpad timeouts, make it less chatty --- src/drivers/touchwheel.cpp | 79 +++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 43 deletions(-) (limited to 'src/drivers/touchwheel.cpp') diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index 11a115fb..9e0d99af 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -1,11 +1,15 @@ #include "touchwheel.hpp" +#include #include #include "assert.h" +#include "driver/gpio.h" #include "driver/i2c.h" #include "esp_err.h" #include "esp_log.h" +#include "freertos/projdefs.h" +#include "hal/gpio_types.h" #include "hal/i2c_types.h" #include "i2c.hpp" @@ -15,34 +19,18 @@ namespace drivers { static const char* kTag = "TOUCHWHEEL"; static const uint8_t kTouchWheelAddress = 0x1C; -static const uint8_t kWriteMask = 0x80; -static const uint8_t kReadMask = 0xA0; +TouchWheel::TouchWheel() { + gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT); + gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLUP_ONLY); -double normalise(uint16_t min, uint16_t max, uint16_t value) { - if (value >= max) { - return 1.0; - } - if (value <= min) { - return 0.0; - } - uint16_t range = max - min; - return (double)(value - min) / range; -} - - -auto TouchWheel::create(GpioExpander* expander) - -> cpp::result, Error> { - std::unique_ptr wheel = std::make_unique(expander); - wheel->WriteRegister(Register::SLIDER_OPTIONS, 0xC0); - return wheel; + WriteRegister(Register::RESET, 1); + // TODO(daniel): do we need this? how long does reset take? + vTaskDelay(pdMS_TO_TICKS(1)); + WriteRegister(Register::SLIDER_OPTIONS, 0b11000000); + WriteRegister(Register::CALIBRATE, 1); } -TouchWheel::TouchWheel(GpioExpander* gpio) { - this->gpio_ = gpio; -}; - -TouchWheel::~TouchWheel(){ -}; +TouchWheel::~TouchWheel() {} void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { // uint8_t maskedReg = reg | kWriteMask; @@ -55,38 +43,43 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { ESP_ERROR_CHECK(transaction.Execute()); } -void TouchWheel::ReadRegister(uint8_t reg, uint8_t* data, uint8_t count) { - // uint8_t maskedReg = reg | kReadMask; - uint8_t maskedReg = reg; - - if (count <= 0) { - return; - } - +uint8_t TouchWheel::ReadRegister(uint8_t reg) { + uint8_t res; I2CTransaction transaction; transaction.start() .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) - .write_ack(maskedReg) - .stop() + .write_ack(reg) .start() .write_addr(kTouchWheelAddress, I2C_MASTER_READ) - .read(data, I2C_MASTER_NACK) + .read(&res, I2C_MASTER_NACK) .stop(); - - // TODO: Handle errors here. ESP_ERROR_CHECK(transaction.Execute()); + return res; } void TouchWheel::Update() { - // Read data from device into member struct - uint8_t position; - this->ReadRegister(Register::SLIDER_POSITION, &position, 1); - data_.wheel_position = position; + // Read data from device into member struct + bool has_data = !gpio_get_level(GPIO_NUM_25); + if (!has_data) { + return; + } + uint8_t status = ReadRegister(Register::DETECTION_STATUS); + if (status & 0b10000000) { + // Still calibrating. + return; + } + if (status & 0b10) { + // Slider detect. + data_.wheel_position = ReadRegister(Register::SLIDER_POSITION); + } + if (status & 0b1) { + // Key detect. + // TODO(daniel): implement me + } } TouchWheelData TouchWheel::GetTouchWheelData() const { return data_; } - } // namespace drivers -- cgit v1.2.3 From a1cef17c5bb66c453e4a8e83e52a56fe9173346c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 12 Apr 2023 16:37:27 +1000 Subject: Leave the display reset pin alone; we don't need it --- src/drivers/touchwheel.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/drivers/touchwheel.cpp') diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index 9e0d99af..d5382b3d 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -18,10 +18,17 @@ namespace drivers { static const char* kTag = "TOUCHWHEEL"; static const uint8_t kTouchWheelAddress = 0x1C; +static const gpio_num_t kIntPin = GPIO_NUM_25; TouchWheel::TouchWheel() { - gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT); - gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLUP_ONLY); + gpio_config_t int_config{ + .pin_bit_mask = 1ULL << kIntPin, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&int_config); WriteRegister(Register::RESET, 1); // TODO(daniel): do we need this? how long does reset take? -- cgit v1.2.3 From 2a46eecdc6334c31cee2b40427d2536b48cbb6be Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 10:27:33 +1000 Subject: Temporarily allow the touchwheel to be missing --- src/drivers/touchwheel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/drivers/touchwheel.cpp') diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index d5382b3d..51a67187 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -47,7 +47,10 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) .write_ack(maskedReg, val) .stop(); - ESP_ERROR_CHECK(transaction.Execute()); + transaction.Execute(); + // TODO(jacqueline): check for errors again when i find where all the ffc + // cables went q.q + // ESP_ERROR_CHECK(transaction.Execute()); } uint8_t TouchWheel::ReadRegister(uint8_t reg) { -- cgit v1.2.3