2021-04-22 16:07:38 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <math.h>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "is31fl3731.hpp"
|
2021-09-13 16:40:51 +00:00
|
|
|
#include "common/pimoroni_common.hpp"
|
2021-04-22 16:07:38 +00:00
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
|
|
|
constexpr uint8_t CONFIG_BANK = 0x0b;
|
|
|
|
constexpr uint8_t NUM_PIXELS = 144;
|
|
|
|
constexpr uint8_t NUM_FRAMES = 8;
|
|
|
|
|
|
|
|
constexpr uint8_t ENABLE_OFFSET = 0x00;
|
|
|
|
constexpr uint8_t COLOR_OFFSET = 0x24;
|
|
|
|
|
|
|
|
enum mode {
|
|
|
|
PICTURE = 0x00,
|
|
|
|
AUTOPLAY = 0x08,
|
|
|
|
AUDIOPLAY = 0x18
|
|
|
|
};
|
|
|
|
|
|
|
|
enum reg : uint8_t {
|
|
|
|
MODE = 0x00,
|
|
|
|
FRAME = 0x01,
|
|
|
|
AUTPLAY1 = 0x02,
|
|
|
|
AUTOPLAY2 = 0x03,
|
|
|
|
BLINK = 0x05,
|
|
|
|
AUDIOSYNC = 0x06,
|
|
|
|
BREATH1 = 0x07,
|
|
|
|
BREATH2 = 0x08,
|
|
|
|
SHUTDOWN = 0x0a,
|
|
|
|
GAIN = 0x0b,
|
|
|
|
ADC = 0x0c,
|
|
|
|
BANK = 0xfd
|
|
|
|
};
|
|
|
|
|
|
|
|
bool IS31FL3731::init() {
|
|
|
|
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::BANK, CONFIG_BANK);
|
|
|
|
i2c->reg_write_uint8(address, reg::SHUTDOWN, 0b00000000);
|
2021-03-01 12:42:48 +00:00
|
|
|
|
|
|
|
clear();
|
|
|
|
update(0);
|
|
|
|
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::SHUTDOWN, 0b00000001);
|
2021-04-22 16:07:38 +00:00
|
|
|
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::MODE, mode::PICTURE);
|
2021-04-22 16:07:38 +00:00
|
|
|
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::AUDIOSYNC, 0);
|
2021-04-22 16:07:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_inst_t* IS31FL3731::get_i2c() const {
|
2021-05-14 17:12:37 +00:00
|
|
|
return i2c->get_i2c();
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:14:47 +00:00
|
|
|
int IS31FL3731::get_address() const {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:07:38 +00:00
|
|
|
int IS31FL3731::get_sda() const {
|
2021-05-14 17:12:37 +00:00
|
|
|
return i2c->get_sda();
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int IS31FL3731::get_scl() const {
|
2021-05-14 17:12:37 +00:00
|
|
|
return i2c->get_scl();
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IS31FL3731::clear() {
|
|
|
|
for(auto i = 0u; i < sizeof(buf); i++) {
|
|
|
|
buf[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IS31FL3731::enable(std::initializer_list<uint8_t> pattern, uint8_t frame) {
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::BANK, frame);
|
2022-01-25 17:49:24 +00:00
|
|
|
i2c->write_bytes(address, ENABLE_OFFSET, pattern.begin(), pattern.size());
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IS31FL3731::set(uint8_t index, uint8_t brightness) {
|
2022-07-07 10:25:34 +00:00
|
|
|
buf[index + 1] = pimoroni::GAMMA_8BIT[brightness];
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IS31FL3731::update(uint8_t frame) {
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->reg_write_uint8(address, reg::BANK, frame);
|
2021-04-22 16:07:38 +00:00
|
|
|
buf[0] = COLOR_OFFSET;
|
2021-05-14 17:12:37 +00:00
|
|
|
i2c->write_blocking(address, buf, sizeof(buf), false);
|
|
|
|
i2c->reg_write_uint8(address, reg::BANK, CONFIG_BANK); // Switch back to config bank
|
|
|
|
i2c->reg_write_uint8(address, reg::FRAME, frame); // Set the desired frame as active
|
2021-04-22 16:07:38 +00:00
|
|
|
}
|
|
|
|
}
|