alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * 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 Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 19 | #include "log_macros.h" |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 20 | #include "glcd.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
| 22 | #include <string.h> |
| 23 | #include <assert.h> |
| 24 | |
| 25 | static 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 Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame^] | 33 | #elif defined (ARMv81MML_DSP_DP_MVE_FP) |
| 34 | const char* cpu_name = "ARMv8.1-M CPU"; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | #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áin | a439076 | 2021-09-01 11:27:52 +0100 | [diff] [blame] | 51 | snprintf(title, sizeof(title), "%s + Arm Ethos-U NPU", cpu_name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 52 | #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 | |
| 59 | int lcd_init(void) |
| 60 | { |
| 61 | GLCD_Initialize(); |
| 62 | GLCD_Clear(Black); |
| 63 | return show_title(); |
| 64 | } |
| 65 | |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 66 | int lcd_display_image(const uint8_t* data, const uint32_t width, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | 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 | |
| 90 | int 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 | |
| 138 | int 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 | |
| 151 | int lcd_clear(const uint16_t color) |
| 152 | { |
| 153 | GLCD_Clear(color); |
| 154 | GLCD_SetTextColor(White); |
| 155 | return show_title(); |
| 156 | } |
| 157 | |
| 158 | int lcd_set_text_color(const uint16_t color) |
| 159 | { |
| 160 | GLCD_SetTextColor(color); |
| 161 | return 0; |
| 162 | } |