i2ctools.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "i2ctools_i.h"
  2. void i2ctools_draw_callback(Canvas* canvas, void* ctx) {
  3. i2cTools* i2ctools = ctx;
  4. if(furi_mutex_acquire(i2ctools->mutex, 200) != FuriStatusOk) {
  5. return;
  6. }
  7. switch(i2ctools->main_view->current_view) {
  8. case MAIN_VIEW:
  9. draw_main_view(canvas, i2ctools->main_view);
  10. break;
  11. case SCAN_VIEW:
  12. draw_scanner_view(canvas, i2ctools->scanner);
  13. break;
  14. case SNIFF_VIEW:
  15. draw_sniffer_view(canvas, i2ctools->sniffer);
  16. break;
  17. case SEND_VIEW:
  18. draw_sender_view(canvas, i2ctools->sender);
  19. break;
  20. default:
  21. break;
  22. }
  23. furi_mutex_release(i2ctools->mutex);
  24. }
  25. void i2ctools_input_callback(InputEvent* input_event, void* ctx) {
  26. furi_assert(ctx);
  27. FuriMessageQueue* event_queue = ctx;
  28. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  29. }
  30. int32_t i2ctools_app(void* p) {
  31. UNUSED(p);
  32. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  33. // Alloc i2ctools
  34. i2cTools* i2ctools = malloc(sizeof(i2cTools));
  35. i2ctools->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  36. // Alloc viewport
  37. i2ctools->view_port = view_port_alloc();
  38. view_port_draw_callback_set(i2ctools->view_port, i2ctools_draw_callback, i2ctools);
  39. view_port_input_callback_set(i2ctools->view_port, i2ctools_input_callback, event_queue);
  40. // Register view port in GUI
  41. Gui* gui = furi_record_open(RECORD_GUI);
  42. gui_add_view_port(gui, i2ctools->view_port, GuiLayerFullscreen);
  43. InputEvent event;
  44. i2ctools->main_view = i2c_main_view_alloc();
  45. i2ctools->sniffer = i2c_sniffer_alloc();
  46. i2ctools->sniffer->menu_index = 0;
  47. i2ctools->scanner = i2c_scanner_alloc();
  48. i2ctools->sender = i2c_sender_alloc();
  49. // Share scanner with sender
  50. i2ctools->sender->scanner = i2ctools->scanner;
  51. while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  52. // Back
  53. if(event.key == InputKeyBack && event.type == InputTypeRelease) {
  54. if(i2ctools->main_view->current_view == MAIN_VIEW) {
  55. break;
  56. } else {
  57. if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  58. stop_interrupts();
  59. i2ctools->sniffer->started = false;
  60. i2ctools->sniffer->state = I2C_BUS_FREE;
  61. }
  62. i2ctools->main_view->current_view = MAIN_VIEW;
  63. }
  64. }
  65. // Up
  66. else if(event.key == InputKeyUp && event.type == InputTypeRelease) {
  67. if(i2ctools->main_view->current_view == MAIN_VIEW) {
  68. if((i2ctools->main_view->menu_index > SCAN_VIEW)) {
  69. i2ctools->main_view->menu_index--;
  70. }
  71. } else if(i2ctools->main_view->current_view == SCAN_VIEW) {
  72. if(i2ctools->scanner->menu_index > 0) {
  73. i2ctools->scanner->menu_index--;
  74. }
  75. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  76. if(i2ctools->sniffer->row_index > 0) {
  77. i2ctools->sniffer->row_index--;
  78. }
  79. } else if(i2ctools->main_view->current_view == SEND_VIEW) {
  80. if(i2ctools->sender->value < 0xFF) {
  81. i2ctools->sender->value++;
  82. i2ctools->sender->sended = false;
  83. }
  84. }
  85. }
  86. // Long Up
  87. else if(
  88. event.key == InputKeyUp &&
  89. (event.type == InputTypeLong || event.type == InputTypeRepeat)) {
  90. if(i2ctools->main_view->current_view == SCAN_VIEW) {
  91. if(i2ctools->scanner->menu_index > 5) {
  92. i2ctools->scanner->menu_index -= 5;
  93. }
  94. } else if(i2ctools->main_view->current_view == SEND_VIEW) {
  95. if(i2ctools->sender->value < 0xF9) {
  96. i2ctools->sender->value += 5;
  97. i2ctools->sender->sended = false;
  98. }
  99. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  100. if(i2ctools->sniffer->row_index > 5) {
  101. i2ctools->sniffer->row_index -= 5;
  102. } else {
  103. i2ctools->sniffer->row_index = 0;
  104. }
  105. }
  106. }
  107. // Down
  108. else if(event.key == InputKeyDown && event.type == InputTypeRelease) {
  109. if(i2ctools->main_view->current_view == MAIN_VIEW) {
  110. if(i2ctools->main_view->menu_index < MENU_SIZE - 1) {
  111. i2ctools->main_view->menu_index++;
  112. }
  113. } else if(i2ctools->main_view->current_view == SCAN_VIEW) {
  114. if(i2ctools->scanner->menu_index < ((int)i2ctools->scanner->nb_found / 3)) {
  115. i2ctools->scanner->menu_index++;
  116. }
  117. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  118. if((i2ctools->sniffer->row_index + 3) <
  119. (int)i2ctools->sniffer->frames[i2ctools->sniffer->menu_index].data_index) {
  120. i2ctools->sniffer->row_index++;
  121. }
  122. } else if(i2ctools->main_view->current_view == SEND_VIEW) {
  123. if(i2ctools->sender->value > 0x00) {
  124. i2ctools->sender->value--;
  125. i2ctools->sender->sended = false;
  126. }
  127. }
  128. }
  129. // Long Down
  130. else if(
  131. event.key == InputKeyDown &&
  132. (event.type == InputTypeLong || event.type == InputTypeRepeat)) {
  133. if(i2ctools->main_view->current_view == SEND_VIEW) {
  134. if(i2ctools->sender->value > 0x05) {
  135. i2ctools->sender->value -= 5;
  136. i2ctools->sender->sended = false;
  137. } else {
  138. i2ctools->sender->value = 0;
  139. i2ctools->sender->sended = false;
  140. }
  141. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  142. if((i2ctools->sniffer->row_index + 8) <
  143. (int)i2ctools->sniffer->frames[i2ctools->sniffer->menu_index].data_index) {
  144. i2ctools->sniffer->row_index += 5;
  145. }
  146. }
  147. } else if(event.key == InputKeyOk && event.type == InputTypeRelease) {
  148. if(i2ctools->main_view->current_view == MAIN_VIEW) {
  149. i2ctools->main_view->current_view = i2ctools->main_view->menu_index;
  150. } else if(i2ctools->main_view->current_view == SCAN_VIEW) {
  151. scan_i2c_bus(i2ctools->scanner);
  152. } else if(i2ctools->main_view->current_view == SEND_VIEW) {
  153. i2ctools->sender->must_send = true;
  154. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  155. if(i2ctools->sniffer->started) {
  156. stop_interrupts();
  157. i2ctools->sniffer->started = false;
  158. i2ctools->sniffer->state = I2C_BUS_FREE;
  159. } else {
  160. start_interrupts(i2ctools->sniffer);
  161. i2ctools->sniffer->started = true;
  162. i2ctools->sniffer->state = I2C_BUS_FREE;
  163. }
  164. }
  165. } else if(event.key == InputKeyRight && event.type == InputTypeRelease) {
  166. if(i2ctools->main_view->current_view == SEND_VIEW) {
  167. if(i2ctools->sender->address_idx < (i2ctools->scanner->nb_found - 1)) {
  168. i2ctools->sender->address_idx++;
  169. i2ctools->sender->sended = false;
  170. }
  171. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  172. if(i2ctools->sniffer->menu_index < i2ctools->sniffer->frame_index) {
  173. i2ctools->sniffer->menu_index++;
  174. i2ctools->sniffer->row_index = 0;
  175. }
  176. }
  177. } else if(event.key == InputKeyLeft && event.type == InputTypeRelease) {
  178. if(i2ctools->main_view->current_view == SEND_VIEW) {
  179. if(i2ctools->sender->address_idx > 0) {
  180. i2ctools->sender->address_idx--;
  181. i2ctools->sender->sended = false;
  182. }
  183. } else if(i2ctools->main_view->current_view == SNIFF_VIEW) {
  184. if(i2ctools->sniffer->menu_index > 0) {
  185. i2ctools->sniffer->menu_index--;
  186. i2ctools->sniffer->row_index = 0;
  187. }
  188. }
  189. }
  190. view_port_update(i2ctools->view_port);
  191. }
  192. gui_remove_view_port(gui, i2ctools->view_port);
  193. view_port_free(i2ctools->view_port);
  194. furi_message_queue_free(event_queue);
  195. i2c_sniffer_free(i2ctools->sniffer);
  196. i2c_scanner_free(i2ctools->scanner);
  197. i2c_sender_free(i2ctools->sender);
  198. i2c_main_view_free(i2ctools->main_view);
  199. free(i2ctools);
  200. furi_record_close(RECORD_GUI);
  201. return 0;
  202. }