blob: 752fe8699d535696299efb5fd867d6dfb445bed8 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Conor Kennedy5cf8e742023-02-13 10:50:40 +00002 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
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{
Kshitij Sisodiaff570342022-06-10 17:19:22 +010027 const char title[] = "Arm ML embedded code samples";
alexander3c798932021-03-26 21:42:19 +000028 lcd_set_text_color(White);
Kshitij Sisodiaff570342022-06-10 17:19:22 +010029 return lcd_display_text(title, strlen(title), 10, 0, false);
alexander3c798932021-03-26 21:42:19 +000030}
31
32int lcd_init(void)
33{
34 GLCD_Initialize();
35 GLCD_Clear(Black);
36 return show_title();
37}
38
Richard Burton9c549902022-02-15 16:39:18 +000039int lcd_display_image(const uint8_t* data, const uint32_t width,
alexander3c798932021-03-26 21:42:19 +000040 const uint32_t height, const uint32_t channels,
41 const uint32_t pos_x, const uint32_t pos_y,
42 const uint32_t downsample_factor)
43{
Conor Kennedy5cf8e742023-02-13 10:50:40 +000044 /* Health checks */
alexander3c798932021-03-26 21:42:19 +000045 assert(data);
46 if ((pos_x + width/downsample_factor > GLCD_WIDTH) ||
47 (pos_y + height/downsample_factor > GLCD_HEIGHT)) {
48 printf_err("Invalid image size for given location!\n");
49 return 1;
50 }
51
52 if (1 == channels || 3 == channels) {
53 GLCD_Image(data, width, height, channels, pos_x, pos_y,
54 downsample_factor);
55 } else {
56 printf_err("Only single and three channel images are supported!\n");
57 return 1;
58 }
59
60 return 0;
61}
62
63int lcd_display_text(const char* str, const size_t str_sz,
64 const uint32_t pos_x, const uint32_t pos_y,
65 const bool allow_multiple_lines)
66{
67 /* We use a font 0 which is 9x15. */
68 const uint32_t x_span = 9; /* Each character is this 9 pixels "wide". */
69 const uint32_t y_span = 15; /* Each character is this 15 pixels "high". */
70
71 if (str_sz == 0) {
72 return 1;
73 }
74
75 /* If not within the LCD bounds, return error. */
76 if (pos_x + x_span > GLCD_WIDTH || pos_y + y_span > GLCD_HEIGHT) {
77 return 1;
78 } else {
79 const unsigned char font_idx = 0; /* We are using the custom font = 0 */
80
81 const uint32_t col = pos_x/x_span;
82 const uint32_t max_cols = GLCD_WIDTH/x_span - 1;
83 const uint32_t max_lines = GLCD_HEIGHT/y_span - 1;
84
85 uint32_t i = 0;
86 uint32_t current_line = pos_y/y_span;
87 uint32_t current_col = col;
88
89 /* Display the string on the LCD. */
90 for (i = 0; i < str_sz; ++i) {
91
92 if (allow_multiple_lines) {
93
94 /* If the next character won't fit. */
95 if (current_col > max_cols) {
96 current_col = col;
97
98 /* If the next line won't fit. */
99 if (++current_line > max_lines) {
100 return 1;
101 }
102 }
103 }
104
105 GLCD_DisplayChar(current_line, current_col++, font_idx, str[i]);
106 }
107 }
108 return 0;
109}
110
111int lcd_display_box(const uint32_t pos_x, const uint32_t pos_y,
112 const uint32_t width, const uint32_t height, const uint16_t color)
113{
114 /* If not within the LCD bounds, return error. */
115 if (pos_x > GLCD_WIDTH || pos_y > GLCD_HEIGHT) {
116 return 1;
117 }
118 else {
119 GLCD_Box(pos_x, pos_y, width, height, color);
120 }
121 return 0;
122}
123
124int lcd_clear(const uint16_t color)
125{
126 GLCD_Clear(color);
127 GLCD_SetTextColor(White);
128 return show_title();
129}
130
131int lcd_set_text_color(const uint16_t color)
132{
133 GLCD_SetTextColor(color);
134 return 0;
135}