altosui: Add ao_lcd_cursor_on/off
[fw/altos] / src / drivers / ao_lcd.c
1 /*
2  * Copyright © 2011 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 uint16_t ao_lcd_time = 3;
21
22 static __xdata uint8_t  ao_lcd_mutex;
23
24 static void
25 ao_lcd_delay(void)
26 {
27         volatile uint16_t       count;
28
29         for (count = 0; count < ao_lcd_time; count++)
30                 ;
31 }
32
33 static void
34 ao_lcd_send_ins(uint8_t ins)
35 {
36 //      printf("send ins %02x\n", ins);
37 //      ao_lcd_wait_idle();
38 //      ao_delay(1);
39         ao_lcd_delay();
40         ao_lcd_port_put_nibble(0, ins >> 4);
41         ao_lcd_port_put_nibble(0, ins & 0xf);
42 }
43
44 static void
45 ao_lcd_put_byte(uint8_t c)
46 {
47 //      printf ("send data %02x\n", c);
48 //      ao_lcd_wait_idle();
49         ao_lcd_delay();
50         ao_lcd_port_put_nibble(1, c >> 4);
51         ao_lcd_port_put_nibble(1, c & 0x0f);
52 }
53
54 void
55 ao_lcd_putstring(char *string)
56 {
57         char    c;
58
59         ao_mutex_get(&ao_lcd_mutex);
60         while ((c = (uint8_t) *string++))
61                 ao_lcd_put_byte((uint8_t) c);
62         ao_mutex_put(&ao_lcd_mutex);
63 }
64
65 #define AO_LCD_POWER_CONTROL    0x54
66
67 void
68 ao_lcd_contrast_set(uint8_t contrast)
69 {
70         ao_mutex_get(&ao_lcd_mutex);
71         ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
72         ao_lcd_send_ins(0x70 | (contrast & 0xf));
73         ao_mutex_put(&ao_lcd_mutex);
74 }
75
76 void
77 ao_lcd_cursor_on(void)
78 {
79         ao_mutex_get(&ao_lcd_mutex);
80         ao_lcd_send_ins(0x08 | 0x04 | 0x02 | 0x01);
81         ao_mutex_put(&ao_lcd_mutex);
82 }
83
84 void
85 ao_lcd_cursor_off(void)
86 {
87         ao_mutex_get(&ao_lcd_mutex);
88         ao_lcd_send_ins(0x08 | 0x04);
89         ao_mutex_put(&ao_lcd_mutex);
90 }
91
92 void
93 ao_lcd_clear(void)
94 {
95         ao_mutex_get(&ao_lcd_mutex);
96         ao_lcd_send_ins(0x01);
97         ao_delay(1);
98         /* Entry mode */
99         ao_lcd_send_ins(0x04 | 0x02);
100         ao_mutex_put(&ao_lcd_mutex);
101 }
102
103 void
104 ao_lcd_goto(uint8_t addr)
105 {
106         ao_mutex_get(&ao_lcd_mutex);
107         ao_lcd_send_ins(0x80 | addr);
108         ao_lcd_send_ins(0x04 | 0x02);
109         ao_mutex_put(&ao_lcd_mutex);
110 }
111
112 void
113 ao_lcd_start(void)
114 {
115         /* get to 4bit mode */
116         ao_lcd_port_put_nibble(0, 0x3);
117         ao_lcd_port_put_nibble(0, 0x3);
118         ao_lcd_port_put_nibble(0, 0x3);
119         ao_lcd_port_put_nibble(0, 0x2);
120
121         /* function set */
122         ao_lcd_send_ins(0x28);
123         /* function set, instruction table 1 */
124         ao_lcd_send_ins(0x29);
125
126         /* freq set */
127         ao_lcd_send_ins(0x14);
128
129         /* Power/icon/contrast control*/
130         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
131
132         /* Follower control */
133         ao_lcd_send_ins(0x6d);
134         ao_delay(AO_MS_TO_TICKS(200));
135
136         /* contrast set */
137         ao_lcd_contrast_set(0x18);
138
139         /* Display on */
140         ao_lcd_send_ins(0x08 | 0x04);
141
142         /* Clear */
143         ao_lcd_clear();
144 }
145
146 void
147 ao_lcd_contrast(void)
148 {
149         ao_cmd_hex();
150         if (ao_cmd_status == ao_cmd_success) {
151                 printf("setting contrast to %02x\n", ao_cmd_lex_i);
152                 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
153         }
154 }
155
156 static uint8_t
157 ao_cmd_hex_nibble(void)
158 {
159         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
160                 return ao_cmd_lex_c - '0';
161         if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
162                 return ao_cmd_lex_c - ('a' - 10);
163         if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
164                 return ao_cmd_lex_c - ('A' - 10);
165         ao_cmd_status = ao_cmd_syntax_error;
166         return 0;
167 }
168
169 void
170 ao_lcd_string(void)
171 {
172         uint8_t col = 0;
173         char    c;
174
175         ao_cmd_decimal();
176         if (ao_cmd_status != ao_cmd_success)
177                 return;
178         ao_lcd_send_ins(0x80 | (ao_cmd_lex_i ? 0x40 : 0x00));
179         ao_cmd_white();
180         while (ao_cmd_lex_c != '\n') {
181                 c = ao_cmd_lex_c;
182                 if (c == '\\') {
183                         ao_cmd_lex();
184                         c = ao_cmd_hex_nibble() << 4;
185                         ao_cmd_lex();
186                         c |= ao_cmd_hex_nibble();
187                 }
188                 ao_lcd_put_byte(c);
189                 ao_cmd_lex();
190                 col++;
191         }
192         while (col < 16) {
193                 ao_lcd_put_byte(' ');
194                 col++;
195         }
196 }
197
198 void
199 ao_lcd_delay_set(void)
200 {
201         ao_cmd_decimal();
202         if (ao_cmd_status == ao_cmd_success) {
203                 printf("setting LCD delay to %d\n", ao_cmd_lex_i);
204                 ao_lcd_time = ao_cmd_lex_i;
205         }
206 }
207
208 __code struct ao_cmds ao_lcd_cmds[] = {
209         { ao_lcd_start, "S\0Start LCD" },
210         { ao_lcd_contrast, "C <contrast>\0Set LCD contrast" },
211         { ao_lcd_string, "s <line> <string>\0Send string to LCD" },
212         { ao_lcd_delay_set, "t <delay>\0Set LCD delay" },
213         { 0, NULL },
214 };
215
216 void
217 ao_lcd_init(void)
218 {
219         ao_lcd_port_init();
220         ao_cmd_register(&ao_lcd_cmds[0]);
221 }