diff options
| author | ailurux <me@dbyron.id.au> | 2023-03-13 15:14:32 +1100 |
|---|---|---|
| committer | ailurux <me@dbyron.id.au> | 2023-03-13 15:14:32 +1100 |
| commit | 78ec09c494faadf9e7d06dc7d3e04531c3a34ff7 (patch) | |
| tree | 7f328e7236492f6bbcc66aea044b5e78fda620cc /src/drivers/touchwheel.cpp | |
| parent | b9a75cd55a11fd404a1977539acb64a6705f3809 (diff) | |
| download | tangara-fw-78ec09c494faadf9e7d06dc7d3e04531c3a34ff7.tar.gz | |
Touchwheel test
Diffstat (limited to 'src/drivers/touchwheel.cpp')
| -rw-r--r-- | src/drivers/touchwheel.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
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 <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 |
