2018-09-30 17:35:10 +02:00
|
|
|
/* Copyright 2018 Jason Williams (Wilba)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "is31fl3218.h"
|
|
|
|
#include "i2c_master.h"
|
|
|
|
|
|
|
|
// This is the full 8-bit address
|
2023-09-04 01:18:34 +02:00
|
|
|
#define IS31FL3218_I2C_ADDRESS 0xA8
|
2018-09-30 17:35:10 +02:00
|
|
|
|
|
|
|
// These are the register addresses
|
2023-09-04 01:18:34 +02:00
|
|
|
#define IS31FL3218_REG_SHUTDOWN 0x00
|
|
|
|
#define IS31FL3218_REG_PWM 0x01
|
|
|
|
#define IS31FL3218_REG_CONTROL 0x13
|
|
|
|
#define IS31FL3218_REG_UPDATE 0x16
|
|
|
|
#define IS31FL3218_REG_RESET 0x17
|
2018-09-30 17:35:10 +02:00
|
|
|
|
|
|
|
// Default timeout if no I2C response
|
2023-09-04 01:18:34 +02:00
|
|
|
#define IS31FL3218_I2C_TIMEOUT 100
|
2018-09-30 17:35:10 +02:00
|
|
|
|
|
|
|
// Reusable buffer for transfers
|
|
|
|
uint8_t g_twi_transfer_buffer[20];
|
|
|
|
|
|
|
|
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
|
|
|
|
// If used as RGB LED driver, LEDs are assigned RGB,RGB,RGB,RGB,RGB,RGB
|
|
|
|
uint8_t g_pwm_buffer[18];
|
2019-08-30 20:19:03 +02:00
|
|
|
bool g_pwm_buffer_update_required = false;
|
2018-09-30 17:35:10 +02:00
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
|
2019-08-30 20:19:03 +02:00
|
|
|
g_twi_transfer_buffer[0] = reg;
|
|
|
|
g_twi_transfer_buffer[1] = data;
|
2023-09-04 01:18:34 +02:00
|
|
|
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
|
2023-09-04 01:18:34 +02:00
|
|
|
g_twi_transfer_buffer[0] = IS31FL3218_REG_PWM;
|
2023-04-30 04:35:27 +02:00
|
|
|
memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18);
|
2019-08-30 20:19:03 +02:00
|
|
|
|
2023-09-04 01:18:34 +02:00
|
|
|
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_init(void) {
|
2019-08-30 20:19:03 +02:00
|
|
|
// In case we ever want to reinitialize (?)
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_RESET, 0x00);
|
2019-08-30 20:19:03 +02:00
|
|
|
|
|
|
|
// Turn off software shutdown
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_SHUTDOWN, 0x01);
|
2019-08-30 20:19:03 +02:00
|
|
|
|
|
|
|
// Set all PWM values to zero
|
|
|
|
for (uint8_t i = 0; i < 18; i++) {
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_PWM + i, 0x00);
|
2019-08-30 20:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable all channels
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, 0b00111111);
|
2019-08-30 20:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load PWM registers and LED Control register data
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
|
2023-06-08 03:58:53 +02:00
|
|
|
if (g_pwm_buffer[index * 3 + 0] == red && g_pwm_buffer[index * 3 + 1] == green && g_pwm_buffer[index * 3 + 2] == blue) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
g_pwm_buffer[index * 3 + 0] = red;
|
|
|
|
g_pwm_buffer[index * 3 + 1] = green;
|
|
|
|
g_pwm_buffer[index * 3 + 2] = blue;
|
|
|
|
g_pwm_buffer_update_required = true;
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
|
2019-08-30 20:19:03 +02:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2023-08-23 02:00:03 +02:00
|
|
|
is31fl3218_set_color(i, red, green, blue);
|
2019-08-30 20:19:03 +02:00
|
|
|
}
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:00:03 +02:00
|
|
|
void is31fl3218_update_pwm_buffers(void) {
|
2019-08-30 20:19:03 +02:00
|
|
|
if (g_pwm_buffer_update_required) {
|
2023-08-23 02:00:03 +02:00
|
|
|
is31fl3218_write_pwm_buffer(g_pwm_buffer);
|
2019-08-30 20:19:03 +02:00
|
|
|
// Load PWM registers and LED Control register data
|
2023-09-04 01:18:34 +02:00
|
|
|
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
|
2019-08-30 20:19:03 +02:00
|
|
|
}
|
|
|
|
g_pwm_buffer_update_required = false;
|
2018-09-30 17:35:10 +02:00
|
|
|
}
|