-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonohorn.c
122 lines (101 loc) · 2.51 KB
/
monohorn.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include "lo/lo.h"
#include "matrix.h"
#define SOCKET_PATH "/tmp/monohorn.socket"
static void mh_cleanup() {
printf("Shutting down\n");
matrix_clear();
matrix_end();
exit(1);
}
static void mh_sigint(int sig) {
mh_cleanup();
}
int chmod_socket(const char *path) {
return chmod(path, S_IWGRP | S_IWOTH);
}
void mh_osc_error(int num, const char *msg, const char *path);
int mh_osc_set(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
);
int mh_osc_render(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
);
int mh_osc_clear(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
);
int main(int argc, char *argv[]) {
// Catch signals
signal(SIGINT, mh_sigint);
signal(SIGTERM, mh_sigint);
// Initialise LEDs
if (matrix_init() < 0) return 1;
// Set up OSC server
const char *path;
lo_server_thread st;
int chmod_err = 0;
if (argc > 1) {
path = argv[1];
} else {
path = SOCKET_PATH;
}
unlink(path);
st = lo_server_thread_new(path, mh_osc_error);
chmod_err = chmod_socket(path);
if (chmod_err != 0) {
fprintf(stderr, "Error changing socket permissions\n");
return 1;
}
// Set up OSC message handlers
lo_server_thread_add_method(st, "/set", "iii", mh_osc_set, NULL);
lo_server_thread_add_method(st, "/render", NULL, mh_osc_render, NULL);
lo_server_thread_add_method(st, "/clear", NULL, mh_osc_clear, NULL);
lo_server_thread_start(st);
// Ready
printf("Listening on socket: %s\n", path);
while (1) {
usleep(1000);
}
// Cleanup
lo_server_thread_free(st);
mh_cleanup();
return 0;
}
void mh_osc_error(int num, const char *msg, const char *path) {
fprintf(stderr, "mh_osc_error [%d] (%s) %s\n", num, path, msg);
fflush(stderr);
}
int mh_osc_set(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
)
{
int x = argv[0]->i;
int y = argv[1]->i;
ws2811_led_t colour = (ws2811_led_t)(argv[2]->i);
matrix_set(x, y, colour);
return 0;
}
int mh_osc_render(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
)
{
matrix_render();
return 0;
}
int mh_osc_clear(
const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data
)
{
matrix_clear();
return 0;
}