simple.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, "digital button count:", 1, OHMD_BUTTON_COUNT);
  85. print_infoi(hmd, "control count: ", 1, OHMD_CONTROL_COUNT);
  86. int control_count;
  87. ohmd_device_geti(hmd, OHMD_CONTROL_COUNT, &control_count);
  88. const char* controls_fn_str[] = { "generic", "trigger", "trigger_click", "squeeze", "menu", "home",
  89. "analog-x", "analog-y", "anlog_press", "button-a", "button-b", "button-x", "button-y"};
  90. const char* controls_type_str[] = {"digital", "analog"};
  91. int controls_fn[64];
  92. int controls_types[64];
  93. ohmd_device_geti(hmd, OHMD_CONTROLS_FUNCTIONS, controls_fn);
  94. ohmd_device_geti(hmd, OHMD_CONTROLS_TYPES, controls_types);
  95. printf("%-25s", "controls:");
  96. for(int i = 0; i < control_count; i++){
  97. printf("%s (%s)%s", controls_fn_str[controls_fn[i]], controls_type_str[controls_types[i]], i == control_count - 1 ? "" : ", ");
  98. }
  99. printf("\n\n");
  100. // Ask for n rotation quaternions and position vectors
  101. for(int i = 0; i < 10000; i++){
  102. ohmd_ctx_update(ctx);
  103. // this can be used to set a different zero point
  104. // for rotation and position, but is not required.
  105. //float zero[] = {.0, .0, .0, 1};
  106. //ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
  107. //ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
  108. // get rotation and postition
  109. print_infof(hmd, "rotation quat:", 4, OHMD_ROTATION_QUAT);
  110. print_infof(hmd, "position vec: ", 3, OHMD_POSITION_VECTOR);
  111. // read controls
  112. float control_state[256];
  113. ohmd_device_getf(hmd, OHMD_CONTROLS_STATE, control_state);
  114. printf("%-25s", "controls state:");
  115. for(int i = 0; i < control_count; i++)
  116. {
  117. printf("%f ", control_state[i]);
  118. }
  119. puts("");
  120. // handle digital button events
  121. print_infoi(hmd, "button event count:", 1, OHMD_BUTTON_EVENT_COUNT);
  122. int event_count = 0;
  123. ohmd_device_geti(hmd, OHMD_BUTTON_EVENT_COUNT, &event_count);
  124. for(int i = 0; i < event_count; i++){
  125. int event[2] = {0, 0};
  126. ohmd_device_geti(hmd, OHMD_BUTTON_POP_EVENT, event);
  127. printf("button %d: %s", event[0], event[1] == OHMD_BUTTON_DOWN ? "down" : "up");
  128. }
  129. ohmd_sleep(.01);
  130. }
  131. ohmd_ctx_destroy(ctx);
  132. return 0;
  133. }