view_settings.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if(app->txrx->debug_timer_sampling)
  20. canvas_draw_str(canvas, 3, 52, "(DEBUG timer sampling is ON)");
  21. /* Show frequency. We can use big numbers font since it's just a number. */
  22. if(app->current_view == ViewFrequencySettings) {
  23. char buf[16];
  24. snprintf(buf, sizeof(buf), "%.2f", (double)app->frequency / 1000000);
  25. canvas_set_font(canvas, FontBigNumbers);
  26. canvas_draw_str(canvas, 30, 40, buf);
  27. } else if(app->current_view == ViewModulationSettings) {
  28. int current = app->modulation;
  29. canvas_set_font(canvas, FontPrimary);
  30. canvas_draw_str(canvas, 33, 39, ProtoViewModulations[current].name);
  31. }
  32. }
  33. /* Handle input for the settings view. */
  34. void process_input_settings(ProtoViewApp* app, InputEvent input) {
  35. if(input.type == InputTypeLong && input.key == InputKeyOk) {
  36. /* Long pressing to OK sets the default frequency and
  37. * modulation. */
  38. app->frequency = subghz_setting_get_default_frequency(app->setting);
  39. app->modulation = 0;
  40. } else if(0 && input.type == InputTypeLong && input.key == InputKeyDown) {
  41. /* Long pressing to down switches between normal and debug
  42. * timer sampling mode. NOTE: this feature is disabled for users,
  43. * only useful for devs (if useful at all). */
  44. /* We have to stop the previous sampling system. */
  45. radio_rx_end(app);
  46. /* Then switch mode and start the new one. */
  47. app->txrx->debug_timer_sampling = !app->txrx->debug_timer_sampling;
  48. radio_begin(app);
  49. radio_rx(app);
  50. } else if(input.type == InputTypePress && (input.key != InputKeyDown || input.key != InputKeyUp)) {
  51. /* Handle up and down to change frequency or modulation. */
  52. if(app->current_view == ViewFrequencySettings) {
  53. size_t curidx = 0, i;
  54. size_t count = subghz_setting_get_frequency_count(app->setting);
  55. /* Scan the list of frequencies to check for the index of the
  56. * currently set frequency. */
  57. for(i = 0; i < count; i++) {
  58. uint32_t freq = subghz_setting_get_frequency(app->setting, i);
  59. if(freq == app->frequency) {
  60. curidx = i;
  61. break;
  62. }
  63. }
  64. if(i == count) return; /* Should never happen. */
  65. if(input.key == InputKeyUp) {
  66. curidx = curidx == 0 ? count - 1 : curidx - 1;
  67. } else if(input.key == InputKeyDown) {
  68. curidx = (curidx + 1) % count;
  69. } else {
  70. return;
  71. }
  72. app->frequency = subghz_setting_get_frequency(app->setting, curidx);
  73. } else if(app->current_view == ViewModulationSettings) {
  74. uint32_t count = 0;
  75. uint32_t modid = app->modulation;
  76. while(ProtoViewModulations[count].name != NULL)
  77. count++;
  78. if(input.key == InputKeyUp) {
  79. modid = modid == 0 ? count - 1 : modid - 1;
  80. } else if(input.key == InputKeyDown) {
  81. modid = (modid + 1) % count;
  82. } else {
  83. return;
  84. }
  85. app->modulation = modid;
  86. }
  87. } else {
  88. return;
  89. }
  90. /* Apply changes when switching to other views. */
  91. app->txrx->freq_mod_changed = true;
  92. }
  93. /* When the user switches to some other view, if they changed the parameters
  94. * we need to restart the radio with the right frequency and modulation. */
  95. void view_exit_settings(ProtoViewApp* app) {
  96. if(app->txrx->freq_mod_changed) {
  97. FURI_LOG_E(
  98. TAG,
  99. "Setting view, setting frequency/modulation to %lu %s",
  100. app->frequency,
  101. ProtoViewModulations[app->modulation].name);
  102. radio_rx_end(app);
  103. radio_begin(app);
  104. radio_rx(app);
  105. app->txrx->freq_mod_changed = false;
  106. }
  107. }