simple.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  3. * Copyright (C) 2013 Fredrik Hultin.
  4. * Copyright (C) 2013 Jakob Bornecrantz.
  5. * Distributed under the Boost 1.0 licence, see LICENSE for full text.
  6. */
  7. /* Simple Test */
  8. #include <openhmd.h>
  9. #include <stdio.h>
  10. void ohmd_sleep(double);
  11. // gets float values from the device and prints them
  12. void print_infof(ohmd_device* hmd, const char* name, int len, ohmd_float_value val)
  13. {
  14. float f[len];
  15. ohmd_device_getf(hmd, val, f);
  16. printf("%-25s", name);
  17. for(int i = 0; i < len; i++)
  18. printf("%f ", f[i]);
  19. printf("\n");
  20. }
  21. // gets int values from the device and prints them
  22. void print_infoi(ohmd_device* hmd, const char* name, int len, ohmd_int_value val)
  23. {
  24. int iv[len];
  25. ohmd_device_geti(hmd, val, iv);
  26. printf("%-25s", name);
  27. for(int i = 0; i < len; i++)
  28. printf("%d ", iv[i]);
  29. printf("\n");
  30. }
  31. int main(int argc, char** argv)
  32. {
  33. ohmd_context* ctx = ohmd_ctx_create();
  34. // Probe for devices
  35. int num_devices = ohmd_ctx_probe(ctx);
  36. if(num_devices < 0){
  37. printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
  38. return -1;
  39. }
  40. printf("num devices: %d\n\n", num_devices);
  41. // Print device information
  42. for(int i = 0; i < num_devices; i++){
  43. int device_class = 0, device_flags = 0;
  44. char* device_class_s[] = {"HMD", "Controller", "Generic Tracker", "Unknown"};
  45. ohmd_list_geti(ctx, i, OHMD_DEVICE_CLASS, &device_class);
  46. ohmd_list_geti(ctx, i, OHMD_DEVICE_FLAGS, &device_flags);
  47. printf("device %d\n", i);
  48. printf(" vendor: %s\n", ohmd_list_gets(ctx, i, OHMD_VENDOR));
  49. printf(" product: %s\n", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
  50. printf(" path: %s\n", ohmd_list_gets(ctx, i, OHMD_PATH));
  51. printf(" class: %s\n", device_class_s[device_class > OHMD_DEVICE_CLASS_GENERIC_TRACKER ? 4 : device_class]);
  52. printf(" flags: %02x\n", device_flags);
  53. printf(" null device: %s\n", device_flags & OHMD_DEVICE_FLAGS_NULL_DEVICE ? "yes" : "no");
  54. printf(" rotational tracking: %s\n", device_flags & OHMD_DEVICE_FLAGS_ROTATIONAL_TRACKING ? "yes" : "no");
  55. printf(" positional tracking: %s\n\n", device_flags & OHMD_DEVICE_FLAGS_POSITIONAL_TRACKING ? "yes" : "no");
  56. }
  57. // Open default device (0)
  58. ohmd_device* hmd = ohmd_list_open_device(ctx, 0);
  59. if(!hmd){
  60. printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
  61. return -1;
  62. }
  63. // Print hardware information for the opened device
  64. int ivals[2];
  65. ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
  66. ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
  67. printf("resolution: %i x %i\n", ivals[0], ivals[1]);
  68. print_infof(hmd, "hsize:", 1, OHMD_SCREEN_HORIZONTAL_SIZE);
  69. print_infof(hmd, "vsize:", 1, OHMD_SCREEN_VERTICAL_SIZE);
  70. print_infof(hmd, "lens separation:", 1, OHMD_LENS_HORIZONTAL_SEPARATION);
  71. print_infof(hmd, "lens vcenter:", 1, OHMD_LENS_VERTICAL_POSITION);
  72. print_infof(hmd, "left eye fov:", 1, OHMD_LEFT_EYE_FOV);
  73. print_infof(hmd, "right eye fov:", 1, OHMD_RIGHT_EYE_FOV);
  74. print_infof(hmd, "left eye aspect:", 1, OHMD_LEFT_EYE_ASPECT_RATIO);
  75. print_infof(hmd, "right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
  76. print_infof(hmd, "distortion k:", 6, OHMD_DISTORTION_K);
  77. print_infoi(hmd, "digital button count:", 1, OHMD_BUTTON_COUNT);
  78. printf("\n");
  79. // Ask for n rotation quaternions
  80. for(int i = 0; i < 10000; i++){
  81. ohmd_ctx_update(ctx);
  82. float zero[] = {.0, .1, .2, 1};
  83. ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
  84. ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
  85. print_infof(hmd, "rotation quat:", 4, OHMD_ROTATION_QUAT);
  86. print_infoi(hmd, "button event count:", 1, OHMD_BUTTON_EVENT_COUNT);
  87. int event_count = 0;
  88. ohmd_device_geti(hmd, OHMD_BUTTON_EVENT_COUNT, &event_count);
  89. for(int i = 0; i < event_count; i++){
  90. int event[2] = {0, 0};
  91. ohmd_device_geti(hmd, OHMD_BUTTON_POP_EVENT, event);
  92. printf("button %d: %s", event[0], event[1] == OHMD_BUTTON_DOWN ? "down" : "up");
  93. }
  94. ohmd_sleep(.01);
  95. }
  96. ohmd_ctx_destroy(ctx);
  97. return 0;
  98. }