feature: Switch console logging to Unix sockets
This commit is contained in:
parent
682daf4385
commit
258b7c4f26
3 changed files with 100 additions and 55 deletions
0
C
Normal file
0
C
Normal file
|
@ -8,6 +8,10 @@ find_package(json-c REQUIRED CONFIG)
|
|||
add_subdirectory(modules/hidapi)
|
||||
add_subdirectory(modules/Fusion/Fusion)
|
||||
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
add_definitions(-DNDEBUG)
|
||||
endif()
|
||||
|
||||
add_library(
|
||||
xrealAirLibrary
|
||||
src/crc32.c
|
||||
|
|
125
src/driver.c
125
src/driver.c
|
@ -1,7 +1,8 @@
|
|||
//
|
||||
// Created by thejackimonster on 29.03.23.
|
||||
// Created by thejackimonster on 29.03.23. Modified by UnrealXR on 08.06.25.
|
||||
//
|
||||
// Copyright (c) 2023-2024 thejackimonster. All rights reserved.
|
||||
// Copyright (c) 2025 UnrealXR. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -25,68 +26,79 @@
|
|||
#include "device_imu.h"
|
||||
#include "device_mcu.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
void test_imu(uint64_t timestamp,
|
||||
device_imu_event_type event,
|
||||
const device_imu_ahrs_type* ahrs) {
|
||||
static device_imu_quat_type old;
|
||||
static float dmax = -1.0f;
|
||||
enum XrealDriverCommand {
|
||||
ROLL,
|
||||
PITCH,
|
||||
YAW,
|
||||
GENERIC_MESSAGE,
|
||||
BRIGHTNESS_UP,
|
||||
BRIGHTNESS_DOWN,
|
||||
};
|
||||
|
||||
int sock_fd;
|
||||
|
||||
void imu_event(uint64_t timestamp, device_imu_event_type event, const device_imu_ahrs_type* ahrs) {
|
||||
if (event != DEVICE_IMU_EVENT_UPDATE) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_imu_quat_type q = device_imu_get_orientation(ahrs);
|
||||
|
||||
const float dx = (old.x - q.x) * (old.x - q.x);
|
||||
const float dy = (old.y - q.y) * (old.y - q.y);
|
||||
const float dz = (old.z - q.z) * (old.z - q.z);
|
||||
const float dw = (old.w - q.w) * (old.w - q.w);
|
||||
|
||||
const float d = sqrtf(dx*dx + dy*dy + dz*dz + dw*dw);
|
||||
|
||||
if (dmax < 0.0f) {
|
||||
dmax = 0.0f;
|
||||
} else {
|
||||
dmax = (d > dmax? d : dmax);
|
||||
}
|
||||
|
||||
device_imu_euler_type e = device_imu_get_euler(q);
|
||||
|
||||
if (d >= 0.00005f) {
|
||||
device_imu_euler_type e0 = device_imu_get_euler(old);
|
||||
char status_event = 0;
|
||||
uint32_t encoded_data;
|
||||
|
||||
printf("Roll: %f; Pitch: %f; Yaw: %f\n", e0.roll, e0.pitch, e0.yaw);
|
||||
printf("Roll: %f; Pitch: %f; Yaw: %f\n", e.roll, e.pitch, e.yaw);
|
||||
printf("D = %f; ( %f )\n", d, dmax);
|
||||
status_event = ROLL;
|
||||
memcpy(&encoded_data, &e.roll, sizeof(encoded_data));
|
||||
encoded_data = htonl(encoded_data);
|
||||
write(sock_fd, &status_event, 1);
|
||||
write(sock_fd, &encoded_data, sizeof(encoded_data));
|
||||
|
||||
printf("X: %f; Y: %f; Z: %f; W: %f;\n", old.x, old.y, old.z, old.w);
|
||||
printf("X: %f; Y: %f; Z: %f; W: %f;\n", q.x, q.y, q.z, q.w);
|
||||
} else {
|
||||
printf("Roll: %.2f; Pitch: %.2f; Yaw: %.2f\n", e.roll, e.pitch, e.yaw);
|
||||
}
|
||||
status_event = PITCH;
|
||||
memcpy(&encoded_data, &e.pitch, sizeof(encoded_data));
|
||||
encoded_data = htonl(encoded_data);
|
||||
write(sock_fd, &status_event, 1);
|
||||
write(sock_fd, &encoded_data, sizeof(encoded_data));
|
||||
|
||||
old = q;
|
||||
status_event = YAW;
|
||||
memcpy(&encoded_data, &e.yaw, sizeof(encoded_data));
|
||||
encoded_data = htonl(encoded_data);
|
||||
write(sock_fd, &status_event, 1);
|
||||
write(sock_fd, &encoded_data, sizeof(encoded_data));
|
||||
}
|
||||
|
||||
void test_mcu(uint64_t timestamp,
|
||||
device_mcu_event_type event,
|
||||
uint8_t brightness,
|
||||
const char* msg) {
|
||||
void mcu_event(uint64_t timestamp, device_mcu_event_type event, uint8_t brightness, const char* msg) {
|
||||
char status_event = 0;
|
||||
|
||||
switch (event) {
|
||||
case DEVICE_MCU_EVENT_MESSAGE:
|
||||
printf("Message: `%s`\n", msg);
|
||||
status_event = GENERIC_MESSAGE;
|
||||
write(sock_fd, &status_event, 1);
|
||||
|
||||
uint32_t msg_len = strlen(msg);
|
||||
uint32_t len_net = htonl(msg_len);
|
||||
|
||||
write(sock_fd, &len_net, sizeof(len_net));
|
||||
write(sock_fd, msg, msg_len);
|
||||
break;
|
||||
case DEVICE_MCU_EVENT_BRIGHTNESS_UP:
|
||||
printf("Increase Brightness: %u\n", brightness);
|
||||
status_event = BRIGHTNESS_UP;
|
||||
write(sock_fd, &status_event, 1);
|
||||
write(sock_fd, &brightness, 1);
|
||||
break;
|
||||
case DEVICE_MCU_EVENT_BRIGHTNESS_DOWN:
|
||||
printf("Decrease Brightness: %u\n", brightness);
|
||||
status_event = BRIGHTNESS_DOWN;
|
||||
write(sock_fd, &status_event, 1);
|
||||
write(sock_fd, &brightness, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -94,6 +106,29 @@ void test_mcu(uint64_t timestamp,
|
|||
}
|
||||
|
||||
int main(int argc, const char** argv) {
|
||||
printf("Connecting to Unix socket\n");
|
||||
char *unix_socket_path = getenv("UNREALXR_NREAL_DRIVER_SOCK");
|
||||
|
||||
if (unix_socket_path == NULL) {
|
||||
printf("Unix socket not set!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
struct sockaddr_un server_addr;
|
||||
server_addr.sun_family = AF_UNIX;
|
||||
strcpy(server_addr.sun_path, unix_socket_path);
|
||||
|
||||
int connection_result = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
|
||||
if (connection_result == -1) {
|
||||
perror("Failed to connect to Unix socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Connected to Unix socket\n");
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == -1) {
|
||||
|
@ -103,24 +138,30 @@ int main(int argc, const char** argv) {
|
|||
|
||||
if (pid == 0) {
|
||||
device_imu_type dev_imu;
|
||||
if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev_imu, test_imu)) {
|
||||
if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev_imu, imu_event)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Clearing and calibrating IMU\n");
|
||||
device_imu_clear(&dev_imu);
|
||||
device_imu_calibrate(&dev_imu, 1000, true, true, false);
|
||||
printf("Finished IMU tasks. Beginning data read\n");
|
||||
|
||||
while (DEVICE_IMU_ERROR_NO_ERROR == device_imu_read(&dev_imu, -1));
|
||||
device_imu_close(&dev_imu);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
int status = 0;
|
||||
|
||||
device_mcu_type dev_mcu;
|
||||
if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev_mcu, test_mcu)) {
|
||||
if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev_mcu, mcu_event)) {
|
||||
status = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf("Reading MCU data\n");
|
||||
|
||||
device_mcu_clear(&dev_mcu);
|
||||
while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev_mcu, -1));
|
||||
device_mcu_close(&dev_mcu);
|
||||
|
|
Reference in a new issue