fix up touchpad timeouts, make it less chatty

custom
jacqueline 2 years ago
parent 9799ab458d
commit 1b245316fe
  1. 1
      src/drivers/i2c.cpp
  2. 17
      src/drivers/include/touchwheel.hpp
  3. 77
      src/drivers/touchwheel.cpp
  4. 21
      src/main/main.cpp

@ -40,7 +40,6 @@ esp_err_t init_i2c(void) {
return err; return err;
} }
// TODO: INT line // TODO: INT line
return ESP_OK; return ESP_OK;

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <functional>
#include <stdint.h> #include <stdint.h>
#include <functional>
#include "esp_err.h" #include "esp_err.h"
#include "result.hpp" #include "result.hpp"
@ -18,14 +17,7 @@ struct TouchWheelData {
class TouchWheel { class TouchWheel {
public: public:
enum Error { TouchWheel();
FAILED_TO_BOOT,
FAILED_TO_CONFIGURE,
};
static auto create(GpioExpander* expander)
-> cpp::result<std::unique_ptr<TouchWheel>, Error>;
TouchWheel(GpioExpander* gpio);
~TouchWheel(); ~TouchWheel();
// Not copyable or movable. // Not copyable or movable.
@ -36,7 +28,6 @@ class TouchWheel {
auto GetTouchWheelData() const -> TouchWheelData; auto GetTouchWheelData() const -> TouchWheelData;
private: private:
GpioExpander* gpio_;
TouchWheelData data_; TouchWheelData data_;
enum Register { enum Register {
@ -51,10 +42,8 @@ class TouchWheel {
SLIDER_OPTIONS = 0x14, SLIDER_OPTIONS = 0x14,
}; };
void WriteRegister(uint8_t reg, uint8_t val); void WriteRegister(uint8_t reg, uint8_t val);
void ReadRegister(uint8_t reg, uint8_t* data, uint8_t count); uint8_t ReadRegister(uint8_t reg);
}; };
} // namespace drivers } // namespace drivers

@ -1,11 +1,15 @@
#include "touchwheel.hpp" #include "touchwheel.hpp"
#include <stdint.h>
#include <cstdint> #include <cstdint>
#include "assert.h" #include "assert.h"
#include "driver/gpio.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/projdefs.h"
#include "hal/gpio_types.h"
#include "hal/i2c_types.h" #include "hal/i2c_types.h"
#include "i2c.hpp" #include "i2c.hpp"
@ -15,34 +19,18 @@ namespace drivers {
static const char* kTag = "TOUCHWHEEL"; static const char* kTag = "TOUCHWHEEL";
static const uint8_t kTouchWheelAddress = 0x1C; static const uint8_t kTouchWheelAddress = 0x1C;
static const uint8_t kWriteMask = 0x80; TouchWheel::TouchWheel() {
static const uint8_t kReadMask = 0xA0; 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) { WriteRegister(Register::RESET, 1);
if (value >= max) { // TODO(daniel): do we need this? how long does reset take?
return 1.0; vTaskDelay(pdMS_TO_TICKS(1));
} WriteRegister(Register::SLIDER_OPTIONS, 0b11000000);
if (value <= min) { WriteRegister(Register::CALIBRATE, 1);
return 0.0;
}
uint16_t range = max - min;
return (double)(value - min) / range;
}
auto TouchWheel::create(GpioExpander* expander)
-> cpp::result<std::unique_ptr<TouchWheel>, Error> {
std::unique_ptr<TouchWheel> wheel = std::make_unique<TouchWheel>(expander);
wheel->WriteRegister(Register::SLIDER_OPTIONS, 0xC0);
return wheel;
} }
TouchWheel::TouchWheel(GpioExpander* gpio) { TouchWheel::~TouchWheel() {}
this->gpio_ = gpio;
};
TouchWheel::~TouchWheel(){
};
void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) {
// uint8_t maskedReg = reg | kWriteMask; // uint8_t maskedReg = reg | kWriteMask;
@ -55,38 +43,43 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) {
ESP_ERROR_CHECK(transaction.Execute()); ESP_ERROR_CHECK(transaction.Execute());
} }
void TouchWheel::ReadRegister(uint8_t reg, uint8_t* data, uint8_t count) { uint8_t TouchWheel::ReadRegister(uint8_t reg) {
// uint8_t maskedReg = reg | kReadMask; uint8_t res;
uint8_t maskedReg = reg;
if (count <= 0) {
return;
}
I2CTransaction transaction; I2CTransaction transaction;
transaction.start() transaction.start()
.write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE)
.write_ack(maskedReg) .write_ack(reg)
.stop()
.start() .start()
.write_addr(kTouchWheelAddress, I2C_MASTER_READ) .write_addr(kTouchWheelAddress, I2C_MASTER_READ)
.read(data, I2C_MASTER_NACK) .read(&res, I2C_MASTER_NACK)
.stop(); .stop();
// TODO: Handle errors here.
ESP_ERROR_CHECK(transaction.Execute()); ESP_ERROR_CHECK(transaction.Execute());
return res;
} }
void TouchWheel::Update() { void TouchWheel::Update() {
// Read data from device into member struct // Read data from device into member struct
uint8_t position; bool has_data = !gpio_get_level(GPIO_NUM_25);
this->ReadRegister(Register::SLIDER_POSITION, &position, 1); if (!has_data) {
data_.wheel_position = position; 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 { TouchWheelData TouchWheel::GetTouchWheelData() const {
return data_; return data_;
} }
} // namespace drivers } // namespace drivers

@ -1,4 +1,5 @@
#include <dirent.h> #include <dirent.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <cstddef> #include <cstddef>
@ -121,16 +122,9 @@ extern "C" void app_main(void) {
storage = std::move(storage_res.value()); storage = std::move(storage_res.value());
} }
/*
ESP_LOGI(TAG, "Init touch wheel"); ESP_LOGI(TAG, "Init touch wheel");
auto touchwheel_res = drivers::TouchWheel::create(expander); std::unique_ptr<drivers::TouchWheel> touchwheel =
std::shared_ptr<drivers::TouchWheel> touchwheel; std::make_unique<drivers::TouchWheel>();
if (touchwheel_res.has_error()) {
ESP_LOGE(TAG, "Failed!");
} else {
touchwheel = std::move(touchwheel_res.value());
}
*/
LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs)); LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs));
lvglArgs->gpio_expander = expander; lvglArgs->gpio_expander = expander;
@ -156,9 +150,14 @@ extern "C" void app_main(void) {
console::AppConsole console(playback.get()); console::AppConsole console(playback.get());
console.Launch(); console.Launch();
uint8_t prev_position = 0;
while (1) { while (1) {
//touchwheel->Update(); touchwheel->Update();
//ESP_LOGI(TAG, "Touch wheel pos: %d", touchwheel->GetTouchWheelData().wheel_position); auto wheel_data = touchwheel->GetTouchWheelData();
if (wheel_data.wheel_position != prev_position) {
prev_position = wheel_data.wheel_position;
ESP_LOGI(TAG, "Touch wheel pos: %u", prev_position);
}
vTaskDelay(pdMS_TO_TICKS(100)); vTaskDelay(pdMS_TO_TICKS(100));
} }
} }

Loading…
Cancel
Save