Merge remote-tracking branch 'origin/master' into develop

This commit is contained in:
Joshua Diamond
2022-09-28 16:47:03 -04:00
7 changed files with 76 additions and 45 deletions

View File

@@ -24,16 +24,17 @@
#include "print.h"
#include "leds.h"
struct __attribute__((packed)) cRGB {
// Color order of LEDs is Green, Red, Blue.
typedef struct PACKED {
uint8_t r;
uint8_t g;
uint8_t b;
};
} raiseRGB;
#define LEDS_PER_HAND 72
#define LED_BANKS 9
#define LEDS_PER_BANK 8
#define LED_BYTES_PER_BANK (sizeof(cRGB) * LEDS_PER_BANK)
#define LED_BYTES_PER_BANK (sizeof(raiseRGB) * LEDS_PER_BANK)
// shifting << 1 is because drivers/chibios/i2c_master.h expects the address
// shifted.
@@ -44,37 +45,28 @@ struct __attribute__((packed)) cRGB {
#define LEFT 0
#define RIGHT 1
static cRGB led_state[2 * LEDS_PER_HAND];
void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
uint8_t buf[] = {TWI_CMD_LED_SET_ALL_TO, b, g, r};
i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
wait_us(10);
i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
wait_us(10);
}
void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b) {
int sled = led_map[led];
uint8_t buf[] = {TWI_CMD_LED_SET_ONE_TO, sled & 0x1f, b, g, r};
int hand = (sled >= LEDS_PER_HAND) ? RIGHT : LEFT;
i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
wait_us(10);
}
static raiseRGB led_pending[2 * LEDS_PER_HAND];
static raiseRGB led_state[2 * LEDS_PER_HAND];
static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
int sled = led_map[index];
led_state[sled].r = r;
led_state[sled].g = g;
led_state[sled].b = b;
// The red component of the LED is apparently stronger than the others.
// From: https://github.com/keyboardio/Kaleidoscope/blob/aba8c9ee66bbb5ded15135618d2b2964ee82b2cc/plugins/Kaleidoscope-Hardware-Dygma-Raise/src/kaleidoscope/device/dygma/raise/RaiseSide.cpp#L235-L242
if (r >= 26) {
r -= 26;
}
led_pending[sled].r = r;
led_pending[sled].g = g;
led_pending[sled].b = b;
}
static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) set_color(i, r, g, b);
}
static void init(void) {}
static void init(void) {
set_color_all(0,0,0);
}
static void flush(void) {
uint8_t command[1 + LED_BYTES_PER_BANK];
@@ -86,10 +78,17 @@ static void flush(void) {
for (int hand = 0; hand < 2; hand++) {
int addr = I2C_ADDR(hand);
int i = (hand * LEDS_PER_HAND) + (bank * LEDS_PER_BANK);
uint8_t *bank_data = (uint8_t *)&led_state[i];
if (memcmp(&led_state[i], &led_pending[i], LED_BYTES_PER_BANK) == 0) {
// No change.
continue;
}
// Update LED state
memcpy(&led_state[i], &led_pending[i], LED_BYTES_PER_BANK);
command[0] = TWI_CMD_LED_BASE + bank;
memcpy(&command[1], bank_data, LED_BYTES_PER_BANK);
memcpy(&command[1], &led_pending[i], LED_BYTES_PER_BANK);
i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
// delay to prevent issues with the i2c bus