alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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 | { |
Kshitij Sisodia | ff57034 | 2022-06-10 17:19:22 +0100 | [diff] [blame] | 27 | const char title[] = "Arm ML embedded code samples"; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | lcd_set_text_color(White); |
Kshitij Sisodia | ff57034 | 2022-06-10 17:19:22 +0100 | [diff] [blame] | 29 | return lcd_display_text(title, strlen(title), 10, 0, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | int lcd_init(void) |
| 33 | { |
| 34 | GLCD_Initialize(); |
| 35 | GLCD_Clear(Black); |
| 36 | return show_title(); |
| 37 | } |
| 38 | |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 39 | int lcd_display_image(const uint8_t* data, const uint32_t width, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 40 | 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 Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 44 | /* Health checks */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | 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 | |
| 63 | int 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 | |
| 111 | int 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 | |
| 124 | int lcd_clear(const uint16_t color) |
| 125 | { |
| 126 | GLCD_Clear(color); |
| 127 | GLCD_SetTextColor(White); |
| 128 | return show_title(); |
| 129 | } |
| 130 | |
| 131 | int lcd_set_text_color(const uint16_t color) |
| 132 | { |
| 133 | GLCD_SetTextColor(color); |
| 134 | return 0; |
| 135 | } |