Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aw9523 device, driveri #7

Merged
merged 15 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(SOURCES "src/Object/Class.cpp" "src/Drivers/InputGPIO.cpp" "src/Devices/Camera.cpp" "src/Drivers/InputTouchGPIO.cpp" "src/Drivers/OutputGPIO.cpp" "src/Devices/ButtonInput.cpp" "src/Periphery/GPIO.cpp" "src/Periphery/I2C.cpp" "src/Util/StateMachine/State.cpp" "src/Util/StateMachine/StateMachine.cpp" "src/Containers/Archive.cpp" "src/Misc/Singleton.cpp" "src/Memory/ObjectMemory.cpp" "src/Object/Object.cpp" "src/Core/Application.cpp" "src/Memory/ObjectManager.cpp" "src/Memory/GarbageCollector.cpp" "src/Entity/Entity.cpp" "src/Entity/AsyncEntity.cpp" "src/Entity/SyncEntity.cpp" "src/Statics/ApplicationStatics.cpp" "src/Thread/Threaded.cpp" "src/Util/stdafx.cpp" CACHE INTERNAL "" FORCE)
set(SOURCES "src/Object/Class.cpp" "src/Devices/Camera.cpp" "src/Devices/AW9523.cpp" "src/Drivers/Input/InputAW.cpp" "src/Drivers/Output/OutputDigAW.cpp" "src/Drivers/Output/OutputCurrAW.cpp" "src/Drivers/Input/InputGPIO.cpp" "src/Drivers/Input/InputTouchGPIO.cpp" "src/Drivers/Output/OutputGPIO.cpp" "src/Services/ButtonInput.cpp" "src/Periphery/GPIO.cpp" "src/Periphery/I2C.cpp" "src/Util/StateMachine/State.cpp" "src/Util/StateMachine/StateMachine.cpp" "src/Containers/Archive.cpp" "src/Misc/Singleton.cpp" "src/Memory/ObjectMemory.cpp" "src/Object/Object.cpp" "src/Core/Application.cpp" "src/Memory/ObjectManager.cpp" "src/Memory/GarbageCollector.cpp" "src/Entity/Entity.cpp" "src/Entity/AsyncEntity.cpp" "src/Entity/SyncEntity.cpp" "src/Statics/ApplicationStatics.cpp" "src/Thread/Threaded.cpp" "src/Util/stdafx.cpp" CACHE INTERNAL "" FORCE)

file(GLOB_RECURSE LIBS "lib/*/src/**.cpp" "lib/*/src/**.c")
set(LIBS_INCL "lib/glm/glm")
Expand Down
2 changes: 1 addition & 1 deletion src/Containers/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Queue {
}

private:
static constexpr size_t DefaultSize = 32;
static constexpr size_t DefaultSize = 16;
T* buffer;
Allocator allocator = Allocator();
size_t bufferSize;
Expand Down
167 changes: 167 additions & 0 deletions src/Devices/AW9523.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#include "AW9523.h"
#include "Util/stdafx.h"
#include <esp_log.h>

#define REG_RESET 0x7F
#define REG_ID 0x10
#define REG_CONF 0x11
#define REG_INPUT 0x00
#define REG_OUTPUT 0x02
#define REG_DIR 0x04
#define REG_INTR 0x06
#define REG_MODE 0x12
#define REG_DIM 0x20

#define VAL_RESET 0x00
#define VAL_ID 0x23

#define CFG_MASK (0b00010011)

#define IT(pin) ((pin) <= 7 ? 0 : 1)
#define REG(reg, pin) ((reg) + IT(pin))
#define BIT(pin) ((pin) <= 7 ? (pin) : (pin) - 8)
#define MASK(pin) (1 << BIT(pin))

static const char* TAG = "AW9523";

const uint8_t AW9523::dimmap[16] = { 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 12, 13, 14, 15 };

AW9523::AW9523(I2C* i2c, uint8_t addr) : i2c(i2c), Addr(addr){
if(i2c->probe(Addr) != ESP_OK){
ESP_LOGE(TAG, "Transmission error");
return;
}

// Reset
writeReg(REG_RESET, VAL_RESET);
regs = Regs();

delayMicros(50);

uint8_t id = readReg(REG_ID);
if(id != VAL_ID){
ESP_LOGE(TAG, "ID missmatch: expected %d, got %d", VAL_ID, id);
return;
}

// All input
regs.dir[0] = regs.dir[1] = 0xff;
writeReg(REG_DIR, regs.dir, 2);

// Push-pull mode for port 0
regs.conf |= 0b00010000;
writeReg(REG_CONF, regs.conf);

// Disable interrupts
regs.intr[0] = regs.intr[1] = 0xff;
writeReg(REG_INTR, regs.intr, 2);
}

void AW9523::resetDimOutputs(){
for(int i = 0; i < 16; ++i){
dim(i, 0);
}
}

void AW9523::pinMode(uint8_t pin, AW9523::PinMode mode){
if(pin >= 16) return;

const uint8_t it = IT(pin);
const uint8_t mask = MASK(pin);
const uint8_t regDir = REG(REG_DIR, pin);
const uint8_t regMode = REG(REG_MODE, pin);
uint8_t& intRegDir = regs.dir[it];
uint8_t& intRegMode = regs.mode[it];

if(mode == LED){
intRegMode = intRegMode & ~mask;
writeReg(regMode, intRegMode);
}else{
intRegMode = intRegMode | mask;
writeReg(regMode, intRegMode);

if(mode == OUT){
intRegDir = intRegDir & ~mask;
}else{
intRegDir = intRegDir | mask;
}

writeReg(regDir, intRegDir);
}
}

bool AW9523::read(uint8_t pin){
if(pin >= 16) return false;

const uint8_t reg = REG(REG_INPUT, pin);
return readReg(reg) & MASK(pin);
}


uint16_t AW9523::readAll(){
const auto regL = REG(REG_INPUT, 0);
const auto regH = REG(REG_INPUT, 15);

return (readReg(regH) << 8) | readReg(regL);
}

void AW9523::write(uint8_t pin, bool state){
if(pin >= 16) return;

const uint8_t reg = REG(REG_OUTPUT, pin);
const uint8_t mask = MASK(pin);
uint8_t& intReg = regs.output[IT(pin)];

if(state){
intReg |= mask;
}else{
intReg &= ~mask;
}

writeReg(reg, intReg);
}

void AW9523::dim(uint8_t pin, uint8_t factor){
if(pin >= 16) return;

pin = dimmap[pin];
regs.dim[pin] = factor;
writeReg(REG_DIM + pin, factor);
}

void AW9523::setInterrupt(uint8_t pin, bool enabled){
if(pin >= 16) return;

const uint8_t reg = REG(REG_INTR, pin);
const uint8_t mask = MASK(pin);
uint8_t& intReg = regs.intr[IT(pin)];

if(enabled){
intReg |= mask;
}else{
intReg &= ~mask;
}

writeReg(reg, intReg);
}

void AW9523::setCurrentLimit(AW9523::CurrentLimit limit){
const uint8_t mask = 0b00000011;
uint8_t& conf = regs.conf;
conf = (conf & ~mask) | (limit & mask);
writeReg(REG_CONF, conf & CFG_MASK);
}

uint8_t AW9523::readReg(uint8_t reg) const{
uint8_t data;
i2c->readRegister(Addr, reg, data);
return data;
}

void AW9523::writeReg(uint8_t reg, const uint8_t* data, size_t size) const{
ESP_ERROR_CHECK(i2c->writeRegister(Addr, reg, data, size));
}

void AW9523::writeReg(uint8_t reg, uint8_t data) const{
ESP_ERROR_CHECK(i2c->writeRegister(Addr, reg, data));
}
105 changes: 105 additions & 0 deletions src/Devices/AW9523.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef CMF_AW9523_H
#define CMF_AW9523_H

#include "Periphery/I2C.h"

/**
* GPIO expander and LED driver, 16-pin.
* https://cdn-shop.adafruit.com/product-files/4886/AW9523+English+Datasheet.pdf
*/


class AW9523 : public Object {
GENERATED_BODY(AW9523, Object)
public:
AW9523(I2C* i2c = nullptr, uint8_t addr = 0x58);

void resetDimOutputs();

enum PinMode : uint8_t {
IN, OUT, LED
};

/**
* Set pin mode:
* IN - GPIO input
* OUT - GPIO output
* LED - GPIO output with current control. Use dim(pin, factor) function to set dimming factor.
* @param pin Pin index
* @param mode Pin mode
*/
void pinMode(uint8_t pin, PinMode mode);

/**
* Read pin input state.
* @param pin Pin index
* @return State - true for high, false for low
*/
bool read(uint8_t pin);

/**
* Get read register for all pins.
* Does not check if all pins are Input-defined, so user is responsible for correct masking!
* @return bit-vector of pin states
*/
uint16_t readAll();

/**
* Set pin output state.
* @param pin Pin index
* @param state True for high, false for low
*/
void write(uint8_t pin, bool state);

/**
* Set LED dimming factor for pin.
* @param pin Pin index
* @param factor Dimming factor. Range 0-255
*/
void dim(uint8_t pin, uint8_t factor);

/**
* Enable or disable interrupt triggering for pin.
* @param pin Pin index
* @param enabled
*/
void setInterrupt(uint8_t pin, bool enabled);

enum CurrentLimit : uint8_t {
IMAX, // I_max
IMAX_3Q, // I_max * 3/4
IMAX_2Q, // I_max * 2/4
IMAX_1Q // I_max * 1/4
};

/**
* Set LED drive current limit. Only applies to pins configured as LED output. I_max is 37mA.
* IMAX - full I_max value (37mA)
* IMAX_3Q - 3/4 * I_max
* IMAX_2Q - 2/4 * I_max
* IMAX_1Q - 1/4 * I_max
*/
void setCurrentLimit(CurrentLimit limit);

private:
I2C* i2c;
const uint8_t Addr;

static const uint8_t dimmap[16];

struct Regs {
uint8_t conf = 0b00000000;
uint8_t dir[2] = { 0, 0 };
uint8_t output[2] = { 0, 0 };
uint8_t intr[2] = { 0, 0 };
uint8_t mode[2] = { 0xff, 0xff };
uint8_t dim[16] = { 0 };
} regs;

uint8_t readReg(uint8_t reg) const;
void writeReg(uint8_t reg, const uint8_t* data, size_t size) const;
void writeReg(uint8_t reg, uint8_t data) const;

};

#endif //CMF_AW9523_H
24 changes: 0 additions & 24 deletions src/Devices/ButtonInput.cpp

This file was deleted.

17 changes: 17 additions & 0 deletions src/Drivers/Input/InputAW.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "InputAW.h"

InputAW::InputAW(const std::vector<InputPinDef>& inputs, AW9523* aw9523) : Super(inputs), aw9523(aw9523){

}

void InputAW::scan() noexcept{
const auto inputReg = aw9523->readAll();

forEachInput([this, &inputReg](const InputPinDef& input){
getStates()[input.port] = (inputReg & (1 << input.port)) ^ input.inverted;
});
}

void InputAW::performRegister(InputPinDef input) noexcept{
aw9523->pinMode(input.port, AW9523::PinMode::IN);
}
22 changes: 22 additions & 0 deletions src/Drivers/Input/InputAW.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CMF_INPUTAW_H
#define CMF_INPUTAW_H

#include "Drivers/Interface/InputDriver.h"
#include "Devices/AW9523.h"

class InputAW : public InputDriver<> {
GENERATED_BODY(InputAW, InputDriver)
public:
InputAW() = default;
InputAW(const std::vector<InputPinDef>& inputs, AW9523* aw9523);

private:
void scan() noexcept override;

void performRegister(InputPinDef input) noexcept override;

StrongObjectPtr<AW9523> aw9523;
};


#endif //CMF_INPUTAW_H
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions src/Drivers/Interface/OutputDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OutputDriverBase : public SyncEntity {

virtual void write(int port, float value) noexcept{ }

virtual void write(int port, bool value) noexcept{
void write(int port, bool value) noexcept{
write(port, value ? 1.0f : 0.0f);
}

Expand All @@ -53,13 +53,14 @@ class OutputDriver : public OutputDriverBase {
return states.at(port);
}

virtual void write(int port, float value) noexcept override{
virtual void write(int port, float value) noexcept override final{
states[port] = value;
performWrite(port, value);
}

void registerOutput(T pinDef) noexcept{
outputs.emplace_back(pinDef);
inversions[pinDef.port] = pinDef.inverted;
performRegister(pinDef);
}

Expand Down Expand Up @@ -109,6 +110,7 @@ class OutputDriver : public OutputDriverBase {
Super::postInitProperties();

for(const auto& output: outputs){
inversions[output.port] = output.inverted;
performRegister(output);
}
}
Expand Down
Loading