remote.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "remote.h"
  2. #include "../subghz_remote_app_i.h"
  3. #include <input/input.h>
  4. #include <gui/elements.h>
  5. #include <lib/toolbox/path.h>
  6. #define SUBREM_VIEW_REMOTE_MAX_LABEL_LENGTH 30
  7. #define SUBREM_VIEW_REMOTE_LEFT_OFFSET 10
  8. #define SUBREM_VIEW_REMOTE_RIGHT_OFFSET 22
  9. struct SubRemViewRemote {
  10. View* view;
  11. SubRemViewRemoteCallback callback;
  12. void* context;
  13. };
  14. typedef struct {
  15. char* labels[SubRemSubKeyNameMaxCount];
  16. SubRemViewRemoteState state;
  17. uint8_t pressed_btn;
  18. bool is_external;
  19. } SubRemViewRemoteModel;
  20. void subrem_view_remote_set_callback(
  21. SubRemViewRemote* subrem_view_remote,
  22. SubRemViewRemoteCallback callback,
  23. void* context) {
  24. furi_assert(subrem_view_remote);
  25. subrem_view_remote->callback = callback;
  26. subrem_view_remote->context = context;
  27. }
  28. void subrem_view_remote_update_data_labels(
  29. SubRemViewRemote* subrem_view_remote,
  30. SubRemSubFilePreset** subs_presets) {
  31. furi_assert(subrem_view_remote);
  32. furi_assert(subs_presets);
  33. FuriString* labels[SubRemSubKeyNameMaxCount];
  34. SubRemSubFilePreset* sub_preset;
  35. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  36. sub_preset = subs_presets[i];
  37. switch(sub_preset->load_state) {
  38. case SubRemLoadSubStateOK:
  39. if(!furi_string_empty(sub_preset->label)) {
  40. labels[i] = furi_string_alloc_set(sub_preset->label);
  41. } else if(!furi_string_empty(sub_preset->file_path)) {
  42. labels[i] = furi_string_alloc();
  43. path_extract_filename(sub_preset->file_path, labels[i], true);
  44. } else {
  45. labels[i] = furi_string_alloc_set("Empty Label");
  46. }
  47. break;
  48. case SubRemLoadSubStateErrorNoFile:
  49. labels[i] = furi_string_alloc_set("[X] Can't open file");
  50. break;
  51. case SubRemLoadSubStateErrorFreq:
  52. case SubRemLoadSubStateErrorMod:
  53. case SubRemLoadSubStateErrorProtocol:
  54. labels[i] = furi_string_alloc_set("[X] Error in .sub file");
  55. break;
  56. default:
  57. labels[i] = furi_string_alloc_set("");
  58. break;
  59. }
  60. }
  61. with_view_model(
  62. subrem_view_remote->view,
  63. SubRemViewRemoteModel * model,
  64. {
  65. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  66. strncpy(
  67. model->labels[i],
  68. furi_string_get_cstr(labels[i]),
  69. SUBREM_VIEW_REMOTE_MAX_LABEL_LENGTH);
  70. }
  71. },
  72. true);
  73. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  74. furi_string_free(labels[i]);
  75. }
  76. }
  77. void subrem_view_remote_set_state(
  78. SubRemViewRemote* subrem_view_remote,
  79. SubRemViewRemoteState state,
  80. uint8_t presed_btn) {
  81. furi_assert(subrem_view_remote);
  82. with_view_model(
  83. subrem_view_remote->view,
  84. SubRemViewRemoteModel * model,
  85. {
  86. model->state = state;
  87. model->pressed_btn = presed_btn;
  88. },
  89. true);
  90. }
  91. void subrem_view_remote_set_radio(SubRemViewRemote* subrem_view_remote, bool external) {
  92. furi_assert(subrem_view_remote);
  93. with_view_model(
  94. subrem_view_remote->view,
  95. SubRemViewRemoteModel * model,
  96. { model->is_external = external; },
  97. true);
  98. }
  99. void subrem_view_remote_draw(Canvas* canvas, SubRemViewRemoteModel* model) {
  100. canvas_clear(canvas);
  101. canvas_set_color(canvas, ColorBlack);
  102. canvas_clear(canvas);
  103. //Icons for Labels
  104. //canvas_draw_icon(canvas, 0, 0, &I_SubGHzRemote_LeftAlignedButtons_9x64);
  105. canvas_draw_icon(canvas, 1, 5, &I_ButtonUp_7x4);
  106. canvas_draw_icon(canvas, 1, 15, &I_ButtonDown_7x4);
  107. canvas_draw_icon(canvas, 2, 23, &I_ButtonLeft_4x7);
  108. canvas_draw_icon(canvas, 2, 33, &I_ButtonRight_4x7);
  109. canvas_draw_icon(canvas, 0, 42, &I_Ok_btn_9x9);
  110. canvas_draw_icon(canvas, 0, 53, &I_back_10px);
  111. //Labels
  112. canvas_set_font(canvas, FontSecondary);
  113. uint8_t y = 0;
  114. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  115. elements_text_box(
  116. canvas,
  117. SUBREM_VIEW_REMOTE_LEFT_OFFSET,
  118. y + 2,
  119. 126 - SUBREM_VIEW_REMOTE_LEFT_OFFSET - SUBREM_VIEW_REMOTE_RIGHT_OFFSET,
  120. 12,
  121. AlignLeft,
  122. AlignBottom,
  123. model->labels[i],
  124. false);
  125. y += 10;
  126. }
  127. if(model->state == SubRemViewRemoteStateOFF) {
  128. elements_button_left(canvas, "Back");
  129. elements_button_right(canvas, "Save");
  130. } else {
  131. canvas_draw_str_aligned(canvas, 11, 62, AlignLeft, AlignBottom, "Hold=Exit.");
  132. canvas_draw_str_aligned(
  133. canvas, 126, 62, AlignRight, AlignBottom, ((model->is_external) ? "Ext" : "Int"));
  134. }
  135. //Status text and indicator
  136. canvas_draw_icon(canvas, 113, 15, &I_Pin_cell_13x13);
  137. if(model->state == SubRemViewRemoteStateIdle || model->state == SubRemViewRemoteStateOFF) {
  138. canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, "Idle");
  139. } else {
  140. switch(model->state) {
  141. case SubRemViewRemoteStateSending:
  142. canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, "Send");
  143. break;
  144. case SubRemViewRemoteStateLoading:
  145. canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, "Load");
  146. break;
  147. default:
  148. #if FURI_DEBUG
  149. canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, "Wrong_state");
  150. #endif
  151. break;
  152. }
  153. switch(model->pressed_btn) {
  154. case SubRemSubKeyNameUp:
  155. canvas_draw_icon(canvas, 116, 17, &I_Pin_arrow_up_7x9);
  156. break;
  157. case SubRemSubKeyNameDown:
  158. canvas_draw_icon_ex(canvas, 116, 17, &I_Pin_arrow_up_7x9, IconRotation180);
  159. break;
  160. case SubRemSubKeyNameLeft:
  161. canvas_draw_icon_ex(canvas, 115, 18, &I_Pin_arrow_up_7x9, IconRotation270);
  162. break;
  163. case SubRemSubKeyNameRight:
  164. canvas_draw_icon_ex(canvas, 115, 18, &I_Pin_arrow_up_7x9, IconRotation90);
  165. break;
  166. case SubRemSubKeyNameOk:
  167. canvas_draw_icon(canvas, 116, 18, &I_Pin_star_7x7);
  168. break;
  169. }
  170. }
  171. }
  172. bool subrem_view_remote_input(InputEvent* event, void* context) {
  173. furi_assert(context);
  174. SubRemViewRemote* subrem_view_remote = context;
  175. if(event->key == InputKeyBack && event->type == InputTypeLong) {
  176. subrem_view_remote->callback(SubRemCustomEventViewRemoteBack, subrem_view_remote->context);
  177. return true;
  178. } else if(event->key == InputKeyBack && event->type == InputTypeShort) {
  179. with_view_model(
  180. subrem_view_remote->view,
  181. SubRemViewRemoteModel * model,
  182. { model->pressed_btn = 0; },
  183. true);
  184. subrem_view_remote->callback(
  185. SubRemCustomEventViewRemoteForcedStop, subrem_view_remote->context);
  186. return true;
  187. } else if(event->key == InputKeyBack) {
  188. return true;
  189. }
  190. // BACK button processing end
  191. if(event->key == InputKeyUp && event->type == InputTypePress) {
  192. subrem_view_remote->callback(
  193. SubRemCustomEventViewRemoteStartUP, subrem_view_remote->context);
  194. return true;
  195. } else if(event->key == InputKeyDown && event->type == InputTypePress) {
  196. subrem_view_remote->callback(
  197. SubRemCustomEventViewRemoteStartDOWN, subrem_view_remote->context);
  198. return true;
  199. } else if(event->key == InputKeyLeft && event->type == InputTypePress) {
  200. subrem_view_remote->callback(
  201. SubRemCustomEventViewRemoteStartLEFT, subrem_view_remote->context);
  202. return true;
  203. } else if(event->key == InputKeyRight && event->type == InputTypePress) {
  204. subrem_view_remote->callback(
  205. SubRemCustomEventViewRemoteStartRIGHT, subrem_view_remote->context);
  206. return true;
  207. } else if(event->key == InputKeyOk && event->type == InputTypePress) {
  208. subrem_view_remote->callback(
  209. SubRemCustomEventViewRemoteStartOK, subrem_view_remote->context);
  210. return true;
  211. } else if(event->type == InputTypeRelease) {
  212. subrem_view_remote->callback(SubRemCustomEventViewRemoteStop, subrem_view_remote->context);
  213. return true;
  214. }
  215. return true;
  216. }
  217. void subrem_view_remote_enter(void* context) {
  218. furi_assert(context);
  219. }
  220. void subrem_view_remote_exit(void* context) {
  221. furi_assert(context);
  222. }
  223. SubRemViewRemote* subrem_view_remote_alloc() {
  224. SubRemViewRemote* subrem_view_remote = malloc(sizeof(SubRemViewRemote));
  225. // View allocation and configuration
  226. subrem_view_remote->view = view_alloc();
  227. view_allocate_model(
  228. subrem_view_remote->view, ViewModelTypeLocking, sizeof(SubRemViewRemoteModel));
  229. view_set_context(subrem_view_remote->view, subrem_view_remote);
  230. view_set_draw_callback(subrem_view_remote->view, (ViewDrawCallback)subrem_view_remote_draw);
  231. view_set_input_callback(subrem_view_remote->view, subrem_view_remote_input);
  232. view_set_enter_callback(subrem_view_remote->view, subrem_view_remote_enter);
  233. view_set_exit_callback(subrem_view_remote->view, subrem_view_remote_exit);
  234. with_view_model(
  235. subrem_view_remote->view,
  236. SubRemViewRemoteModel * model,
  237. {
  238. model->state = SubRemViewRemoteStateIdle;
  239. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  240. model->labels[i] = malloc(sizeof(char) * SUBREM_VIEW_REMOTE_MAX_LABEL_LENGTH + 1);
  241. strcpy(model->labels[i], "");
  242. }
  243. model->pressed_btn = 0;
  244. model->is_external = false;
  245. },
  246. true);
  247. return subrem_view_remote;
  248. }
  249. void subrem_view_remote_free(SubRemViewRemote* subghz_remote) {
  250. furi_assert(subghz_remote);
  251. with_view_model(
  252. subghz_remote->view,
  253. SubRemViewRemoteModel * model,
  254. {
  255. for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
  256. free(model->labels[i]);
  257. }
  258. },
  259. true);
  260. view_free(subghz_remote->view);
  261. free(subghz_remote);
  262. }
  263. View* subrem_view_remote_get_view(SubRemViewRemote* subrem_view_remote) {
  264. furi_assert(subrem_view_remote);
  265. return subrem_view_remote->view;
  266. }