I2C PWM expander added from different Project

This commit is contained in:
2024-11-16 16:42:29 +01:00
parent deccb3186c
commit f6608075ff
7 changed files with 327 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
/**
* @file i2cPort.hpp
* @author Gustice
* @brief C++ Wrapper for I2C-interface
*
* @copyright Copyright (c) 2024
*/
#pragma once
#include "driver/gpio.h"
#include "driver/i2c_master.h"
#include "portBase.hpp"
#include <vector>
/**
* @brief I2C-Master Class
* @details More recent implementation of IDF-based I2C master
*/
class I2cMaster : public PortBase {
public:
class Slave {
public:
enum ClockRate {
_100k = 100000,
_400k = 400000,
};
Slave(I2cMaster &interface, uint8_t addr, ClockRate rate = _100k);
~Slave();
bool writeTo(const std::vector<uint8_t> &data);
std::vector<uint8_t> readFrom(const std::vector<uint8_t> &address, unsigned size);
protected:
const uint8_t Addr;
I2cMaster &Interface;
i2c_master_dev_handle_t _devHandle;
};
I2cMaster(i2c_port_t port, gpio_num_t sda, gpio_num_t scl);
~I2cMaster() = default;
bool probe(uint8_t addr);
const i2c_port_t Port;
private:
friend class Slave;
i2c_master_bus_handle_t _busHandle;
};
+17
View File
@@ -0,0 +1,17 @@
/**
* @file portBase.hpp
* @author Gustice
* @brief Base-class for all hardware ports
*
* @copyright Copyright (c) 2024
*/
#pragma once
class PortBase {
public:
PortBase(/* args */) {};
// Forbid Copy and Move assignments
PortBase(const PortBase &other) = delete;
PortBase &operator=(const PortBase &other) = delete;
};
@@ -0,0 +1,39 @@
/**
* @file pwmExpanderPca9685.hpp
* @author Gustice
* @brief PWM-Expander PCA9685
* @details https://www.nxp.com/docs/en/data-sheet/PCA9685.pdf
*
* @copyright Copyright (c) 2024
*/
#pragma once
#include "i2cPort.hpp"
#include <array>
#include <memory>
#include <optional>
/**
* @brief I2C-Connected LED-Driver with 16 ports
* @note This module can also be used to generate Servo-Motor signals
*/
class PmwExpanderPca9685 {
public:
static constexpr int PwmChannels = 16;
using GrayValues = std::array<uint16_t, PwmChannels>;
PmwExpanderPca9685(I2cMaster &port, bool inverted = false, uint8_t address = 0x40); // 0x40 default Address
void sendImage(GrayValues &grayValues);
void reset();
void setPwmFreq(float frequency, bool inverted);
private:
I2cMaster::Slave _port;
const uint8_t _address;
const size_t _count = 16;
// 16 LED-Channels with 4 bytes each + 4 byte overhead
std::vector<uint8_t> _data;
bool write(uint8_t reg, uint8_t data);
std::optional<uint8_t> read(uint8_t reg);
};