-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
124 lines (89 loc) · 2.46 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
#include <stdint.h>
#include <unistd.h>
#include <math.h>
#include <ev.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "npspi.h"
#define FPS 30
#define PERIOD (1.0 / FPS)
#ifndef N_PIXELS
#define N_PIXELS 64
#endif
#define BRIGHTNESS_MIN 0
#define BRIGHTNESS_MAX 0.4
struct animation_ctx {
ev_timer timer;
NpSpiContext *npspi_ctx;
GdkPixbuf *pixbuf;
};
static void
frame_cb (EV_P_ ev_timer *const timer, int const revents)
{
struct animation_ctx *const animation = (struct animation_ctx *) timer;
NpSpiContext *const npspi = animation->npspi_ctx;
ev_tstamp const now = ev_now (loop);
double const value = pow (sin (now), 2);
double const brightness = BRIGHTNESS_MIN + (value * BRIGHTNESS_MAX);
double const brightness_inv = BRIGHTNESS_MIN + ((1 - value) * BRIGHTNESS_MAX);
for (int i = 0; i < N_PIXELS; i++) {
uint8_t const level = (uint8_t) (0xff * brightness);
uint8_t const level_inv = (uint8_t) (0xff * brightness_inv);
switch (i % 3) {
case 0:
npspi_set_color (npspi, i, level);
break;
case 1:
npspi_set_color (npspi, i, (level_inv << 16) | level_inv);
break;
case 2:
npspi_set_color (npspi, i, (level << 8) | level_inv);
break;
}
}
npspi_show (npspi);
}
static void
image_cb (EV_P_ ev_timer *const timer, int const revents)
{
struct animation_ctx *const animation = (struct animation_ctx *) timer;
NpSpiContext *const npspi = animation->npspi_ctx;
GdkPixbuf *const pixbuf = animation->pixbuf;
uint8_t const *const pixels = gdk_pixbuf_read_pixels (pixbuf);
size_t const pixels_length = (size_t) gdk_pixbuf_get_byte_length (pixbuf);
for (int i = 0; i < N_PIXELS; i++) {
uint8_t const *const p = &(pixels[i * 4]);
npspi_set_color (
npspi,
i,
(p[0] << 16) | (p[1] << 8) | (p[2])
);
}
npspi_show (npspi);
}
int
main (void)
{
struct ev_loop *const loop = EV_DEFAULT;
NpSpiContext *const ctx = npspi_new (N_PIXELS);
struct animation_ctx animation = {
.npspi_ctx = ctx,
};
ev_timer *const timer = &(animation.timer);
npspi_open (ctx);
#if 0
ev_timer_init (timer, frame_cb, 0, PERIOD);
ev_timer_start (loop, timer);
ev_run (loop, 0);
#else
g_autoptr(GdkPixbuf) pixbuf = gdk_pixbuf_new_from_file (
"/home/joe/display.png",
NULL
);
animation.pixbuf = pixbuf;
ev_timer_init (timer, image_cb, 0, PERIOD);
ev_timer_start (loop, timer);
ev_run (loop, 0);
#endif
npspi_free (ctx);
return 0;
}