view_info.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. #include <gui/view.h>
  5. #include <lib/toolbox/random_name.h>
  6. /* This view has subviews accessible navigating up/down. This
  7. * enumaration is used to track the currently active subview. */
  8. enum {
  9. SubViewInfoMain,
  10. SubViewInfoSave,
  11. SubViewInfoLast, /* Just a sentinel. */
  12. };
  13. /* Our view private data. */
  14. #define SAVE_FILENAME_LEN 64
  15. typedef struct {
  16. /* Our save view displays an oscilloscope-alike resampled signal,
  17. * so that the user can see what they are saving. With left/right
  18. * you can move to next rows. Here we store where we are. */
  19. uint32_t signal_display_start_row;
  20. char *filename;
  21. } InfoViewPrivData;
  22. /* Draw the text label and value of the specified info field at x,y. */
  23. static void render_info_field(Canvas *const canvas,
  24. ProtoViewField *f, uint8_t x, uint8_t y)
  25. {
  26. char buf[64];
  27. canvas_set_font(canvas, FontSecondary);
  28. switch(f->type) {
  29. case FieldTypeStr:
  30. snprintf(buf,sizeof(buf),"%s: %s", f->name, f->str);
  31. break;
  32. case FieldTypeSignedInt:
  33. snprintf(buf,sizeof(buf),"%s: %lld", f->name, (long long) f->value);
  34. break;
  35. case FieldTypeUnsignedInt:
  36. snprintf(buf,sizeof(buf),"%s: %llu", f->name,
  37. (unsigned long long) f->uvalue);
  38. break;
  39. case FieldTypeBinary:
  40. {
  41. uint64_t test_bit = (1 << (f->len-1));
  42. uint64_t idx = snprintf(buf,sizeof(buf),"%s: ", f->name);
  43. while(idx < sizeof(buf)-1 && test_bit) {
  44. buf[idx++] = (f->uvalue & test_bit) ? '1' : '0';
  45. test_bit >>= 1;
  46. }
  47. buf[idx] = 0;
  48. }
  49. break;
  50. case FieldTypeHex:
  51. snprintf(buf, sizeof(buf), "%s: 0x%*llX", f->name,
  52. (int)(f->len+7)/8, f->uvalue);
  53. break;
  54. case FieldTypeFloat:
  55. snprintf(buf, sizeof(buf), "%s: 0x%.*f", f->name,
  56. (int)f->len, (double)f->fvalue);
  57. break;
  58. case FieldTypeBytes:
  59. {
  60. uint64_t idx = snprintf(buf,sizeof(buf),"%s: ", f->name);
  61. uint32_t nibble_num = 0;
  62. while(idx < sizeof(buf)-1 && nibble_num < f->len) {
  63. const char *charset = "0123456789ABCDEF";
  64. uint32_t nibble = nibble_num & 1 ?
  65. (f->bytes[nibble_num/2] >> 4) :
  66. (f->bytes[nibble_num/2] & 0xf);
  67. buf[idx++] = charset[nibble];
  68. nibble_num++;
  69. }
  70. buf[idx] = 0;
  71. }
  72. break;
  73. }
  74. canvas_draw_str(canvas, x, y, buf);
  75. }
  76. /* Render the view with the detected message information. */
  77. static void render_subview_main(Canvas *const canvas, ProtoViewApp *app) {
  78. /* Protocol name as title. */
  79. canvas_set_font(canvas, FontPrimary);
  80. uint8_t y = 8, lineheight = 10;
  81. canvas_draw_str(canvas, 0, y, app->msg_info->decoder->name);
  82. y += lineheight;
  83. /* Draw the info fields. */
  84. for (uint32_t j = 0; j < app->msg_info->fieldset->numfields; j++) {
  85. render_info_field(canvas,app->msg_info->fieldset->fields[j],0,y);
  86. y += lineheight;
  87. }
  88. /* Draw a vertical "save" label. Temporary solution, to switch to
  89. * something better ASAP. */
  90. y = 37;
  91. lineheight = 7;
  92. canvas_draw_str(canvas, 119, y, "s"); y += lineheight;
  93. canvas_draw_str(canvas, 119, y, "a"); y += lineheight;
  94. canvas_draw_str(canvas, 119, y, "v"); y += lineheight;
  95. canvas_draw_str(canvas, 119, y, "e"); y += lineheight;
  96. }
  97. /* Render view with save option. */
  98. static void render_subview_save(Canvas *const canvas, ProtoViewApp *app) {
  99. InfoViewPrivData *privdata = app->view_privdata;
  100. /* Display our signal in digital form: here we don't show the
  101. * signal with the exact timing of the received samples, but as it
  102. * is in its logic form, in exact multiples of the short pulse length. */
  103. uint8_t rows = 6;
  104. uint8_t rowheight = 11;
  105. uint8_t bitwidth = 4;
  106. uint8_t bitheight = 5;
  107. uint32_t idx = privdata->signal_display_start_row * (128/4);
  108. bool prevbit = false;
  109. for (uint8_t y = bitheight+12; y <= rows*rowheight; y += rowheight) {
  110. for (uint8_t x = 0; x < 128; x += 4) {
  111. bool bit = bitmap_get(app->msg_info->bits,
  112. app->msg_info->bits_bytes,idx);
  113. uint8_t prevy = y + prevbit*(bitheight*-1) - 1;
  114. uint8_t thisy = y + bit*(bitheight*-1) - 1;
  115. canvas_draw_line(canvas,x,prevy,x,thisy);
  116. canvas_draw_line(canvas,x,thisy,x+bitwidth-1,thisy);
  117. prevbit = bit;
  118. if (idx >= app->msg_info->pulses_count) {
  119. canvas_set_color(canvas, ColorWhite);
  120. canvas_draw_dot(canvas, x+1,thisy);
  121. canvas_draw_dot(canvas, x+3,thisy);
  122. canvas_set_color(canvas, ColorBlack);
  123. }
  124. idx++; // Draw next bit
  125. }
  126. }
  127. canvas_set_font(canvas, FontSecondary);
  128. canvas_draw_str(canvas, 0, 6, "ok: send, long ok: save");
  129. }
  130. /* Render the selected subview of this view. */
  131. void render_view_info(Canvas *const canvas, ProtoViewApp *app) {
  132. if (app->signal_decoded == false) {
  133. canvas_set_font(canvas, FontSecondary);
  134. canvas_draw_str(canvas, 30,36,"No signal decoded");
  135. return;
  136. }
  137. ui_show_available_subviews(canvas,app,SubViewInfoLast);
  138. switch(app->current_subview[app->current_view]) {
  139. case SubViewInfoMain: render_subview_main(canvas,app); break;
  140. case SubViewInfoSave: render_subview_save(canvas,app); break;
  141. }
  142. }
  143. /* The user typed the file name. Let's save it and remove the keyboard
  144. * view. */
  145. void text_input_done_callback(void* context) {
  146. ProtoViewApp *app = context;
  147. InfoViewPrivData *privdata = app->view_privdata;
  148. FuriString *save_path = furi_string_alloc_printf(
  149. "%s/%s.sub", EXT_PATH("subghz"), privdata->filename);
  150. save_signal(app, furi_string_get_cstr(save_path));
  151. furi_string_free(save_path);
  152. free(privdata->filename);
  153. ui_dismiss_keyboard(app);
  154. }
  155. /* Replace all the occurrences of character c1 with c2 in the specified
  156. * string. */
  157. void str_replace(char *buf, char c1, char c2) {
  158. char *p = buf;
  159. while(*p) {
  160. if (*p == c1) *p = c2;
  161. p++;
  162. }
  163. }
  164. /* Set a random filename the user can edit. */
  165. void set_signal_random_filename(ProtoViewApp *app, char *buf, size_t buflen) {
  166. char suffix[6];
  167. set_random_name(suffix,sizeof(suffix));
  168. snprintf(buf,buflen,"%.10s-%s-%d",app->msg_info->decoder->name,suffix,rand()%1000);
  169. str_replace(buf,' ','_');
  170. str_replace(buf,'-','_');
  171. str_replace(buf,'/','_');
  172. }
  173. /* ========================== Signal transmission =========================== */
  174. /* This is the context we pass to the data yield callback for
  175. * asynchronous tx. */
  176. typedef enum {
  177. SendSignalSendStartGap,
  178. SendSignalSendBits,
  179. SendSignalSendEndGap,
  180. SendSignalEndTransmission
  181. } SendSignalState;
  182. #define PROTOVIEW_SENDSIGNAL_START_GAP 10000 /* microseconds. */
  183. #define PROTOVIEW_SENDSIGNAL_END_GAP 10000 /* microseconds. */
  184. typedef struct {
  185. SendSignalState state; // Current state.
  186. uint32_t curpos; // Current bit position of data to send.
  187. ProtoViewApp *app; // App reference.
  188. uint32_t start_gap_dur; // Gap to send at the start.
  189. uint32_t end_gap_dur; // Gap to send at the end.
  190. } SendSignalCtx;
  191. /* Setup the state context for the callback responsible to feed data
  192. * to the subghz async tx system. */
  193. static void send_signal_init(SendSignalCtx *ss, ProtoViewApp *app) {
  194. ss->state = SendSignalSendStartGap;
  195. ss->curpos = 0;
  196. ss->app = app;
  197. ss->start_gap_dur = PROTOVIEW_SENDSIGNAL_START_GAP;
  198. ss->end_gap_dur = PROTOVIEW_SENDSIGNAL_END_GAP;
  199. }
  200. /* Send signal data feeder callback. When the asynchronous transmission is
  201. * active, this function is called to return new samples from the currently
  202. * decoded signal in app->msg_info. The subghz subsystem aspects this function,
  203. * that is the data feeder, to return LevelDuration types (that is a structure
  204. * with level, that is pulse or gap, and duration in microseconds).
  205. *
  206. * The position into the transmission is stored in the context 'ctx', that
  207. * references a SendSignalCtx structure.
  208. *
  209. * In the SendSignalCtx structure 'ss' we remember at which bit of the
  210. * message we are, in ss->curoff. We also send a start and end gap in order
  211. * to make sure the transmission is clear.
  212. */
  213. LevelDuration radio_tx_feed_data(void *ctx) {
  214. SendSignalCtx *ss = ctx;
  215. /* Send start gap. */
  216. if (ss->state == SendSignalSendStartGap) {
  217. ss->state = SendSignalSendBits;
  218. return level_duration_make(0,ss->start_gap_dur);
  219. }
  220. /* Send data. */
  221. if (ss->state == SendSignalSendBits) {
  222. uint32_t dur = 0, j;
  223. uint32_t level = 0;
  224. /* Let's see how many consecutive bits we have with the same
  225. * level. */
  226. for (j = 0; ss->curpos+j < ss->app->msg_info->pulses_count; j++) {
  227. uint32_t l = bitmap_get(ss->app->msg_info->bits,
  228. ss->app->msg_info->bits_bytes,
  229. ss->curpos+j);
  230. if (j == 0) {
  231. /* At the first bit of this sequence, we store the
  232. * level of the sequence. */
  233. level = l;
  234. dur += ss->app->msg_info->short_pulse_dur;
  235. continue;
  236. }
  237. /* As long as the level is the same, we update the duration.
  238. * Otherwise stop the loop and return this sample. */
  239. if (l != level) break;
  240. dur += ss->app->msg_info->short_pulse_dur;
  241. }
  242. ss->curpos += j;
  243. /* If this was the last set of bits, change the state to
  244. * send the final gap. */
  245. if (ss->curpos >= ss->app->msg_info->pulses_count)
  246. ss->state = SendSignalSendEndGap;
  247. return level_duration_make(level, dur);
  248. }
  249. /* Send end gap. */
  250. if (ss->state == SendSignalSendEndGap) {
  251. ss->state = SendSignalEndTransmission;
  252. return level_duration_make(0,ss->end_gap_dur);
  253. }
  254. /* End transmission. Here state is guaranteed
  255. * to be SendSignalEndTransmission */
  256. return level_duration_reset();
  257. }
  258. /* Vibrate and produce a click sound when a signal is sent. */
  259. void notify_signal_sent(ProtoViewApp *app) {
  260. static const NotificationSequence sent_seq = {
  261. &message_blue_255,
  262. &message_vibro_on,
  263. &message_note_g1,
  264. &message_delay_10,
  265. &message_sound_off,
  266. &message_vibro_off,
  267. &message_blue_0,
  268. NULL
  269. };
  270. notification_message(app->notification, &sent_seq);
  271. }
  272. /* Handle input for the info view. */
  273. void process_input_info(ProtoViewApp *app, InputEvent input) {
  274. /* If we don't have a decoded signal, we don't allow to go up/down
  275. * in the subviews: they are only useful when a loaded signal. */
  276. if (app->signal_decoded &&
  277. ui_process_subview_updown(app,input,SubViewInfoLast)) return;
  278. InfoViewPrivData *privdata = app->view_privdata;
  279. int subview = ui_get_current_subview(app);
  280. /* Main subview. */
  281. if (subview == SubViewInfoMain) {
  282. if (input.type == InputTypeShort && input.key == InputKeyOk) {
  283. /* Reset the current sample to capture the next. */
  284. reset_current_signal(app);
  285. }
  286. } else if (subview == SubViewInfoSave) {
  287. /* Save subview. */
  288. if (input.type == InputTypePress && input.key == InputKeyRight) {
  289. privdata->signal_display_start_row++;
  290. } else if (input.type == InputTypePress && input.key == InputKeyLeft) {
  291. if (privdata->signal_display_start_row != 0)
  292. privdata->signal_display_start_row--;
  293. } else if (input.type == InputTypeLong && input.key == InputKeyOk)
  294. {
  295. privdata->filename = malloc(SAVE_FILENAME_LEN);
  296. set_signal_random_filename(app,privdata->filename,SAVE_FILENAME_LEN);
  297. ui_show_keyboard(app, privdata->filename, SAVE_FILENAME_LEN,
  298. text_input_done_callback);
  299. } else if (input.type == InputTypeShort && input.key == InputKeyOk) {
  300. SendSignalCtx send_state;
  301. send_signal_init(&send_state,app);
  302. radio_tx_signal(app,radio_tx_feed_data,&send_state);
  303. notify_signal_sent(app);
  304. }
  305. }
  306. }