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>
|
2023-09-28 12:31:14 +00:00
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
2023-09-21 10:01:02 +00:00
|
|
|
#include "esp_system.h"
|
2023-09-18 20:41:46 +00:00
|
|
|
|
2023-09-28 12:43:07 +00:00
|
|
|
typedef enum LedsSendStatus {
|
2023-09-27 12:59:03 +00:00
|
|
|
LEDS_SEND_WAITING,
|
|
|
|
LEDS_SEND_REQUESTED,
|
|
|
|
LEDS_SENDING,
|
2023-09-28 12:43:07 +00:00
|
|
|
} LedsSendStatus;
|
2023-09-27 12:59:03 +00:00
|
|
|
|
2023-09-28 14:58:44 +00:00
|
|
|
typedef enum MoveEdgeAction {
|
|
|
|
LEDS_EDGE_WRAP,
|
|
|
|
LEDS_EDGE_REVERSE,
|
|
|
|
LEDS_EDGE_STOP
|
|
|
|
} MoveEdgeAction;
|
|
|
|
|
2023-09-18 20:41:46 +00:00
|
|
|
// pack the struct to match exactly 8 * 4 = 32bits
|
2023-09-28 12:43:07 +00:00
|
|
|
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;
|
2023-09-21 10:01:02 +00:00
|
|
|
uint8_t global; // global baseline brightness, highest 3 bits should always be ones
|
2023-09-28 12:43:07 +00:00
|
|
|
} 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
|
2023-09-28 12:43:07 +00:00
|
|
|
typedef union Led {
|
2023-09-28 12:31:14 +00:00
|
|
|
struct LedComponents components;
|
2023-09-18 20:41:46 +00:00
|
|
|
uint32_t bits;
|
2023-09-28 12:43:07 +00:00
|
|
|
} Led;
|
2023-09-18 20:41:46 +00:00
|
|
|
|
|
|
|
// point on a gradient
|
2023-09-28 12:43:07 +00:00
|
|
|
typedef struct GradientPoint {
|
2023-09-28 12:31:14 +00:00
|
|
|
union Led led; // value of the led at this point
|
2023-09-28 14:58:44 +00:00
|
|
|
int offset; // offset (measured in leds) from the beginning
|
2023-09-22 10:32:14 +00:00
|
|
|
short movement; // direction of movement over time
|
2023-09-28 14:58:44 +00:00
|
|
|
MoveEdgeAction edge_action; // what to do when movement hits an end of the strip
|
2023-09-28 12:43:07 +00:00
|
|
|
} GradientPoint;
|
2023-09-22 10:32:14 +00:00
|
|
|
|
2023-09-28 12:43:07 +00:00
|
|
|
typedef struct Gradient {
|
2023-09-28 12:31:14 +00:00
|
|
|
struct GradientPoint points[16]; // array of gradient points, support at most 16 points in a gradient
|
2023-09-22 10:32:14 +00:00
|
|
|
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-28 12:43:07 +00:00
|
|
|
} Gradient;
|
2023-09-18 20:41:46 +00:00
|
|
|
|
2023-09-28 12:43:07 +00:00
|
|
|
typedef struct LedThreadData {
|
2023-09-27 12:59:03 +00:00
|
|
|
TaskHandle_t task;
|
|
|
|
TaskFunction_t func;
|
2023-09-28 12:43:07 +00:00
|
|
|
} LedThreadData;
|
2023-09-27 12:59:03 +00:00
|
|
|
|
2023-09-28 12:31:14 +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
|
2023-09-28 12:43:07 +00:00
|
|
|
extern Led* g_leds;
|
|
|
|
extern Gradient g_default_gradient;
|
|
|
|
extern Gradient g_current_gradient;
|
2023-09-28 12:31:14 +00:00
|
|
|
extern int g_leds_are_default;
|
|
|
|
extern enum LedsSendStatus g_leds_send_state;
|
|
|
|
extern SemaphoreHandle_t g_led_mutex;
|
2023-09-28 12:43:07 +00:00
|
|
|
extern LedThreadData g_led_thread_data;
|
2023-09-27 12:59:03 +00:00
|
|
|
|
2023-09-28 12:31:14 +00:00
|
|
|
#define CLOCK 4
|
|
|
|
#define DATA 5
|
2023-09-21 10:01:02 +00:00
|
|
|
|
2023-09-28 12:31:14 +00:00
|
|
|
extern void send_leds();
|
2023-09-27 12:59:03 +00:00
|
|
|
|
2023-09-28 12:43:07 +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);
|
2023-09-28 12:31:14 +00:00
|
|
|
extern void leds_reset_gradient(int defer_send);
|
2023-09-27 12:59:03 +00:00
|
|
|
|
2023-09-28 12:31:14 +00:00
|
|
|
extern void leds_thread();
|
|
|
|
extern void leds_init();
|
2023-09-18 20:41:46 +00:00
|
|
|
|
|
|
|
#endif // !_leds_h
|