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