-
Notifications
You must be signed in to change notification settings - Fork 3
/
text.c
384 lines (334 loc) · 8.01 KB
/
text.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* This code contains a port of the text management functions from:
*
* 8 x 8 x 8 Cube Application Template, Version 7.0 © 2014 by Doug Domke
* Downloads of this template and upcoming versions, along with detailed
* instructions, are available at: http://d2-webdesign.com/cube
*
* Many of the functions have been rewritten or optimised in the process.
*
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "cube.h"
#include "tables.h"
#include "text.h"
#include "util.h"
#include "include/font.h"
// 46 columns (30 around the cube,
// 0-7 are for the character which has just left,
// 37-45 are for a character yet to come on,
// 8 rows
static char text_buffer[46][8];
int scroll_rate = 800;
void
text_getchar(char c, uint8_t (*grid)[8][8][3],
uint8_t red, uint8_t green, uint8_t blue)
{
int offset, x, y;
memset(*grid, '\0', sizeof(*grid));
if (c < ' ' || c > 127)
return;
offset = (c - ' ') * 8;
// Place the character in the grid.
for (x = 0; x < 8; x++)
{
for (y = 0; y < 8; y++)
{
if (myfont[offset+x][y])
{
(*grid)[x][7-y][RED] = red;
(*grid)[x][7-y][GREEN] = green;
(*grid)[x][7-y][BLUE] = blue;
}
}
}
}
static void
text_scroll_character(uint8_t red, uint8_t green, uint8_t blue, int mode)
{
int shift, col, row;
// 8 columns to scroll across for one character.
for (shift = 0; shift < 8; shift++)
{
// Shift the text buffer down by one column.
for (col = 0; col < 45; col++)
for (row = 0; row < 8; row++)
text_buffer[col][row] = text_buffer[col+1][row];
// Draw the new walls.
for (col = 0; col < 8; col++)
{
for (row = 0; row < 8; row++)
{
if (text_buffer[col+22][row])
LED(7, col, row, red, green, blue);
if (text_buffer[col+15][row])
LED(col, 0, row, red, green, blue);
if (mode == 4)
{
if (text_buffer[col+29][row])
LED(7-col, 7, row, red, green, blue);
if (text_buffer[col+8][row])
LED(0, 7-col, row, red, green, blue);
}
}
}
delay(scroll_rate / 8);
// Clear all four walls.
cube_slice(0, 0, 0, 0, 0);
cube_slice(7, 0, 0, 0, 0);
cube_panel(0, 0, 0, 0, 0);
cube_panel(7, 0, 0, 0, 0);
}
}
/* This subroutine scrolls text around the outside walls of the cube.
* You first specify a string of text.
* Then specify its color as red, green, blue components.
* Finally specify Mode, which is 2 for scolling on only 2 walls of the
* or 4 for scrolling on all 4 walls.
*/
static void
text_scroll_walls(char *str, uint8_t red, uint8_t green, uint8_t blue, int mode)
{
int col, row, count;
for (; *str; str++)
{
// Get font code.
int offset = (*str - ' ') * 8;
for (col = 0; col < 8; col++)
{
for (row = 0; row < 8; row++)
{
if (myfont[offset+col][row])
text_buffer[col + 37][row] = 1;
}
}
text_scroll_character(red, green, blue, mode);
}
// Last character, four walls.
for (count = 0; count < 4; count++)
text_scroll_character(red, green, blue, mode);
delay(1000);
// Clear the text buffer
memset(text_buffer, '\0', sizeof(text_buffer));
}
enum textscrolldir {X, Y, Z};
static void
text_scroll_through(char *str, uint8_t red, uint8_t green, uint8_t blue,
enum textscrolldir dir)
{
uint8_t grid[8][8][3];
int x, y, sx, sy, sz;
sx = sy = sz = 0;
switch (dir)
{
case X: sx = 1; break;
case Y: sy = 1; break;
case Z: sz = -1; break;
}
for (; *str; str++)
{
// Place the character in the grid.
text_getchar(*str, &grid, red, green, blue);
// Place the character grid on the correct cube face
// and scroll it through.
switch (dir)
{
case X:
cube_plane(0, PLANE_SLICE, 0, &grid);
break;
case Y:
cube_plane(0, PLANE_PANEL, 0, &grid);
break;
case Z:
cube_plane(7, PLANE_LAYER, 0, &grid);
break;
}
for (x = 0; x < 5; x++)
{
delay(scroll_rate / 8);
cube_translate(sx, sy, sz);
}
}
// Last character
for (x = 0; x < 4; x++)
{
delay(scroll_rate / 8);
cube_translate(sx, sy, sz);
}
delay(1000);
}
void
text_scroll(char *str, uint8_t red, uint8_t green, uint8_t blue,
enum textmode mode)
{
memset(text_buffer, '\0', sizeof(text_buffer));
switch (mode)
{
case TEXTMODE_TWOWALLS:
text_scroll_walls(str, red, green, blue, 2);
break;
case TEXTMODE_FOURWALLS:
text_scroll_walls(str, red, green, blue, 4);
break;
case TEXTMODE_THROUGHX:
text_scroll_through(str, red, green, blue, X);
break;
case TEXTMODE_THROUGHY:
text_scroll_through(str, red, green, blue, Y);
break;
case TEXTMODE_THROUGHZ:
text_scroll_through(str, red, green, blue, Z);
break;
}
}
#if 0
void
rotate(char *str, uint8_t red, uint8_t green, uint8_t blue, int num)
{
int col, row, panel, rotation;
for (; *str; str++)
{
int offset = (*str - ' ') * 8;
// Populate text buffer
for (col = 0; col < 46; col++)
for (row = 0; row < 8; row++)
if (myfont[offset+col][row])
text_buffer[col][row] = 1;
else
text_buffer[col][row] = 0;
for (rotation = 0; rotation < num; rotation++)
{
// Display in first position
for (panel = 0; panel < 8; panel++)
{
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(3, panel+1, row, red, green, blue);
LED(4, panel+1, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 45 degrees
for (panel = 0; panel < 7; panel++)
{
float mypanel = 1 + (float)panel / 1.414;
int panelx = mypanel;
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(panelx+1, panelx+1, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 90 degrees
for (panel = 0; panel < 7; panel++)
{
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(panel+1, 3, row, red, green, blue);
LED(panel+1, 4, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 135 degrees
for (panel = 0; panel < 7; panel++)
{
float mypanel = 1 + (float)panel / 1.414;
int panelx = mypanel;
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(panelx+1, 6-panelx, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 180 degrees
for (panel = 0; panel < 7; panel++)
{
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(3, 6 - panel, row, red, green, blue);
LED(4, 6 - panel, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 225 degrees
for (panel = 0; panel < 7; panel++)
{
float mypanel = 1 + (float)panel / 1.414;
int panelx = mypanel;
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(6-panelx, 6-panelx, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 270 degrees
for (panel = 0; panel < 7; panel++)
{
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(6-panel, 3, row, red, green, blue);
LED(6-panel, 4, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// Rotate 315 degrees
for (panel = 0; panel < 7; panel++)
{
float mypanel = 1 + (float)panel / 1.414;
int panelx = mypanel;
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(6-panelx, panelx+1, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
// and back in its original position
for (panel = 0; panel < 8; panel++)
{
for (row = 0; row < 8; row++)
{
if (!text_buffer[panel][row])
continue;
LED(3, panel+1, row, red, green, blue);
LED(4, panel+1, row, red, green, blue);
}
}
delay(scroll_rate / 4);
clearCube();
delay(500);
}
}
// Last character
delay(1000);
}
#endif