|
| 1 | +/* |
| 2 | + * Copyright 2025 Arduino SA |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Camera driver. |
| 18 | + */ |
| 19 | +#ifdef __ZEPHYR__ |
| 20 | + |
| 21 | +#include "Arduino.h" |
| 22 | +#include "Arduino_GigaDisplay.h" |
| 23 | + |
| 24 | +#include <zephyr/kernel.h> |
| 25 | +#include <zephyr/device.h> |
| 26 | +#include <zephyr/drivers/display.h> |
| 27 | + |
| 28 | +Display::Display() : gdev(NULL){ |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +bool Display::begin(DisplayPixelFormat pixformat, int rotation) { |
| 33 | + |
| 34 | + int ret = 0; |
| 35 | + |
| 36 | + #if DT_HAS_CHOSEN(zephyr_display) |
| 37 | + this->gdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)); |
| 38 | + #endif |
| 39 | + |
| 40 | + if (!this->gdev || !device_is_ready(this->gdev)) { |
| 41 | + Serial.println("\t<err> Zephy Display Not Ready!..."); |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + // Get capabilities |
| 46 | + struct display_capabilities capabilities = {0}; |
| 47 | + display_get_capabilities(this->gdev, &capabilities); |
| 48 | + |
| 49 | + uint8_t format_spec = 0; |
| 50 | + |
| 51 | + switch (pixformat) { |
| 52 | + case DISPLAY_RGB565: |
| 53 | + //Serial.println("Set RGB565"); |
| 54 | + if (capabilities.current_pixel_format != PIXEL_FORMAT_RGB_565) { |
| 55 | + ret = display_set_pixel_format(this->gdev, PIXEL_FORMAT_RGB_565); |
| 56 | + } |
| 57 | + format_spec = 1; |
| 58 | + break; |
| 59 | + case DISPLAY_RGB888: |
| 60 | + //Serial.println("Set RGB888"); |
| 61 | + if (capabilities.current_pixel_format != PIXEL_FORMAT_RGB_888) { |
| 62 | + ret = display_set_pixel_format(this->gdev, PIXEL_FORMAT_RGB_888); |
| 63 | + } |
| 64 | + format_spec = 1; |
| 65 | + break; |
| 66 | + default: |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + if(format_spec == 0) { |
| 71 | + Serial.println("\t<err> The specified format is not supported"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + if (ret) { |
| 76 | + Serial.println("\t<err> Unable to set display format"); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + display_orientation orientation; |
| 82 | + switch(rotation){ |
| 83 | + case 0: |
| 84 | + orientation = DISPLAY_ORIENTATION_NORMAL; |
| 85 | + break; |
| 86 | + case 1: |
| 87 | + orientation = DISPLAY_ORIENTATION_ROTATED_90; |
| 88 | + break; |
| 89 | + case 2: |
| 90 | + orientation = DISPLAY_ORIENTATION_ROTATED_180; |
| 91 | + break; |
| 92 | + case 3: |
| 93 | + orientation = DISPLAY_ORIENTATION_ROTATED_270; |
| 94 | + break; |
| 95 | + default: |
| 96 | + orientation = DISPLAY_ORIENTATION_NORMAL; |
| 97 | + break; |
| 98 | + } |
| 99 | + //Rotation not supported |
| 100 | + //Serial.print("Orientation: "); Serial.println(orientation); |
| 101 | + ret = display_set_orientation(this->gdev, orientation); |
| 102 | + Serial.println(ret); |
| 103 | + if(ret) { |
| 104 | + Serial.println("\t<err> Failed to set display rotation"); |
| 105 | + //return false; |
| 106 | + } |
| 107 | + |
| 108 | + display_get_capabilities(this->gdev, &capabilities); |
| 109 | + |
| 110 | + printk("- Capabilities:\n"); |
| 111 | + printk(" x_resolution = %u, y_resolution = %u\n, supported_pixel_formats = %u\n" |
| 112 | + " current_pixel_format = %u, current_orientation = %u\n", |
| 113 | + capabilities.x_resolution, capabilities.y_resolution, |
| 114 | + capabilities.supported_pixel_formats, capabilities.current_pixel_format, |
| 115 | + capabilities.current_orientation); |
| 116 | + |
| 117 | + _height = capabilities.y_resolution; |
| 118 | + _width = capabilities.x_resolution; |
| 119 | + |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +int Display::write8(const uint16_t x, |
| 124 | + const uint16_t y, |
| 125 | + const void *buf) { |
| 126 | + |
| 127 | + |
| 128 | +// printk("Display::write8(%u %u %p) %p %x\n", x, y, buf, |
| 129 | +// &((struct display_driver_api *)gdev->api)->write, *((uint32_t*)(&((struct display_driver_api *)gdev->api)->write))); |
| 130 | + |
| 131 | + return display_write(this->gdev, x, y, this->buf_desc, buf); |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +void Display::setFrameDesc(uint16_t w, uint16_t h, uint16_t pitch, uint32_t buf_size) { |
| 136 | + this->buf_desc->buf_size = buf_size; |
| 137 | + this->buf_desc->width = w; /** Number of pixels between consecutive rows in the data buffer */ |
| 138 | + this->buf_desc->height = h; /** Data buffer row width in pixels */ |
| 139 | + this->buf_desc->pitch = pitch; /** Data buffer row height in pixels */ |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +void Display::startFrameBuffering() { |
| 144 | + |
| 145 | + this->buf_desc->frame_incomplete = false; |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +void Display::endFrameBuffering() { |
| 150 | + |
| 151 | + this->buf_desc->frame_incomplete = true; |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +int Display::setBlanking(bool on) { |
| 156 | + int ret = 0; |
| 157 | + if(on) { |
| 158 | + ret = display_blanking_on(this->gdev); |
| 159 | + } else { |
| 160 | + ret = display_blanking_off(this->gdev); |
| 161 | + } |
| 162 | + if(ret < 0) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + return true; |
| 166 | +} |
| 167 | + |
| 168 | +void* Display::getFrameBuffer() { |
| 169 | + void* fb = display_get_framebuffer(this->gdev); |
| 170 | + return fb; |
| 171 | +} |
| 172 | + |
| 173 | +#endif //__ZEPHYR__ |
0 commit comments