radio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. *
  3. * @author Coolshrimp - CoolshrimpModz.com
  4. *
  5. * @brief FM Radio using the TEA5767 FM radio chip.
  6. * @version 0.8
  7. * @date 2023-09-29
  8. *
  9. * @copyright GPLv3
  10. */
  11. #include <furi.h>
  12. #include <furi_hal.h>
  13. #include <stdint.h>
  14. #include <gui/gui.h>
  15. #include <gui/view.h>
  16. #include <gui/elements.h>
  17. #include <gui/view_dispatcher.h>
  18. #include <gui/modules/submenu.h>
  19. #include <gui/modules/widget.h>
  20. #include <gui/modules/variable_item_list.h>
  21. #include <notification/notification.h>
  22. #include <notification/notification_messages.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "TEA5767/TEA5767.h"
  26. #include "fmradio_controller_icons.h"
  27. // Define a macro for enabling the backlight always on.
  28. #define BACKLIGHT_ALWAYS_ON
  29. #define TAG "FMRadio"
  30. // Define volume options and names
  31. uint8_t volume_values[] = {0, 1};
  32. char* volume_names[] = {"Un-Muted", "Muted"};
  33. bool current_volume = 0; // Muted or not
  34. char* current_vol = "Un-Muted"; // Current volume status as text. Changed type to char*
  35. int* signal_strength;
  36. int loopcount = 0;
  37. uint8_t tea5767_registers[5];
  38. // Define values for frequency selection
  39. float frequency_values[] = {
  40. 88.1, 88.9, 89.1, 90.3, 91.5, 91.7, 92.0, 92.5, 94.1, 95.9, 96.3, 96.9,
  41. 97.3, 98.1, 98.7, 99.1, 99.9, 100.7, 101.3, 103.9, 104.5, 105.1, 105.5, 106.5,
  42. 107.1, 102.7, 105.3
  43. };
  44. uint32_t current_frequency_index = 0; // Default to the first frequency
  45. //lib can only do bottom left/right
  46. void elements_button_top_left(Canvas* canvas, const char* str) {
  47. const uint8_t button_height = 12;
  48. const uint8_t vertical_offset = 3;
  49. const uint8_t horizontal_offset = 3;
  50. // You may need to declare or pass 'button_width' here.
  51. const uint8_t string_width = canvas_string_width(canvas, str);
  52. // 'button_width' should be declared or passed here.
  53. const uint8_t button_width = string_width + horizontal_offset * 2 + 3;
  54. const uint8_t x = 0;
  55. const uint8_t y = 0 + button_height;
  56. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  57. canvas_draw_line(canvas, x + button_width + 0, y - button_height, x + button_width + 0, y - 1);
  58. canvas_draw_line(canvas, x + button_width + 1, y - button_height, x + button_width + 1, y - 2);
  59. canvas_draw_line(canvas, x + button_width + 2, y - button_height, x + button_width + 2, y - 3);
  60. canvas_invert_color(canvas);
  61. canvas_draw_str(
  62. canvas, x + horizontal_offset + 3, y - vertical_offset, str);
  63. canvas_invert_color(canvas);
  64. }
  65. void elements_button_top_right(Canvas* canvas, const char* str) {
  66. const uint8_t button_height = 12;
  67. const uint8_t vertical_offset = 3;
  68. const uint8_t horizontal_offset = 3;
  69. // You may need to declare or pass 'button_width' here.
  70. const uint8_t string_width = canvas_string_width(canvas, str);
  71. // 'button_width' should be declared or passed here.
  72. const uint8_t button_width = string_width + horizontal_offset * 2 + 3;
  73. const uint8_t x = canvas_width(canvas);
  74. const uint8_t y = 0 + button_height;
  75. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  76. canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
  77. canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
  78. canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
  79. canvas_invert_color(canvas);
  80. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  81. canvas_invert_color(canvas);
  82. }
  83. // Enumerations for submenu and view indices
  84. typedef enum {
  85. FMRadioSubmenuIndexConfigure,
  86. FMRadioSubmenuIndexFlipTheWorld,
  87. FMRadioSubmenuIndexAbout,
  88. } FMRadioSubmenuIndex;
  89. typedef enum {
  90. FMRadioViewSubmenu,
  91. FMRadioViewConfigure,
  92. FMRadioViewFlipTheWorld,
  93. FMRadioViewAbout,
  94. } FMRadioView;
  95. // Define a struct to hold the application's components
  96. typedef struct {
  97. ViewDispatcher* view_dispatcher;
  98. NotificationApp* notifications;
  99. Submenu* submenu;
  100. VariableItemList* variable_item_list_config;
  101. View* view_flip_the_world;
  102. Widget* widget_about;
  103. } FMRadio;
  104. // Define a model struct for your application
  105. typedef struct {
  106. uint32_t frequency_index;
  107. uint8_t volume_index;
  108. } MyModel;
  109. // Callback for navigation events
  110. uint32_t fmradio_controller_navigation_exit_callback(void* context) {
  111. UNUSED(context);
  112. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  113. tea5767_sleep(buffer); // Call the tea5767_sleep function, passing the buffer as an argument
  114. return VIEW_NONE;
  115. }
  116. // Callback for navigating to the submenu
  117. uint32_t fmradio_controller_navigation_submenu_callback(void* context) {
  118. UNUSED(context);
  119. return FMRadioViewSubmenu;
  120. }
  121. // Callback for handling submenu selections
  122. void fmradio_controller_submenu_callback(void* context, uint32_t index) {
  123. FMRadio* app = (FMRadio*)context;
  124. switch(index) {
  125. case FMRadioSubmenuIndexConfigure:
  126. view_dispatcher_switch_to_view(app->view_dispatcher, FMRadioViewConfigure);
  127. break;
  128. case FMRadioSubmenuIndexFlipTheWorld:
  129. view_dispatcher_switch_to_view(app->view_dispatcher, FMRadioViewFlipTheWorld);
  130. break;
  131. case FMRadioSubmenuIndexAbout:
  132. view_dispatcher_switch_to_view(app->view_dispatcher, FMRadioViewAbout);
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. bool fmradio_controller_view_input_callback(InputEvent* event, void* context) {
  139. UNUSED(context);
  140. if (event->type == InputTypeShort && event->key == InputKeyLeft) {
  141. tea5767_seekDown();
  142. current_volume = 0;
  143. current_vol = "Un-Muted";
  144. return true; // Event was handled
  145. } else if (event->type == InputTypeShort && event->key == InputKeyRight) {
  146. tea5767_seekUp();
  147. current_volume = 0;
  148. current_vol = "Un-Muted";
  149. return true; // Event was handled
  150. } else if (event->type == InputTypeShort && event->key == InputKeyOk) {
  151. if (current_volume == 0) { // Fixed: == instead of =
  152. tea5767_MuteOn();
  153. current_volume = 1;
  154. current_vol = "Muted";
  155. } else {
  156. tea5767_MuteOff();
  157. current_volume = 0;
  158. current_vol = "Un-Muted";
  159. }
  160. return true; // Event was handled
  161. } else if (event->type == InputTypeShort && event->key == InputKeyUp) {
  162. // Increment the current frequency index and loop back if at the end
  163. current_frequency_index = (current_frequency_index + 1) % (sizeof(frequency_values) / sizeof(frequency_values[0]));
  164. // Set the new frequency
  165. tea5767_SetFreqMHz(frequency_values[current_frequency_index]);
  166. current_volume = 0;
  167. current_vol = "Un-Muted";
  168. return true; // Event was handled
  169. } else if (event->type == InputTypeShort && event->key == InputKeyDown) {
  170. // Decrement the current frequency index and loop back if at the beginning
  171. if (current_frequency_index == 0) {
  172. current_frequency_index = (sizeof(frequency_values) / sizeof(frequency_values[0])) - 1;
  173. } else {
  174. current_frequency_index--;
  175. }
  176. // Set the new frequency
  177. tea5767_SetFreqMHz(frequency_values[current_frequency_index]);
  178. current_volume = 0;
  179. current_vol = "Un-Muted";
  180. return true; // Event was handled
  181. }
  182. return false; // Event was not handled
  183. }
  184. // Callback for handling frequency changes
  185. void fmradio_controller_frequency_change(VariableItem* item) {
  186. FMRadio* app = variable_item_get_context(item);
  187. uint8_t index = variable_item_get_current_value_index(item);
  188. MyModel* model = view_get_model(app->view_flip_the_world);
  189. model->frequency_index = index;
  190. // Display the selected frequency value as text
  191. char frequency_display[16]; // Adjust the buffer size as needed
  192. snprintf(frequency_display, sizeof(frequency_display), "%.1f MHz", (double)frequency_values[index]);
  193. variable_item_set_current_value_text(item, frequency_display);
  194. }
  195. // Callback for handling volume changes
  196. void fmradio_controller_volume_change(VariableItem* item) {
  197. FMRadio* app = variable_item_get_context(item);
  198. uint8_t index = variable_item_get_current_value_index(item);
  199. variable_item_set_current_value_text(item, volume_names[index]); // Display the selected volume as text
  200. MyModel* model = view_get_model(app->view_flip_the_world);
  201. model->volume_index = index;
  202. }
  203. // Callback for drawing the view
  204. void fmradio_controller_view_draw_callback(Canvas* canvas, void* model) {
  205. (void)model; // Mark model as unused
  206. char frequency_display[64];
  207. char signal_display[64];
  208. char volume_display[32];
  209. // tea5767_get_radio_info() populates the info
  210. struct RADIO_INFO info;
  211. uint8_t buffer[5];
  212. // Draw strings on the canvas
  213. canvas_draw_str(canvas, 45, 10, "FM Radio");
  214. // Draw button prompts
  215. canvas_set_font(canvas, FontSecondary);
  216. elements_button_left(canvas, "Scan-");
  217. elements_button_right(canvas, "Scan+");
  218. elements_button_center(canvas, "Mute");
  219. elements_button_top_left(canvas, " Pre");
  220. elements_button_top_right(canvas, "Pre ");
  221. if (tea5767_get_radio_info(buffer, &info)) {
  222. snprintf(frequency_display, sizeof(frequency_display), "Frequency: %.1f MHz", (double)info.frequency);
  223. canvas_draw_str(canvas, 10, 25, frequency_display);
  224. snprintf(signal_display, sizeof(signal_display), "RSSI: %d (%s) %d", info.signalLevel, info.signalQuality, loopcount);
  225. canvas_draw_str(canvas, 10, 45, signal_display);
  226. snprintf(volume_display, sizeof(volume_display), "Status: %s %s", info.muted ? "Playing" : "Muted", info.stereo ? "(Stereo)" : "(Mono)");
  227. canvas_draw_str(canvas, 10, 35, volume_display);
  228. } else {
  229. snprintf(frequency_display, sizeof(frequency_display), "TEA5767 Not Detected");
  230. canvas_draw_str(canvas, 10, 25, frequency_display);
  231. snprintf(signal_display, sizeof(signal_display), "Pin 15 = SDA | Pin 16 = SLC");
  232. canvas_draw_str(canvas, 10, 45, signal_display);
  233. }
  234. }
  235. // Allocate memory for the application
  236. FMRadio* fmradio_controller_alloc() {
  237. FMRadio* app = (FMRadio*)malloc(sizeof(FMRadio));
  238. Gui* gui = furi_record_open(RECORD_GUI);
  239. // Initialize the view dispatcher
  240. app->view_dispatcher = view_dispatcher_alloc();
  241. view_dispatcher_enable_queue(app->view_dispatcher);
  242. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  243. // Initialize the submenu
  244. app->submenu = submenu_alloc();
  245. submenu_add_item(app->submenu,"Listen Now",FMRadioSubmenuIndexFlipTheWorld,fmradio_controller_submenu_callback,app);
  246. //submenu_add_item(app->submenu, "Config", FMRadioSubmenuIndexConfigure, fmradio_controller_submenu_callback, app);
  247. submenu_add_item(app->submenu, "About", FMRadioSubmenuIndexAbout, fmradio_controller_submenu_callback, app);
  248. view_set_previous_callback(submenu_get_view(app->submenu), fmradio_controller_navigation_exit_callback);
  249. view_dispatcher_add_view(app->view_dispatcher, FMRadioViewSubmenu, submenu_get_view(app->submenu));
  250. view_dispatcher_switch_to_view(app->view_dispatcher, FMRadioViewSubmenu);
  251. // Initialize the variable item list for configuration
  252. app->variable_item_list_config = variable_item_list_alloc();
  253. variable_item_list_reset(app->variable_item_list_config);
  254. // Add frequency configuration
  255. VariableItem* frequency_item = variable_item_list_add(app->variable_item_list_config,"Freq (MHz)", COUNT_OF(frequency_values),fmradio_controller_frequency_change,app);
  256. uint32_t frequency_index = 0;
  257. variable_item_set_current_value_index(frequency_item, frequency_index);
  258. // Add volume configuration
  259. VariableItem* volume_item = variable_item_list_add(app->variable_item_list_config,"Volume", COUNT_OF(volume_values),fmradio_controller_volume_change,app);
  260. uint8_t volume_index = 0;
  261. variable_item_set_current_value_index(volume_item, volume_index);
  262. view_set_previous_callback(variable_item_list_get_view(app->variable_item_list_config),fmradio_controller_navigation_submenu_callback);
  263. view_dispatcher_add_view(app->view_dispatcher,FMRadioViewConfigure,variable_item_list_get_view(app->variable_item_list_config));
  264. // Initialize the view for flipping the world
  265. app->view_flip_the_world = view_alloc();
  266. view_set_draw_callback(app->view_flip_the_world, fmradio_controller_view_draw_callback);
  267. view_set_input_callback(app->view_flip_the_world, fmradio_controller_view_input_callback);
  268. view_set_previous_callback(app->view_flip_the_world, fmradio_controller_navigation_submenu_callback);
  269. view_allocate_model(app->view_flip_the_world, ViewModelTypeLockFree, sizeof(MyModel));
  270. MyModel* model = view_get_model(app->view_flip_the_world);
  271. model->frequency_index = frequency_index;
  272. model->volume_index = volume_index;
  273. view_dispatcher_add_view(app->view_dispatcher, FMRadioViewFlipTheWorld, app->view_flip_the_world);
  274. // Initialize the widget for displaying information about the app
  275. app->widget_about = widget_alloc();
  276. widget_add_text_scroll_element(app->widget_about,0,0,128,64,"FM Radio. (v0.8)\n---\n Created By Coolshrimp\n\nUp = Preset Up\nDown = Preset Down\nLeft = Seek Down \nRight = Seek Up \n OK = Toggle Mute");
  277. view_set_previous_callback(widget_get_view(app->widget_about), fmradio_controller_navigation_submenu_callback);
  278. view_dispatcher_add_view(app->view_dispatcher, FMRadioViewAbout, widget_get_view(app->widget_about));
  279. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  280. #ifdef BACKLIGHT_ALWAYS_ON
  281. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  282. #endif
  283. return app;
  284. }
  285. // Free memory used by the application
  286. void fmradio_controller_free(FMRadio* app) {
  287. #ifdef BACKLIGHT_ALWAYS_ON
  288. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  289. #endif
  290. furi_record_close(RECORD_NOTIFICATION);
  291. view_dispatcher_remove_view(app->view_dispatcher, FMRadioViewAbout);
  292. widget_free(app->widget_about);
  293. view_dispatcher_remove_view(app->view_dispatcher, FMRadioViewFlipTheWorld);
  294. view_free(app->view_flip_the_world);
  295. view_dispatcher_remove_view(app->view_dispatcher, FMRadioViewConfigure);
  296. variable_item_list_free(app->variable_item_list_config);
  297. view_dispatcher_remove_view(app->view_dispatcher, FMRadioViewSubmenu);
  298. submenu_free(app->submenu);
  299. view_dispatcher_free(app->view_dispatcher);
  300. furi_record_close(RECORD_GUI);
  301. free(app);
  302. }
  303. // Main function to start the application
  304. int32_t fmradio_controller_app(void* p) {
  305. UNUSED(p);
  306. FMRadio* app = fmradio_controller_alloc();
  307. view_dispatcher_run(app->view_dispatcher);
  308. fmradio_controller_free(app);
  309. return 0;
  310. }