|
16 | 16 | #include "py/runtime.h" |
17 | 17 | #include "supervisor/port_heap.h" |
18 | 18 |
|
19 | | -void picodvi_autoconstruct(void) { |
20 | | - #if defined(DEFAULT_DVI_BUS_CLK_DP) |
21 | | - if (get_safe_mode() != SAFE_MODE_NONE) { |
22 | | - return; |
| 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; |
23 | 32 | } |
24 | | - // check if address 0x50 is live on the I2C bus -- return if not |
| 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 |
25 | 37 | busio_i2c_obj_t *i2c = common_hal_board_create_i2c(0); |
26 | 38 | if (!i2c) { |
27 | | - return; |
| 39 | + return false; |
28 | 40 | } |
29 | 41 | if (!common_hal_busio_i2c_try_lock(i2c)) { |
30 | | - return; |
| 42 | + return false; |
31 | 43 | } |
32 | 44 | bool probed = common_hal_busio_i2c_probe(i2c, 0x50); |
33 | 45 | common_hal_busio_i2c_unlock(i2c); |
34 | | - if (!probed) { |
| 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) { |
35 | 53 | return; |
36 | 54 | } |
37 | 55 |
|
| 56 | + if (!picodvi_autoconstruct_enabled()) { |
| 57 | + return; |
| 58 | + } |
38 | 59 |
|
39 | 60 | mp_int_t width = 320; |
40 | | - mp_int_t height = 240; |
| 61 | + mp_int_t height = 0; |
41 | 62 | mp_int_t color_depth = 16; |
| 63 | + mp_int_t rotation = 0; |
42 | 64 |
|
43 | 65 | (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_WIDTH", &width); |
44 | 66 | (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_HEIGHT", &height); |
45 | 67 | (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_COLOR_DEPTH", &color_depth); |
| 68 | + (void)common_hal_os_getenv_int("CIRCUITPY_DISPLAY_ROTATION", &rotation); |
46 | 69 |
|
47 | | - if (width == 0) { |
| 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 |
48 | 83 | return; |
49 | 84 | } |
| 85 | + |
50 | 86 | if (!common_hal_picodvi_framebuffer_preflight(width, height, color_depth)) { |
51 | 87 | // TODO: User configuration can fail without a self-explanatory message. |
52 | 88 | // sadly, a print from here does NOT reach boot_out.txt, so no point in |
@@ -75,7 +111,10 @@ void picodvi_autoconstruct(void) { |
75 | 111 | common_hal_framebufferio_framebufferdisplay_construct( |
76 | 112 | display, |
77 | 113 | MP_OBJ_FROM_PTR(fb), |
78 | | - 0, |
| 114 | + rotation, |
79 | 115 | true); |
80 | | - #endif |
81 | 116 | } |
| 117 | +#else |
| 118 | +void picodvi_autoconstruct(void) { |
| 119 | +} |
| 120 | +#endif |
0 commit comments