-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
185 lines (155 loc) · 4.47 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
/*! \file */
#include <gif_lib.h> //https://sourceforge.net/projects/giflib/
#include <ws2811/ws2811.h> //https://github.com/jgarff/rpi_ws281x
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include "led.h"
#include "arguments.h"
#include "tasbot.h"
#include "palette.h"
#include "filesystem.h"
#include "network.h"
#include "stack.h"
void finish() {
printf("\n"); //pretty uwu
running = false;
if (activateLEDModule) {
clearLEDs();
ws2811_render(&display);
ws2811_fini(&display);
}
while (!isEmpty()) {
char* file = (char*) pop();
free(file);
}
free(buffer);
free(palette);
pthread_kill(serverInject, SIGKILL);
pthread_exit(NULL);
exit(EXIT_SUCCESS);
}
/**
* Central handler for leaving the application. Handler for SIGINT, SIGTERM and SIGKILL. Gets also called after the endless loop
* @param _number uwu
*/
void exitHandler(int _number) {
finish();
}
/**
* Register the handler for SIGINT, SIGTERM and SIGKILL
*/
void setupHandler() {
struct sigaction sa = {.sa_handler = exitHandler,};
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGKILL, &sa, NULL);
}
void initPalette() {
if (pathForPalette != NULL) {
readPalette(pathForPalette);
} else {
//Default palette
paletteCount = 8;
palette = malloc(sizeof(ws2811_led_t) * paletteCount);
if (!palette) {
fprintf(stderr, "[ERROR] Failed to allocate memory for palette");
clearLEDs();
exit(EXIT_FAILURE);
}
palette[0] = 0xFF0000; // red
palette[1] = 0xFF8000; // orange
palette[2] = 0xFFFF00; // yellow
palette[3] = 0x00FF00; // green
palette[4] = 0x0000FF; // cyan
palette[5] = 0xFF0000; // blue
palette[6] = 0xFF00FF; // magenta
palette[7] = 0xFF80FF; // pink
}
}
void initBlinking() {
minTimeBetweenBlinks *= 1000;
maxTimeBetweenBlinks *= 1000;
}
void specificAnimation() {
while (running) {
playAnimationFromFilepath(specificAnimationToShow, false, false);
}
}
void tasbotsEyes() {
bool firstIteration = true;
while (running) {
//play startup animation in first iteration
if (!firstIteration) {
if (isEmpty()) {
fillStack(pathForAnimations);
}
//Get animation from stack
char* file = (char*) pop();
//Play animation
playAnimationFromFilepath(file, useRandomColors, true);
free(file);
} else {
if (!skipStartupAnimation) {
playAnimationFromFilepath(STARTUP_PATH, false, false);
}
firstIteration = false;
}
//show base
if (maxBlinks != 0 && minTimeBetweenBlinks != 0) { //skip base, when no blinks at all
playAnimationFromFilepath(BASE_PATH, false, false);
}
//blink for a random amount of times
usleep(getBlinkDelay() * 1000);
for (unsigned int blinks = getBlinkAmount(); blinks > 0; --blinks) {
playRandomAnimationFromDirectory(pathForBlinks, false, false);
playAnimationFromFilepath(BASE_PATH, false, false);
unsigned int blinkTime = getBlinkDelay();
if (verbose) {
printf("[INFO] Blink #%d for %d milliseconds \n", blinks, blinkTime);
}
usleep(blinkTime * 1000);
}
}
}
int main(int _argc, char** _argv) {
srand(time(NULL));
//can't use LED hardware on desktops
#if defined(__x86_64__)
activateLEDModule = false;
#endif
//Init things
setupHandler();
parseArguments(_argc, _argv);
initPalette();
initBlinking();
initLEDs();
startAnimationInjectionServer();
//Start rendering
startRenderThread();
if (rainbowMode){
startHueThread();
}
if (useRealtimeControl){
if (verbose){
printf("[WARNING] Can't use verbose mode with realtime control, as the logging adds to much overhead. Turning it off\n");
verbose = false;
}
startRealtimeControlServer();
}
//Option for playing a given specific animation
if (specificAnimationToShow != NULL) {
specificAnimation();
} else {
//Main loop
tasbotsEyes();
}
while (running){}
//Clean up
finish();
return 0;
}