altos/telelco-v3.0: Control LCD backlight with PWM
[fw/altos] / src / telelco-v3.0 / ao_telelco.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; 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 #include <ao_exti.h>
21 #include <ao_packet.h>
22 #include <ao_companion.h>
23 #include <ao_aes.h>
24 #include <ao_quadrature.h>
25 #include <ao_button.h>
26 #include <ao_lco.h>
27 #include <ao_lco_cmd.h>
28 #include <ao_radio_cmac_cmd.h>
29 #include <ao_eeprom.h>
30 #include <ao_adc_single.h>
31 #include <ao_st7565.h>
32 #include <ao_pwm.h>
33
34 #define WIDTH   AO_ST7565_WIDTH
35 #define HEIGHT  AO_ST7565_HEIGHT
36 #define STRIDE  AO_BITMAP_STRIDE(WIDTH)
37
38 static uint32_t image[STRIDE * HEIGHT];
39
40 static struct ao_bitmap fb = {
41         .base = image,
42         .stride = STRIDE,
43         .width = WIDTH,
44         .height = HEIGHT,
45         .damage = AO_BOX_INIT,
46 };
47
48 static void
49 ao_st7565_test(void)
50 {
51         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
52         ao_st7565_update(&fb);
53         ao_text(&fb, &BitstreamVeraSans_Roman_24_font,
54                 0, 20, "hello world", AO_BLACK, AO_COPY);
55         ao_st7565_update(&fb);
56 }
57
58 static int16_t  x1 = 32, _y1 = 10, x2 = 32, y2 = 40;
59 static int16_t  dx1 = 2, dy1 = 2, dx2 = -2, dy2 = -1;
60
61 #define bounds(v,m,M,d) \
62                 if (v < m) {                    \
63                         v = m + m - v;          \
64                         d = -d;                 \
65                 } else if (v > M) {             \
66                         v = M - (v - M);        \
67                         d = -d;                 \
68                 }
69
70 static void
71 ao_st7565_line(void)
72 {
73         int     i;
74
75         for (i = 0; i < 100; i++) {
76                 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
77                 ao_line(&fb, x1, _y1, x2, y2, AO_BLACK, AO_COPY);
78                 ao_st7565_update(&fb);
79                 x1 += dx1;
80                 _y1 += dy1;
81                 x2 += dx2;
82                 y2 += dy2;
83                 printf("%d,%d - %d,%d\n", x1, _y1, x2, y2);
84                 fflush(stdout);
85                 bounds(x1, 0, WIDTH, dx1);
86                 bounds(x2, 0, WIDTH, dx2);
87                 bounds(_y1, 0, HEIGHT, dy1);
88                 bounds(y2, 0, HEIGHT, dy2);
89                 ao_delay(AO_MS_TO_TICKS(200));
90         }
91 }
92
93 static const float pad_volts = 12.3f;
94 static const float lco_volts = 4.1f;
95 static const int rssi = -30;
96
97 static int      boxes[] = { 1, 2, 3, 5, 8, 11, 13, 17, 19, 23, 29, 31, 37, 62, 97 };
98
99 //static int    max_box = 97;
100
101 #define ARRAYSIZE(a)    (sizeof(a) / sizeof((a)[0]))
102
103 static bool
104 valid_box(int box)
105 {
106         size_t i;
107         if (box == 0)
108                 return true;
109         for (i = 0; i < ARRAYSIZE(boxes); i++)
110                 if (boxes[i] == box)
111                         return true;
112         return false;
113 }
114
115 #if 0
116 static void
117 next_box(void)
118 {
119         for (int n = box_number + 1; n <= max_box; n++)
120                 if (valid_box(n)) {
121                         box_number = n;
122                         return;
123                 }
124         box_number = 0;
125 }
126
127 static void
128 prev_box(void)
129 {
130         for (int n = box_number - 1; n >= 0; n--)
131                 if (valid_box(n)) {
132                         box_number = n;
133                         return;
134                 }
135         box_number = max_box;
136 }
137 #endif
138
139 static const struct ao_transform logo_transform = {
140         .x_scale = 48, .x_off = 2,
141         .y_scale = 48, .y_off = 0,
142 };
143
144 #define BIG_FONT BitstreamVeraSans_Roman_58_font
145 #define VOLT_FONT BitstreamVeraSans_Roman_58_font
146 #define SMALL_FONT BitstreamVeraSans_Roman_12_font
147 #define TINY_FONT BitstreamVeraSans_Roman_10_font
148 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
149
150 #define LABEL_Y         (int16_t) (SMALL_FONT.ascent)
151 #define VALUE_Y         (int16_t) (LABEL_Y + BIG_FONT.ascent + 5)
152 #define BOX_X           2
153 #define PAD_X           90
154 #define BOX_LABEL_X     30
155 #define VOLT_LABEL_X    25
156 #define RSSI_LABEL_X    15
157 #define PAD_LABEL_X     95
158 #define SEP_X           (PAD_X - 8)
159 #define SCAN_X          (WIDTH - 100) / 2
160 #define SCAN_Y          50
161 #define SCAN_HEIGHT     3
162 #define FOUND_Y         63
163 #define FOUND_X         6
164 #define FOUND_WIDTH     17
165 #define MAX_VALID       (WIDTH / FOUND_WIDTH)
166
167 static int16_t  box_number = 88;
168 static int16_t  pad_number = 8;
169
170 static void
171 ao_st7565_poly(void)
172 {
173         int16_t scan_number;
174         char    str[8];
175         int     i;
176         int     v;
177         int     last_box;
178         int16_t b;
179
180         for (scan_number = 0; scan_number < 100; scan_number++) {
181                 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
182                 ao_logo(&fb, &logo_transform, &LOGO_FONT, AO_BLACK, AO_COPY);
183                 if (scan_number) {
184                         ao_rect(&fb, SCAN_X, SCAN_Y, (int16_t) scan_number, SCAN_HEIGHT, AO_BLACK, AO_COPY);
185                         b = 0;
186                         v = 0;
187                         last_box = 0;
188                         for (i = scan_number; i > 1; i--) {
189                                 if (valid_box(i)) {
190                                         if (!last_box)
191                                                 last_box = i;
192                                         v++;
193                                         if (v == MAX_VALID)
194                                                 break;
195                                 }
196                         }
197                         for (; i <= scan_number; i++) {
198                                 if (valid_box(i)) {
199                                         sprintf(str, "%02d%s", i, i == last_box ? "" : ",");
200                                         ao_text(&fb, &TINY_FONT, (int16_t) (FOUND_X + FOUND_WIDTH * b),
201                                                 FOUND_Y, str, AO_BLACK, AO_COPY);
202                                         b++;
203                                 }
204                         }
205                 }
206                 ao_st7565_update(&fb);
207                 ao_delay(AO_MS_TO_TICKS(50));
208         }
209         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
210         switch (box_number) {
211         case 0:
212                 sprintf(str, "%4.1f", lco_volts);
213                 ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
214                 ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "LCO Battery", AO_BLACK, AO_COPY);
215                 break;
216         default:
217                 switch (pad_number) {
218                 case -1:
219                         sprintf(str, "%4.1f", pad_volts);
220                         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
221                         ao_text(&fb, &SMALL_FONT, VOLT_LABEL_X, LABEL_Y, "Pad Battery", AO_BLACK, AO_COPY);
222                         break;
223                 case 0:
224                         sprintf(str, "%4d", rssi);
225                         ao_text(&fb, &VOLT_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
226                         ao_text(&fb, &SMALL_FONT, RSSI_LABEL_X, LABEL_Y, "Signal Strength", AO_BLACK, AO_COPY);
227                         break;
228                 default:
229                         sprintf(str, "%02d", box_number);
230                         ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, AO_BLACK, AO_COPY);
231                         ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "Box", AO_BLACK, AO_COPY);
232
233                         sprintf(str, "%d", pad_number);
234                         ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, AO_BLACK, AO_COPY);
235                         ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "Pad", AO_BLACK, AO_COPY);
236
237                         ao_rect(&fb, SEP_X, 0, 2, HEIGHT, AO_BLACK, AO_COPY);
238                 }
239                 break;
240         }
241         ao_st7565_update(&fb);
242 }
243
244 const struct ao_cmds ao_st7565_cmds[] = {
245         { ao_st7565_test, "g\0Test ST7565 display" },
246         { ao_st7565_line, "l\0Draw lines" },
247         { ao_st7565_poly, "p\0Draw polygon" },
248         { 0, NULL },
249 };
250
251 int
252 main(void)
253 {
254         ao_clock_init();
255
256         ao_led_init();
257         ao_task_init();
258
259         ao_timer_init();
260
261         ao_spi_init();
262         ao_dma_init();
263         ao_exti_init();
264         ao_adc_single_init();
265
266         ao_beep_init();
267         ao_pwm_init();
268         ao_cmd_init();
269
270         ao_quadrature_init();
271         ao_button_init();
272
273         ao_radio_init();
274
275         ao_usb_init();
276
277         ao_st7565_init();
278
279         ao_config_init();
280
281         ao_lco_init();
282         ao_lco_cmd_init();
283
284 //      ao_cmd_register(ao_st7565_cmds);
285
286         ao_start_scheduler();
287         return 0;
288 }