view_settings.c 4.4 KB

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