diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 480eb10..371dccc 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,5 @@ -add_subdirectory(debug_d3) -add_subdirectory(debug_d4) +add_subdirectory(debug_imu) +add_subdirectory(debug_mcu) add_subdirectory(mcu_firmware) diff --git a/examples/debug_d3/CMakeLists.txt b/examples/debug_imu/CMakeLists.txt similarity index 56% rename from examples/debug_d3/CMakeLists.txt rename to examples/debug_imu/CMakeLists.txt index f626a9b..1255db7 100644 --- a/examples/debug_d3/CMakeLists.txt +++ b/examples/debug_imu/CMakeLists.txt @@ -1,17 +1,17 @@ cmake_minimum_required(VERSION 3.16) -project(xrealAirDebugD3 C) +project(xrealAirDebugIMU C) set(CMAKE_C_STANDARD 17) add_executable( - xrealAirDebugD3 + xrealAirDebugIMU src/debug.c ) -target_include_directories(xrealAirDebugD3 +target_include_directories(xrealAirDebugIMU BEFORE PUBLIC ${XREAL_AIR_INCLUDE_DIR} ) -target_link_libraries(xrealAirDebugD3 +target_link_libraries(xrealAirDebugIMU ${XREAL_AIR_LIBRARY} ) diff --git a/examples/debug_d3/src/debug.c b/examples/debug_imu/src/debug.c similarity index 69% rename from examples/debug_d3/src/debug.c rename to examples/debug_imu/src/debug.c index a622a54..52873ee 100644 --- a/examples/debug_d3/src/debug.c +++ b/examples/debug_imu/src/debug.c @@ -1,7 +1,7 @@ // // Created by thejackimonster on 05.04.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 @@ -22,23 +22,23 @@ // THE SOFTWARE. // -#include "device3.h" +#include "device_imu.h" #include -void test3(uint64_t timestamp, - device3_event_type event, - const device3_ahrs_type* ahrs) { - device3_quat_type orientation; - device3_euler_type euler; +void test(uint64_t timestamp, + device_imu_event_type event, + const device_imu_ahrs_type* ahrs) { + device_imu_quat_type orientation; + device_imu_euler_type euler; switch (event) { - case DEVICE3_EVENT_INIT: + case DEVICE_IMU_EVENT_INIT: printf("Initialized\n"); break; - case DEVICE3_EVENT_UPDATE: - orientation = device3_get_orientation(ahrs); - euler = device3_get_euler(orientation); + case DEVICE_IMU_EVENT_UPDATE: + orientation = device_imu_get_orientation(ahrs); + euler = device_imu_get_euler(orientation); printf("Roll: %.2f; Pitch: %.2f; Yaw: %.2f\n", euler.roll, euler.pitch, euler.yaw); break; default: @@ -47,13 +47,13 @@ void test3(uint64_t timestamp, } int main(int argc, const char** argv) { - device3_type dev3; - if (DEVICE3_ERROR_NO_ERROR != device3_open(&dev3, test3)) { + device_imu_type dev; + if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev, test)) { return 1; } - device3_clear(&dev3); - while (DEVICE3_ERROR_NO_ERROR == device3_read(&dev3, -1)); - device3_close(&dev3); + device_imu_clear(&dev); + while (DEVICE_IMU_ERROR_NO_ERROR == device_imu_read(&dev, -1)); + device_imu_close(&dev); return 0; } diff --git a/examples/debug_d4/CMakeLists.txt b/examples/debug_mcu/CMakeLists.txt similarity index 56% rename from examples/debug_d4/CMakeLists.txt rename to examples/debug_mcu/CMakeLists.txt index 125be7d..09d04b4 100644 --- a/examples/debug_d4/CMakeLists.txt +++ b/examples/debug_mcu/CMakeLists.txt @@ -1,17 +1,17 @@ cmake_minimum_required(VERSION 3.16) -project(xrealAirDebugD4 C) +project(xrealAirDebugMCU C) set(CMAKE_C_STANDARD 17) add_executable( - xrealAirDebugD4 + xrealAirDebugMCU src/debug.c ) -target_include_directories(xrealAirDebugD4 +target_include_directories(xrealAirDebugMCU BEFORE PUBLIC ${XREAL_AIR_INCLUDE_DIR} ) -target_link_libraries(xrealAirDebugD4 +target_link_libraries(xrealAirDebugMCU ${XREAL_AIR_LIBRARY} ) diff --git a/examples/debug_d4/src/debug.c b/examples/debug_mcu/src/debug.c similarity index 72% rename from examples/debug_d4/src/debug.c rename to examples/debug_mcu/src/debug.c index df0b4af..e8bea0c 100644 --- a/examples/debug_d4/src/debug.c +++ b/examples/debug_mcu/src/debug.c @@ -1,7 +1,7 @@ // // Created by thejackimonster on 05.04.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 @@ -22,22 +22,22 @@ // THE SOFTWARE. // -#include "device4.h" +#include "device_mcu.h" #include -void test4(uint64_t timestamp, - device4_event_type event, - uint8_t brightness, - const char* msg) { +void test(uint64_t timestamp, + device_mcu_event_type event, + uint8_t brightness, + const char* msg) { switch (event) { - case DEVICE4_EVENT_MESSAGE: + case DEVICE_MCU_EVENT_MESSAGE: printf("Message: `%s`\n", msg); break; - case DEVICE4_EVENT_BRIGHTNESS_UP: + case DEVICE_MCU_EVENT_BRIGHTNESS_UP: printf("Increase Brightness: %u\n", brightness); break; - case DEVICE4_EVENT_BRIGHTNESS_DOWN: + case DEVICE_MCU_EVENT_BRIGHTNESS_DOWN: printf("Decrease Brightness: %u\n", brightness); break; default: @@ -46,13 +46,13 @@ void test4(uint64_t timestamp, } int main(int argc, const char** argv) { - device4_type dev4; - if (DEVICE4_ERROR_NO_ERROR != device4_open(&dev4, test4)) { + device_mcu_type dev; + if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev, test)) { return 1; } - device4_clear(&dev4); - while (DEVICE4_ERROR_NO_ERROR == device4_read(&dev4, -1)); - device4_close(&dev4); + device_mcu_clear(&dev); + while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev, -1)); + device_mcu_close(&dev); return 0; } diff --git a/examples/mcu_firmware/src/upgrade.c b/examples/mcu_firmware/src/upgrade.c index 25e344e..5ffe8df 100644 --- a/examples/mcu_firmware/src/upgrade.c +++ b/examples/mcu_firmware/src/upgrade.c @@ -22,7 +22,7 @@ // THE SOFTWARE. // -#include "device4.h" +#include "device_mcu.h" #include #include @@ -67,13 +67,13 @@ int main(int argc, const char** argv) { return 0; } - device4_type dev4; - if (DEVICE4_ERROR_NO_ERROR != device4_open(&dev4, NULL)) { + device_mcu_type dev; + if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev, NULL)) { return 1; } - device4_clear(&dev4); - device4_update_mcu_firmware(&dev4, path); - device4_close(&dev4); + device_mcu_clear(&dev); + device_mcu_update_firmware(&dev, path); + device_mcu_close(&dev); return 0; } diff --git a/interface_lib/include/device_imu.h b/interface_lib/include/device_imu.h index 18d40cc..c356975 100644 --- a/interface_lib/include/device_imu.h +++ b/interface_lib/include/device_imu.h @@ -2,7 +2,7 @@ // // Created by thejackimonster on 30.03.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 diff --git a/interface_lib/include/device_mcu.h b/interface_lib/include/device_mcu.h index a5b5979..2d9448c 100644 --- a/interface_lib/include/device_mcu.h +++ b/interface_lib/include/device_mcu.h @@ -2,7 +2,7 @@ // // Created by thejackimonster on 29.03.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 @@ -135,12 +135,12 @@ struct __attribute__((__packed__)) device_mcu_packet_t { }; enum device_mcu_event_t { - DEVICE_MCU_EVENT_UNKNOWN = 0, - DEVICE_MCU_EVENT_SCREEN_ON = 1, - DEVICE_MCU_EVENT_SCREEN_OFF = 2, - DEVICE_MCU_EVENT_BRIGHTNESS_UP = 3, - DEVICE_MCU_EVENT_BRIGHTNESS_DOWN = 4, - DEVICE_MCU_EVENT_MESSAGE = 5, + DEVICE_MCU_EVENT_UNKNOWN = 0, + DEVICE_MCU_EVENT_SCREEN_ON = 1, + DEVICE_MCU_EVENT_SCREEN_OFF = 2, + DEVICE_MCU_EVENT_BRIGHTNESS_UP = 3, + DEVICE_MCU_EVENT_BRIGHTNESS_DOWN = 4, + DEVICE_MCU_EVENT_MESSAGE = 5, }; typedef enum device_mcu_error_t device_mcu_error_type; @@ -183,7 +183,7 @@ device_mcu_error_type device_mcu_poll_display_mode(device_mcu_type* device); device_mcu_error_type device_mcu_update_display_mode(device_mcu_type* device); -device_mcu_error_type device_mcu_update_mcu_firmware(device_mcu_type* device, const char* path); +device_mcu_error_type device_mcu_update_firmware(device_mcu_type* device, const char* path); device_mcu_error_type device_mcu_close(device_mcu_type* device); diff --git a/interface_lib/include/hid_ids.h b/interface_lib/include/hid_ids.h index 2b6dfbc..c1c8fbd 100644 --- a/interface_lib/include/hid_ids.h +++ b/interface_lib/include/hid_ids.h @@ -1,3 +1,28 @@ +#pragma once +// +// Created by wheaney on 20.11.23. +// +// Copyright (c) 2023-2024 thejackimonster. 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 +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + #ifndef __cplusplus #include #endif @@ -10,7 +35,18 @@ #define NUM_SUPPORTED_PRODUCTS 4 +#ifdef __cplusplus +extern "C" { +#endif + extern const uint16_t xreal_vendor_id; -extern const uint16_t xreal_product_ids[NUM_SUPPORTED_PRODUCTS]; +extern const uint16_t xreal_product_ids [NUM_SUPPORTED_PRODUCTS]; + +bool is_xreal_product_id(uint16_t product_id); + int xreal_imu_interface_id(uint16_t product_id); -int xreal_mcu_interface_id(uint16_t product_id); \ No newline at end of file +int xreal_mcu_interface_id(uint16_t product_id); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/interface_lib/src/crc32.c b/interface_lib/src/crc32.c index 9cf8809..2e2d955 100644 --- a/interface_lib/src/crc32.c +++ b/interface_lib/src/crc32.c @@ -1,7 +1,7 @@ // // Created by thejackimonster on 21.04.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 @@ -100,5 +100,3 @@ uint32_t crc32_checksum(const uint8_t* buf, uint32_t len) { return ~CRC32_data; } - - diff --git a/interface_lib/src/device_mcu.c b/interface_lib/src/device_mcu.c index 4958227..9e76f0a 100644 --- a/interface_lib/src/device_mcu.c +++ b/interface_lib/src/device_mcu.c @@ -517,7 +517,7 @@ device_mcu_error_type device_mcu_update_display_mode(device_mcu_type* device) { return DEVICE_MCU_ERROR_NO_ERROR; } -device_mcu_error_type device_mcu_update_mcu_firmware(device_mcu_type* device, const char* path) { +device_mcu_error_type device_mcu_update_firmware(device_mcu_type* device, const char* path) { if (!device) { device_mcu_error("No device"); return DEVICE_MCU_ERROR_NO_DEVICE; diff --git a/interface_lib/src/hid_ids.c b/interface_lib/src/hid_ids.c index cff80af..25799b3 100644 --- a/interface_lib/src/hid_ids.c +++ b/interface_lib/src/hid_ids.c @@ -1,3 +1,29 @@ +// +// Created by wheaney on 12.11.23. +// +// Copyright (c) 2023-2024 thejackimonster. 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 +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#include "hid_ids.h" + #ifndef __cplusplus #include #endif @@ -8,8 +34,6 @@ #include #endif -#include "hid_ids.h" - const uint16_t xreal_vendor_id = 0x3318; const uint16_t xreal_product_ids[NUM_SUPPORTED_PRODUCTS] = { 0x0424, // XREAL Air @@ -32,22 +56,36 @@ const int xreal_mcu_interface_ids[NUM_SUPPORTED_PRODUCTS] = { -1 // TODO - XREAL Air 2 Ultra MCU support via interface 0 }; -int xreal_imu_interface_id(uint16_t product_id) { +static int xreal_product_index(uint16_t product_id) { for (int i = 0; i < NUM_SUPPORTED_PRODUCTS; i++) { if (xreal_product_ids[i] == product_id) { - return xreal_imu_interface_ids[i]; + return i; } } return -1; } -int xreal_mcu_interface_id(uint16_t product_id) { - for (int i = 0; i < NUM_SUPPORTED_PRODUCTS; i++) { - if (xreal_product_ids[i] == product_id) { - return xreal_mcu_interface_ids[i]; - } - } +bool is_xreal_product_id(uint16_t product_id) { + return xreal_product_index(product_id) >= 0; +} - return -1; -} \ No newline at end of file +int xreal_imu_interface_id(uint16_t product_id) { + const int index = xreal_product_index(product_id); + + if (index >= 0) { + return xreal_imu_interface_ids[index]; + } else { + return -1; + } +} + +int xreal_mcu_interface_id(uint16_t product_id) { + const int index = xreal_product_index(product_id); + + if (index >= 0) { + return xreal_mcu_interface_ids[index]; + } else { + return -1; + } +} diff --git a/src/driver.c b/src/driver.c index 66a3953..4d6b433 100644 --- a/src/driver.c +++ b/src/driver.c @@ -1,7 +1,7 @@ // // Created by thejackimonster on 29.03.23. // -// Copyright (c) 2023 thejackimonster. All rights reserved. +// Copyright (c) 2023-2024 thejackimonster. 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 @@ -31,9 +31,9 @@ #include -void test3(uint64_t timestamp, - device_imu_event_type event, - const device_imu_ahrs_type* ahrs) { +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; @@ -74,10 +74,10 @@ void test3(uint64_t timestamp, old = q; } -void test4(uint64_t timestamp, - device_mcu_event_type event, - uint8_t brightness, - const char* msg) { +void test_mcu(uint64_t timestamp, + device_mcu_event_type event, + uint8_t brightness, + const char* msg) { switch (event) { case DEVICE_MCU_EVENT_MESSAGE: printf("Message: `%s`\n", msg); @@ -102,28 +102,28 @@ int main(int argc, const char** argv) { } if (pid == 0) { - device_imu_type dev3; - if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev3, test3)) { + device_imu_type dev_imu; + if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev_imu, test_imu)) { return 1; } - device_imu_clear(&dev3); - device_imu_calibrate(&dev3, 1000, true, true, false); - while (DEVICE_IMU_ERROR_NO_ERROR == device_imu_read(&dev3, -1)); - device_imu_close(&dev3); + device_imu_clear(&dev_imu); + device_imu_calibrate(&dev_imu, 1000, true, true, false); + 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 dev4; - if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev4, test4)) { + device_mcu_type dev_mcu; + if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev_mcu, test_mcu)) { status = 1; goto exit; } - device_mcu_clear(&dev4); - while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev4, -1)); - device_mcu_close(&dev4); + device_mcu_clear(&dev_mcu); + while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev_mcu, -1)); + device_mcu_close(&dev_mcu); exit: if (pid != waitpid(pid, &status, 0)) {