view_settings.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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,52,"(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. /* We have to stop the previous sampling system. */
  44. radio_rx_end(app);
  45. /* Then switch mode and start the new one. */
  46. app->txrx->debug_direct_sampling = !app->txrx->debug_direct_sampling;
  47. radio_begin(app);
  48. radio_rx(app);
  49. } else if (input.type == InputTypePress &&
  50. (input.key != InputKeyDown || input.key != InputKeyUp))
  51. {
  52. /* Handle up and down to change frequency or modulation. */
  53. if (app->current_view == ViewFrequencySettings) {
  54. size_t curidx = 0, i;
  55. size_t count = subghz_setting_get_frequency_count(app->setting);
  56. /* Scan the list of frequencies to check for the index of the
  57. * currently set frequency. */
  58. for(i = 0; i < count; i++) {
  59. uint32_t freq = subghz_setting_get_frequency(app->setting,i);
  60. if (freq == app->frequency) {
  61. curidx = i;
  62. break;
  63. }
  64. }
  65. if (i == count) return; /* Should never happen. */
  66. if (input.key == InputKeyUp) {
  67. curidx = curidx == 0 ? count-1 : curidx-1;
  68. } else if (input.key == InputKeyDown) {
  69. curidx = (curidx+1) % count;
  70. } else {
  71. return;
  72. }
  73. app->frequency = subghz_setting_get_frequency(app->setting,curidx);
  74. } else if (app->current_view == ViewModulationSettings) {
  75. uint32_t count = 0;
  76. uint32_t modid = app->modulation;
  77. while(ProtoViewModulations[count].name != NULL) 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(TAG, "Setting view, setting frequency/modulation to %lu %s", app->frequency, ProtoViewModulations[app->modulation].name);
  98. radio_rx_end(app);
  99. radio_begin(app);
  100. radio_rx(app);
  101. app->txrx->freq_mod_changed = false;
  102. }
  103. }