ArmbandInterface.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "stdafx.h"
  2. extern "C" {
  3. #include "MotorHeader/BodyActuator.h"
  4. }
  5. #include <ctime>
  6. #include <stdio.h>
  7. #include "ArmbandInterface.h"
  8. #include <stdlib.h>
  9. typedef void(__cdecl *InitFunctionType)(BodyActuator*, BodyActuator_Type, char*, int);
  10. typedef void(__cdecl *StopFunctionType)(BodyActuator*, uint8_t);
  11. typedef void(__cdecl *StartFunctionType)(BodyActuator*, uint8_t, float);
  12. typedef void(__cdecl *ActuateFunctionType)(BodyActuator*, uint8_t, double, uint64_t);
  13. typedef void(__cdecl *DeleteFunctionType)(BodyActuator*);
  14. static InitFunctionType initFunctionHandle;
  15. static StartFunctionType startFunctionHandle;
  16. static StopFunctionType stopFunctionHandle;
  17. static ActuateFunctionType actuateFunctionHandle;
  18. static DeleteFunctionType deleteFunctionHandle;
  19. static BodyActuator* armband;
  20. //static char *port =
  21. static HINSTANCE lib;
  22. extern "C" {
  23. DllExport int __cdecl ArmbandInterface::setupArmband() {
  24. lib = LoadLibrary(TEXT("BodyActuator.dll"));
  25. if (lib == NULL) {
  26. printf("ERROR: library could not be loaded");
  27. return 0;
  28. }
  29. initFunctionHandle = (InitFunctionType)GetProcAddress(lib, "BodyActuator_init");
  30. if (initFunctionHandle == NULL) {
  31. printf("ERROR: init function could not be retrieved");
  32. return 1;
  33. }
  34. startFunctionHandle = (StartFunctionType)GetProcAddress(lib, "BodyActuator_startActuation");
  35. if (startFunctionHandle == NULL) {
  36. printf("ERROR: start function could not be retrieved");
  37. return 2;
  38. }
  39. stopFunctionHandle = (StopFunctionType)GetProcAddress(lib, "BodyActuator_stopActuation");
  40. if (stopFunctionHandle == NULL) {
  41. printf("ERROR: stop function could not be retrieved");
  42. return 3;
  43. }
  44. actuateFunctionHandle = (ActuateFunctionType)GetProcAddress(lib, "BodyActuator_actuate");
  45. if (actuateFunctionHandle == NULL) {
  46. printf("ERROR: actuate function could not be retrieved");
  47. return 4;
  48. }
  49. deleteFunctionHandle = (DeleteFunctionType)GetProcAddress(lib, "BodyActuator_delete");
  50. if (deleteFunctionHandle == NULL) {
  51. printf("ERROR: delete function could not be retrieved");
  52. return 5;
  53. }
  54. //strcpy(port, "COM5");
  55. setupMotors();
  56. //startVibrate(0, 1.0);
  57. return -1;
  58. }
  59. DllExport void __cdecl ArmbandInterface::startVibrate(int tactor, float intensity) {
  60. (startFunctionHandle)(armband, (uint8_t)tactor, intensity);
  61. }
  62. DllExport void __cdecl ArmbandInterface::stopVibrate(int tactor) {
  63. (stopFunctionHandle)(armband, (uint8_t)tactor);
  64. }
  65. DllExport void __cdecl ArmbandInterface::actuate(int tactor, double intensity, int duration) {
  66. (actuateFunctionHandle)(armband, (uint8_t)tactor, intensity, (uint64_t)duration);
  67. }
  68. }
  69. void ArmbandInterface::setupMotors() {
  70. char* port = (char*) "COM5";//malloc(7);
  71. armband = (BodyActuator*) malloc(sizeof(BodyActuator*));
  72. //strcpy_s(port, "COM5");
  73. (initFunctionHandle) (armband, BODYACTUATOR_TYPE_EAI, port, 8);
  74. printf("armband initialized");
  75. }
  76. void ArmbandInterface::deleteArmband() {
  77. deleteFunctionHandle(armband);
  78. }