Switch from GPLv2 to GPLv2+
[fw/altos] / src / stm / ao_lcd_font.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20
21 static const uint16_t ao_lcd_font[] = {
22 #include "ao_lcd_font.h"
23 };
24
25 /*
26          -----          0
27         |\ | /|         1 2 3 4 5
28         | \|/ | @               14
29          -- --          6 7
30         | /|\ |         8 9 10 11 12    
31         |/ | \| @               14
32          -----          13
33                 @               15
34 */
35
36 static const uint8_t    ao_bit_src[] = {
37          8,  7,  5,  6,
38         13, 12,  0,  1,
39         10, 14,  4,  9,
40         11, 15,  3,  2
41 };
42
43 static const uint8_t    ao_bit_dst[6][4] = {
44         {  0,  1, 28, 29 },
45         {  2,  7, 26, 27 },
46         {  8,  9, 24, 25 },
47         { 10, 11, 20, 21 },
48         { 12, 13, 18, 19 },
49         { 14, 15, 17, 16 },
50 };
51
52 static void
53 ao_draw_glyph(uint8_t pos, uint16_t glyph) {
54         uint8_t         col, seg, src, dst;
55         uint32_t        ram;
56
57         for (col = 0; col < 4; col++) {
58                 for (seg = 0; seg < 4; seg++) {
59                         src = ao_bit_src[(col << 2) | seg];
60                         dst = ao_bit_dst[pos][seg];
61                         ram = stm_lcd.ram[col * 2];
62                         ram &= ~(1 << dst);
63                         ram |= (uint32_t) ((glyph >> src) & 1) << dst;
64                         stm_lcd.ram[col*2] = ram;
65                 }
66         }
67 }
68
69 #define AO_LCD_FONT_COLON       (1 << 14)
70 #define AO_LCD_FONT_DECIMAL     (1 << 15)
71
72 void
73 ao_lcd_font_char(uint8_t pos, char c, uint16_t flags) {
74         if (pos > 5)
75                 pos = 5;
76         if (c < ' ' || c > 127)
77                 c = 127;
78         ao_draw_glyph(pos, ao_lcd_font[c - ' '] | flags);
79 }
80
81 void
82 ao_lcd_font_string(char *s) {
83         uint8_t pos = 0;
84         uint16_t flags;
85         char c;
86
87         while (pos < 6 && (c = *s++)) {
88                 flags = 0;
89                 for (;;) {
90                         if (*s == ':')
91                                 flags |= AO_LCD_FONT_COLON;
92                         else if (*s == '.')
93                                 flags |= AO_LCD_FONT_DECIMAL;
94                         else
95                                 break;
96                         s++;
97                 }
98                 ao_lcd_font_char(pos++, c, flags);
99         }
100         while (pos < 6)
101                 ao_lcd_font_char(pos++, ' ', 0);
102         stm_lcd.sr = (1 << STM_LCD_SR_UDR);
103 }
104
105 static void
106 ao_lcd_font_text(void)
107 {
108         char    string[20];
109         uint8_t c = 0;
110         ao_cmd_white();
111         while (ao_cmd_lex_c != '\n' && c < sizeof (string) - 1) {
112                 string[c++] = ao_cmd_lex_c;
113                 ao_cmd_lex();
114         }
115         string[c++] = '\0';
116         ao_lcd_font_string(string);
117 }
118
119 const struct ao_cmds ao_lcd_font_cmds[] = {
120         { ao_lcd_font_text,     "t <string>\0Write <string> to LCD" },
121         { 0, NULL }
122 };
123
124 void
125 ao_lcd_font_init(void)
126 {
127         ao_cmd_register(ao_lcd_font_cmds);
128 }
129