/ src / display / led / display_led_rgb_led_data.h
display_led_rgb_led_data.h
  1  #pragma once
  2  
  3  #ifdef __cplusplus
  4  extern "C" {
  5  #endif // __cplusplus
  6  
  7  #include <stddef.h>
  8  #include <stdint.h>
  9  #include <stdbool.h>
 10  
 11  #include "sdk_errors.h"
 12  
 13  #define DISPLAY_LED_RGB_COLOR_ORDER_RGB		0
 14  #define DISPLAY_LED_RGB_COLOR_ORDER_RBG		1
 15  #define DISPLAY_LED_RGB_COLOR_ORDER_GRB		2
 16  #define DISPLAY_LED_RGB_COLOR_ORDER_GBR		3
 17  #define DISPLAY_LED_RGB_COLOR_ORDER_BRG		4
 18  #define DISPLAY_LED_RGB_COLOR_ORDER_BGR		5
 19  
 20  // a struct describing a 32 bit color
 21  typedef struct display_led_rgb_color_s
 22  {
 23  	union
 24  	{
 25  		uint8_t raw[4];
 26  		struct
 27  		{
 28  			uint8_t alpha;
 29  			uint8_t red;
 30  			uint8_t green;
 31  			uint8_t blue;
 32  		};
 33  	};
 34  } display_led_rgb_color_t;
 35  
 36  typedef struct display_led_rgb_driver_config_pin_s {
 37  	uint32_t pin;
 38  } display_led_rgb_driver_config_pin_t;
 39  
 40  typedef struct display_led_rgb_driver_config_s {
 41  	size_t pin_count;
 42  	display_led_rgb_driver_config_pin_t* pins;
 43  	bool invert_polarity;
 44  } display_led_rgb_driver_config_t;
 45  
 46  #define DISPLAY_LED_RGB_DRIVER_CONFIG_DEFINE(name, pinCount)	\
 47  static struct {													\
 48  	display_led_rgb_driver_config_pin_t pinsData[pinCount];		\
 49  } name ## pinDataDefs ;											\
 50  static display_led_rgb_driver_config_t name = {					\
 51  	.pin_count = pinCount,										\
 52  	.pins = (name ## pinDataDefs).pinsData,						\
 53  }
 54  
 55  // a struct describing a rgb led driver
 56  typedef struct display_led_rgb_driver_s
 57  {
 58  	ret_code_t (*initialize)(const display_led_rgb_driver_config_t* config);
 59  	void (*uninitialize)();
 60  	void (*set_leds)(const display_led_rgb_color_t* led_colors, size_t led_count, uint8_t brightness);
 61  	uint8_t default_brightness;
 62  	const char* name;
 63  	void* instance_data;
 64  } display_led_rgb_driver_t;
 65  
 66  #ifdef __cplusplus
 67  }
 68  #endif // __cplusplus
 69  
 70  #ifdef __cplusplus
 71  
 72  namespace display::led
 73  {
 74  	class RgbLedColorBufferDescriptor
 75  	{
 76  	public:
 77  		const size_t Size;
 78  		display_led_rgb_color_t* const Data;
 79  		bool ShouldUpdate = true;
 80  		uint8_t Brightness = 0;
 81  
 82  		display_led_rgb_color_t& operator[](size_t index)
 83  		{
 84  			if (index >= Size)
 85  			{
 86  				return Data[0];
 87  			}
 88  
 89  			return Data[index];
 90  		}
 91  
 92  	protected:
 93  		RgbLedColorBufferDescriptor(int ledCount, display_led_rgb_color_t* const colorData) :
 94  			Size(ledCount), Data(colorData)
 95  		{ }
 96  	};
 97  
 98  	template<size_t LedCount>
 99  	class RgbLedColorBuffer : public RgbLedColorBufferDescriptor
100  	{
101  	public:
102  		RgbLedColorBuffer() :
103  			RgbLedColorBufferDescriptor(LedCount, colorBuffer)
104  		{}
105  	private:
106  		display_led_rgb_color_t colorBuffer[LedCount];
107  	};
108  } // namespace display::led
109  
110  static_assert("RGB color size should be expected to be 4 bytes." && sizeof(display_led_rgb_color_t) == 4);
111  
112  #endif // __cplusplus