-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.c
More file actions
158 lines (141 loc) · 3.24 KB
/
ui.c
File metadata and controls
158 lines (141 loc) · 3.24 KB
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
#include "ui.h"
#include "ASCII-SMALL.h"
Window message_window;
char message_buffer[24];
void init_windows( void )
{
for(uint8_t i=0 ; i<MAX_WINDOWS ; i++)
{
windows[i] = 0;
}
}
bool show_window(Window* w, uint16_t lifetime)
{
if (lifetime > 0)
w->timer = millis() + lifetime;
else
w->timer = 0;
for(uint8_t i=0 ; i<MAX_WINDOWS ; i++)
{
if (windows[i] == 0)
{
windows[i] = w;
return TRUE;
}
}
return FALSE;
}
void close_window(Window* w)
{
w->timer = 1;
}
void update_windows( void )
{
for(uint8_t i=0 ; i<MAX_WINDOWS ; i++)
{
if (windows[i] != 0 && windows[i]->timer > 0 && windows[i]->timer <= millis())
{
windows[i] = 0;
}
}
}
void draw_windows( void )
{
for(uint8_t i=0 ; i<MAX_WINDOWS ; i++)
{
if (windows[i] != 0)
{
draw_window(windows[i]);
}
}
}
void draw_window(Window* w)
{
uint8_t tile;
for(uint8_t y=0 ; y<w->h ; y++)
{
for(uint8_t x=0 ; x<w->w ; x++)
{
if (x==0 && y==0)
tile = WIN_TL;
else if (x==w->w-1 && y==0)
tile = WIN_TR;
else if (x==0 && y==w->h-1)
tile = WIN_BL;
else if (x==w->w-1 && y==w->h-1)
tile = WIN_BR;
else if (x==0)
tile = WIN_L;
else if (x==w->w-1)
tile = WIN_R;
else if (y==0)
tile = WIN_T;
else if (y==w->h-1)
tile = WIN_B;
else
tile = WIN_EM;
draw_tile(&WINDOW_TILES[tile], &WINDOW_TILE_MASKS[tile], (w->x+x)*8, (w->y+y)*8, FALSE);
}
}
//TODO: deal with \n in content
w->_draw(w);
//draw_string(w->content, (w->x+1)*8, (w->y+1)*8);
}
void draw_small_string(const __memx char *string, int16_t x, int16_t y)
{
for(uint8_t i=0 ; string[i] != '\0' ; i++)
{
draw_tile(&SMALL_ASCII[(string[i]-32)*8], &SMALL_CHAR_MASK[0], x+(i*4), y, FALSE);
}
}
void draw_small_int(int16_t n, uint8_t width, int16_t x, int16_t y)
{
//TODO: Negative numbers
if (width == 0)
width = num_digits(n);
int32_t n_;
for (uint8_t i=0 ; i<width ; i++)
{
n_ = (6554*(int32_t)n)>>16;
draw_tile(&SMALL_DIGITS[(n - (n_*10))*8], &SMALL_CHAR_MASK[0], x+((width-i-1)*4), y, FALSE);
n = (int16_t)n_;
}
}
uint8_t num_digits(int16_t n)
{
//TODO: negative numbers
if (n < 10)
return 1;
if (n < 100)
return 2;
if (n < 1000)
return 3;
if (n < 10000)
return 4;
return 5;
}
void show_message(const __memx char* message)
{
uint8_t len = 0;
for(uint8_t i=0 ; message[i] != '\0' ; i++)
{
message_buffer[i] = message[i];
len += 1;
}
message_buffer[len] = '\0';
if (len & 1)
len += 1;
len = (len>>1)+2;
message_window = (Window){
.x=8 - (len>>1),
.y=3,
.w=len,
.h=3,
._draw=draw_message_window,
};
show_window(&message_window, THREE_SECONDS);
}
void draw_message_window(Window* w)
{
draw_small_string(&message_buffer[0], (w->x+1)*8, (w->y+1)*8+2);
}