-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
227 lines (183 loc) · 5.69 KB
/
driver.c
File metadata and controls
227 lines (183 loc) · 5.69 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// To enable debugging
//#define DEBUG
// NUM CHANNELS + 1
#define BUF_LEN 13
#define NUM_VECTORS 32767
#define COMMAND_IDLE_MICROSECONDS 10000
#define false 0
#define true 1
typedef struct LightVector {
int dwell_time; // How long to show this vector?
unsigned char buffer[BUF_LEN]; // Buffer bytes (MUST START with 0)
} LightVector;
typedef struct Command {
int length;
LightVector vectors[NUM_VECTORS];
} Command;
typedef struct SharedState {
int new_command_ready;
Command* command;
Command* new_command;
} SharedState;
static volatile int keepRunning = 1;
void intHandler(int dummy) {
keepRunning = 0;
}
void zeroize(unsigned char* buffer, int length) {
int i;
for(i=0;i<length;i++) {
buffer[i] = 0;
}
}
void *DriverThread(void *inp)
{
// UNCOMMENT TO DISABLE THE HARDWARE THREAD (for testing)
//while(1);
Command* temp_command;
int fd;
int ip, iterations;
LightVector current;
SharedState* state;
state = (SharedState*)inp;
unsigned char zero_buffer[BUF_LEN];
zeroize(zero_buffer, BUF_LEN);
// Open the DMX fd
#ifndef DEBUG
fd = open("/dev/dmx0", O_WRONLY);
#endif
#ifdef DEBUG
fd = open("/dev/null", O_WRONLY);
#endif
if (fd < 0) {
perror("failure opening /dev/dmx0");
exit(-1);
}
current = state->command->vectors[0];
iterations = 0;
ip = 0;
// driver!
while(keepRunning) {
#ifndef DEBUG
// Write the current buffer value
write(fd, current.buffer, BUF_LEN);
#endif
#ifdef DEBUG
// Print what we would have written
printf("Iteration %d of %d for vector %d of %d...\n", iterations, current.dwell_time, ip, state->command->length);
printf("Would write: %02X %02X %02X %02X\n", current.buffer[0], current.buffer[1], current.buffer[2], current.buffer[3]);
usleep(500000);
#endif
// Increment the iter count
iterations++;
if (iterations >= current.dwell_time) {
iterations = 0;
ip++;
if (ip >= state->command->length) {
if (state->new_command_ready) {
// Pointer SWAP!
temp_command = state->command;
state->command = state->new_command;
state->new_command = temp_command;
state->new_command_ready = false;
}
ip = 0;
}
current = state->command->vectors[ip];
}
}
// If we got here, someone hit Ctrl-C - write all 0s
write(fd, zero_buffer, BUF_LEN);
close(fd);
printf("Driver thread exiting\n");
printf("Press Ctrl-D (to send EOF) if MainThread doesn't quit promptly\n");
pthread_exit(NULL);
}
int parse_light_vector(LightVector* vector, char* line) {
int vec_pos = 0;
// Find the dwell time from the first item in the string
char *p = strtok(line, " ");
if(!p) {
return false;
}
vector->dwell_time = atoi(p);
#ifdef DEBUG
//printf("vector %d dwell time: '%s' -> %d\n", vec_num, p, state.new_command->vectors[vec_num].dwell_time);
#endif
p = strtok(NULL, " ");
// The rest of the items in the string are
for (; p != NULL; p = strtok(NULL, " ")) {
vector->buffer[vec_pos] = (unsigned char)atoi(p);
vec_pos++;
}
return true;
}
int main() {
char line[1024];
// Handle SIGINT to gracefully shutdown
signal(SIGINT, intHandler);
Command command_a = { 3, {
{1, {0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00}},
{1, {0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00}},
{1, {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01}}
}};
Command command_b;
SharedState state;
state.command = &command_a;
state.new_command = &command_b;
state.new_command_ready = false;
// Three vectors to process
// END default command for testing
// Start the driver thread!
pthread_t driver_thread;
int rc;
rc = pthread_create(&driver_thread, NULL, DriverThread, (void *)(&state));
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
int vec_num, vec_pos;
int eof;
vec_num = 0;
// DO INPUT STUFF
while(keepRunning) {
if(!state.new_command_ready) {
printf("waiting for input:\n");
// We can ready a new command!
eof = (fgets(line, sizeof(line), stdin) == NULL);
//printf("line *%s*\n", line);
if(eof || !strcmp(line, "END\n")) {
if (vec_num > 0) {
state.new_command_ready = true;
state.new_command->length = vec_num;
vec_num=0;
printf("loading new command of length %d\n", state.new_command->length);
}
if (eof) break;
continue;
}
state.new_command->vectors[vec_num] = (LightVector) {0, {0x00, 0x00, 0x00, 0x00}};
if (parse_light_vector(&(state.new_command->vectors[vec_num]),
line)) vec_num++;
}
else {
printf("not ready for a new command yet...\n");
usleep(COMMAND_IDLE_MICROSECONDS);
}
}
while(eof && keepRunning) {
usleep(COMMAND_IDLE_MICROSECONDS);
}
printf("Main thread exiting\n");
/* Last thing that main() should do */
pthread_exit(NULL);
return 0;
}