|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Jeff Epler for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "common-hal/picodvi/__init__.h" |
| 8 | +#include "common-hal/picodvi/Framebuffer.h" |
| 9 | +#include "bindings/picodvi/Framebuffer.h" |
| 10 | +#include "shared-bindings/busio/I2C.h" |
| 11 | +#include "shared-bindings/board/__init__.h" |
| 12 | +#include "shared-module/displayio/__init__.h" |
| 13 | +#include "shared-module/os/__init__.h" |
| 14 | +#include "supervisor/shared/safe_mode.h" |
| 15 | +#include "py/gc.h" |
| 16 | +#include "py/runtime.h" |
| 17 | +#include "supervisor/port_heap.h" |
| 18 | + |
| 19 | +#if defined(DEFAULT_DVI_BUS_CLK_DP) |
| 20 | +static bool picodvi_autoconstruct_enabled(void) { |
| 21 | + char buf[sizeof("detect")]; |
| 22 | + buf[0] = 0; |
| 23 | + |
| 24 | + // (any failure leaves the content of buf untouched: an empty nul-terminated string |
| 25 | + (void)common_hal_os_getenv_str("CIRCUITPY_PICODVI_ENABLE", buf, sizeof(buf)); |
| 26 | + |
| 27 | + if (!strcasecmp(buf, "never")) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + if (!strcasecmp(buf, "always")) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + // It's "detect" or else an invalid value which is treated the same as "detect". |
| 35 | + |
| 36 | + // check if address 0x50 is live on the I2C bus |
| 37 | + busio_i2c_obj_t *i2c = common_hal_board_create_i2c(0); |
| 38 | + if (!i2c) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + if (!common_hal_busio_i2c_try_lock(i2c)) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + bool probed = common_hal_busio_i2c_probe(i2c, 0x50); |
| 45 | + common_hal_busio_i2c_unlock(i2c); |
| 46 | + return probed; |
| 47 | +} |
| 48 | + |
| 49 | +// For picodvi_autoconstruct to work, the 8 DVI/HSTX pin names must be defined, AND |
| 50 | +// i2c bus 0 must also be connected to DVI with on-board pull ups |
| 51 | +void picodvi_autoconstruct(void) { |
| 52 | + if (get_safe_mode() != SAFE_MODE_NONE) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!picodvi_autoconstruct_enabled()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + mp_int_t width = 320; |
| 61 | + mp_int_t height = 0; |
| 62 | + mp_int_t color_depth = 16; |
| 63 | + mp_int_t rotation = 0; |
| 64 | + |
| 65 | + (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_WIDTH", &width); |
| 66 | + (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_HEIGHT", &height); |
| 67 | + (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_COLOR_DEPTH", &color_depth); |
| 68 | + (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_ROTATION", &rotation); |
| 69 | + |
| 70 | + if (height == 0) { |
| 71 | + switch (width) { |
| 72 | + case 640: |
| 73 | + height = 480; |
| 74 | + break; |
| 75 | + case 320: |
| 76 | + height = 240; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270) { |
| 82 | + // invalid rotation |
| 83 | + rotation = 0; |
| 84 | + } |
| 85 | + |
| 86 | + if (!common_hal_picodvi_framebuffer_preflight(width, height, color_depth)) { |
| 87 | + // invalid configuration, set back to default |
| 88 | + width = 320; |
| 89 | + height = 240; |
| 90 | + color_depth = 16; |
| 91 | + } |
| 92 | + |
| 93 | + // construct framebuffer and display |
| 94 | + picodvi_framebuffer_obj_t *fb = &allocate_display_bus_or_raise()->picodvi; |
| 95 | + fb->base.type = &picodvi_framebuffer_type; |
| 96 | + common_hal_picodvi_framebuffer_construct(fb, |
| 97 | + width, height, |
| 98 | + DEFAULT_DVI_BUS_CLK_DP, |
| 99 | + DEFAULT_DVI_BUS_CLK_DN, |
| 100 | + DEFAULT_DVI_BUS_RED_DP, |
| 101 | + DEFAULT_DVI_BUS_RED_DN, |
| 102 | + DEFAULT_DVI_BUS_GREEN_DP, |
| 103 | + DEFAULT_DVI_BUS_GREEN_DN, |
| 104 | + DEFAULT_DVI_BUS_BLUE_DP, |
| 105 | + DEFAULT_DVI_BUS_BLUE_DN, |
| 106 | + color_depth); |
| 107 | + |
| 108 | + framebufferio_framebufferdisplay_obj_t *display = &allocate_display()->framebuffer_display; |
| 109 | + display->base.type = &framebufferio_framebufferdisplay_type; |
| 110 | + common_hal_framebufferio_framebufferdisplay_construct( |
| 111 | + display, |
| 112 | + MP_OBJ_FROM_PTR(fb), |
| 113 | + rotation, |
| 114 | + true); |
| 115 | +} |
| 116 | +#else |
| 117 | +void picodvi_autoconstruct(void) { |
| 118 | +} |
| 119 | +#endif |
0 commit comments