simple.c 5.5 KB

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