Fork of Tangara with customizations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tangara-fw/src/drivers/touchwheel.cpp

86 lines
2.0 KiB

2 years ago
#include "touchwheel.hpp"
#include <stdint.h>
2 years ago
#include <cstdint>
#include "assert.h"
#include "driver/gpio.h"
2 years ago
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/projdefs.h"
#include "hal/gpio_types.h"
2 years ago
#include "hal/i2c_types.h"
#include "i2c.hpp"
namespace drivers {
static const char* kTag = "TOUCHWHEEL";
static const uint8_t kTouchWheelAddress = 0x1C;
TouchWheel::TouchWheel() {
gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT);
gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLUP_ONLY);
2 years ago
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);
2 years ago
}
TouchWheel::~TouchWheel() {}
2 years ago
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());
}
uint8_t TouchWheel::ReadRegister(uint8_t reg) {
uint8_t res;
2 years ago
I2CTransaction transaction;
transaction.start()
.write_addr(kTouchWheelAddress, I2C_MASTER_WRITE)
.write_ack(reg)
2 years ago
.start()
.write_addr(kTouchWheelAddress, I2C_MASTER_READ)
.read(&res, I2C_MASTER_NACK)
2 years ago
.stop();
ESP_ERROR_CHECK(transaction.Execute());
return res;
2 years ago
}
void TouchWheel::Update() {
// 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
}
2 years ago
}
TouchWheelData TouchWheel::GetTouchWheelData() const {
return data_;
}
} // namespace drivers