potion_party_leds/main/leds.h

81 lines
2.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
2023-09-27 12:59:03 +00:00
#include <FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include "esp_system.h"
2023-09-18 20:41:46 +00:00
typedef enum LedsSendStatus {
2023-09-27 12:59:03 +00:00
LEDS_SEND_WAITING,
LEDS_SEND_REQUESTED,
LEDS_SENDING,
} LedsSendStatus;
2023-09-27 12:59:03 +00:00
2023-09-18 20:41:46 +00:00
// pack the struct to match exactly 8 * 4 = 32bits
typedef struct __attribute__((__packed__)) LedComponents {
2023-09-18 20:41:46 +00:00
// 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
} LedComponents;
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
typedef union Led {
struct LedComponents components;
2023-09-18 20:41:46 +00:00
uint32_t bits;
} Led;
2023-09-18 20:41:46 +00:00
// point on a gradient
typedef struct GradientPoint {
union Led led; // value of the led at this point
2023-09-18 20:41:46 +00:00
size_t offset; // offset (measured in leds) from the beginning
short movement; // direction of movement over time
} GradientPoint;
typedef struct Gradient {
struct GradientPoint 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
} Gradient;
2023-09-18 20:41:46 +00:00
typedef struct LedThreadData {
2023-09-27 12:59:03 +00:00
TaskHandle_t task;
TaskFunction_t func;
} LedThreadData;
2023-09-27 12:59:03 +00:00
// buffer that will be written out to the led strip over serial
extern uint32_t g_serial_out_buffer[122];
// 120-long slice of the out buffer that represents the first few leds
extern Led* g_leds;
extern Gradient g_default_gradient;
extern Gradient g_current_gradient;
extern int g_leds_are_default;
extern enum LedsSendStatus g_leds_send_state;
extern SemaphoreHandle_t g_led_mutex;
extern LedThreadData g_led_thread_data;
2023-09-27 12:59:03 +00:00
#define CLOCK 4
#define DATA 5
extern void send_leds();
2023-09-27 12:59:03 +00:00
extern void set_led_range(int start, int end, Led value);
extern void leds_set_default_gradient(const Gradient* gradient);
extern void leds_set_current_gradient(const Gradient* gradient, int defer_send);
extern void leds_reset_gradient(int defer_send);
2023-09-27 12:59:03 +00:00
extern void leds_thread();
extern void leds_init();
2023-09-18 20:41:46 +00:00
#endif // !_leds_h