view_settings.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /* Renders a single view with frequency and modulation setting. However
  5. * this are logically two different views, and only one of the settings
  6. * will be highlighted. */
  7. void render_view_settings(Canvas *const canvas, ProtoViewApp *app) {
  8. canvas_set_font(canvas, FontPrimary);
  9. if (app->current_view == ViewFrequencySettings)
  10. canvas_draw_str_with_border(canvas,1,10,"Frequency",ColorWhite,ColorBlack);
  11. else
  12. canvas_draw_str(canvas,1,10,"Frequency");
  13. if (app->current_view == ViewModulationSettings)
  14. canvas_draw_str_with_border(canvas,70,10,"Modulation",ColorWhite,ColorBlack);
  15. else
  16. canvas_draw_str(canvas,70,10,"Modulation");
  17. canvas_set_font(canvas, FontSecondary);
  18. canvas_draw_str(canvas,10,61,"Use up and down to modify");
  19. /* Show frequency. We can use big numbers font since it's just a number. */
  20. if (app->current_view == ViewFrequencySettings) {
  21. char buf[16];
  22. snprintf(buf,sizeof(buf),"%.2f",(double)app->frequency/1000000);
  23. canvas_set_font(canvas, FontBigNumbers);
  24. canvas_draw_str(canvas, 30, 40, buf);
  25. } else if (app->current_view == ViewModulationSettings) {
  26. int current = app->modulation;
  27. canvas_set_font(canvas, FontPrimary);
  28. canvas_draw_str(canvas, 33, 39, ProtoViewModulations[current].name);
  29. }
  30. }
  31. /* Handle input for the settings view. */
  32. void process_input_settings(ProtoViewApp *app, InputEvent input) {
  33. if (input.type == InputTypeLong && input.key == InputKeyOk) {
  34. /* Long pressing to OK sets the default frequency and
  35. * modulation. */
  36. app->frequency = subghz_setting_get_default_frequency(app->setting);
  37. app->modulation = 0;
  38. } else if (input.type == InputTypePress &&
  39. (input.key != InputKeyDown || input.key != InputKeyUp))
  40. {
  41. /* Handle up and down to change frequency or modulation. */
  42. if (app->current_view == ViewFrequencySettings) {
  43. size_t curidx = 0, i;
  44. size_t count = subghz_setting_get_frequency_count(app->setting);
  45. /* Scan the list of frequencies to check for the index of the
  46. * currently set frequency. */
  47. for(i = 0; i < count; i++) {
  48. uint32_t freq = subghz_setting_get_frequency(app->setting,i);
  49. if (freq == app->frequency) {
  50. curidx = i;
  51. break;
  52. }
  53. }
  54. if (i == count) return; /* Should never happen. */
  55. if (input.key == InputKeyUp) {
  56. curidx = (curidx+1) % count;
  57. } else if (input.key == InputKeyDown) {
  58. curidx = curidx == 0 ? count-1 : curidx-1;
  59. } else {
  60. return;
  61. }
  62. app->frequency = subghz_setting_get_frequency(app->setting,curidx);
  63. } else if (app->current_view == ViewModulationSettings) {
  64. uint32_t count = 0;
  65. uint32_t modid = app->modulation;
  66. while(ProtoViewModulations[count].name != NULL) count++;
  67. if (input.key == InputKeyUp) {
  68. modid = (modid+1) % count;
  69. } else if (input.key == InputKeyDown) {
  70. modid = modid == 0 ? count-1 : modid-1;
  71. } else {
  72. return;
  73. }
  74. app->modulation = modid;
  75. }
  76. } else {
  77. return;
  78. }
  79. /* Apply changes. */
  80. FURI_LOG_E(TAG, "Setting view, setting frequency/modulation to %lu %s", app->frequency, ProtoViewModulations[app->modulation].name);
  81. radio_rx_end(app);
  82. radio_begin(app);
  83. radio_rx(app);
  84. }