LibDriver APA102C
Loading...
Searching...
No Matches
driver_apa102c.c
Go to the documentation of this file.
1
36
37#include "driver_apa102c.h"
38
42#define CHIP_NAME "Shiji Lighting APA102C"
43#define MANUFACTURER_NAME "Shiji Lighting"
44#define SUPPLY_VOLTAGE_MIN 5.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 200.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 70.0f
49#define DRIVER_VERSION 1000
50
62{
63 if (handle == NULL) /* check handle */
64 {
65 return 2; /* return error */
66 }
67 if (handle->debug_print == NULL) /* check debug_print */
68 {
69 return 3; /* return error */
70 }
71 if (handle->spi_init == NULL) /* check spi_init */
72 {
73 handle->debug_print("apa102c: spi_init is null.\n"); /* spi_init is null */
74
75 return 3; /* return error */
76 }
77 if (handle->spi_deinit == NULL) /* check spi_deinit */
78 {
79 handle->debug_print("apa102c: spi_deinit is null.\n"); /* spi_deinit is null */
80
81 return 3; /* return error */
82 }
83 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
84 {
85 handle->debug_print("apa102c: spi_write_cmd is null.\n"); /* spi_write_cmd is null */
86
87 return 3; /* return error */
88 }
89 if (handle->delay_ms == NULL) /* check delay_ms */
90 {
91 handle->debug_print("apa102c: delay_ms is null.\n"); /* delay_ms is null */
92
93 return 3; /* return error */
94 }
95
96 if (handle->spi_init() != 0) /* spi init */
97 {
98 handle->debug_print("apa102c: spi init failed.\n"); /* spi init failed */
99
100 return 1; /* return error */
101 }
102 handle->inited = 1; /* flag finish initialization */
103
104 return 0; /* success return 0 */
105}
106
124uint8_t apa102c_write(apa102c_handle_t *handle, uint32_t *color, uint32_t len, uint8_t *temp, uint32_t temp_len)
125{
126 uint32_t i;
127 uint16_t byte_size;
128
129 if (handle == NULL) /* check handle */
130 {
131 return 2; /* return error */
132 }
133 if (handle->inited != 1) /* check handle initialization */
134 {
135 return 3; /* return error */
136 }
137 if (color == NULL) /* check color */
138 {
139 handle->debug_print("apa102c: color is null.\n"); /* color is null */
140
141 return 4; /* return error */
142 }
143 if (temp == NULL) /* check temp */
144 {
145 handle->debug_print("apa102c: temp is null.\n"); /* temp is null */
146
147 return 5; /* return error */
148 }
149
150 byte_size = len * 4 + 8; /* get byte size */
151 if (byte_size > temp_len) /* check byte size */
152 {
153 handle->debug_print("apa102c: temp buffer is too small.\n"); /* temp buffer is too small */
154
155 return 6; /* return error */
156 }
157 temp[0] = 0x00; /* start frame */
158 temp[1] = 0x00; /* start frame */
159 temp[2] = 0x00; /* start frame */
160 temp[3] = 0x00; /* start frame */
161 for (i = 0; i < len; i++) /* set the color frame */
162 {
163 temp[4 + i * 4 + 0] = (color[i] >> 24) & 0xFF; /* set brightness */
164 temp[4 + i * 4 + 1] = (color[i] >> 16) & 0xFF; /* set blue */
165 temp[4 + i * 4 + 2] = (color[i] >> 8) & 0xFF; /* set green */
166 temp[4 + i * 4 + 3] = (color[i] >> 0) & 0xFF; /* set red */
167 }
168 temp[4 + len * 4 + 0] = 0xFF; /* end frame */
169 temp[4 + len * 4 + 1] = 0xFF; /* end frame */
170 temp[4 + len * 4 + 2] = 0xFF; /* end frame */
171 temp[4 + len * 4 + 3] = 0xFF; /* end frame */
172
173 if (handle->spi_write_cmd(temp, (uint16_t)byte_size) != 0) /* write command */
174 {
175 handle->debug_print("apa102c: write command failed.\n"); /* write command failed */
176
177 return 1; /* return error */
178 }
179
180 return 0; /* success return 0 */
181}
182
198uint8_t apa102c_write_off(apa102c_handle_t *handle, uint32_t len, uint8_t *temp, uint32_t temp_len)
199{
200 uint32_t i;
201 uint16_t byte_size;
202
203 if (handle == NULL) /* check handle */
204 {
205 return 2; /* return error */
206 }
207 if (handle->inited != 1) /* check handle initialization */
208 {
209 return 3; /* return error */
210 }
211 if (temp == NULL) /* check temp */
212 {
213 handle->debug_print("apa102c: temp is null.\n"); /* temp is null */
214
215 return 4; /* return error */
216 }
217
218 byte_size = len * 4 + 8; /* get byte size */
219 if (byte_size > temp_len) /* check byte size */
220 {
221 handle->debug_print("apa102c: temp buffer is too small.\n"); /* temp buffer is too small */
222
223 return 5; /* return error */
224 }
225 temp[0] = 0x00; /* start frame */
226 temp[1] = 0x00; /* start frame */
227 temp[2] = 0x00; /* start frame */
228 temp[3] = 0x00; /* start frame */
229 for (i = 0; i < len; i++) /* set the color frame */
230 {
231 temp[4 + i * 4 + 0] = 0xE0; /* set brightness */
232 temp[4 + i * 4 + 1] = 0x00; /* set blue */
233 temp[4 + i * 4 + 2] = 0x00; /* set green */
234 temp[4 + i * 4 + 3] = 0x00; /* set red */
235 }
236 temp[4 + len * 4 + 0] = 0xFF; /* end frame */
237 temp[4 + len * 4 + 1] = 0xFF; /* end frame */
238 temp[4 + len * 4 + 2] = 0xFF; /* end frame */
239 temp[4 + len * 4 + 3] = 0xFF; /* end frame */
240
241 if (handle->spi_write_cmd(temp, (uint16_t)byte_size) != 0) /* write command */
242 {
243 handle->debug_print("apa102c: write command failed.\n"); /* write command failed */
244
245 return 1; /* return error */
246 }
247
248 return 0; /* success return 0 */
249}
250
262{
263 uint8_t res;
264
265 if (handle == NULL) /* check handle */
266 {
267 return 2; /* return error */
268 }
269 if (handle->inited != 1) /* check handle initialization */
270 {
271 return 3; /* return error */
272 }
273
274 res = handle->spi_deinit(); /* spi deinit */
275 if (res != 0) /* check error */
276 {
277 handle->debug_print("apa102c: spi deinit failed.\n"); /* spi deinit failed */
278
279 return 1; /* return error */
280 }
281 handle->inited = 0; /* flag closed */
282
283 return 0; /* success return 0 */
284}
285
298uint8_t apa102c_set_reg(apa102c_handle_t *handle, uint8_t *buf, uint16_t len)
299{
300 if (handle == NULL) /* check handle */
301 {
302 return 2; /* return error */
303 }
304 if (handle->inited != 1) /* check handle initialization */
305 {
306 return 3; /* return error */
307 }
308
309 return handle->spi_write_cmd(buf, len); /* write to reg */
310}
311
321{
322 if (info == NULL) /* check handle */
323 {
324 return 2; /* return error */
325 }
326
327 memset(info, 0, sizeof(apa102c_info_t)); /* initialize apa102c info structure */
328 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
329 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
330 strncpy(info->interface, "SPI", 8); /* copy interface name */
331 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
332 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
333 info->max_current_ma = MAX_CURRENT; /* set maximum current */
334 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
335 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
336 info->driver_version = DRIVER_VERSION; /* set driver version */
337
338 return 0; /* success return 0 */
339}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip register definition
#define DRIVER_VERSION
driver apa102c header file
uint8_t apa102c_info(apa102c_info_t *info)
get chip's information
uint8_t apa102c_init(apa102c_handle_t *handle)
initialize the chip
uint8_t apa102c_deinit(apa102c_handle_t *handle)
close the chip
uint8_t apa102c_write(apa102c_handle_t *handle, uint32_t *color, uint32_t len, uint8_t *temp, uint32_t temp_len)
write color frame
uint8_t apa102c_write_off(apa102c_handle_t *handle, uint32_t len, uint8_t *temp, uint32_t temp_len)
write off frame
struct apa102c_info_s apa102c_info_t
apa102c information structure definition
struct apa102c_handle_s apa102c_handle_t
apa102c handle structure definition
uint8_t apa102c_set_reg(apa102c_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v