view_settings.c 3.7 KB

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