From 83a7416f96660f2d6537f29a624124d23d92ba67 Mon Sep 17 00:00:00 2001 From: loxck Date: Sun, 29 Nov 2026 19:45:00 -1000 Subject: [PATCH 1/3] feat(pico4-monado): control the backlight from SteamVR --- phoenix/pico4-monado/PKGBUILD | 16 ++-- .../monado-steamvr-brightness.patch | 96 +++++++++++++++++++ phoenix/pico4-monado/pico4_hmd.c | 78 +++++++++++++-- phoenix/pico4-monado/pico4_interface.h | 8 ++ phoenix/pico4-monado/pico4_ipd.c | 53 ++++++++-- phoenix/pico4-monado/pico4_ipd.h | 19 +++- phoenix/pico4-monado/test_pico4_lens.c | 46 ++++++++- 7 files changed, 285 insertions(+), 31 deletions(-) create mode 100644 phoenix/pico4-monado/monado-steamvr-brightness.patch diff --git a/phoenix/pico4-monado/PKGBUILD b/phoenix/pico4-monado/PKGBUILD index 796259a..df6d751 100644 --- a/phoenix/pico4-monado/PKGBUILD +++ b/phoenix/pico4-monado/PKGBUILD @@ -19,7 +19,7 @@ # overlays (Overpressure Settings' dashboard tab, the first-run setup). pkgname=pico4-monado pkgver=25.1.0 -pkgrel=24 +pkgrel=25 arch=(aarch64) url="https://monado.freedesktop.org" license=(BSL-1.0) @@ -35,7 +35,7 @@ source=("monado::git+https://gitlab.freedesktop.org/monado/monado.git#commit=$_c monado-slam-prediction.patch monado-steamvr-battery.patch monado-vk-mutex-init-failure.patch monado-steamvr-presence.patch monado-steamvr-camera.patch monado-steamvr-room.patch monado-steamvr-pico-controller.patch monado-steamvr-roomview-mesh.patch monado-steamvr-floor.patch - monado-steamvr-frame-rates.patch monado-steamvr-lenses-standby.patch + monado-steamvr-frame-rates.patch monado-steamvr-lenses-standby.patch monado-steamvr-brightness.patch pico4_interface.h pico4_hmd.c pico4_ssc.h pico4_ssc.c test_pico4_ssc.c pico4_lens.h pico4_lens.c pico4_ipd.h pico4_ipd.c test_pico4_lens.c pico4_cam.h pico4_cam.c pico4_station.h pico4_station.c pico4_tcam.h pico4_tcam.c @@ -71,16 +71,17 @@ sha256sums=(SKIP '41be8ce4a88a79a8f2b98cb82a8a4bcc4693890410acc1e9776bd16d347310f9' '079215437455bb24dabf36ca8598a43e69b25b7fbb20caebf8850fd7d137dc4e' '4ea7235b043e92d3ccb9a21277868a7afb2d8963a03d93a1623de8681a136ac6' - 'e712b477138e19966b8a4ba62758c096be2979664c7d97f40c1a1c91feeb5b8b' - '7b45549338fbf50e7db10762b105108382c1aa6644cf27b0a763a0db79b935a3' + '48f9760ee10780cc29aee6f5fa0862d1f5b1b9d3257cec0da231c05022ba43ce' + '90e9836e6a487cc1aefa62432f16db6c22943e7ed1b14a3817e28b3c7bf5adaa' + 'e8230e2fd03ad77c796bd7dcea0cc85ffb9a6e887456f0f605983459eb6b62bc' '44160658b28ea1bb24c6c2fc9f378fba14ffe6b1ae4ae2e8b9cbbb44c5ce96ac' '5a5317c24d19d91ac502707d6fa8e391d4ba7e1c93875bc8b4f77f14bb84a712' '3d3a356292827bde4644810731386f366e4f2afa029ee36fec9ecc1db4692363' '38f67d51128d14b8d8600b64d90a500b84c3d8650b7bc7a81579105f3cc998de' 'f50a147b36ef42f58ee25e47e21f2a2665a4bebc54dcaae54dbc2412d0879ab8' - 'e80568bc5a6f6b0def8c80f5ad8facb8569bd92759c6c540cfb9751f28b52195' - '5862557f9d2abfc932e4532741a8bcf5f58dc2c431a0d89805fa81461368880d' - '42d4a1ec923f5a32b93847370cf2c63e9b07a5d68d51c8a2e5214d995d51c1a0' + 'ac40b4f6c4a6ca12d2ebf870ad20f31076e5d0b1c61f49e3f535b3986022a84f' + 'f52d2669f6b5d9cdc2feddece8b6018bf51ed56f657cd2ee489b5ab48fae4e26' + '52edf1e445759dec6a92aa2ff8c13452402e5181c14ff781f818c573556328d1' '787136e02024254b355d1aae427799c206456307040ecc29d543962179364a96' '732a6738b8e89063d247c820f0fdf6de7be2b2b9435e751886450474ffa7a2d0' 'c6f9b33382bc9abb75942be3588072cb8acc1944116e3f3406bd9dce8565ff48' @@ -180,6 +181,7 @@ prepare() { patch -d monado -p1 < monado-steamvr-frame-rates.patch # The eye positions follow the lens motor, the backlight follows SteamVR's standby. patch -d monado -p1 < monado-steamvr-lenses-standby.patch + patch -d monado -p1 < monado-steamvr-brightness.patch } build() { diff --git a/phoenix/pico4-monado/monado-steamvr-brightness.patch b/phoenix/pico4-monado/monado-steamvr-brightness.patch new file mode 100644 index 0000000..175cba9 --- /dev/null +++ b/phoenix/pico4-monado/monado-steamvr-brightness.patch @@ -0,0 +1,96 @@ +diff --git a/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp b/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp +index 2d07b82..1d4ccaa 100644 +--- a/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp ++++ b/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp +@@ -83,6 +83,10 @@ pico4_hmd_detect_floor(struct xrt_device *xdev) __attribute__((weak)); + // The lens separation, which the motor can change during a session, and SteamVR's standby. + extern "C" bool + pico4_hmd_ipd(struct xrt_device *xdev, float *out_ipd_m) __attribute__((weak)); ++extern "C" int ++pico4_hmd_initial_brightness(struct xrt_device *xdev) __attribute__((weak)); ++extern "C" void ++pico4_hmd_set_brightness(struct xrt_device *xdev, int level) __attribute__((weak)); + extern "C" void + pico4_hmd_standby(struct xrt_device *xdev, bool standby) __attribute__((weak)); + extern "C" bool +@@ -1255,6 +1259,7 @@ public: + virtual void DebugRequest(const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize); + virtual vr::DriverPose_t GetPose(); + ++ void UpdateBrightness(); + // IVRDisplayComponent + virtual void GetWindowBounds(int32_t *pnX, int32_t *pnY, uint32_t *pnWidth, uint32_t *pnHeight); + virtual bool IsDisplayOnDesktop(); +@@ -1278,6 +1283,7 @@ private: + float m_flSecondsFromVsyncToPhotons = -1; + float m_flDisplayFrequency = -1; + float m_flIPD = -1; ++ bool m_hasBrightness = false; + + struct xrt_fov m_fovs[2]; + struct xrt_pose m_view_pose[2]; +@@ -2118,6 +2124,27 @@ CDeviceDriver_Monado::Camera() + return m_camera; + } + ++// The SteamVR dashboard stores (UI percentage / 100)^2.2 in steamvr.analogGain. ++static constexpr float kMinBrightness = 0.30f; ++static constexpr float kBrightnessGamma = 2.2f; ++ ++static bool ++read_brightness_gain(float *gain) ++{ ++ vr::EVRSettingsError error; ++ *gain = vr::VRSettings()->GetFloat(vr::k_pch_SteamVR_Section, "analogGain", &error); ++ return error == vr::VRSettingsError_None && *gain >= powf(kMinBrightness, kBrightnessGamma) - 0.000001f && *gain <= 1.f; ++} ++ ++void ++CDeviceDriver_Monado::UpdateBrightness() ++{ ++ float gain; ++ if (m_hasBrightness && read_brightness_gain(&gain)) { ++ pico4_hmd_set_brightness(m_xdev, (int)lroundf(100.f * powf(gain, 1.f / kBrightnessGamma))); ++ } ++} ++ + vr::EVRInitError + CDeviceDriver_Monado::Activate(vr::TrackedDeviceIndex_t unObjectId) + { +@@ -2128,6 +2155,24 @@ CDeviceDriver_Monado::Activate(vr::TrackedDeviceIndex_t unObjectId) + // clang-format off + + m_ulPropertyContainer = vr::VRProperties()->TrackedDeviceToPropertyContainer(unObjectId); ++ int level = pico4_hmd_initial_brightness != NULL ? pico4_hmd_initial_brightness(m_xdev) : -1; ++ m_hasBrightness = level >= 0 && pico4_hmd_set_brightness != NULL; ++ if (m_hasBrightness) { ++ float gain; ++ if (!read_brightness_gain(&gain)) { ++ // The runtime's default gain is outside this display's range. ++ level = level >= 30 ? level : 63; ++ gain = powf(level / 100.f, kBrightnessGamma); ++ vr::VRSettings()->SetFloat(vr::k_pch_SteamVR_Section, "analogGain", gain); ++ } ++ pico4_hmd_set_brightness(m_xdev, (int)lroundf(100.f * powf(gain, 1.f / kBrightnessGamma))); ++ auto *props = vr::VRProperties(); ++ props->SetFloatProperty(m_ulPropertyContainer, vr::Prop_DisplayMinAnalogGain_Float, powf(kMinBrightness, kBrightnessGamma)); ++ props->SetFloatProperty(m_ulPropertyContainer, vr::Prop_DisplayMaxAnalogGain_Float, 1.f); ++ // Prop_DisplayMinUIAnalogGain_Float, added after the bundled OpenVR header. ++ props->SetFloatProperty(m_ulPropertyContainer, static_cast(2098), kMinBrightness); ++ props->SetBoolProperty(m_ulPropertyContainer, vr::Prop_DisplaySupportsAnalogGain_Bool, true); ++ } + //! @todo: proper serial and model number + + vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_HMD); +@@ -2673,6 +2718,11 @@ CServerDriver_Monado::RunFrame() + ovrd_log("Device interaction started %d\n", event.trackedDeviceIndex); + break; + case vr::VREvent_IpdChanged: ovrd_log("ipd changed to %fm\n", event.data.ipd.ipdMeters); break; ++ case vr::VREvent_SteamVRSectionSettingChanged: ++ if (m_MonadoDeviceDriver != NULL) { ++ m_MonadoDeviceDriver->UpdateBrightness(); ++ } ++ break; + case vr::VREvent_EnterStandbyMode: + case vr::VREvent_LeaveStandbyMode: + if (pico4_hmd_standby != NULL && m_xhmd != NULL) { diff --git a/phoenix/pico4-monado/pico4_hmd.c b/phoenix/pico4-monado/pico4_hmd.c index 45f11a7..89e7d41 100644 --- a/phoenix/pico4-monado/pico4_hmd.c +++ b/phoenix/pico4-monado/pico4_hmd.c @@ -217,6 +217,9 @@ struct pico4_hmd //! Lens separation read from the IPD MCU, metres. double ipd_m; //!< Written by the IPD thread, read with relaxed atomics. struct os_thread_helper ipd_oth; + int initial_brightness; //!< -1 if the MCU or its worker is unavailable. + int target_brightness; //!< Zero until a runtime requests a level. + bool in_standby; //! The newest gyro sample's time: the 3DoF filter needs each one later than the last. int64_t last_gyro_ns; @@ -772,6 +775,28 @@ station_thread(void *ptr) return NULL; } +// The fan may wake the GD32 at its firmware level. Check the actual brightness periodically +// and after standby, even when SteamVR's setting has stayed the same. +static void +update_brightness(struct pico4_hmd *h, int *applied, bool verify) +{ + int target = __atomic_load_n(&h->target_brightness, __ATOMIC_RELAXED); + if (target == 0 || __atomic_load_n(&h->in_standby, __ATOMIC_ACQUIRE)) { + *applied = -1; + return; + } + if (verify || *applied < 0) { + if (pico4_brightness_read(PICO4_IPD_DEVICE, applied) != 0) { + *applied = -1; + return; + } + } + if (*applied != target && !__atomic_load_n(&h->in_standby, __ATOMIC_ACQUIRE) && + pico4_brightness_write(PICO4_IPD_DEVICE, target) == 0) { + *applied = target; + } +} + /*! * The lens separation while the session runs: pico4-lens moves the lenses. A move ends within * 0.05 mm of its target and readings jitter by 0.02 mm, so only a real move counts. @@ -781,16 +806,24 @@ ipd_thread(void *ptr) { struct pico4_hmd *h = (struct pico4_hmd *)ptr; + int applied = -1; + uint64_t next_poll = 0; os_thread_helper_name(&h->ipd_oth, "PICO 4 lenses"); os_thread_helper_lock(&h->ipd_oth); while (os_thread_helper_is_running_locked(&h->ipd_oth)) { os_thread_helper_unlock(&h->ipd_oth); - os_nanosleep(U_TIME_1S_IN_NS); - double now, known; - __atomic_load(&h->ipd_m, &known, __ATOMIC_RELAXED); - if (pico4_ipd_read(PICO4_IPD_DEVICE, &now) == 0 && fabs(now - known) > 0.0002) { - __atomic_store(&h->ipd_m, &now, __ATOMIC_RELAXED); - P4_INFO(h, "IPD %.2f mm", now * 1000); + os_nanosleep(100 * U_TIME_1MS_IN_NS); + uint64_t ns = os_monotonic_get_ns(); + bool poll = ns >= next_poll; + update_brightness(h, &applied, poll); + if (poll) { + next_poll = ns + U_TIME_1S_IN_NS; + double now, known; + __atomic_load(&h->ipd_m, &known, __ATOMIC_RELAXED); + if (pico4_ipd_read(PICO4_IPD_DEVICE, &now) == 0 && fabs(now - known) > 0.0002) { + __atomic_store(&h->ipd_m, &now, __ATOMIC_RELAXED); + P4_INFO(h, "IPD %.2f mm", now * 1000); + } } os_thread_helper_lock(&h->ipd_oth); } @@ -1374,11 +1407,16 @@ pico4_hmd_create(void) P4_INFO(h, "No IPD from the MCU (%s), using the runtime default", strerror(-ipd_ret)); } else { P4_INFO(h, "IPD %.2f mm", h->ipd_m * 1000); + } + set_backlight(h, true); // A session that ended in standby without closing. + h->initial_brightness = -1; + pico4_brightness_read(PICO4_IPD_DEVICE, &h->initial_brightness); + if (ipd_ret == 0 || h->initial_brightness >= 0) { if (os_thread_helper_init(&h->ipd_oth) != 0 || os_thread_helper_start(&h->ipd_oth, ipd_thread, h) != 0) { - P4_WARN(h, "The IPD stays as it is for this session"); + h->initial_brightness = -1; + P4_WARN(h, "Cannot monitor the lens separation or adjust the backlight"); } } - set_backlight(h, true); // A session that ended in standby without closing. if (os_thread_helper_init(&h->oth) != 0 || !setup_display(h)) { P4_ERROR(h, "Failed to set up the device"); goto fail; @@ -1721,11 +1759,33 @@ pico4_hmd_ipd(struct xrt_device *xdev, float *out_ipd_m) return ipd_m > 0; } +int +pico4_hmd_initial_brightness(struct xrt_device *xdev) +{ + if (xdev == NULL || xdev->get_tracked_pose != pico4_hmd_get_tracked_pose) { + return -1; + } + return pico4_hmd(xdev)->initial_brightness; +} + +void +pico4_hmd_set_brightness(struct xrt_device *xdev, int level) +{ + if (pico4_hmd_initial_brightness(xdev) >= 0 && level >= PICO4_MIN_BRIGHTNESS && level <= 100) { + __atomic_store_n(&pico4_hmd(xdev)->target_brightness, level, __ATOMIC_RELAXED); + } +} + void pico4_hmd_standby(struct xrt_device *xdev, bool standby) { if (xdev != NULL && xdev->get_tracked_pose == pico4_hmd_get_tracked_pose) { - set_backlight(pico4_hmd(xdev), !standby); + struct pico4_hmd *h = pico4_hmd(xdev); + if (standby) { + __atomic_store_n(&h->in_standby, true, __ATOMIC_RELEASE); + } + set_backlight(h, !standby); + __atomic_store_n(&h->in_standby, standby, __ATOMIC_RELEASE); } } diff --git a/phoenix/pico4-monado/pico4_interface.h b/phoenix/pico4-monado/pico4_interface.h index 7a68e35..1c6d1a1 100644 --- a/phoenix/pico4-monado/pico4_interface.h +++ b/phoenix/pico4-monado/pico4_interface.h @@ -121,6 +121,14 @@ pico4_hmd_detect_floor(struct xrt_device *xdev); bool pico4_hmd_ipd(struct xrt_device *xdev, float *out_ipd_m); +//! Hardware backlight level at startup, or -1 if the driver cannot adjust it. +int +pico4_hmd_initial_brightness(struct xrt_device *xdev); + +//! Queue the user's backlight level for the MCU worker. +void +pico4_hmd_set_brightness(struct xrt_device *xdev, int level); + /*! * SteamVR's standby: nobody wears the headset. Its compositor keeps the panels on with black * frames and holds the display, so the backlight goes off here. diff --git a/phoenix/pico4-monado/pico4_ipd.c b/phoenix/pico4-monado/pico4_ipd.c index bb99a9e..8094255 100644 --- a/phoenix/pico4-monado/pico4_ipd.c +++ b/phoenix/pico4-monado/pico4_ipd.c @@ -2,7 +2,7 @@ // SPDX-License-Identifier: BSL-1.0 /*! * @file - * @brief Read the PICO 4 lens separation from the GD32 MCU (see pico4_ipd.h). + * @brief Read lens separation and control the panel backlight from the GD32 MCU (see pico4_ipd.h). * @ingroup drv_pico4 */ @@ -41,8 +41,8 @@ pico4_ipd_parse_position(const uint8_t *reply, size_t size, double *out_ipd_m) return false; } -int -pico4_ipd_read(const char *device, double *out_ipd_m) +static int +transact(const char *device, const uint8_t request[6], uint8_t *reply, size_t length) { int fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC); if (fd < 0) { @@ -75,18 +75,17 @@ pico4_ipd_read(const char *device, double *out_ipd_m) goto out; } tcflush(fd, TCIOFLUSH); - if (write(fd, pico4_ipd_get_position, sizeof(pico4_ipd_get_position)) != sizeof(pico4_ipd_get_position)) { + if (write(fd, request, 6) != 6) { ret = -EIO; goto out; } - uint8_t reply[16]; size_t n = 0; ret = -ETIMEDOUT; for (int i = 0; i < 200 && ret == -ETIMEDOUT; i++) { const struct timespec ms = {0, 1000 * 1000}; nanosleep(&ms, NULL); - ssize_t r = read(fd, reply + n, sizeof(reply) - n); + ssize_t r = read(fd, reply + n, length - n); if (r > 0) { n += (size_t)r; } @@ -94,11 +93,49 @@ pico4_ipd_read(const char *device, double *out_ipd_m) while (n > 0 && reply[0] != 0x79) { memmove(reply, reply + 1, --n); } - if (n >= 6) { - ret = pico4_ipd_parse_position(reply, n, out_ipd_m) ? 0 : -EPROTO; + if (n == length) { + ret = 0; } } out: close(fd); return ret; } + +int +pico4_ipd_read(const char *device, double *out_ipd_m) +{ + uint8_t reply[6]; + int ret = transact(device, pico4_ipd_get_position, reply, sizeof(reply)); + if (ret != 0) { + return ret; + } + return pico4_ipd_parse_position(reply, sizeof(reply), out_ipd_m) ? 0 : -EPROTO; +} + +int +pico4_brightness_read(const char *device, int *out_level) +{ + const uint8_t request[] = {0xA0, 0x06, 0, 0, 0, 0xA6}; + uint8_t reply[4]; + int ret = transact(device, request, reply, sizeof(reply)); + if (ret != 0) { + return ret; + } + if ((reply[0] ^ reply[1] ^ reply[2]) != reply[3] || reply[2] != 0 || reply[1] > 100) { + return -EPROTO; + } + *out_level = reply[1]; + return 0; +} + +int +pico4_brightness_write(const char *device, int level) +{ + if (level < PICO4_MIN_BRIGHTNESS || level > 100) { + return -EINVAL; + } + const uint8_t request[] = {0xA0, 0x05, 0, (uint8_t)level, 0, (uint8_t)(0xA5 ^ level)}; + uint8_t ack; + return transact(device, request, &ack, sizeof(ack)); +} diff --git a/phoenix/pico4-monado/pico4_ipd.h b/phoenix/pico4-monado/pico4_ipd.h index 4ae30ef..9f29477 100644 --- a/phoenix/pico4-monado/pico4_ipd.h +++ b/phoenix/pico4-monado/pico4_ipd.h @@ -5,10 +5,8 @@ * @brief Read the PICO 4 lens separation (IPD) from the GD32 MCU. * * The MCU on the UART behind /dev/ttyHS1 moves the lenses, drives the panel - * backlight, runs the fan and takes firmware updates. This module only ever - * sends its read-only "get IPD position" command; it has no way to build any - * other frame. pico4-fan shares the port: both hold an exclusive flock() on it - * for each transaction. + * backlight and runs the fan. IPD and brightness transactions share the same + * exclusive flock() with pico4-fan. * * @ingroup drv_pico4 */ @@ -25,7 +23,7 @@ extern "C" { #define PICO4_IPD_DEVICE "/dev/ttyHS1" -//! The one request this module sends: A0 08 00 00 00, XOR checksum. +//! Lens position request: A0 08 00 00 00, XOR checksum. extern const uint8_t pico4_ipd_get_position[6]; /*! @@ -43,6 +41,17 @@ pico4_ipd_parse_position(const uint8_t *reply, size_t size, double *out_ipd_m); int pico4_ipd_read(const char *device, double *out_ipd_m); +//! The dashboard's usable backlight range. Zero is reserved for screen-off. +#define PICO4_MIN_BRIGHTNESS 30 + +//! Get the GD32 backlight level, 0..100. Returns zero on success or a negative errno. +int +pico4_brightness_read(const char *device, int *out_level); + +//! Set a level in the dashboard range and wait for the MCU's acknowledgement. +int +pico4_brightness_write(const char *device, int level); + #ifdef __cplusplus } #endif diff --git a/phoenix/pico4-monado/test_pico4_lens.c b/phoenix/pico4-monado/test_pico4_lens.c index 54e8e75..991ca52 100644 --- a/phoenix/pico4-monado/test_pico4_lens.c +++ b/phoenix/pico4-monado/test_pico4_lens.c @@ -3,7 +3,7 @@ /* * Unit test of the lens model and the IPD reply parser, with synthetic values * (no device data): file parsing, the polynomial, chromatic aberration, the - * fitted radius, the field of view, the only MCU request the driver sends and + * fitted radius, the field of view, the MCU requests the driver sends and * the port lock it shares with pico4-fan. */ @@ -11,6 +11,9 @@ #include "pico4_lens.h" #include +#include +#include +#include #include #include #include @@ -122,6 +125,37 @@ static const char *sdk_other_eqn = "{\"keyName\": \"sdk_lens_distortionMode\", \"keyValue\": \"1\"}]}"; #undef SDK_COEFFICIENT +// A pseudo-terminal with a GD32 peer: check all six request bytes and return the wire reply. +static void +brightness_exchange(bool write_level, const uint8_t request[6], const uint8_t *reply, size_t size, int expected) +{ + int master = posix_openpt(O_RDWR | O_NOCTTY); + if (master < 0 || grantpt(master) != 0 || unlockpt(master) != 0) { + EXPECT(false); + return; + } + char *device = ptsname(master); + int slave = open(device, O_RDWR | O_NOCTTY); // Hold the PTY open until the transaction ends. + pid_t peer = fork(); + if (peer == 0) { + struct pollfd pfd = {.fd = master, .events = POLLIN}; + uint8_t received[6]; + bool ok = poll(&pfd, 1, 2000) == 1 && read(master, received, 6) == 6 && + memcmp(received, request, 6) == 0 && write(master, reply, size) == (ssize_t)size; + _exit(ok ? 0 : 1); + } + int level = -1; + int ret = write_level ? pico4_brightness_write(device, request[3]) : pico4_brightness_read(device, &level); + EXPECT(ret == expected); + if (!write_level) { + EXPECT(level == (expected == 0 ? reply[1] : -1)); + } + int status = 0; + EXPECT(peer > 0 && waitpid(peer, &status, 0) == peer && WIFEXITED(status) && WEXITSTATUS(status) == 0); + close(master); + close(slave); +} + int main(void) { @@ -289,7 +323,7 @@ main(void) EXPECT(NEAR(l, -atan(0.023 / 0.025)) && NEAR(r, atan(0.023 / 0.025))); EXPECT(NEAR(u, atan(0.023 / 0.025)) && NEAR(d, -atan(0.023 / 0.025))); - // The only MCU request: A0 08 00 00 00 with its XOR checksum. + // IPD request: A0 08 00 00 00 with its XOR checksum. EXPECT(pico4_ipd_get_position[0] == 0xA0 && pico4_ipd_get_position[1] == 0x08 && pico4_ipd_get_position[5] == 0xA8); // Reply: 79, position 6350 (63.50 mm) and ADC 1800 in either order, XOR checksum. @@ -307,6 +341,14 @@ main(void) reply[5] ^= 1; EXPECT(!pico4_ipd_parse_position(reply, sizeof(reply), &ipd)); + const uint8_t get[] = {0xa0, 6, 0, 0, 0, 0xa6}, set[] = {0xa0, 5, 0, 30, 0, 0xbb}; + uint8_t backlight[] = {0x79, 63, 0, 0x46}; + brightness_exchange(false, get, backlight, 4, 0); + brightness_exchange(true, set, backlight, 1, 0); + backlight[3] ^= 1; + brightness_exchange(false, get, backlight, 4, -EPROTO); + EXPECT(pico4_brightness_write("", 29) == -EINVAL && pico4_brightness_write("", 101) == -EINVAL); + // The port lock shared with pico4-fan: while another descriptor holds it, the read // gives up after a second; once it is free the read goes on (and a plain file then // fails at tcgetattr). From 6ffbd857257a8e38b2465cc48bc28ef419537d24 Mon Sep 17 00:00:00 2001 From: loxck Date: Sun, 29 Nov 2026 19:48:44 -1000 Subject: [PATCH 2/3] docs(pico4-monado): clarify backlight and UART comments --- phoenix/pico4-monado/PKGBUILD | 10 +++++----- phoenix/pico4-monado/monado-steamvr-brightness.patch | 4 ++-- phoenix/pico4-monado/pico4_hmd.c | 5 ++--- phoenix/pico4-monado/pico4_ipd.c | 2 +- phoenix/pico4-monado/pico4_ipd.h | 9 +++------ phoenix/pico4-monado/test_pico4_lens.c | 9 ++------- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/phoenix/pico4-monado/PKGBUILD b/phoenix/pico4-monado/PKGBUILD index df6d751..8c4d402 100644 --- a/phoenix/pico4-monado/PKGBUILD +++ b/phoenix/pico4-monado/PKGBUILD @@ -71,17 +71,17 @@ sha256sums=(SKIP '41be8ce4a88a79a8f2b98cb82a8a4bcc4693890410acc1e9776bd16d347310f9' '079215437455bb24dabf36ca8598a43e69b25b7fbb20caebf8850fd7d137dc4e' '4ea7235b043e92d3ccb9a21277868a7afb2d8963a03d93a1623de8681a136ac6' - '48f9760ee10780cc29aee6f5fa0862d1f5b1b9d3257cec0da231c05022ba43ce' + 'a32fd915079777a172288a3390844a1737450b411c6bb57e7ee6a1d90e6c160c' '90e9836e6a487cc1aefa62432f16db6c22943e7ed1b14a3817e28b3c7bf5adaa' - 'e8230e2fd03ad77c796bd7dcea0cc85ffb9a6e887456f0f605983459eb6b62bc' + '3040f8747a0d19593822f97addb668c2cefa94eb6b8270dc3b307904f06ae4c0' '44160658b28ea1bb24c6c2fc9f378fba14ffe6b1ae4ae2e8b9cbbb44c5ce96ac' '5a5317c24d19d91ac502707d6fa8e391d4ba7e1c93875bc8b4f77f14bb84a712' '3d3a356292827bde4644810731386f366e4f2afa029ee36fec9ecc1db4692363' '38f67d51128d14b8d8600b64d90a500b84c3d8650b7bc7a81579105f3cc998de' 'f50a147b36ef42f58ee25e47e21f2a2665a4bebc54dcaae54dbc2412d0879ab8' - 'ac40b4f6c4a6ca12d2ebf870ad20f31076e5d0b1c61f49e3f535b3986022a84f' - 'f52d2669f6b5d9cdc2feddece8b6018bf51ed56f657cd2ee489b5ab48fae4e26' - '52edf1e445759dec6a92aa2ff8c13452402e5181c14ff781f818c573556328d1' + '3f2108c57acc9cf9e509d538e0051d26b430791fc535c6cc188d283e61d84569' + '32f824248f7d102c0783ad27083c59c4097f44db073843a463f54da01efb9c6f' + '195eddef3869e2a4893a648009406085364294a88e06779328ca630ae37d86f9' '787136e02024254b355d1aae427799c206456307040ecc29d543962179364a96' '732a6738b8e89063d247c820f0fdf6de7be2b2b9435e751886450474ffa7a2d0' 'c6f9b33382bc9abb75942be3588072cb8acc1944116e3f3406bd9dce8565ff48' diff --git a/phoenix/pico4-monado/monado-steamvr-brightness.patch b/phoenix/pico4-monado/monado-steamvr-brightness.patch index 175cba9..ebf716a 100644 --- a/phoenix/pico4-monado/monado-steamvr-brightness.patch +++ b/phoenix/pico4-monado/monado-steamvr-brightness.patch @@ -1,5 +1,5 @@ diff --git a/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp b/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp -index 2d07b82..1d4ccaa 100644 +index 2d07b82..4061c4d 100644 --- a/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp +++ b/src/xrt/state_trackers/steamvr_drv/ovrd_driver.cpp @@ -83,6 +83,10 @@ pico4_hmd_detect_floor(struct xrt_device *xdev) __attribute__((weak)); @@ -66,7 +66,7 @@ index 2d07b82..1d4ccaa 100644 + if (m_hasBrightness) { + float gain; + if (!read_brightness_gain(&gain)) { -+ // The runtime's default gain is outside this display's range. ++ // Use the current backlight level, or the GD32 wake level if it is below the slider range. + level = level >= 30 ? level : 63; + gain = powf(level / 100.f, kBrightnessGamma); + vr::VRSettings()->SetFloat(vr::k_pch_SteamVR_Section, "analogGain", gain); diff --git a/phoenix/pico4-monado/pico4_hmd.c b/phoenix/pico4-monado/pico4_hmd.c index 89e7d41..20bee09 100644 --- a/phoenix/pico4-monado/pico4_hmd.c +++ b/phoenix/pico4-monado/pico4_hmd.c @@ -217,7 +217,7 @@ struct pico4_hmd //! Lens separation read from the IPD MCU, metres. double ipd_m; //!< Written by the IPD thread, read with relaxed atomics. struct os_thread_helper ipd_oth; - int initial_brightness; //!< -1 if the MCU or its worker is unavailable. + int initial_brightness; int target_brightness; //!< Zero until a runtime requests a level. bool in_standby; @@ -775,8 +775,7 @@ station_thread(void *ptr) return NULL; } -// The fan may wake the GD32 at its firmware level. Check the actual brightness periodically -// and after standby, even when SteamVR's setting has stayed the same. +// pico4-fan can reset the backlight to 63 when it wakes the GD32. Recheck even if the target is unchanged. static void update_brightness(struct pico4_hmd *h, int *applied, bool verify) { diff --git a/phoenix/pico4-monado/pico4_ipd.c b/phoenix/pico4-monado/pico4_ipd.c index 8094255..a86b4fe 100644 --- a/phoenix/pico4-monado/pico4_ipd.c +++ b/phoenix/pico4-monado/pico4_ipd.c @@ -2,7 +2,7 @@ // SPDX-License-Identifier: BSL-1.0 /*! * @file - * @brief Read lens separation and control the panel backlight from the GD32 MCU (see pico4_ipd.h). + * @brief UART transactions for PICO 4 lens separation and panel brightness. * @ingroup drv_pico4 */ diff --git a/phoenix/pico4-monado/pico4_ipd.h b/phoenix/pico4-monado/pico4_ipd.h index 9f29477..49f4086 100644 --- a/phoenix/pico4-monado/pico4_ipd.h +++ b/phoenix/pico4-monado/pico4_ipd.h @@ -2,11 +2,9 @@ // SPDX-License-Identifier: BSL-1.0 /*! * @file - * @brief Read the PICO 4 lens separation (IPD) from the GD32 MCU. + * @brief Lens separation (IPD) and panel brightness on the PICO 4's GD32 MCU. * - * The MCU on the UART behind /dev/ttyHS1 moves the lenses, drives the panel - * backlight and runs the fan. IPD and brightness transactions share the same - * exclusive flock() with pico4-fan. + * Hold an exclusive flock() for each request and reply. pico4-fan uses the same UART. * * @ingroup drv_pico4 */ @@ -23,7 +21,6 @@ extern "C" { #define PICO4_IPD_DEVICE "/dev/ttyHS1" -//! Lens position request: A0 08 00 00 00, XOR checksum. extern const uint8_t pico4_ipd_get_position[6]; /*! @@ -41,7 +38,7 @@ pico4_ipd_parse_position(const uint8_t *reply, size_t size, double *out_ipd_m); int pico4_ipd_read(const char *device, double *out_ipd_m); -//! The dashboard's usable backlight range. Zero is reserved for screen-off. +//! Lower levels can be too dim to see the dashboard. Level 0 turns the backlight off. #define PICO4_MIN_BRIGHTNESS 30 //! Get the GD32 backlight level, 0..100. Returns zero on success or a negative errno. diff --git a/phoenix/pico4-monado/test_pico4_lens.c b/phoenix/pico4-monado/test_pico4_lens.c index 991ca52..555c71a 100644 --- a/phoenix/pico4-monado/test_pico4_lens.c +++ b/phoenix/pico4-monado/test_pico4_lens.c @@ -1,10 +1,7 @@ // Copyright 2026, overpressure contributors. // SPDX-License-Identifier: BSL-1.0 /* - * Unit test of the lens model and the IPD reply parser, with synthetic values - * (no device data): file parsing, the polynomial, chromatic aberration, the - * fitted radius, the field of view, the MCU requests the driver sends and - * the port lock it shares with pico4-fan. + * Tests for the lens model and the GD32 lens and backlight commands, using synthetic data. */ #include "pico4_ipd.h" @@ -125,7 +122,6 @@ static const char *sdk_other_eqn = "{\"keyName\": \"sdk_lens_distortionMode\", \"keyValue\": \"1\"}]}"; #undef SDK_COEFFICIENT -// A pseudo-terminal with a GD32 peer: check all six request bytes and return the wire reply. static void brightness_exchange(bool write_level, const uint8_t request[6], const uint8_t *reply, size_t size, int expected) { @@ -135,7 +131,7 @@ brightness_exchange(bool write_level, const uint8_t request[6], const uint8_t *r return; } char *device = ptsname(master); - int slave = open(device, O_RDWR | O_NOCTTY); // Hold the PTY open until the transaction ends. + int slave = open(device, O_RDWR | O_NOCTTY); // Keep the master from reporting a hangup before the driver opens the slave. pid_t peer = fork(); if (peer == 0) { struct pollfd pfd = {.fd = master, .events = POLLIN}; @@ -323,7 +319,6 @@ main(void) EXPECT(NEAR(l, -atan(0.023 / 0.025)) && NEAR(r, atan(0.023 / 0.025))); EXPECT(NEAR(u, atan(0.023 / 0.025)) && NEAR(d, -atan(0.023 / 0.025))); - // IPD request: A0 08 00 00 00 with its XOR checksum. EXPECT(pico4_ipd_get_position[0] == 0xA0 && pico4_ipd_get_position[1] == 0x08 && pico4_ipd_get_position[5] == 0xA8); // Reply: 79, position 6350 (63.50 mm) and ADC 1800 in either order, XOR checksum. From 0d2d5780333c019e322c2abe298e3424e9579d84 Mon Sep 17 00:00:00 2001 From: Xenolphthalein <21215869+Xenolphthalein@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:10:29 +0100 Subject: [PATCH 3/3] docs(pico4-monado): the GD32 frames the driver can build, an allowlist of PICO OS's three --- phoenix/pico4-monado/PKGBUILD | 2 +- phoenix/pico4-monado/pico4_ipd.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/phoenix/pico4-monado/PKGBUILD b/phoenix/pico4-monado/PKGBUILD index 8c4d402..7ff39b5 100644 --- a/phoenix/pico4-monado/PKGBUILD +++ b/phoenix/pico4-monado/PKGBUILD @@ -79,7 +79,7 @@ sha256sums=(SKIP '3d3a356292827bde4644810731386f366e4f2afa029ee36fec9ecc1db4692363' '38f67d51128d14b8d8600b64d90a500b84c3d8650b7bc7a81579105f3cc998de' 'f50a147b36ef42f58ee25e47e21f2a2665a4bebc54dcaae54dbc2412d0879ab8' - '3f2108c57acc9cf9e509d538e0051d26b430791fc535c6cc188d283e61d84569' + '3fd32bd8320986f415ba1f049dcd0740f634687dd8e9dbf06c36c4380dd9b8f1' '32f824248f7d102c0783ad27083c59c4097f44db073843a463f54da01efb9c6f' '195eddef3869e2a4893a648009406085364294a88e06779328ca630ae37d86f9' '787136e02024254b355d1aae427799c206456307040ecc29d543962179364a96' diff --git a/phoenix/pico4-monado/pico4_ipd.h b/phoenix/pico4-monado/pico4_ipd.h index 49f4086..24a33a9 100644 --- a/phoenix/pico4-monado/pico4_ipd.h +++ b/phoenix/pico4-monado/pico4_ipd.h @@ -4,7 +4,11 @@ * @file * @brief Lens separation (IPD) and panel brightness on the PICO 4's GD32 MCU. * - * Hold an exclusive flock() for each request and reply. pico4-fan uses the same UART. + * The MCU on the UART behind /dev/ttyHS1 moves the lenses, drives the panel backlight, runs the + * fan and takes firmware updates. This module builds only three of PICO OS's frames: the position + * query (A0 08), GetBrightness (A0 06) and SetBrightness (A0 05) with a level of + * PICO4_MIN_BRIGHTNESS to 100; it has no way to build any other frame. pico4-fan shares the port: + * both hold an exclusive flock() on it for each transaction. * * @ingroup drv_pico4 */