-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
335 lines (280 loc) · 7.37 KB
/
main.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <pthread.h>
#include <errno.h>
//#define CONSOLE
#define false 0
#define true 1
#define NFDS 1
#define LED_GPIO 13
#define DIMMING_GPIO 12
#define BLANKING_GPIO 23
#define HV_EN_GPIO 24
#define SPI_PATH "/dev/spidev0.0"
#define SPI_MODE_0 (0 | SPI_CPHA)
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
static int GPIOExport(int pin);
static int GPIOUnexport(int pin);
static int GPIODirection(int pin, int dir);
static int GPIORead(int pin);
static int GPIOWrite(int pin, int value);
static uint32_t fillOutBuffer(uint32_t hour, uint32_t minute);
void writeTime(uint32_t buffer);
void initSPI();
int initGPIO();
int enableOutput();
int disableOutput();
int freeGPIO();
void *UpdateTimeOutput(void *);
int msleep(long msec);
uint8_t TimeConverterTable[4][10] = {{3, 1, 2, 0, 0, 0, 0, 0, 0, 0}, {13, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {21, 15, 16, 17, 18, 19, 20, 0, 0, 0}, {31, 22, 23, 24, 25, 26, 27, 28, 29, 30}};
time_t t = NULL;
int fd_spi;
unsigned int speed = 500000;
uint8_t mode = SPI_MODE_0;
int IsOutputEnabled = 0;
int main()
{
if (system("ping -c1 -s1 8.8.8.8"))
{
printf("There is no internet connection \n");
//return;
}
else
{
printf("Connected to internet\n");
}
uint32_t prevMin = 0;
initGPIO();
initSPI();
enableOutput();
while (1)
{
time(&t);
struct tm Tm = *localtime(&t);
if (Tm.tm_min % 10 == 0 && prevMin !=Tm.tm_min)
{
printf("yo\n");
prevMin = Tm.tm_min;
loopHours(Tm.tm_hour, Tm.tm_min, (Tm.tm_min/10+1)%2);
}
writeTime(fillOutBuffer(Tm.tm_hour, Tm.tm_min));
sleep(1);
}
disableOutput();
freeGPIO();
return 0;
}
void loopHours(uint32_t hour, uint32_t minute, int dir)
{
printf("looping\n");
for (int ctr = 0 ; ctr<= 144; ctr++)
{
int i = ctr;
if(dir == 0) i = 144-ctr;
int j = (int)((float)i*60.0/144.0);
//printf("%d:%d\n",(i/6+hour)%24, ((j%60)+minute)%60);
writeTime(fillOutBuffer((i/6+hour)%24, ((j%60)+minute)%60));
msleep(10);
}
}
void *UpdateTimeOutput(void *retval)
{
time(&t);
struct tm Tm = *localtime(&t);
writeTime(fillOutBuffer(Tm.tm_hour, Tm.tm_min));
sleep(1);
}
uint32_t fillOutBuffer(uint32_t hour, uint32_t minute)
{
return (uint32_t)1 << (TimeConverterTable[0][hour / 10]) | (uint32_t)1 << (TimeConverterTable[1][hour % 10]) | (uint32_t)1 << (TimeConverterTable[2][minute / 10]) | (uint32_t)1 << (TimeConverterTable[3][minute % 10]);
}
void writeTime(uint32_t buffer)
{
if (!IsOutputEnabled)
enableOutput();
uint8_t OutBuffer[4];
uint32_t buffer2 = 0;
for (int i = 0; i < 4; i++)
{
OutBuffer[3 - i] = ((uint8_t)(buffer >> (i * 8) & 0xFF));
}
write(fd_spi, OutBuffer, 4);
}
int freeGPIO()
{
/*
* Disable GPIO pins
*/
if (-1 == GPIOUnexport(LED_GPIO) || -1 == GPIOUnexport(DIMMING_GPIO) || -1 == GPIOUnexport(BLANKING_GPIO) || -1 == GPIOUnexport(HV_EN_GPIO))
return (4);
return 0;
}
static int GPIOExport(int pin)
{
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd)
{
fprintf(stderr, "Failed to open export for writing!\n");
return (-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return (0);
}
static int GPIOUnexport(int pin)
{
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd)
{
fprintf(stderr, "Failed to open unexport for writing!\n");
return (-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return (0);
}
static int GPIODirection(int pin, int dir)
{
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX];
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd)
{
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return (-1);
}
if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3))
{
fprintf(stderr, "Failed to set direction!\n");
return (-1);
}
close(fd);
return (0);
}
static int GPIORead(int pin)
{
#define VALUE_MAX 30
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd)
{
fprintf(stderr, "Failed to open gpio value for reading!\n");
return (-1);
}
if (-1 == read(fd, value_str, 3))
{
fprintf(stderr, "Failed to read value!\n");
return (-1);
}
close(fd);
return (atoi(value_str));
}
static int GPIOWrite(int pin, int value)
{
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd)
{
fprintf(stderr, "Failed to open gpio value for writing!\n");
return (-1);
}
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1))
{
fprintf(stderr, "Failed to write value!\n");
return (-1);
}
close(fd);
return (0);
}
int enableOutput()
{
if (-1 == GPIOWrite(BLANKING_GPIO, HIGH) || -1 == GPIOWrite(HV_EN_GPIO, LOW) || -1 == GPIOWrite(DIMMING_GPIO, HIGH))
return (3);
printf("Turning on !\n");
IsOutputEnabled = 1;
}
int disableOutput()
{
if (-1 == GPIOWrite(BLANKING_GPIO, LOW) || -1 == GPIOWrite(HV_EN_GPIO, HIGH) || -1 == GPIOWrite(DIMMING_GPIO, LOW))
return (3);
printf("Turning off\n");
IsOutputEnabled = 0;
}
int initGPIO()
{
/*
* Enable GPIO pins
*/
if (-1 == GPIOExport(LED_GPIO) || -1 == GPIOExport(DIMMING_GPIO) || -1 == GPIOExport(BLANKING_GPIO) || -1 == GPIOExport(HV_EN_GPIO))
return (1);
sleep(1);
/*
* Set GPIO directions
*/
if (-1 == GPIODirection(LED_GPIO, OUT) || -1 == GPIODirection(DIMMING_GPIO, OUT) || -1 == GPIODirection(BLANKING_GPIO, OUT) || -1 == GPIODirection(HV_EN_GPIO, OUT))
return (2);
sleep(1);
}
void initSPI()
{
fd_spi = open("/dev/spidev0.0", O_RDWR);
if (ioctl(fd_spi, SPI_IOC_WR_MAX_SPEED_HZ, &speed) != 0)
{
perror("ioctl");
exit(EXIT_FAILURE);
}
if (ioctl(fd_spi, SPI_IOC_WR_MODE, &mode) != 0)
{
perror("ioctl");
exit(EXIT_FAILURE);
}
}
/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0)
{
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}