potion_party_leds/main/leds.h

196 lines
5.4 KiB
C
Raw Normal View History

2023-09-19 08:34:06 +00:00
///
//
// leds.h defines structs and functions for addressing an HD107s ledstrip over serial using the GPIO pins.
//
///
2023-09-18 20:41:46 +00:00
#ifndef _potion_leds_h
#define _potion_leds_h
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include "shared.h"
#include "esp8266/gpio_register.h"
#include "esp_system.h"
2023-09-18 20:41:46 +00:00
#include "rom/ets_sys.h"
#include "driver/gpio.h"
// pack the struct to match exactly 8 * 4 = 32bits
struct __attribute__((__packed__)) led_components_t {
// RGB component values
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t global; // global baseline brightness, highest 3 bits should always be ones
2023-09-18 20:41:46 +00:00
};
// union of components and their representation as a u32
// allows for easier sending of data over serial
union led_t {
struct led_components_t components;
uint32_t bits;
};
// point on a gradient
struct gradient_point_t {
union led_t led; // value of the led at this point
size_t offset; // offset (measured in leds) from the beginning
short movement; // direction of movement over time
};
struct gradient_t {
struct gradient_point_t points[16]; // array of gradient points, support at most 16 points in a gradient
size_t points_len; // number of used gradient points
float duration; // amount of time to allow this gradient to last
// positive means an amount in second 0 or negative means indefinitely until further notice
2023-09-18 20:41:46 +00:00
};
// buffer that will be written out to the led strip over serial
uint32_t g_serial_out_buffer[62];
// 60-long slice of the out buffer that represents the first few leds
union led_t* g_leds = ((union led_t*)g_serial_out_buffer + 1);
struct gradient_t g_default_gradient;
struct gradient_t g_current_gradient;
int g_leds_are_default = 1;
2023-09-18 20:41:46 +00:00
#define CLOCK 4
#define DATA 5
static
void leds_config_gpio() {
gpio_config_t config = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 0x18030,
.pull_up_en = 0,
.pull_down_en = 0,
};
gpio_config(&config);
}
static
void serial_write(int high) {
// set clock out to high, triggering a rising edge
gpio_set_level(CLOCK, 1);
// write bit to data out
gpio_set_level(DATA, high);
os_delay_us(2);
// set clock to low, triggering a falling edge, shifting the LEDs shift register
gpio_set_level(CLOCK, 0);
}
2023-09-18 20:41:46 +00:00
static
void send_leds() {
LOGLN("entering send_leds()");
2023-09-18 20:41:46 +00:00
// index of the bit being written
// fixed point number where the first 5 bits are the bit of a 32bit integger, and the rest is the integer
2023-09-18 20:41:46 +00:00
int write_bit = 0;
int write_next = 0;
LOGLN("Setting clock low");
2023-09-18 20:41:46 +00:00
gpio_set_level(CLOCK, 0);
LOGLN("Writing to leds");
2023-09-18 20:41:46 +00:00
while(write_bit < sizeof(g_serial_out_buffer) * 8) {
// fetch the bit being addressed
write_next = 0x1 & (g_serial_out_buffer[write_bit >> 5] >> (32 - (write_bit & 0x1F)));
2023-09-18 20:41:46 +00:00
serial_write(write_next);
2023-09-18 20:41:46 +00:00
write_bit++;
}
gpio_set_level(CLOCK, 0);
gpio_set_level(DATA, 0);
2023-09-18 20:41:46 +00:00
}
static inline
uint8_t lerp_uint8(uint8_t a, uint8_t b, float t) {
if(t <= 0) return a;
else if(t >= 1.0) return b;
else {
int dir = b - a;
return a + dir * t;
}
2023-09-18 20:41:46 +00:00
}
static inline
void lerp_led(union led_t* out, const union led_t* from, const union led_t* to, float t) {
out->components.red = lerp_uint8(from->components.red, to->components.red, t);
out->components.green = lerp_uint8(from->components.green, to->components.green, t);
out->components.blue = lerp_uint8(from->components.blue, to->components.blue, t);
uint8_t glob_from = from->components.global & ~0xE0;
uint8_t glob_to = to->components.global & ~0xE0;
out->components.global = GLOBAL(lerp_uint8(glob_from, glob_to, t));
2023-09-18 20:41:46 +00:00
}
static inline
void lerp_points_between(const struct gradient_point_t from, const struct gradient_point_t to) {
const int dif = to.offset - from.offset;
float t = 0.f;
for(int led = from.offset; led <= to.offset; ++led) {
2023-09-18 20:41:46 +00:00
t = (float)(led - from.offset) / (float)dif;
lerp_led(g_leds + led, &from.led, &to.led, t);
}
}
static
void set_led_range(int start, int end, union led_t value) {
for(int i = start; i < end; ++i) {
g_leds[i] = value;
}
}
static
void leds_set_default_gradient(const struct gradient_t* gradient) {
g_default_gradient = *gradient;
}
static
void leds_set_current_gradient(const struct gradient_t* gradient, int defer_send) {
struct gradient_point_t from = gradient->points[0];
2023-09-18 20:41:46 +00:00
struct gradient_point_t to;
set_led_range(0, gradient->points[0].offset, gradient->points[0].led);
set_led_range(gradient->points[gradient->points_len-1].offset, 60, gradient->points[gradient->points_len-1].led);
g_current_gradient = *gradient;
for(int i = 1; i < gradient->points_len; ++i) {
to = gradient->points[i];
2023-09-18 20:41:46 +00:00
lerp_points_between(from, to);
from = to;
}
if(!defer_send) {
send_leds();
}
}
static
void leds_init() {
g_serial_out_buffer[0] = 0u;
g_serial_out_buffer[61] = ~0u;
set_led_range(0, 60,
(union led_t){.components =
(struct led_components_t) {
.red = 0,
.green = 0,
.blue = 0,
.global = GLOBAL(5)
}}
);
leds_config_gpio();
2023-09-18 20:41:46 +00:00
}
#endif // !_leds_h