-
Notifications
You must be signed in to change notification settings - Fork 3
/
blink_test.c
144 lines (121 loc) · 3.47 KB
/
blink_test.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
/*
* blink_test.c
*
* Copyright 2012 Christopher De Vries
* This program is distributed under the Artistic License 2.0, a copy of which
* is included in the file LICENSE.txt
*/
#include "tclled.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#ifndef SPIFILE
#define SPIFILE "/dev/spidev2.0"
#endif
static const char *device = SPIFILE;
static const int leds = 500;
static const unsigned long min_interval = 700000L;
static const unsigned long max_interval = 1300000L;
static int continue_looping;
unsigned long microseconds_since(struct timeval *tv);
void stop_program(int sig);
int main(int argc, char *argv[]) {
tcl_buffer buf;
int fd;
int return_value;
tcl_color *p;
int i;
struct timeval start_time;
unsigned long *change_times;
unsigned long current_time;
/* Open the device file using Low-Level IO */
fd = open(device,O_WRONLY);
if(fd<0) {
fprintf(stderr,"Error %d: %s\n",errno,strerror(errno));
exit(1);
}
/* Initialize the SPI bus for Total Control Lighting */
return_value = spi_init(fd);
if(return_value==-1) {
fprintf(stderr,"SPI initialization error %d: %s\n",errno, strerror(errno));
exit(1);
}
/* Initialize pixel buffer */
if(tcl_init(&buf,leds)<0) {
fprintf(stderr,"Pixel buffer initialization error: Not enough memory.\n");
exit(1);
}
/* Set the gamma correction factors for each color */
set_gamma(2.2,2.2,2.2);
/* Get the current time */
return_value = gettimeofday(&start_time,NULL);
if(return_value==-1) {
fprintf(stderr, "Error reading the current time: %s\n", strerror(errno));
exit(1);
}
/* Set scattered intervals for the lights to change */
/* Set all pixels to black */
change_times = (unsigned long *)malloc(leds*sizeof(unsigned long));
if(change_times==NULL) {
fprintf(stderr, "Error allocating array of change times.\n");
exit(1);
}
for(i=0;i<leds;i++) {
change_times[i] = min_interval+random()%(max_interval-min_interval);
write_gamma_color(&buf.pixels[i],0x00,0x00,0x00);
}
send_buffer(fd,&buf);
/* Prepare to receive ctrl-c to stop looping */
continue_looping = 1;
signal(SIGINT,stop_program);
/* Loop while continue_looping is true */
while(continue_looping) {
current_time = microseconds_since(&start_time);
for(i=0;i<leds;i++) {
if(current_time>change_times[i]) {
p = &buf.pixels[i];
if(p->red==0x00 && p->blue==0x00 && p->green==0x00) {
write_gamma_color(p,(int)random()%256,(int)random()%256,(int)random%256);
}
else {
write_gamma_color(p,0x00,0x00,0x00);
}
change_times[i]=current_time+min_interval+random()%(max_interval-min_interval);
}
}
send_buffer(fd,&buf);
}
tcl_free(&buf);
free(change_times);
close(fd);
printf("Program Terminated.\n");
return 0;
}
unsigned long microseconds_since(struct timeval *tv) {
int ret;
struct timeval now;
struct timeval diff;
unsigned long retval;
ret = gettimeofday(&now,NULL);
if(ret==-1) {
return 0L;
}
diff.tv_sec=now.tv_sec-tv->tv_sec;
diff.tv_usec=now.tv_usec-tv->tv_usec;
retval = diff.tv_sec*1000000L;
retval += diff.tv_usec;
return retval;
}
void stop_program(int sig) {
/* Ignore the signal */
signal(sig,SIG_IGN);
/* stop the looping */
continue_looping = 0;
/* Put the ctrl-c to default action in case something goes wrong */
signal(sig,SIG_DFL);
}