Control MIDI in Ableton Live with a MYO armband
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libmyo.h 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright (C) 2013-2014 Thalmic Labs Inc.
  2. // Distributed under the Myo SDK license agreement. See LICENSE.txt for details.
  3. #ifndef MYO_LIBMYO_H
  4. #define MYO_LIBMYO_H
  5. #include <stdint.h>
  6. #include "libmyo/detail/visibility.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /// @file libmyo.h
  11. /// libmyo C API declarations.
  12. typedef void* libmyo_hub_t;
  13. /// \defgroup errors Error Handling
  14. /// @{
  15. /// Function result codes.
  16. /// All libmyo functions that can fail return a value of this type.
  17. typedef enum {
  18. libmyo_success,
  19. libmyo_error,
  20. libmyo_error_invalid_argument,
  21. libmyo_error_runtime
  22. } libmyo_result_t;
  23. /// Opaque handle to detailed error information.
  24. typedef void* libmyo_error_details_t;
  25. /// Return a null-terminated string with a detailed error message.
  26. LIBMYO_EXPORT
  27. const char* libmyo_error_cstring(libmyo_error_details_t);
  28. /// Returns the kind of error that occurred.
  29. LIBMYO_EXPORT
  30. libmyo_result_t libmyo_error_kind(libmyo_error_details_t);
  31. /// Free the resources allocated by an error object.
  32. LIBMYO_EXPORT
  33. void libmyo_free_error_details(libmyo_error_details_t);
  34. /// @}
  35. /// \defgroup libmyo_string Strings
  36. /// @{
  37. // Opaque string.
  38. typedef void* libmyo_string_t;
  39. // Return a null-terminated string from the opaque string.
  40. LIBMYO_EXPORT
  41. const char* libmyo_string_c_str(libmyo_string_t);
  42. // Free the resources allocated by the string object.
  43. LIBMYO_EXPORT
  44. void libmyo_string_free(libmyo_string_t);
  45. /// @}
  46. /// \defgroup libmyo_direct_mac_addresses MAC address utilities
  47. /// @{
  48. /// Retrieve the string representation of a MAC address in hex.
  49. /// Returns a string in the format of 00-00-00-00-00-00.
  50. LIBMYO_EXPORT
  51. libmyo_string_t libmyo_mac_address_to_string(uint64_t);
  52. /// Retrieve the MAC address from a null-terminated string in the format of 00-00-00-00-00-00.
  53. /// Returns 0 if the string does not match the format.
  54. LIBMYO_EXPORT
  55. uint64_t libmyo_string_to_mac_address(const char*);
  56. /// @}
  57. /// @defgroup libmyo_hub Hub instance
  58. /// @{
  59. /// Initialize a connection to the hub.
  60. /// \a application_identifier must follow a reverse domain name format (ex. com.domainname.appname). Application
  61. /// identifiers can be formed from the set of alphanumeric ASCII characters (a-z, A-Z, 0-9). The hyphen (-) and
  62. /// underscore (_) characters are permitted if they are not adjacent to a period (.) character (i.e. not at the start or
  63. /// end of each segment), but are not permitted in the top-level domain. Application identifiers must have three or more
  64. /// segments. For example, if a company's domain is example.com and the application is named hello-world, one could use
  65. /// "com.example.hello-world" as a valid application identifier. \a application_identifier can be NULL or empty.
  66. /// @returns libmyo_success if the connection is successfully established, otherwise:
  67. /// - libmyo_error_runtime if a connection could not be established
  68. /// - libmyo_error_invalid_argument if \a out_hub is NULL
  69. /// - libmyo_error_invalid_argument if \a application_identifier is longer than 255 characters
  70. /// - libmyo_error_invalid_argument if \a application_identifier is not in the proper reverse domain name format
  71. LIBMYO_EXPORT
  72. libmyo_result_t libmyo_init_hub(libmyo_hub_t* out_hub, const char* application_identifier,
  73. libmyo_error_details_t* out_error);
  74. /// Free the resources allocated to a hub.
  75. /// @returns libmyo_success if shutdown is successful, otherwise:
  76. /// - libmyo_error_invalid_argument if \a hub is NULL
  77. /// - libmyo_error if \a hub is not a valid hub
  78. LIBMYO_EXPORT
  79. libmyo_result_t libmyo_shutdown_hub(libmyo_hub_t hub, libmyo_error_details_t* out_error);
  80. // Locking policies.
  81. typedef enum {
  82. libmyo_locking_policy_none, ///< Pose events are always sent.
  83. libmyo_locking_policy_standard ///< Pose events are not sent while a Myo is locked.
  84. } libmyo_locking_policy_t;
  85. /// Set the locking policy for Myos connected to the hub.
  86. /// @returns libmyo_success if the locking policy is successfully set, otherwise
  87. /// - libmyo_error_invalid_argument if \a hub is NULL
  88. /// - libmyo_error if \a hub is not a valid hub
  89. LIBMYO_EXPORT
  90. libmyo_result_t libmyo_set_locking_policy(libmyo_hub_t hub, libmyo_locking_policy_t locking_policy,
  91. libmyo_error_details_t* out_error);
  92. /// @}
  93. /// @defgroup libmyo_myo Myo instances
  94. /// @{
  95. /// Opaque type corresponding to a known Myo device.
  96. typedef void* libmyo_myo_t;
  97. /// Types of vibration
  98. typedef enum {
  99. libmyo_vibration_short,
  100. libmyo_vibration_medium,
  101. libmyo_vibration_long
  102. } libmyo_vibration_type_t;
  103. /// Retrieve the MAC address of a Myo.
  104. /// The MAC address is unique to the physical Myo, and is a 48-bit number.
  105. LIBMYO_EXPORT
  106. uint64_t libmyo_get_mac_address(libmyo_myo_t myo);
  107. /// Vibrate the given myo.
  108. /// Can be called when a Myo is paired.
  109. /// @returns libmyo_success if the Myo successfully vibrated, otherwise
  110. /// - libmyo_error_invalid_argument if \a myo is NULL
  111. LIBMYO_EXPORT
  112. libmyo_result_t libmyo_vibrate(libmyo_myo_t myo, libmyo_vibration_type_t type, libmyo_error_details_t* out_error);
  113. /// Request the RSSI for a given myo.
  114. /// Can be called when a Myo is paired. A libmyo_event_rssi event will likely be generated with the value of the RSSI.
  115. /// @returns libmyo_success if the Myo successfully got the RSSI, otherwise
  116. /// - libmyo_error_invalid_argument if \a myo is NULL
  117. LIBMYO_EXPORT
  118. libmyo_result_t libmyo_request_rssi(libmyo_myo_t myo, libmyo_error_details_t* out_error);
  119. /// Request the battery level for a given Myo.
  120. /// A libmyo_event_battery_level event will be generated with the value of the battery level.
  121. /// @returns libmyo_success if the Myo successfully requested the battery level, otherwise
  122. /// - libmyo_error_invalid_argument if \a myo is NULL
  123. LIBMYO_EXPORT
  124. libmyo_result_t libmyo_request_battery_level(libmyo_myo_t myo_opq, libmyo_error_details_t* out_error);
  125. /// EMG streaming modes.
  126. typedef enum {
  127. libmyo_stream_emg_disabled, ///< Do not send EMG data.
  128. libmyo_stream_emg_enabled ///< Send EMG data.
  129. } libmyo_stream_emg_t;
  130. /// Set whether or not to stream EMG data for a given myo.
  131. /// Can be called when a Myo is paired.
  132. /// @returns libmyo_success if the EMG mode was set successfully, otherwise
  133. /// - libmyo_error_invalid_argument if \a myo is NULL
  134. LIBMYO_EXPORT
  135. libmyo_result_t libmyo_set_stream_emg(libmyo_myo_t myo, libmyo_stream_emg_t emg, libmyo_error_details_t* out_error);
  136. /// @}
  137. /// @defgroup libmyo_poses Pose recognition.
  138. /// @{
  139. /// Supported poses.
  140. typedef enum {
  141. libmyo_pose_rest = 0, ///< Rest pose.
  142. libmyo_pose_fist = 1, ///< User is making a fist.
  143. libmyo_pose_wave_in = 2, ///< User has an open palm rotated towards the posterior of their wrist.
  144. libmyo_pose_wave_out = 3, ///< User has an open palm rotated towards the anterior of their wrist.
  145. libmyo_pose_fingers_spread = 4, ///< User has an open palm with their fingers spread away from each other.
  146. libmyo_pose_double_tap = 5, ///< User tapped their thumb and middle finger together twice in succession.
  147. libmyo_num_poses, ///< Number of poses supported; not a valid pose.
  148. libmyo_pose_unknown = 0xffff ///< Unknown pose.
  149. } libmyo_pose_t;
  150. /// @}
  151. /// @defgroup libmyo_locking Myo locking mechanism
  152. /// Valid unlock types.
  153. typedef enum {
  154. libmyo_unlock_timed = 0, ///< Unlock for a fixed period of time.
  155. libmyo_unlock_hold = 1, ///< Unlock until explicitly told to re-lock.
  156. } libmyo_unlock_type_t;
  157. /// Unlock the given Myo.
  158. /// Can be called when a Myo is paired. A libmyo_event_unlocked event will be generated if the Myo was locked.
  159. /// @returns libmyo_success if the Myo was successfully unlocked, otherwise
  160. /// - libmyo_error_invalid_argument if \a myo is NULL
  161. LIBMYO_EXPORT
  162. libmyo_result_t libmyo_myo_unlock(libmyo_myo_t myo, libmyo_unlock_type_t type, libmyo_error_details_t* out_error);
  163. /// Lock the given Myo immediately.
  164. /// Can be called when a Myo is paired. A libmyo_event_locked event will be generated if the Myo was unlocked.
  165. /// @returns libmyo_success if the Myo was successfully locked, otherwise
  166. /// - libmyo_error_invalid_argument if \a myo is NULL
  167. LIBMYO_EXPORT
  168. libmyo_result_t libmyo_myo_lock(libmyo_myo_t myo, libmyo_error_details_t* out_error);
  169. /// User action types.
  170. typedef enum {
  171. libmyo_user_action_single = 0, ///< User did a single, discrete action, such as pausing a video.
  172. } libmyo_user_action_type_t;
  173. /// Notify the given Myo that a user action was recognized.
  174. /// Can be called when a Myo is paired. Will cause Myo to vibrate.
  175. /// @returns libmyo_success if the Myo was successfully notified, otherwise
  176. /// - libmyo_error_invalid_argument if \a myo is NULL
  177. LIBMYO_EXPORT
  178. libmyo_result_t libmyo_myo_notify_user_action(libmyo_myo_t myo, libmyo_user_action_type_t type,
  179. libmyo_error_details_t* out_error);
  180. /// @}
  181. /// @defgroup libmyo_events Event Handling
  182. /// @{
  183. /// Types of events.
  184. typedef enum {
  185. libmyo_event_paired, ///< Successfully paired with a Myo.
  186. libmyo_event_unpaired, ///< Successfully unpaired from a Myo.
  187. libmyo_event_connected, ///< A Myo has successfully connected.
  188. libmyo_event_disconnected, ///< A Myo has been disconnected.
  189. libmyo_event_arm_synced, ///< A Myo has recognized that the sync gesture has been successfully performed.
  190. libmyo_event_arm_unsynced, ///< A Myo has been moved or removed from the arm.
  191. libmyo_event_orientation, ///< Orientation data has been received.
  192. libmyo_event_pose, ///< A change in pose has been detected. @see libmyo_pose_t.
  193. libmyo_event_rssi, ///< An RSSI value has been received.
  194. libmyo_event_unlocked, ///< A Myo has become unlocked.
  195. libmyo_event_locked, ///< A Myo has become locked.
  196. libmyo_event_emg, ///< EMG data has been received.
  197. libmyo_event_battery_level, ///< A battery level value has been received.
  198. libmyo_event_warmup_completed, ///< The warmup period has completed.
  199. } libmyo_event_type_t;
  200. /// Information about an event.
  201. typedef const void* libmyo_event_t;
  202. /// Retrieve the type of an event.
  203. LIBMYO_EXPORT
  204. uint32_t libmyo_event_get_type(libmyo_event_t event);
  205. /// Retrieve the timestamp of an event.
  206. /// @see libmyo_now() for details on timestamps.
  207. LIBMYO_EXPORT
  208. uint64_t libmyo_event_get_timestamp(libmyo_event_t event);
  209. /// Retrieve the Myo associated with an event.
  210. LIBMYO_EXPORT
  211. libmyo_myo_t libmyo_event_get_myo(libmyo_event_t event);
  212. /// Retrieve the MAC address of the myo associated with an event.
  213. LIBMYO_EXPORT
  214. uint64_t libmyo_event_get_mac_address(libmyo_event_t event_opq);
  215. /// Retrieve the name of the myo associated with an event.
  216. /// Caller must free the returned string. @see libmyo_string functions.
  217. LIBMYO_EXPORT
  218. libmyo_string_t libmyo_event_get_myo_name(libmyo_event_t event);
  219. /// Components of version.
  220. typedef enum {
  221. libmyo_version_major, ///< Major version.
  222. libmyo_version_minor, ///< Minor version.
  223. libmyo_version_patch, ///< Patch version.
  224. libmyo_version_hardware_rev, ///< Hardware revision.
  225. } libmyo_version_component_t;
  226. /// Hardware revisions.
  227. typedef enum {
  228. libmyo_hardware_rev_c = 1, ///< Alpha units
  229. libmyo_hardware_rev_d = 2, ///< Consumer units
  230. } libmyo_hardware_rev_t;
  231. /// Retrieve the Myo armband's firmware version from this event.
  232. /// Valid for libmyo_event_paired and libmyo_event_connected events.
  233. LIBMYO_EXPORT
  234. unsigned int libmyo_event_get_firmware_version(libmyo_event_t event, libmyo_version_component_t);
  235. /// Enumeration identifying a right arm or left arm. @see libmyo_event_get_arm().
  236. typedef enum {
  237. libmyo_arm_right, ///< Myo is on the right arm.
  238. libmyo_arm_left, ///< Myo is on the left arm.
  239. libmyo_arm_unknown, ///< Unknown arm.
  240. } libmyo_arm_t;
  241. /// Retrieve the arm associated with an event.
  242. /// Valid for libmyo_event_arm_synced events only.
  243. LIBMYO_EXPORT
  244. libmyo_arm_t libmyo_event_get_arm(libmyo_event_t event);
  245. /// Possible directions for Myo's +x axis relative to a user's arm.
  246. typedef enum {
  247. libmyo_x_direction_toward_wrist, ///< Myo's +x axis is pointing toward the user's wrist.
  248. libmyo_x_direction_toward_elbow, ///< Myo's +x axis is pointing toward the user's elbow.
  249. libmyo_x_direction_unknown, ///< Unknown +x axis direction.
  250. } libmyo_x_direction_t;
  251. /// Retrieve the x-direction associated with an event.
  252. /// The x-direction specifies which way Myo's +x axis is pointing relative to the user's arm.
  253. /// Valid for libmyo_event_arm_synced events only.
  254. LIBMYO_EXPORT
  255. libmyo_x_direction_t libmyo_event_get_x_direction(libmyo_event_t event);
  256. /// Possible warmup states for Myo.
  257. typedef enum {
  258. libmyo_warmup_state_unknown = 0, ///< Unknown warm up state.
  259. libmyo_warmup_state_cold = 1, ///< Myo needs to warm up.
  260. libmyo_warmup_state_warm = 2, ///< Myo is already in a warmed up state.
  261. } libmyo_warmup_state_t;
  262. /// Retrieve the warmup state of the Myo associated with an event.
  263. /// Valid for libmyo_event_arm_synced events only.
  264. LIBMYO_EXPORT
  265. libmyo_warmup_state_t libmyo_event_get_warmup_state(libmyo_event_t event);
  266. /// Possible warmup results for Myo.
  267. typedef enum {
  268. libmyo_warmup_result_unknown = 0, ///< Unknown warm up result.
  269. libmyo_warmup_result_success = 1, ///< The warm up period has completed successfully.
  270. libmyo_warmup_result_failed_timeout = 2, ///< The warm up period timed out.
  271. } libmyo_warmup_result_t;
  272. /// Retrieve the warmup result of the Myo associated with an event.
  273. /// Valid for libmyo_event_warmup_completed events only.
  274. LIBMYO_EXPORT
  275. libmyo_warmup_result_t libmyo_event_get_warmup_result(libmyo_event_t event);
  276. /// Retrieve the estimated rotation of Myo on the user's arm after a sync.
  277. /// The values specifies the rotation of the myo on the arm (0 - logo facing down, pi - logo facing up)
  278. /// Only supported by FW 1.3.x and above (older firmware will always report 0 for the rotation)
  279. /// Valid for libmyo_event_arm_synced events only.
  280. LIBMYO_EXPORT
  281. float libmyo_event_get_rotation_on_arm(libmyo_event_t event);
  282. /// Index into orientation data, which is provided as a quaternion.
  283. /// Orientation data is returned as a unit quaternion of floats, represented as `w + x * i + y * j + z * k`.
  284. typedef enum {
  285. libmyo_orientation_x = 0, ///< First component of the quaternion's vector part
  286. libmyo_orientation_y = 1, ///< Second component of the quaternion's vector part
  287. libmyo_orientation_z = 2, ///< Third component of the quaternion's vector part
  288. libmyo_orientation_w = 3, ///< Scalar component of the quaternion.
  289. } libmyo_orientation_index;
  290. /// Retrieve orientation data associated with an event.
  291. /// Valid for libmyo_event_orientation events only.
  292. /// @see libmyo_orientation_index
  293. LIBMYO_EXPORT
  294. float libmyo_event_get_orientation(libmyo_event_t event, libmyo_orientation_index index);
  295. /// Retrieve raw accelerometer data associated with an event in units of g.
  296. /// Valid for libmyo_event_orientation events only.
  297. /// Requires `index < 3`.
  298. LIBMYO_EXPORT
  299. float libmyo_event_get_accelerometer(libmyo_event_t event, unsigned int index);
  300. /// Retrieve raw gyroscope data associated with an event in units of deg/s.
  301. /// Valid for libmyo_event_orientation events only.
  302. /// Requires `index < 3`.
  303. LIBMYO_EXPORT
  304. float libmyo_event_get_gyroscope(libmyo_event_t event, unsigned int index);
  305. /// Retrieve the pose associated with an event.
  306. /// Valid for libmyo_event_pose events only.
  307. LIBMYO_EXPORT
  308. libmyo_pose_t libmyo_event_get_pose(libmyo_event_t event);
  309. /// Retreive the RSSI associated with an event.
  310. /// Valid for libmyo_event_rssi events only.
  311. LIBMYO_EXPORT
  312. int8_t libmyo_event_get_rssi(libmyo_event_t event);
  313. /// Retrieve the battery level of the Myo armband associated with an event.
  314. /// Only valid for libmyo_event_battery_level event.
  315. LIBMYO_EXPORT
  316. uint8_t libmyo_event_get_battery_level(libmyo_event_t event);
  317. /// Retrieve an EMG data point associated with an event.
  318. /// Valid for libmyo_event_emg events only.
  319. /// @a sensor must be smaller than 8.
  320. LIBMYO_EXPORT
  321. int8_t libmyo_event_get_emg(libmyo_event_t event, unsigned int sensor);
  322. /// Return type for event handlers.
  323. typedef enum {
  324. libmyo_handler_continue, ///< Continue processing events
  325. libmyo_handler_stop, ///< Stop processing events
  326. } libmyo_handler_result_t;
  327. /// Callback function type to handle events as they occur from libmyo_run().
  328. typedef libmyo_handler_result_t (*libmyo_handler_t)(void* user_data, libmyo_event_t event);
  329. /// Process events and call the provided callback as they occur.
  330. /// Runs for up to approximately \a duration_ms milliseconds or until a called handler returns libmyo_handler_stop.
  331. /// @returns libmyo_success after a successful run, otherwise
  332. /// - libmyo_error_invalid_argument if \a hub is NULL
  333. /// - libmyo_error_invalid_argument if \a handler is NULL
  334. LIBMYO_EXPORT
  335. libmyo_result_t libmyo_run(libmyo_hub_t hub, unsigned int duration_ms, libmyo_handler_t handler, void* user_data,
  336. libmyo_error_details_t* out_error);
  337. /// @}
  338. #ifdef __cplusplus
  339. } // extern "C"
  340. #endif
  341. #endif // MYO_LIBMYO_H