view_settings.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 &&
  51. (input.key != InputKeyDown || input.key != InputKeyUp))
  52. {
  53. /* Handle up and down to change frequency or modulation. */
  54. if (app->current_view == ViewFrequencySettings) {
  55. size_t curidx = 0, i;
  56. size_t count = subghz_setting_get_frequency_count(app->setting);
  57. /* Scan the list of frequencies to check for the index of the
  58. * currently set frequency. */
  59. for(i = 0; i < count; i++) {
  60. uint32_t freq = subghz_setting_get_frequency(app->setting,i);
  61. if (freq == app->frequency) {
  62. curidx = i;
  63. break;
  64. }
  65. }
  66. if (i == count) return; /* Should never happen. */
  67. if (input.key == InputKeyUp) {
  68. curidx = curidx == 0 ? count-1 : curidx-1;
  69. } else if (input.key == InputKeyDown) {
  70. curidx = (curidx+1) % count;
  71. } else {
  72. return;
  73. }
  74. app->frequency = subghz_setting_get_frequency(app->setting,curidx);
  75. } else if (app->current_view == ViewModulationSettings) {
  76. uint32_t count = 0;
  77. uint32_t modid = app->modulation;
  78. while(ProtoViewModulations[count].name != NULL) count++;
  79. if (input.key == InputKeyUp) {
  80. modid = modid == 0 ? count-1 : modid-1;
  81. } else if (input.key == InputKeyDown) {
  82. modid = (modid+1) % count;
  83. } else {
  84. return;
  85. }
  86. app->modulation = modid;
  87. }
  88. } else {
  89. return;
  90. }
  91. /* Apply changes when switching to other views. */
  92. app->txrx->freq_mod_changed = true;
  93. }
  94. /* When the user switches to some other view, if they changed the parameters
  95. * we need to restart the radio with the right frequency and modulation. */
  96. void view_exit_settings(ProtoViewApp *app) {
  97. if (app->txrx->freq_mod_changed) {
  98. FURI_LOG_E(TAG, "Setting view, setting frequency/modulation to %lu %s", app->frequency, ProtoViewModulations[app->modulation].name);
  99. radio_rx_end(app);
  100. radio_begin(app);
  101. radio_rx(app);
  102. app->txrx->freq_mod_changed = false;
  103. }
  104. }