gpio_simple_motor.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "gpio_simple_motor.h"
  2. #include "../fbp.h"
  3. static const uint16_t BT_SERIAL_BUFFER_SIZE = 128;
  4. static const uint32_t DEFAULT_FREQ = 1000;
  5. static const FuriHalPwmOutputId DEFAULT_PWM_OUTPUT_ID = FuriHalPwmOutputIdTim1PA7;
  6. struct GPIOSimpleMotor {
  7. View* view;
  8. FBP* fbp;
  9. uint8_t current_pwm_duty;
  10. };
  11. typedef struct {
  12. char* display_text_1;
  13. char* display_text_2;
  14. char* display_text_3;
  15. } GPIOSimpleMotorModel;
  16. static void process_general_command(TCodeCommand command, GPIOSimpleMotor* motor) {
  17. if(command.command_type == Magnitude &&
  18. command.data.magnitude_command.motion_type == Vibrate &&
  19. command.data.magnitude_command.channel_id == 0) {
  20. // just enable vibration on X
  21. uint8_t new_duty = (uint8_t)(command.data.magnitude_command.magnitude * 100);
  22. if(new_duty > 100) {
  23. new_duty = 100;
  24. }
  25. FURI_LOG_D(TAG, "Setting vibration power on %u", new_duty);
  26. // using Pulse-Widht Modulation to control a motor via a transistor
  27. // just google for a typical arduino + PWM + motor scheme
  28. if(new_duty == 0) {
  29. furi_hal_pwm_stop(DEFAULT_PWM_OUTPUT_ID);
  30. } else if(motor->current_pwm_duty == 0) {
  31. furi_hal_pwm_start(DEFAULT_PWM_OUTPUT_ID, DEFAULT_FREQ, new_duty);
  32. } else {
  33. furi_hal_pwm_set_params(DEFAULT_PWM_OUTPUT_ID, DEFAULT_FREQ, new_duty);
  34. }
  35. motor->current_pwm_duty = new_duty;
  36. return;
  37. }
  38. }
  39. static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
  40. furi_assert(context);
  41. GPIOSimpleMotor* motor = context;
  42. if(event.event == SerialServiceEventTypeDataReceived) {
  43. TCodeCommandArray commands = tcode_decode(event.data.buffer, event.data.size);
  44. FURI_LOG_D(TAG, "Decoded commands array size: %u", commands.size);
  45. for(uint16_t i = 0; i < commands.size; i++) {
  46. FURI_LOG_D(TAG, "Command #%u, type: %u\n", i, commands.commands[i].command_type);
  47. }
  48. for(uint16_t i = 0; i < commands.size; i++) {
  49. // looking for first vibro command to execute
  50. TCodeCommand current_command = commands.commands[i];
  51. TCodeCommandType type = current_command.command_type;
  52. if((type == Magnitude || type == MagnitudeSpeed || type == MagnitudeTimeInterval)) {
  53. process_general_command(current_command, motor);
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. static bool input_callback(InputEvent* event, void* ctx) {
  60. furi_assert(ctx);
  61. GPIOSimpleMotor* motor = ctx;
  62. if(event->key == InputKeyBack) {
  63. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  64. return false;
  65. }
  66. if(event->key == InputKeyOk) {
  67. if(furi_hal_bt_is_active()) {
  68. FURI_LOG_D(TAG, "BT is working, hijacking the serial connection...");
  69. furi_hal_bt_start_advertising();
  70. furi_hal_bt_serial_set_event_callback(
  71. BT_SERIAL_BUFFER_SIZE, bt_serial_event_callback, motor);
  72. with_view_model(
  73. motor->view,
  74. GPIOSimpleMotorModel * model,
  75. {
  76. model->display_text_1 = "";
  77. model->display_text_2 = "Ready ^_^";
  78. model->display_text_3 = "";
  79. },
  80. true);
  81. } else {
  82. FURI_LOG_E(TAG, "Please, enable the Bluetooth and restart the app");
  83. with_view_model(
  84. motor->view,
  85. GPIOSimpleMotorModel * model,
  86. {
  87. model->display_text_1 = "Error:";
  88. model->display_text_2 = "Bluetooth is not enabled";
  89. model->display_text_3 = "";
  90. },
  91. true);
  92. }
  93. }
  94. return true;
  95. }
  96. static void draw_callback(Canvas* canvas, void* ctx) {
  97. furi_assert(ctx);
  98. GPIOSimpleMotorModel* app = ctx;
  99. canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, (char*)app->display_text_1);
  100. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, (char*)app->display_text_2);
  101. canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignCenter, (char*)app->display_text_3);
  102. }
  103. GPIOSimpleMotor* gpio_simple_motor_alloc(FBP* fbp) {
  104. furi_assert(fbp);
  105. GPIOSimpleMotor* motor = malloc(sizeof(GPIOSimpleMotor));
  106. motor->view = view_alloc();
  107. motor->fbp = fbp;
  108. view_set_context(motor->view, motor);
  109. view_allocate_model(motor->view, ViewModelTypeLocking, sizeof(GPIOSimpleMotorModel));
  110. view_set_draw_callback(motor->view, draw_callback);
  111. view_set_input_callback(motor->view, input_callback);
  112. with_view_model(
  113. motor->view,
  114. GPIOSimpleMotorModel * model,
  115. {
  116. model->display_text_1 = "Please, connect the";
  117. model->display_text_2 = "transistor base to pin A7!";
  118. model->display_text_3 = "Press OK to start";
  119. },
  120. true);
  121. return motor;
  122. }
  123. void gpio_simple_motor_free(GPIOSimpleMotor* motor) {
  124. furi_assert(motor);
  125. furi_hal_pwm_stop(DEFAULT_PWM_OUTPUT_ID);
  126. view_free(motor->view);
  127. free(motor);
  128. }
  129. View* gpio_simple_motor_get_view(GPIOSimpleMotor* motor) {
  130. furi_assert(motor);
  131. return motor->view;
  132. }