potion_party_leds/main/leds.h

88 lines
2.6 KiB
C

///
//
// leds.h defines structs and functions for addressing an HD107s ledstrip over serial using the GPIO pins.
//
///
#ifndef _potion_leds_h
#define _potion_leds_h
#include <FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include "esp_system.h"
typedef enum LedsSendStatus {
LEDS_SEND_WAITING,
LEDS_SEND_REQUESTED,
LEDS_SENDING,
} LedsSendStatus;
typedef enum MoveEdgeAction {
LEDS_EDGE_WRAP,
LEDS_EDGE_REVERSE,
LEDS_EDGE_STOP
} MoveEdgeAction;
// pack the struct to match exactly 8 * 4 = 32bits
typedef struct __attribute__((__packed__)) LedComponents {
// 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;
// union of components and their representation as a u32
// allows for easier sending of data over serial
typedef union Led {
struct LedComponents components;
uint32_t bits;
} Led;
// point on a gradient
typedef struct GradientPoint {
union Led led; // value of the led at this point
int offset; // offset (measured in leds) from the beginning
short movement; // direction of movement over time
MoveEdgeAction edge_action; // what to do when movement hits an end of the strip
} 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;
typedef struct LedThreadData {
TaskHandle_t task;
TaskFunction_t func;
} LedThreadData;
// 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;
#define CLOCK 4
#define DATA 5
extern void send_leds();
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);
extern void leds_thread();
extern void leds_init();
#endif // !_leds_h