simple.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <stdlib.h>
  11. void ohmd_sleep(double);
  12. // gets float values from the device and prints them
  13. void print_infof(ohmd_device* hmd, const char* name, int len, ohmd_float_value val)
  14. {
  15. float f[len];
  16. ohmd_device_getf(hmd, val, f);
  17. printf("%-25s", name);
  18. for(int i = 0; i < len; i++)
  19. printf("%f ", f[i]);
  20. printf("\n");
  21. }
  22. // gets int values from the device and prints them
  23. void print_infoi(ohmd_device* hmd, const char* name, int len, ohmd_int_value val)
  24. {
  25. int iv[len];
  26. ohmd_device_geti(hmd, val, iv);
  27. printf("%-25s", name);
  28. for(int i = 0; i < len; i++)
  29. printf("%d ", iv[i]);
  30. printf("\n");
  31. }
  32. int main(int argc, char** argv)
  33. {
  34. int device_idx = 0;
  35. if(argc > 1)
  36. device_idx = atoi(argv[1]);
  37. ohmd_context* ctx = ohmd_ctx_create();
  38. // Probe for devices
  39. int num_devices = ohmd_ctx_probe(ctx);
  40. if(num_devices < 0){
  41. printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
  42. return -1;
  43. }
  44. printf("num devices: %d\n\n", num_devices);
  45. // Print device information
  46. for(int i = 0; i < num_devices; i++){
  47. int device_class = 0, device_flags = 0;
  48. const char* device_class_s[] = {"HMD", "Controller", "Generic Tracker", "Unknown"};
  49. ohmd_list_geti(ctx, i, OHMD_DEVICE_CLASS, &device_class);
  50. ohmd_list_geti(ctx, i, OHMD_DEVICE_FLAGS, &device_flags);
  51. printf("device %d\n", i);
  52. printf(" vendor: %s\n", ohmd_list_gets(ctx, i, OHMD_VENDOR));
  53. printf(" product: %s\n", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
  54. printf(" path: %s\n", ohmd_list_gets(ctx, i, OHMD_PATH));
  55. printf(" class: %s\n", device_class_s[device_class > OHMD_DEVICE_CLASS_GENERIC_TRACKER ? 4 : device_class]);
  56. printf(" flags: %02x\n", device_flags);
  57. printf(" null device: %s\n", device_flags & OHMD_DEVICE_FLAGS_NULL_DEVICE ? "yes" : "no");
  58. printf(" rotational tracking: %s\n", device_flags & OHMD_DEVICE_FLAGS_ROTATIONAL_TRACKING ? "yes" : "no");
  59. printf(" positional tracking: %s\n", device_flags & OHMD_DEVICE_FLAGS_POSITIONAL_TRACKING ? "yes" : "no");
  60. printf(" left controller: %s\n", device_flags & OHMD_DEVICE_FLAGS_LEFT_CONTROLLER ? "yes" : "no");
  61. printf(" right controller: %s\n\n", device_flags & OHMD_DEVICE_FLAGS_RIGHT_CONTROLLER ? "yes" : "no");
  62. }
  63. // Open specified device idx or 0 (default) if nothing specified
  64. printf("opening device: %d\n", device_idx);
  65. ohmd_device* hmd = ohmd_list_open_device(ctx, device_idx);
  66. if(!hmd){
  67. printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
  68. return -1;
  69. }
  70. // Print hardware information for the opened device
  71. int ivals[2];
  72. ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
  73. ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
  74. printf("resolution: %i x %i\n", ivals[0], ivals[1]);
  75. print_infof(hmd, "hsize:", 1, OHMD_SCREEN_HORIZONTAL_SIZE);
  76. print_infof(hmd, "vsize:", 1, OHMD_SCREEN_VERTICAL_SIZE);
  77. print_infof(hmd, "lens separation:", 1, OHMD_LENS_HORIZONTAL_SEPARATION);
  78. print_infof(hmd, "lens vcenter:", 1, OHMD_LENS_VERTICAL_POSITION);
  79. print_infof(hmd, "left eye fov:", 1, OHMD_LEFT_EYE_FOV);
  80. print_infof(hmd, "right eye fov:", 1, OHMD_RIGHT_EYE_FOV);
  81. print_infof(hmd, "left eye aspect:", 1, OHMD_LEFT_EYE_ASPECT_RATIO);
  82. print_infof(hmd, "right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
  83. print_infof(hmd, "distortion k:", 6, OHMD_DISTORTION_K);
  84. print_infoi(hmd, "control count: ", 1, OHMD_CONTROL_COUNT);
  85. int control_count;
  86. ohmd_device_geti(hmd, OHMD_CONTROL_COUNT, &control_count);
  87. const char* controls_fn_str[] = { "generic", "trigger", "trigger_click", "squeeze", "menu", "home",
  88. "analog-x", "analog-y", "anlog_press", "button-a", "button-b", "button-x", "button-y"};
  89. const char* controls_type_str[] = {"digital", "analog"};
  90. int controls_fn[64];
  91. int controls_types[64];
  92. ohmd_device_geti(hmd, OHMD_CONTROLS_HINTS, controls_fn);
  93. ohmd_device_geti(hmd, OHMD_CONTROLS_TYPES, controls_types);
  94. printf("%-25s", "controls:");
  95. for(int i = 0; i < control_count; i++){
  96. printf("%s (%s)%s", controls_fn_str[controls_fn[i]], controls_type_str[controls_types[i]], i == control_count - 1 ? "" : ", ");
  97. }
  98. printf("\n\n");
  99. // Ask for n rotation quaternions and position vectors
  100. for(int i = 0; i < 10000; i++){
  101. ohmd_ctx_update(ctx);
  102. // this can be used to set a different zero point
  103. // for rotation and position, but is not required.
  104. //float zero[] = {.0, .0, .0, 1};
  105. //ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
  106. //ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
  107. // get rotation and postition
  108. print_infof(hmd, "rotation quat:", 4, OHMD_ROTATION_QUAT);
  109. print_infof(hmd, "position vec: ", 3, OHMD_POSITION_VECTOR);
  110. // read controls
  111. float control_state[256];
  112. ohmd_device_getf(hmd, OHMD_CONTROLS_STATE, control_state);
  113. printf("%-25s", "controls state:");
  114. for(int i = 0; i < control_count; i++)
  115. {
  116. printf("%f ", control_state[i]);
  117. }
  118. puts("");
  119. ohmd_sleep(.01);
  120. }
  121. ohmd_ctx_destroy(ctx);
  122. return 0;
  123. }