blob: e3921a955e6d9970c928f97ca182f5eac4e66a9c [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Isabella Gottardiee4920b2022-02-25 14:29:32 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "lcd_img.h"
18
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000019#include "log_macros.h"
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000020#include "glcd.h"
alexander3c798932021-03-26 21:42:19 +000021
22#include <string.h>
23#include <assert.h>
24
25static int show_title(void)
26{
27 char title[128];
28 int status = 0;
29
30 /* LCD title string */
31#if defined(CPU_CORTEX_M55)
32 const char* cpu_name = "Arm Cortex-M55";
33#else /* defined(CPU_CORTEX_M55) */
34 const char* cpu_name = "Arm CPU";
35#endif /* defined(CPU_CORTEX_M55) */
36
37 lcd_set_text_color(White);
38
39 /* First line */
40 snprintf(title, sizeof(title), "Arm ML embedded code samples");
41
42 if (0 != (status = lcd_display_text(
43 title, strlen(title), 10, 0, false))) {
44 return status;
45 }
46
47 /* Second line */
48#if defined (ARM_NPU)
Éanna Ó Catháina4390762021-09-01 11:27:52 +010049 snprintf(title, sizeof(title), "%s + Arm Ethos-U NPU", cpu_name);
alexander3c798932021-03-26 21:42:19 +000050#else /* defined (ARM_NPU) */
51 snprintf(title, sizeof(title), "%s", cpu_name);
52#endif /* defined (ARM_NPU) */
53
54 return lcd_display_text(title, strlen(title), 10, 20, false);
55}
56
57int lcd_init(void)
58{
59 GLCD_Initialize();
60 GLCD_Clear(Black);
61 return show_title();
62}
63
Richard Burton9c549902022-02-15 16:39:18 +000064int lcd_display_image(const uint8_t* data, const uint32_t width,
alexander3c798932021-03-26 21:42:19 +000065 const uint32_t height, const uint32_t channels,
66 const uint32_t pos_x, const uint32_t pos_y,
67 const uint32_t downsample_factor)
68{
69 /* Sanity checks */
70 assert(data);
71 if ((pos_x + width/downsample_factor > GLCD_WIDTH) ||
72 (pos_y + height/downsample_factor > GLCD_HEIGHT)) {
73 printf_err("Invalid image size for given location!\n");
74 return 1;
75 }
76
77 if (1 == channels || 3 == channels) {
78 GLCD_Image(data, width, height, channels, pos_x, pos_y,
79 downsample_factor);
80 } else {
81 printf_err("Only single and three channel images are supported!\n");
82 return 1;
83 }
84
85 return 0;
86}
87
88int lcd_display_text(const char* str, const size_t str_sz,
89 const uint32_t pos_x, const uint32_t pos_y,
90 const bool allow_multiple_lines)
91{
92 /* We use a font 0 which is 9x15. */
93 const uint32_t x_span = 9; /* Each character is this 9 pixels "wide". */
94 const uint32_t y_span = 15; /* Each character is this 15 pixels "high". */
95
96 if (str_sz == 0) {
97 return 1;
98 }
99
100 /* If not within the LCD bounds, return error. */
101 if (pos_x + x_span > GLCD_WIDTH || pos_y + y_span > GLCD_HEIGHT) {
102 return 1;
103 } else {
104 const unsigned char font_idx = 0; /* We are using the custom font = 0 */
105
106 const uint32_t col = pos_x/x_span;
107 const uint32_t max_cols = GLCD_WIDTH/x_span - 1;
108 const uint32_t max_lines = GLCD_HEIGHT/y_span - 1;
109
110 uint32_t i = 0;
111 uint32_t current_line = pos_y/y_span;
112 uint32_t current_col = col;
113
114 /* Display the string on the LCD. */
115 for (i = 0; i < str_sz; ++i) {
116
117 if (allow_multiple_lines) {
118
119 /* If the next character won't fit. */
120 if (current_col > max_cols) {
121 current_col = col;
122
123 /* If the next line won't fit. */
124 if (++current_line > max_lines) {
125 return 1;
126 }
127 }
128 }
129
130 GLCD_DisplayChar(current_line, current_col++, font_idx, str[i]);
131 }
132 }
133 return 0;
134}
135
136int lcd_display_box(const uint32_t pos_x, const uint32_t pos_y,
137 const uint32_t width, const uint32_t height, const uint16_t color)
138{
139 /* If not within the LCD bounds, return error. */
140 if (pos_x > GLCD_WIDTH || pos_y > GLCD_HEIGHT) {
141 return 1;
142 }
143 else {
144 GLCD_Box(pos_x, pos_y, width, height, color);
145 }
146 return 0;
147}
148
149int lcd_clear(const uint16_t color)
150{
151 GLCD_Clear(color);
152 GLCD_SetTextColor(White);
153 return show_title();
154}
155
156int lcd_set_text_color(const uint16_t color)
157{
158 GLCD_SetTextColor(color);
159 return 0;
160}