96 lines
3.2 KiB
C
96 lines
3.2 KiB
C
/*
|
|
* This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk).
|
|
* Copyright (c) 2022 Pol Henarejos.
|
|
*
|
|
* 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, version 3.
|
|
*
|
|
* 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 "pico_keys.h"
|
|
|
|
#ifdef PICO_DEFAULT_WS2812_PIN
|
|
|
|
#include "hardware/pio.h"
|
|
#include "hardware/clocks.h"
|
|
#define ws2812_wrap_target 0
|
|
#define ws2812_wrap 3
|
|
#define ws2812_T1 2
|
|
#define ws2812_T2 5
|
|
#define ws2812_T3 3
|
|
static const uint16_t ws2812_program_instructions[] = {
|
|
// .wrap_target
|
|
0x6221, // 0: out x, 1 side 0 [2]
|
|
0x1123, // 1: jmp !x, 3 side 1 [1]
|
|
0x1400, // 2: jmp 0 side 1 [4]
|
|
0xa442, // 3: nop side 0 [4]
|
|
// .wrap
|
|
};
|
|
static const struct pio_program ws2812_program = {
|
|
.instructions = ws2812_program_instructions,
|
|
.length = 4,
|
|
.origin = -1,
|
|
};
|
|
|
|
static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
|
|
pio_sm_config c = pio_get_default_sm_config();
|
|
sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
|
|
sm_config_set_sideset(&c, 1, false, false);
|
|
return c;
|
|
}
|
|
static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) {
|
|
pio_gpio_init(pio, pin);
|
|
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
|
|
pio_sm_config c = ws2812_program_get_default_config(offset);
|
|
sm_config_set_sideset_pins(&c, pin);
|
|
sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
|
|
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
|
|
int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
|
|
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
|
|
sm_config_set_clkdiv(&c, div);
|
|
pio_sm_init(pio, sm, offset, &c);
|
|
pio_sm_set_enabled(pio, sm, true);
|
|
}
|
|
|
|
void led_driver_init() {
|
|
PIO pio = pio0;
|
|
int sm = 0;
|
|
uint offset = pio_add_program(pio, &ws2812_program);
|
|
|
|
ws2812_program_init(pio, sm, offset, PICO_DEFAULT_WS2812_PIN, 800000, true);
|
|
}
|
|
|
|
uint32_t pixel[] = {
|
|
0x00000000, // 0: off
|
|
0x00ff0000, // 1: red
|
|
0xff000000, // 2: green
|
|
0x0000ff00, // 3: blue
|
|
0xffff0000, // 4: yellow
|
|
0x00ffff00, // 5: magenta
|
|
0xff00ff00, // 6: cyan
|
|
0xffffff00 // 7: white
|
|
};
|
|
|
|
void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) {
|
|
float brightness = ((float)led_brightness / MAX_BTNESS) * progress;
|
|
uint32_t pixel_color = pixel[color];
|
|
uint8_t r = (pixel_color >> 16) & 0xFF;
|
|
uint8_t g = (pixel_color >> 24) & 0xFF;
|
|
uint8_t b = (pixel_color >> 8) & 0xFF;
|
|
|
|
r = (uint8_t)(r * brightness);
|
|
g = (uint8_t)(g * brightness);
|
|
b = (uint8_t)(b * brightness);
|
|
|
|
pio_sm_put_blocking(pio0, 0, (g << 24) | (r << 16) | (b << 8));
|
|
}
|
|
|
|
#endif
|