mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-06-19 04:15:06 +00:00
Compare commits
3 Commits
b216ba8595
...
ba6b3a7202
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba6b3a7202 | ||
|
|
e0a8ac36dd | ||
|
|
7ff6a7c249 |
@ -1,13 +1,16 @@
|
||||
#include "TdeckKeyboard.h"
|
||||
|
||||
#include "../../../../TactilityKernel/include/tactility/drivers/i2c_controller.h"
|
||||
|
||||
#include <KeyboardBacklight/KeyboardBacklight.h>
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/hal/display/DisplayDevice.h>
|
||||
#include <Tactility/hal/i2c/I2c.h>
|
||||
#include <Tactility/settings/DisplaySettings.h>
|
||||
#include <Tactility/settings/KeyboardSettings.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <lvgl.h>
|
||||
#include <Tactility/settings/KeyboardSettings.h>
|
||||
#include <Tactility/settings/DisplaySettings.h>
|
||||
#include <Tactility/hal/display/DisplayDevice.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/Logger.h>
|
||||
#include <KeyboardBacklight/KeyboardBacklight.h>
|
||||
|
||||
using tt::hal::findFirstDevice;
|
||||
|
||||
@ -91,5 +94,6 @@ bool TdeckKeyboard::stopLvgl() {
|
||||
}
|
||||
|
||||
bool TdeckKeyboard::isAttached() const {
|
||||
return tt::hal::i2c::masterHasDeviceAtAddress(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, 100);
|
||||
auto controller = device_find_by_name("i2c_internal");
|
||||
return i2c_controller_has_device_at_address(controller, TDECK_KEYBOARD_SLAVE_ADDRESS, 100) == ERROR_NONE;
|
||||
}
|
||||
|
||||
@ -156,7 +156,7 @@ bool TpagerKeyboard::stopLvgl() {
|
||||
}
|
||||
|
||||
bool TpagerKeyboard::isAttached() const {
|
||||
return tt::hal::i2c::masterHasDeviceAtAddress(keypad->getPort(), keypad->getAddress(), 100);
|
||||
return i2c_controller_has_device_at_address(keypad->getController(), keypad->getAddress(), 100) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool TpagerKeyboard::initBacklight(gpio_num_t pin, uint32_t frequencyHz, ledc_timer_t timer, ledc_channel_t channel) {
|
||||
|
||||
@ -11,7 +11,8 @@ static const auto LOGGER = tt::Logger("CoreS3Display");
|
||||
static void setBacklightDuty(uint8_t backlightDuty) {
|
||||
const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100] - under 20 is too dark
|
||||
// TODO: Refactor to use Axp2102 driver subproject. Reference: https://github.com/m5stack/M5Unified/blob/b8cfec7fed046242da7f7b8024a4e92004a51ff7/src/utility/AXP2101_Class.cpp#L42
|
||||
if (!tt::hal::i2c::masterWriteRegister(I2C_NUM_0, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000)) { // Sets DLD01
|
||||
auto controller = device_find_by_name("i2c_internal");
|
||||
if (i2c_controller_write_register(controller, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000) != ERROR_NONE) { // Sets DLD01
|
||||
LOGGER.error("Failed to set display backlight voltage");
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,8 @@ static const auto LOGGER = tt::Logger("StackChanDisplay");
|
||||
|
||||
static void setBacklightDuty(uint8_t backlightDuty) {
|
||||
const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100]
|
||||
if (!tt::hal::i2c::masterWriteRegister(I2C_NUM_0, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000)) {
|
||||
auto controller = device_find_by_name("i2c_internal");
|
||||
if (i2c_controller_write_register(controller, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000) != ERROR_NONE) { // Sets DLD01
|
||||
LOGGER.error("Failed to set display backlight voltage");
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <tactility/bindings/root.h>
|
||||
#include <tactility/bindings/esp32_ble.h>
|
||||
#include <tactility/bindings/esp32_gpio.h>
|
||||
#include <tactility/bindings/esp32_grove.h>
|
||||
#include <tactility/bindings/esp32_i2c.h>
|
||||
#include <tactility/bindings/esp32_i2s.h>
|
||||
#include <tactility/bindings/esp32_spi.h>
|
||||
|
||||
@ -168,7 +168,13 @@ static error_t start_device(Device* device) {
|
||||
if (!data) return ERROR_OUT_OF_MEMORY;
|
||||
device_set_driver_data(device, data);
|
||||
|
||||
return start_child(device, config->defaultMode);
|
||||
if (start_child(device, config->defaultMode) != ERROR_NONE) {
|
||||
device_set_driver_data(device, nullptr);
|
||||
delete data;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop_device(Device* device) {
|
||||
|
||||
@ -5,60 +5,11 @@
|
||||
|
||||
#include <Tactility/freertoscompat/RTOS.h>
|
||||
|
||||
#include <climits>
|
||||
#include <string>
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
constexpr TickType_t defaultTimeout = 10 / portTICK_PERIOD_MS;
|
||||
|
||||
enum class Status {
|
||||
Started,
|
||||
Stopped,
|
||||
Unknown
|
||||
};
|
||||
|
||||
/** Start the bus for the specified port. */
|
||||
bool start(i2c_port_t port);
|
||||
|
||||
/** Stop the bus for the specified port. */
|
||||
bool stop(i2c_port_t port);
|
||||
|
||||
/** @return true if the bus is started */
|
||||
bool isStarted(i2c_port_t port);
|
||||
|
||||
/** @return name or nullptr */
|
||||
const char* getName(i2c_port_t port);
|
||||
|
||||
/** Read bytes in master mode. */
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/** Read bytes from the specified register in master mode. */
|
||||
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/** Write bytes in master mode. */
|
||||
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/** Write bytes to a register in master mode */
|
||||
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/**
|
||||
* Write multiple values to multiple registers in master mode.
|
||||
* The input is as follows: { register1, value1, register2, value2, ... }
|
||||
* @return false if any of the write operations failed
|
||||
*/
|
||||
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/** Write bytes and then read the response bytes in master mode*/
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/** @return true when a device is detected at the specified address */
|
||||
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout = defaultTimeout);
|
||||
|
||||
/**
|
||||
* The lock for the specified bus.
|
||||
* This can be used when calling native I2C functionality outside of Tactility.
|
||||
*/
|
||||
Lock& getLock(i2c_port_t port);
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
#include <Tactility/hal/i2c/I2c.h>
|
||||
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <tactility/drivers/esp32_i2c.h>
|
||||
@ -13,13 +11,6 @@
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
class NoLock final : public tt::Lock {
|
||||
bool lock(TickType_t timeout) const override { return true; }
|
||||
void unlock() const override { /* NO-OP */ }
|
||||
};
|
||||
|
||||
static NoLock NO_LOCK;
|
||||
|
||||
Device* findDevice(i2c_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
struct Params {
|
||||
@ -37,7 +28,7 @@ Device* findDevice(i2c_port_t port) {
|
||||
auto* driver = device_get_driver(device);
|
||||
if (driver == nullptr) return true;
|
||||
if (!driver_is_compatible(driver, "espressif,esp32-i2c")) return true;
|
||||
auto* config = static_cast<const Esp32I2cConfig*>(device->config);
|
||||
const auto* config = static_cast<const Esp32I2cConfig*>(device->config);
|
||||
if (config->port != params_ptr->port) return true;
|
||||
// Found it, stop iterating
|
||||
params_ptr->device = device;
|
||||
@ -50,62 +41,10 @@ Device* findDevice(i2c_port_t port) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isStarted(i2c_port_t port) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return device_is_ready(device);
|
||||
}
|
||||
|
||||
const char* getName(i2c_port_t port) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return nullptr;
|
||||
return device->name;
|
||||
}
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_read(device, address, data, dataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_read_register(device, address, reg, data, dataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_write(device, address, data, dataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_write_register(device, address, reg, data, dataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_write_register_array(device, address, data, dataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_write_read(device, address, writeData, writeDataSize, readData, readDataSize, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) {
|
||||
auto* device = findDevice(port);
|
||||
if (device == nullptr) return false;
|
||||
return i2c_controller_has_device_at_address(device, address, timeout) == ERROR_NONE;
|
||||
}
|
||||
|
||||
Lock& getLock(i2c_port_t port) {
|
||||
return NO_LOCK;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user