parent
b9a75cd55a
commit
78ec09c494
@ -0,0 +1 @@ |
||||
Subproject commit 9ffbb5a1b8be06484e23e67202607e6b0b0d3a8e |
@ -0,0 +1,60 @@ |
||||
#pragma once |
||||
|
||||
|
||||
#include <functional> |
||||
#include <stdint.h> |
||||
|
||||
#include "esp_err.h" |
||||
#include "result.hpp" |
||||
|
||||
#include "gpio_expander.hpp" |
||||
|
||||
namespace drivers { |
||||
|
||||
struct TouchWheelData { |
||||
bool is_touched = false; |
||||
uint8_t wheel_position = -1; |
||||
}; |
||||
|
||||
class TouchWheel { |
||||
public: |
||||
enum Error { |
||||
FAILED_TO_BOOT, |
||||
FAILED_TO_CONFIGURE, |
||||
}; |
||||
static auto create(GpioExpander* expander) |
||||
-> cpp::result<std::unique_ptr<TouchWheel>, Error>; |
||||
|
||||
TouchWheel(GpioExpander* gpio); |
||||
~TouchWheel(); |
||||
|
||||
// Not copyable or movable.
|
||||
TouchWheel(const TouchWheel&) = delete; |
||||
TouchWheel& operator=(const TouchWheel&) = delete; |
||||
|
||||
auto Update() -> void; |
||||
auto GetTouchWheelData() const -> TouchWheelData; |
||||
|
||||
private: |
||||
GpioExpander* gpio_; |
||||
TouchWheelData data_; |
||||
|
||||
enum Register { |
||||
FIRMWARE_VERSION = 0x1, |
||||
DETECTION_STATUS = 0x2, |
||||
KEY_STATUS_A = 0x3, |
||||
KEY_STATUS_B = 0x4, |
||||
SLIDER_POSITION = 0x5, |
||||
CALIBRATE = 0x6, |
||||
RESET = 0x7, |
||||
LOW_POWER = 0x8, |
||||
SLIDER_OPTIONS = 0x14, |
||||
}; |
||||
|
||||
|
||||
void WriteRegister(uint8_t reg, uint8_t val); |
||||
void ReadRegister(uint8_t reg, uint8_t* data, uint8_t count); |
||||
|
||||
}; |
||||
|
||||
} // namespace drivers
|
@ -0,0 +1,92 @@ |
||||
#include "touchwheel.hpp" |
||||
|
||||
#include <cstdint> |
||||
|
||||
#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<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) { |
||||
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
|
Loading…
Reference in new issue