2021-05-11 13:00:12 +01:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-14 12:49:48 +01:00
|
|
|
#include "drivers/ioexpander/ioexpander.hpp"
|
2021-05-14 18:12:37 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
2021-05-11 13:00:12 +01:00
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
2021-05-11 17:18:43 +01:00
|
|
|
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
|
2021-05-12 18:27:03 +01:00
|
|
|
static const Direction DEFAULT_DIRECTION = DIRECTION_CW;
|
2021-05-11 17:18:43 +01:00
|
|
|
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;
|
2021-05-12 18:27:03 +01:00
|
|
|
Direction direction = DEFAULT_DIRECTION;
|
2021-05-11 17:18:43 +01:00
|
|
|
float brightness = DEFAULT_BRIGHTNESS;
|
2021-05-17 11:08:23 +01:00
|
|
|
uint interrupt_pin = PIN_UNUSED; // A local copy of the value passed to the IOExpander, used in initialisation
|
2021-05-11 17:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2021-05-17 14:50:49 +01:00
|
|
|
BreakoutEncoder(uint8_t address = DEFAULT_I2C_ADDRESS) : BreakoutEncoder(new I2C(), address) {};
|
2021-05-11 17:18:43 +01:00
|
|
|
|
2021-05-17 11:08:23 +01:00
|
|
|
BreakoutEncoder(I2C *i2c, uint8_t address = DEFAULT_I2C_ADDRESS, uint interrupt = PIN_UNUSED, uint32_t timeout = DEFAULT_TIMEOUT, bool debug = false) :
|
2021-05-14 18:12:37 +01:00
|
|
|
ioe(i2c, address, interrupt, timeout, debug) {}
|
|
|
|
|
2021-05-17 14:50:49 +01:00
|
|
|
// TODO remove MicroPython-binding compatibility constructors
|
|
|
|
BreakoutEncoder(uint8_t address, uint sda, uint scl, uint interrupt = PIN_UNUSED, uint32_t timeout = DEFAULT_TIMEOUT) : BreakoutEncoder(new I2C(sda, scl), address, interrupt, timeout) {};
|
2021-05-11 17:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init(bool skip_chip_id_check = false);
|
|
|
|
|
|
|
|
// For print access in micropython
|
|
|
|
i2c_inst_t* get_i2c() const;
|
2021-05-14 15:14:47 +01:00
|
|
|
int get_address() const;
|
2021-05-11 17:18:43 +01:00
|
|
|
int get_sda() const;
|
|
|
|
int get_scl() const;
|
|
|
|
int get_int() const;
|
|
|
|
|
|
|
|
// Calls through to IOExpander class
|
2021-05-11 21:58:44 +01:00
|
|
|
void set_address(uint8_t address);
|
2021-05-12 20:03:50 +01:00
|
|
|
bool get_interrupt_flag();
|
|
|
|
void clear_interrupt_flag();
|
2021-05-11 17:18:43 +01:00
|
|
|
|
|
|
|
// Encoder breakout specific
|
2021-05-12 18:27:03 +01:00
|
|
|
Direction get_direction();
|
|
|
|
void set_direction(Direction direction);
|
2021-05-11 17:18:43 +01:00
|
|
|
|
|
|
|
void set_brightness(float brightness);
|
|
|
|
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
|
|
|
|
bool available();
|
|
|
|
int16_t read();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|