Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions phoenix/pico4-monado/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -71,16 +71,17 @@ sha256sums=(SKIP
'41be8ce4a88a79a8f2b98cb82a8a4bcc4693890410acc1e9776bd16d347310f9'
'079215437455bb24dabf36ca8598a43e69b25b7fbb20caebf8850fd7d137dc4e'
'4ea7235b043e92d3ccb9a21277868a7afb2d8963a03d93a1623de8681a136ac6'
'e712b477138e19966b8a4ba62758c096be2979664c7d97f40c1a1c91feeb5b8b'
'7b45549338fbf50e7db10762b105108382c1aa6644cf27b0a763a0db79b935a3'
'a32fd915079777a172288a3390844a1737450b411c6bb57e7ee6a1d90e6c160c'
'90e9836e6a487cc1aefa62432f16db6c22943e7ed1b14a3817e28b3c7bf5adaa'
'3040f8747a0d19593822f97addb668c2cefa94eb6b8270dc3b307904f06ae4c0'
'44160658b28ea1bb24c6c2fc9f378fba14ffe6b1ae4ae2e8b9cbbb44c5ce96ac'
'5a5317c24d19d91ac502707d6fa8e391d4ba7e1c93875bc8b4f77f14bb84a712'
'3d3a356292827bde4644810731386f366e4f2afa029ee36fec9ecc1db4692363'
'38f67d51128d14b8d8600b64d90a500b84c3d8650b7bc7a81579105f3cc998de'
'f50a147b36ef42f58ee25e47e21f2a2665a4bebc54dcaae54dbc2412d0879ab8'
'e80568bc5a6f6b0def8c80f5ad8facb8569bd92759c6c540cfb9751f28b52195'
'5862557f9d2abfc932e4532741a8bcf5f58dc2c431a0d89805fa81461368880d'
'42d4a1ec923f5a32b93847370cf2c63e9b07a5d68d51c8a2e5214d995d51c1a0'
'3fd32bd8320986f415ba1f049dcd0740f634687dd8e9dbf06c36c4380dd9b8f1'
'32f824248f7d102c0783ad27083c59c4097f44db073843a463f54da01efb9c6f'
'195eddef3869e2a4893a648009406085364294a88e06779328ca630ae37d86f9'
'787136e02024254b355d1aae427799c206456307040ecc29d543962179364a96'
'732a6738b8e89063d247c820f0fdf6de7be2b2b9435e751886450474ffa7a2d0'
'c6f9b33382bc9abb75942be3588072cb8acc1944116e3f3406bd9dce8565ff48'
Expand Down Expand Up @@ -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() {
Expand Down
96 changes: 96 additions & 0 deletions phoenix/pico4-monado/monado-steamvr-brightness.patch
Original file line number Diff line number Diff line change
@@ -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..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));
// 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)) {
+ // 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);
+ }
+ 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<vr::ETrackedDeviceProperty>(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) {
77 changes: 68 additions & 9 deletions phoenix/pico4-monado/pico4_hmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;
Expand Down Expand Up @@ -772,6 +775,27 @@ station_thread(void *ptr)
return NULL;
}

// 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)
{
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.
Expand All @@ -781,16 +805,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);
}
Expand Down Expand Up @@ -1374,11 +1406,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;
Expand Down Expand Up @@ -1721,11 +1758,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);
}
}

Expand Down
8 changes: 8 additions & 0 deletions phoenix/pico4-monado/pico4_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 45 additions & 8 deletions phoenix/pico4-monado/pico4_ipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 UART transactions for PICO 4 lens separation and panel brightness.
* @ingroup drv_pico4
*/

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -75,30 +75,67 @@ 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;
}
// Skip anything before the reply marker.
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));
}
Loading