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
|
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h> /* for perror() */
#include <string.h> /* for memcpy() */
#include <stdlib.h> /* for realloc(), free() */
#include <unistd.h> /* for write() */
#define LM_ARRAY_LEN(ary) (sizeof(ary)/sizeof((ary)[0]))
#define LM_CEILDIV(n, d) ( ((n)+(d)-1) / (d) )
#define LM_NEXT_POWER_OF_2(x) ( (x) ? 1ULL<<((sizeof(unsigned long long)*8)-__builtin_clzll(x)) : 1)
#define LM_CAT2(a, b) a ## b
#define LM_CAT2_(a, b) LM_CAT2(a, b)
#define assert_notreached(msg) assert(false)
struct buf {
uint8_t *dat;
size_t len;
size_t cap;
};
static void append(struct buf *buf, void *dat, size_t len) {
assert(buf);
assert(len == 0 || dat);
if (buf->len + len > buf->cap) {
buf->cap = LM_NEXT_POWER_OF_2(buf->len + len);
buf->dat = realloc(buf->dat, buf->cap);
assert(buf->dat);
}
memcpy(&buf->dat[buf->len], dat, len);
buf->len += len;
}
static void append_u32be(struct buf *buf, uint32_t val) {
assert(buf);
uint8_t dat[4] = {
(val>>(8*3))&0xFF,
(val>>(8*2))&0xFF,
(val>>(8*1))&0xFF,
(val>>(8*0))&0xFF,
};
append(buf, dat, sizeof(dat));
}
static bool xwrite(int fd, uint8_t *dat, size_t len) {
assert(len == 0 || dat);
for (size_t done = 0; done < len;) {
ssize_t n = write(fd, &dat[done], len-done);
if (n < 0) {
perror("write");
return true;
}
done += n;
}
return false;
}
static char font[10][8*4] = {
" ## "
" # # "
" # # "
" ## ",
" # "
" ## "
" # "
" ### ",
" ## "
" # # "
" # "
" #### ",
" ## "
" # "
" # "
" ## ",
" ## "
" # # "
" #### "
" # ",
" #### "
" ### "
" # "
" ### ",
" # "
" ### "
" # # "
" ## ",
" #### "
" # "
" # "
" # ",
" ## "
" # # "
" #### "
" ## ",
" ## "
" # # "
" ### "
" # ",
};
static_assert(LM_ARRAY_LEN(font) == 10);
static_assert(sizeof(font[0]) == 32);
|