altos/stm32f103-nucleo: Drive a screen too
[fw/altos] / src / stm32f103-nucleo / hello.c
1 /*
2  * Copyright © 2023 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_st7565.h>
21
22 #define WIDTH   AO_ST7565_WIDTH
23 #define HEIGHT  AO_ST7565_HEIGHT
24 #define STRIDE  AO_BITMAP_STRIDE(WIDTH)
25
26 static uint32_t image[STRIDE * HEIGHT];
27
28 static struct ao_bitmap fb = {
29         .base = image,
30         .stride = STRIDE,
31         .width = WIDTH,
32         .height = HEIGHT,
33         .damage = AO_BOX_INIT,
34 };
35
36 static void
37 ao_st7565_test(void)
38 {
39         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
40         ao_st7565_update(&fb);
41         ao_text(&fb, &BitstreamVeraSans_Roman_24_font,
42                 0, 20, "hello world", AO_BLACK, AO_COPY);
43         ao_st7565_update(&fb);
44 }
45
46 static int16_t  x1 = 32, _y1 = 10, x2 = 32, y2 = 40;
47 static int16_t  dx1 = 2, dy1 = 2, dx2 = -2, dy2 = -1;
48
49 #define bounds(v,m,M,d) \
50                 if (v < m) {                    \
51                         v = m + m - v;          \
52                         d = -d;                 \
53                 } else if (v > M) {             \
54                         v = M - (v - M);        \
55                         d = -d;                 \
56                 }
57
58 static void
59 ao_st7565_line(void)
60 {
61         int     i;
62
63         for (i = 0; i < 100; i++) {
64                 ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
65                 ao_line(&fb, x1, _y1, x2, y2, AO_BLACK, AO_COPY);
66                 ao_st7565_update(&fb);
67                 x1 += dx1;
68                 _y1 += dy1;
69                 x2 += dx2;
70                 y2 += dy2;
71                 printf("%d,%d - %d,%d\n", x1, _y1, x2, y2);
72                 fflush(stdout);
73                 bounds(x1, 0, WIDTH, dx1);
74                 bounds(x2, 0, WIDTH, dx2);
75                 bounds(_y1, 0, HEIGHT, dy1);
76                 bounds(y2, 0, HEIGHT, dy2);
77                 ao_delay(AO_MS_TO_TICKS(200));
78         }
79 }
80
81 static const struct ao_transform logo_transform = {
82         .x_scale = 48, .x_off = 0,
83         .y_scale = 48, .y_off = 0,
84 };
85
86 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
87
88 static void
89 ao_st7565_poly(void)
90 {
91         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY);
92         ao_logo(&fb, &logo_transform, &LOGO_FONT, 0x00000000, AO_COPY);
93         ao_st7565_update(&fb);
94 }
95
96 const struct ao_cmds ao_st7565_cmds[] = {
97         { ao_st7565_test, "g\0Test ST7565 display" },
98         { ao_st7565_line, "l\0Draw lines" },
99         { ao_st7565_poly, "p\0Draw polygon" },
100         { 0, NULL },
101 };
102
103 int main(void)
104 {
105         ao_clock_init();
106         ao_led_init();
107         ao_timer_init();
108         ao_task_init();
109         ao_dma_init();
110         ao_spi_init();
111         ao_serial_init();
112         ao_usb_init();
113         ao_st7565_init();
114         ao_cmd_init();
115         ao_cmd_register(ao_st7565_cmds);
116         ao_start_scheduler();
117 }