blob: 92069c28d20b59c9d98405a35d0b8e9ac731e5e5 [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";
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010033#elif defined (ARMv81MML_DSP_DP_MVE_FP)
34 const char* cpu_name = "ARMv8.1-M CPU";
alexander3c798932021-03-26 21:42:19 +000035#else /* defined(CPU_CORTEX_M55) */
36 const char* cpu_name = "Arm CPU";
37#endif /* defined(CPU_CORTEX_M55) */
38
39 lcd_set_text_color(White);
40
41 /* First line */
42 snprintf(title, sizeof(title), "Arm ML embedded code samples");
43
44 if (0 != (status = lcd_display_text(
45 title, strlen(title), 10, 0, false))) {
46 return status;
47 }
48
49 /* Second line */
50#if defined (ARM_NPU)
Éanna Ó Catháina4390762021-09-01 11:27:52 +010051 snprintf(title, sizeof(title), "%s + Arm Ethos-U NPU", cpu_name);
alexander3c798932021-03-26 21:42:19 +000052#else /* defined (ARM_NPU) */
53 snprintf(title, sizeof(title), "%s", cpu_name);
54#endif /* defined (ARM_NPU) */
55
56 return lcd_display_text(title, strlen(title), 10, 20, false);
57}
58
59int lcd_init(void)
60{
61 GLCD_Initialize();
62 GLCD_Clear(Black);
63 return show_title();
64}
65
Richard Burton9c549902022-02-15 16:39:18 +000066int lcd_display_image(const uint8_t* data, const uint32_t width,
alexander3c798932021-03-26 21:42:19 +000067 const uint32_t height, const uint32_t channels,
68 const uint32_t pos_x, const uint32_t pos_y,
69 const uint32_t downsample_factor)
70{
71 /* Sanity checks */
72 assert(data);
73 if ((pos_x + width/downsample_factor > GLCD_WIDTH) ||
74 (pos_y + height/downsample_factor > GLCD_HEIGHT)) {
75 printf_err("Invalid image size for given location!\n");
76 return 1;
77 }
78
79 if (1 == channels || 3 == channels) {
80 GLCD_Image(data, width, height, channels, pos_x, pos_y,
81 downsample_factor);
82 } else {
83 printf_err("Only single and three channel images are supported!\n");
84 return 1;
85 }
86
87 return 0;
88}
89
90int lcd_display_text(const char* str, const size_t str_sz,
91 const uint32_t pos_x, const uint32_t pos_y,
92 const bool allow_multiple_lines)
93{
94 /* We use a font 0 which is 9x15. */
95 const uint32_t x_span = 9; /* Each character is this 9 pixels "wide". */
96 const uint32_t y_span = 15; /* Each character is this 15 pixels "high". */
97
98 if (str_sz == 0) {
99 return 1;
100 }
101
102 /* If not within the LCD bounds, return error. */
103 if (pos_x + x_span > GLCD_WIDTH || pos_y + y_span > GLCD_HEIGHT) {
104 return 1;
105 } else {
106 const unsigned char font_idx = 0; /* We are using the custom font = 0 */
107
108 const uint32_t col = pos_x/x_span;
109 const uint32_t max_cols = GLCD_WIDTH/x_span - 1;
110 const uint32_t max_lines = GLCD_HEIGHT/y_span - 1;
111
112 uint32_t i = 0;
113 uint32_t current_line = pos_y/y_span;
114 uint32_t current_col = col;
115
116 /* Display the string on the LCD. */
117 for (i = 0; i < str_sz; ++i) {
118
119 if (allow_multiple_lines) {
120
121 /* If the next character won't fit. */
122 if (current_col > max_cols) {
123 current_col = col;
124
125 /* If the next line won't fit. */
126 if (++current_line > max_lines) {
127 return 1;
128 }
129 }
130 }
131
132 GLCD_DisplayChar(current_line, current_col++, font_idx, str[i]);
133 }
134 }
135 return 0;
136}
137
138int lcd_display_box(const uint32_t pos_x, const uint32_t pos_y,
139 const uint32_t width, const uint32_t height, const uint16_t color)
140{
141 /* If not within the LCD bounds, return error. */
142 if (pos_x > GLCD_WIDTH || pos_y > GLCD_HEIGHT) {
143 return 1;
144 }
145 else {
146 GLCD_Box(pos_x, pos_y, width, height, color);
147 }
148 return 0;
149}
150
151int lcd_clear(const uint16_t color)
152{
153 GLCD_Clear(color);
154 GLCD_SetTextColor(White);
155 return show_title();
156}
157
158int lcd_set_text_color(const uint16_t color)
159{
160 GLCD_SetTextColor(color);
161 return 0;
162}