-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.c
188 lines (149 loc) · 3.94 KB
/
led.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <json_object.h>
#include <json_util.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define LEDS_MAX 10
#define LED_STATE_FILE "/tmp/ledstate.json"
int start_frame()
{
int i;
uint8_t buf[1];
for (i = 0; i < 4; i++) {
buf[0] = 0x00;
wiringPiSPIDataRW(0, buf, 1);
}
return 0;
}
int end_frame(int bytes)
{
int i;
uint8_t buf[1];
for (i = 0; i < bytes; i++) {
buf[0] = 0xFF;
wiringPiSPIDataRW(0, buf, 1);
}
return 0;
}
int send_frame(uint8_t r, uint8_t g, uint8_t b, uint8_t br)
{
uint8_t led_frame[4];
led_frame[0] = 0b11100000 | (0b00011111 & br);
led_frame[1] = b;
led_frame[2] = g;
led_frame[3] = r;
wiringPiSPIDataRW(0, led_frame, 4);
return 0;
}
int clear_display(int leds) {
int j;
start_frame();
for (j = 0; j <= leds; j++) {
send_frame(0, 0, 0, 16);
}
start_frame(); // One extra clock cycle per LED, but we can only send bytes
return 0;
}
int next(int current, int min, int max) {
static int delta = -1;
if ((current == min) || (current == max)) {
delta = -delta;
}
return current + delta;
}
int randomize(uint8_t *r, uint8_t *g, uint8_t *b) {
*r = (uint8_t) (rand() & 0x01) * 0x20;
*g = (uint8_t) (rand() & 0x01) * 0x20;
*b = (uint8_t) (rand() & 0x01) * 0x20;
return 0;
}
int main(int argc, char *argv[])
{
uint32_t colour = 0;
uint8_t red, green, blue;
uint8_t brightness = 31;
uint8_t leds = LEDS_MAX;
uint16_t delay = 10;
int i, c;
int position = 0;
int clear = 0;
json_object *state = NULL;
while ((c = getopt (argc, argv, "X:b:l:p:Ch")) != -1) {
switch (c) {
case 'X':
colour = strtol(optarg, NULL, 16);
red = (colour & 0xFF0000) >> 16;
green = (colour & 0x00FF00) >> 8;
blue = (colour & 0x0000FF) >> 0;
break;
case 'b':
brightness = atoi(optarg);
break;
case 'p':
position = atoi(optarg);
if ((position < 0) || (position > leds)) {
printf("Incorrect LED position\n");
return 1;
}
break;
case 'l':
leds = atoi(optarg);
break;
case 'C':
clear = 1;
break;
case '?':
case 'h':
printf("Usage: led [-l <1 - 10>] -p <0 - %d> -X <000000 - FFFFFF> -b <0 - 31>\n", leds);
return 1;
default:
abort ();
}
}
srand(time(NULL));
wiringPiSetup();
if (wiringPiSPISetup(0, 6000000) < 0) {
printf("wiringPiSPISetup failed\n");
return -1;
}
if (clear) {
clear_display(LEDS_MAX);
remove(LED_STATE_FILE);
return 0;
}
state = json_object_from_file(LED_STATE_FILE);
// Load led configuration - if doesn't exist, then create it
if (NULL == state) {
state = json_object_new_array();
}
json_object *led = json_object_new_object();
json_object_object_add(led, "red", json_object_new_int((uint32_t) red));
json_object_object_add(led, "green", json_object_new_int((uint32_t) green));
json_object_object_add(led, "blue", json_object_new_int((uint32_t) blue));
json_object_object_add(led, "brightness", json_object_new_int((uint32_t) brightness));
// Replace or insert into array
json_object_array_put_idx(state, position, led);
start_frame();
for (i = 0; i < LEDS_MAX; i++) {
if (NULL == (led = json_object_array_get_idx(state, i))) {
red = green = blue = brightness = 0;
} else {
red = (uint8_t) json_object_get_int(json_object_object_get(led, "red"));
green = (uint8_t) json_object_get_int(json_object_object_get(led, "green"));
blue = (uint8_t) json_object_get_int(json_object_object_get(led, "blue"));
brightness = (uint8_t) json_object_get_int(json_object_object_get(led, "brightness"));
}
//printf("Red = 0x%02x, green = 0x%02x, blue = 0x%02x, brightness = 0x%02x\n", red, green, blue, brightness);
send_frame(red, green, blue, brightness);
usleep(delay * 1000);
}
start_frame(); // One extra clock cycle per LED, but we can only send bytes
// Save the current state for another time
json_object_to_file_ext(LED_STATE_FILE, state, JSON_C_TO_STRING_PRETTY);
json_object_put(state);
return 0;
}