pimoroni-pico/libraries/breakout_encoder/breakout_encoder.hpp

93 lines
2.8 KiB
C++

#pragma once
#include "../../drivers/ioexpander/ioexpander.hpp"
namespace pimoroni {
class BreakoutEncoder {
//--------------------------------------------------
// Enums
//--------------------------------------------------
public:
enum Direction : bool {
DIRECTION_CW = true,
DIRECTION_CCW = false
};
//--------------------------------------------------
// Constants
//--------------------------------------------------
public:
static const uint8_t DEFAULT_I2C_ADDRESS = 0x0F;
static constexpr float DEFAULT_BRIGHTNESS = 1.0f; //Effectively the maximum fraction of the period that the LED will be on
static const Direction DEFAULT_DIRECTION = DIRECTION_CW;
static const uint8_t PIN_UNUSED = UINT8_MAX;
static const uint32_t DEFAULT_TIMEOUT = 1;
private:
static const uint8_t LED_R = 1;
static const uint8_t LED_G = 7;
static const uint8_t LED_B = 2;
static const uint8_t ENC_TERM_A = 12;
static const uint8_t ENC_TERM_B = 3;
static const uint8_t ENC_TERM_C = 11;
static const uint8_t ENC_CHANNEL = 1;
static const bool INVERT_OUTPUT = true; //true for common cathode, false for common anode
//--------------------------------------------------
// Variables
//--------------------------------------------------
private:
IOExpander ioe;
Direction direction = DEFAULT_DIRECTION;
float brightness = DEFAULT_BRIGHTNESS;
uint8_t interrupt_pin = PIN_UNUSED; // A local copy of the value passed to the IOExpander, used in initialisation
//--------------------------------------------------
// Constructors/Destructor
//--------------------------------------------------
public:
BreakoutEncoder() :
ioe(DEFAULT_I2C_ADDRESS) {}
BreakoutEncoder(uint8_t address) :
ioe(address) {}
BreakoutEncoder(i2c_inst_t *i2c, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interrupt = PIN_UNUSED, uint32_t timeout = DEFAULT_TIMEOUT) :
ioe(i2c, address, sda, scl, interrupt, timeout),
interrupt_pin(interrupt) {}
//--------------------------------------------------
// Methods
//--------------------------------------------------
public:
bool init(bool skip_chip_id_check = false);
// For print access in micropython
i2c_inst_t* get_i2c() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
// Calls through to IOExpander class
void set_address(uint8_t address);
// Encoder breakout specific
Direction get_direction();
void set_direction(Direction direction);
void set_brightness(float brightness);
void set_led(uint8_t r, uint8_t g, uint8_t b);
bool available();
int16_t read();
};
}