ws2811.h
Go to the documentation of this file.
1 /*
2  * ws2811.h
3  *
4  * Copyright (c) 2014 Jeremy Garff <jer @ jers.net>
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification, are permitted
9  * provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this list of
12  * conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
14  * of conditions and the following disclaimer in the documentation and/or other materials
15  * provided with the distribution.
16  * 3. Neither the name of the owner nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 
31 #ifndef __WS2811_H__
32 #define __WS2811_H__
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include <stdint.h>
39 
40 #include "rpihw.h"
41 #include "pwm.h"
42 
43 
44 #define WS2811_TARGET_FREQ 800000 // Can go as low as 400000
45 
46 // 4 color R, G, B and W ordering
47 #define SK6812_STRIP_RGBW 0x18100800
48 #define SK6812_STRIP_RBGW 0x18100008
49 #define SK6812_STRIP_GRBW 0x18081000
50 #define SK6812_STRIP_GBRW 0x18080010
51 #define SK6812_STRIP_BRGW 0x18001008
52 #define SK6812_STRIP_BGRW 0x18000810
53 #define SK6812_SHIFT_WMASK 0xf0000000
54 
55 // 3 color R, G and B ordering
56 #define WS2811_STRIP_RGB 0x00100800
57 #define WS2811_STRIP_RBG 0x00100008
58 #define WS2811_STRIP_GRB 0x00081000
59 #define WS2811_STRIP_GBR 0x00080010
60 #define WS2811_STRIP_BRG 0x00001008
61 #define WS2811_STRIP_BGR 0x00000810
62 
63 // predefined fixed LED types
64 #define WS2812_STRIP WS2811_STRIP_GRB
65 #define SK6812_STRIP WS2811_STRIP_GRB
66 #define SK6812W_STRIP SK6812_STRIP_GRBW
67 
69 
70 typedef uint32_t ws2811_led_t; //< 0xWWRRGGBB
71 typedef struct ws2811_channel_t
72 {
73  int gpionum; //< GPIO Pin with PWM alternate function, 0 if unused
74  int invert; //< Invert output signal
75  int count; //< Number of LEDs, 0 if channel is unused
76  int strip_type; //< Strip color layout -- one of WS2811_STRIP_xxx constants
77  ws2811_led_t *leds; //< LED buffers, allocated by driver based on count
78  uint8_t brightness; //< Brightness value between 0 and 255
79  uint8_t wshift; //< White shift value
80  uint8_t rshift; //< Red shift value
81  uint8_t gshift; //< Green shift value
82  uint8_t bshift; //< Blue shift value
83  uint8_t *gamma; //< Gamma correction table
85 
86 typedef struct ws2811_t
87 {
88  uint64_t render_wait_time; //< time in µs before the next render can run
89  struct ws2811_device *device; //< Private data for driver use
90  const rpi_hw_t *rpi_hw; //< RPI Hardware Information
91  uint32_t freq; //< Required output frequency
92  int dmanum; //< DMA number _not_ already in use
94 } ws2811_t;
95 
96 #define WS2811_RETURN_STATES(X) \
97  X(0, WS2811_SUCCESS, "Success"), \
98  X(-1, WS2811_ERROR_GENERIC, "Generic failure"), \
99  X(-2, WS2811_ERROR_OUT_OF_MEMORY, "Out of memory"), \
100  X(-3, WS2811_ERROR_HW_NOT_SUPPORTED, "Hardware revision is not supported"), \
101  X(-4, WS2811_ERROR_MEM_LOCK, "Memory lock failed"), \
102  X(-5, WS2811_ERROR_MMAP, "mmap() failed"), \
103  X(-6, WS2811_ERROR_MAP_REGISTERS, "Unable to map registers into userspace"), \
104  X(-7, WS2811_ERROR_GPIO_INIT, "Unable to initialize GPIO"), \
105  X(-8, WS2811_ERROR_PWM_SETUP, "Unable to initialize PWM"), \
106  X(-9, WS2811_ERROR_MAILBOX_DEVICE, "Failed to create mailbox device"), \
107  X(-10, WS2811_ERROR_DMA, "DMA error"), \
108  X(-11, WS2811_ERROR_ILLEGAL_GPIO, "Selected GPIO not possible"), \
109  X(-12, WS2811_ERROR_PCM_SETUP, "Unable to initialize PCM"), \
110  X(-13, WS2811_ERROR_SPI_SETUP, "Unable to initialize SPI"), \
111  X(-14, WS2811_ERROR_SPI_TRANSFER, "SPI transfer error") \
112 
113 #define WS2811_RETURN_STATES_ENUM(state, name, str) name = state
114 #define WS2811_RETURN_STATES_STRING(state, name, str) str
115 
116 typedef enum {
118 
121 
122 ws2811_return_t ws2811_init(ws2811_t *ws2811); //< Initialize buffers/hardware
123 void ws2811_fini(ws2811_t *ws2811); //< Tear it all down
124 ws2811_return_t ws2811_render(ws2811_t *ws2811); //< Send LEDs off to hardware
125 ws2811_return_t ws2811_wait(ws2811_t *ws2811); //< Wait for DMA completion
126 const char * ws2811_get_return_t_str(const ws2811_return_t state); //< Get string representation of the given return state
127 void ws2811_set_custom_gamma_factor(ws2811_t *ws2811, double gamma_factor); //< Set a custom Gamma correction array based on a gamma correction factor
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* __WS2811_H__ */
struct ws2811_device * device
Definition: ws2811.h:89
uint8_t bshift
Definition: ws2811.h:82
uint8_t gshift
Definition: ws2811.h:81
uint32_t ws2811_led_t
Definition: ws2811.h:68
#define RPI_PWM_CHANNELS
Definition: pwm.h:54
ws2811_return_t
Definition: ws2811.h:116
void ws2811_fini(ws2811_t *ws2811)
Definition: ws2811.c:1079
ws2811_return_t ws2811_init(ws2811_t *ws2811)
Definition: ws2811.c:894
#define WS2811_RETURN_STATES(X)
Definition: ws2811.h:96
Definition: rpihw.h:36
ws2811_return_t ws2811_render(ws2811_t *ws2811)
Definition: ws2811.c:1138
void ws2811_set_custom_gamma_factor(ws2811_t *ws2811, double gamma_factor)
Definition: ws2811.c:1287
#define WS2811_RETURN_STATES_ENUM(state, name, str)
Definition: ws2811.h:113
ws2811_led_t * leds
Definition: ws2811.h:77
struct ws2811_channel_t ws2811_channel_t
uint64_t render_wait_time
Definition: ws2811.h:88
uint8_t rshift
Definition: ws2811.h:80
uint32_t freq
Definition: ws2811.h:91
struct ws2811_t ws2811_t
int strip_type
Definition: ws2811.h:76
const char * ws2811_get_return_t_str(const ws2811_return_t state)
Definition: ws2811.c:1273
uint8_t * gamma
Definition: ws2811.h:83
ws2811_return_t ws2811_wait(ws2811_t *ws2811)
Definition: ws2811.c:1106
const rpi_hw_t * rpi_hw
Definition: ws2811.h:90
int dmanum
Definition: ws2811.h:92
uint8_t wshift
Definition: ws2811.h:79
uint8_t brightness
Definition: ws2811.h:78


ws281x
Author(s): Alexey Rogachevskiy , Oleg Kalachev
autogenerated on Wed Jun 15 2022 02:46:00