-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws2812.c
136 lines (111 loc) · 2.56 KB
/
ws2812.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* ws2812.c
*
* Created on: 24 sty 2014
* Author: slawek
*/
#include <avr/pgmspace.h>
#include <string.h>
#include "ws2812.h"
#include "ws2812_syms.h"
typedef enum { GREEN, RED, BLUE } COLOR;
/*
* Frame buffer
* nr LEDa: 0 1 2 3 4 5 6 7 8 9
* indeks: 012345678901234567890123456789
* 1 2
* kolor: GRBGRBGRBGRBGRBGRBGRBGRBGRBGRB
*/
uint8_t ws2812_fbuf[WS2812_LED_NUM*3];
static uint8_t brightness;
//ustawia pin wyjściowy
void ws2812_init()
{
DDR(WS2812_PIN_LETTER) |=_BV(WS2812_PIN_NR);
}
// ledno - numer kolejny LEDa (0 - WS2812_LED_NUM-1)
// pwm - moc swiecenia (0-255)
static inline void ws2812_set(uint16_t ledno, uint8_t pwm, COLOR color)
{
#ifdef WS2812_BRIGHTNESS
ws2812_fbuf[ledno*3+color] = brightness ? (pwm * brightness) >> 8 : pwm;
#else
ws2812_fbuf[ledno*3+color] = pwm;
#endif
}
void ws2812_set_rgb(uint16_t ledno, uint8_t pwm_red, uint8_t pwm_green, uint8_t pwm_blue)
{
ws2812_set(ledno,pwm_red,RED);
ws2812_set(ledno,pwm_green,GREEN);
ws2812_set(ledno,pwm_blue,BLUE);
}
void ws2812_set_color(uint16_t ledno, uint32_t rgb)
{
ws2812_set(ledno,(uint8_t)(rgb>>16),RED);
ws2812_set(ledno,(uint8_t)(rgb>>8),GREEN);
ws2812_set(ledno,(uint8_t)rgb,BLUE);
}
void ws2812_set_red(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,RED);
}
void ws2812_set_green(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,GREEN);
}
void ws2812_set_blue(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,BLUE);
}
void ws2812_set_yellow(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,RED);
ws2812_set(ledno,pwm,GREEN);
}
void ws2812_set_magenta(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,RED);
ws2812_set(ledno,pwm,BLUE);
}
void ws2812_set_cyan(uint16_t ledno, uint8_t pwm)
{
ws2812_set(ledno,pwm,GREEN);
ws2812_set(ledno,pwm,BLUE);
}
void ws2812_set_white(uint16_t ledno, uint8_t pwm)
{
ws2812_set_rgb(ledno,pwm,pwm,pwm);
}
uint32_t ws2812_color(uint8_t r, uint8_t g, uint8_t b)
{
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
void ws2812_max_brightness(uint8_t br)
{
brightness = br +1;
}
uint8_t ws2812_get_brightness()
{
return brightness - 1;
}
uint32_t ws2812_adj_brightness(uint32_t rgb, uint8_t br)
{
uint8_t r = (uint8_t)(rgb >> 16);
uint8_t g = (uint8_t)(rgb >> 8);
uint8_t b = (uint8_t)rgb;
if(++br)
{
r = (r * br) >> 8;
g = (g * br) >> 8;
b = (b * br) >> 8;
}
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
uint16_t ws2812_num_leds()
{
return WS2812_LED_NUM;
}
void ws2812_clear()
{
memset(ws2812_fbuf,'\0',WS2812_LED_NUM*3);
}