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