internal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "internal.h"
  2. #include "../fbp.h"
  3. static const uint16_t BT_SERIAL_BUFFER_SIZE = 128;
  4. struct FlipperVibrator {
  5. View* view;
  6. FBP* fbp;
  7. };
  8. typedef struct {
  9. char* display_text;
  10. } FlipperVibratorModel;
  11. static void process_general_command(TCodeCommand command) {
  12. if(command.command_type == Magnitude &&
  13. command.data.magnitude_command.motion_type == Vibrate &&
  14. command.data.magnitude_command.channel_id == 0) {
  15. furi_hal_vibro_on(command.data.magnitude_command.magnitude > 0.1f);
  16. return;
  17. }
  18. if(command.command_type == MagnitudeSpeed &&
  19. command.data.magnitude_speed_command.motion_type == Vibrate &&
  20. command.data.magnitude_speed_command.channel_id == 0) {
  21. furi_hal_vibro_on(command.data.magnitude_speed_command.magnitude > 0.1f);
  22. return;
  23. }
  24. if(command.command_type == MagnitudeTimeInterval &&
  25. command.data.magnitude_time_interval_command.motion_type == Vibrate &&
  26. command.data.magnitude_time_interval_command.channel_id == 0) {
  27. furi_hal_vibro_on(command.data.magnitude_time_interval_command.magnitude > 0.1f);
  28. return;
  29. }
  30. }
  31. static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
  32. furi_assert(context);
  33. FlipperVibrator* flipper_vibrator = context;
  34. UNUSED(flipper_vibrator);
  35. if(event.event == SerialServiceEventTypeDataReceived) {
  36. FURI_LOG_D(TAG, "SerialServiceEventTypeDataReceived");
  37. FURI_LOG_D(TAG, "Size: %u", event.data.size);
  38. FURI_LOG_D(TAG, "Data: ");
  39. for(size_t i = 0; i < event.data.size; i++) {
  40. printf("%X ", event.data.buffer[i]);
  41. }
  42. printf("\r\n");
  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);
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. static bool input_callback(InputEvent* event, void* ctx) {
  60. furi_assert(ctx);
  61. FlipperVibrator* flipper_vibrator = 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, flipper_vibrator);
  72. with_view_model(
  73. flipper_vibrator->view,
  74. FlipperVibratorModel * model,
  75. { model->display_text = "Ready ^_^"; },
  76. true);
  77. } else {
  78. FURI_LOG_E(TAG, "Please, enable the Bluetooth and restart the app");
  79. with_view_model(
  80. flipper_vibrator->view,
  81. FlipperVibratorModel * model,
  82. { model->display_text = "Error: Bluetooth not enabled"; },
  83. true);
  84. }
  85. }
  86. return true;
  87. }
  88. static void draw_callback(Canvas* canvas, void* ctx) {
  89. furi_assert(ctx);
  90. FlipperVibratorModel* app = ctx;
  91. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, (char*)app->display_text);
  92. }
  93. FlipperVibrator* flipper_vibrator_alloc(FBP* fbp) {
  94. furi_assert(fbp);
  95. FlipperVibrator* flipper_vibrator = malloc(sizeof(FlipperVibrator));
  96. flipper_vibrator->view = view_alloc();
  97. flipper_vibrator->fbp = fbp;
  98. view_set_context(flipper_vibrator->view, flipper_vibrator);
  99. view_allocate_model(
  100. flipper_vibrator->view, ViewModelTypeLocking, sizeof(FlipperVibratorModel));
  101. view_set_draw_callback(flipper_vibrator->view, draw_callback);
  102. view_set_input_callback(flipper_vibrator->view, input_callback);
  103. with_view_model(
  104. flipper_vibrator->view,
  105. FlipperVibratorModel * model,
  106. { model->display_text = "Press OK to start"; },
  107. true);
  108. return flipper_vibrator;
  109. }
  110. void flipper_vibrator_free(FlipperVibrator* flipper_vibrator) {
  111. furi_assert(flipper_vibrator);
  112. view_free(flipper_vibrator->view);
  113. free(flipper_vibrator);
  114. }
  115. View* flipper_vibrator_get_view(FlipperVibrator* flipper_vibrator) {
  116. furi_assert(flipper_vibrator);
  117. return flipper_vibrator->view;
  118. }