i2ctools.c 8.9 KB

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