radio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. *
  3. * @author Coolshrimp - CoolshrimpModz.com
  4. *
  5. * @brief FM Radio using the TEA5767 FM radio chip.
  6. * @version 1.1
  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 <stdbool.h> // for true/false
  24. #include <stdint.h> // for uint8_t, size_t
  25. #include <stdio.h> // for snprintf
  26. #include <math.h> // for fabsf
  27. #include "TEA5767/TEA5767.h"
  28. #include <gui/icon_i.h>
  29. #include "fm_radio_icons.h"
  30. // Define macros for easier management
  31. #define BACKLIGHT_ALWAYS_ON
  32. #define TAG "FM_Radio"
  33. // Declare global variables
  34. uint8_t volume_values[] = {0, 1};
  35. char* volume_names[] = {"Un-Muted", "Muted"};
  36. bool current_volume = 1; // Current volume state
  37. int* signal_strength; // Signal strength (unused, consider removing or implementing)
  38. uint8_t tea5767_registers[5];
  39. uint32_t current_station_index = 0; // Default to the first frequency
  40. // Station struct to hold frequency and station name
  41. struct Station {
  42. float frequency;
  43. char* name;
  44. };
  45. #define NUM_VOLUME_VALUES (sizeof(volume_values) / sizeof(volume_values[0]))
  46. #define NUM_STATIONS (sizeof(stations) / sizeof(stations[0]))
  47. // Array of Stations
  48. struct Station stations[] = {
  49. {88.1, "CIND Indie 88"},
  50. {88.9, "CIRV Portuguese"},
  51. {89.5, "CIUT FM"},
  52. {89.7, "89.7 K-Love"},
  53. {90.3, "Ici Musique"},
  54. {91.1, "JAZZ.FM91"},
  55. {92.3, "Zeta 92"},
  56. {92.5, "KiSS Radio"},
  57. {93.1, "Easy 93.1"},
  58. {93.5, "93.5 Today"},
  59. {93.9, "Urbana FM"},
  60. {94.1, "CBC Music"},
  61. {94.5, "Star 94.5"},
  62. {95.7, "Ritmo 95.7"},
  63. {95.9, "KX96"},
  64. {96.3, "Classical 96.3"},
  65. {96.5, "Power 96"},
  66. {97.3, "Hits 97.3"},
  67. {98.1, "CHFI"},
  68. {98.3, "Mix 98.3"},
  69. {98.7, "Flow 98.7"},
  70. {99.1, "99 Jamz"},
  71. {99.5, "Viva FM"},
  72. {99.9, "99.9 Kiss Country"},
  73. {100.3, "Rumba FM"},
  74. {100.7, "Y100.7"},
  75. {101.1, "The Rock Station"},
  76. {101.3, "CMR Diversity"},
  77. {101.5, "101.5 Lite FM"},
  78. {102.1, "102.1 Edge"},
  79. {102.7, "102.7 The Beach"},
  80. {103.5, "The Beat 103.5"},
  81. {103.9, "Proud FM"},
  82. {104.5, "CHUM"},
  83. {105.1, "HOT 105"},
  84. {105.5, "VIBE1055 FM"},
  85. {106.5, "106.5 Elmnt"},
  86. {106.7, "106.7 El Zol"},
  87. {107.1, "Q107"},
  88. {107.5, "Amor 107.5"},
  89. };
  90. // Enumerations for submenu and view indices
  91. typedef enum {
  92. MyAppSubmenuIndexConfigure,
  93. MyAppSubmenuIndexFlipTheWorld,
  94. MyAppSubmenuIndexAbout,
  95. } MyAppSubmenuIndex;
  96. typedef enum {
  97. MyAppViewSubmenu,
  98. MyAppViewConfigure,
  99. MyAppViewFlipTheWorld,
  100. MyAppViewAbout,
  101. } MyAppView;
  102. // Define a struct to hold the application's components
  103. typedef struct {
  104. ViewDispatcher* view_dispatcher;
  105. NotificationApp* notifications;
  106. Submenu* submenu;
  107. VariableItemList* variable_item_list_config;
  108. View* view_flip_the_world;
  109. Widget* widget_about;
  110. } MyApp;
  111. // Define a model struct for your application
  112. typedef struct {
  113. uint32_t current_station_index;
  114. uint8_t volume_index;
  115. } MyModel;
  116. // Callback for navigation events
  117. uint32_t my_app_navigation_exit_callback(void* context) {
  118. UNUSED(context);
  119. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  120. tea5767_sleep(buffer); // Call the tea5767_sleep function, passing the buffer as an argument
  121. return VIEW_NONE;
  122. }
  123. // Callback for navigating to the submenu
  124. uint32_t my_app_navigation_submenu_callback(void* context) {
  125. UNUSED(context);
  126. return MyAppViewSubmenu;
  127. }
  128. // Callback for handling submenu selections
  129. void my_app_submenu_callback(void* context, uint32_t index) {
  130. MyApp* app = (MyApp*)context;
  131. switch(index) {
  132. case MyAppSubmenuIndexConfigure:
  133. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewConfigure);
  134. break;
  135. case MyAppSubmenuIndexFlipTheWorld:
  136. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  137. break;
  138. case MyAppSubmenuIndexAbout:
  139. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewAbout);
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. bool my_app_view_input_callback(InputEvent* event, void* context) {
  146. UNUSED(context);
  147. if(event->type == InputTypeShort && event->key == InputKeyLeft) {
  148. tea5767_seekDown();
  149. current_volume = 0;
  150. return true; // Event was handled
  151. } else if(event->type == InputTypeShort && event->key == InputKeyRight) {
  152. tea5767_seekUp();
  153. current_volume = 0;
  154. return true; // Event was handled
  155. } else if(event->type == InputTypeShort && event->key == InputKeyOk) {
  156. if(current_volume == 0) {
  157. tea5767_MuteOn();
  158. current_volume = 1;
  159. } else {
  160. tea5767_MuteOff();
  161. current_volume = 0;
  162. }
  163. return true; // Event was handled
  164. } else if(event->type == InputTypeShort && event->key == InputKeyUp) {
  165. // Increment the current station index and loop back if at the end
  166. current_station_index = (current_station_index + 1) % NUM_STATIONS;
  167. // Set the new frequency from the stations array
  168. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  169. current_volume = 0;
  170. return true; // Event was handled
  171. } else if(event->type == InputTypeShort && event->key == InputKeyDown) {
  172. // Decrement the current station index and loop back if at the beginning
  173. if(current_station_index == 0) {
  174. current_station_index = NUM_STATIONS - 1;
  175. } else {
  176. current_station_index--;
  177. }
  178. // Set the new frequency from the stations array
  179. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  180. current_volume = 0;
  181. return true; // Event was handled
  182. }
  183. return false; // Event was not handled
  184. }
  185. // Callback for handling frequency changes
  186. void my_app_frequency_change(VariableItem* item) {
  187. MyApp* app = variable_item_get_context(item);
  188. uint8_t index = variable_item_get_current_value_index(item);
  189. MyModel* model = view_get_model(app->view_flip_the_world);
  190. model->current_station_index = index;
  191. // Display the selected frequency value as text
  192. char frequency_display[16];
  193. snprintf(
  194. frequency_display,
  195. sizeof(frequency_display),
  196. "%.1f MHz",
  197. (double)stations[(int)index].frequency);
  198. variable_item_set_current_value_text(item, frequency_display);
  199. }
  200. // Callback for handling volume changes
  201. void my_app_volume_change(VariableItem* item) {
  202. MyApp* app = variable_item_get_context(item);
  203. uint8_t index = variable_item_get_current_value_index(item);
  204. variable_item_set_current_value_text(
  205. item, volume_names[index]); // Display the selected volume as text
  206. MyModel* model = view_get_model(app->view_flip_the_world);
  207. model->volume_index = index;
  208. }
  209. // Declare local buffers for text display
  210. char frequency_display[64];
  211. char station_display[256];
  212. char signal_display[64];
  213. char volume_display[32];
  214. // Callback for drawing the view
  215. void my_app_view_draw_callback(Canvas* canvas, void* model) {
  216. (void)model;
  217. // Draw strings on the canvas
  218. canvas_set_font(canvas, FontPrimary);
  219. canvas_draw_str(canvas, 35, 10, "FM Radio");
  220. canvas_draw_icon(canvas, 84, 0, &I_radio);
  221. // Draw button prompts
  222. canvas_set_font(canvas, FontSecondary);
  223. elements_button_up(canvas, "Pre");
  224. elements_button_down(canvas, "Pre");
  225. elements_button_left(canvas, "Scan-");
  226. elements_button_center(canvas, "Mute");
  227. elements_button_right(canvas, "Scan+");
  228. struct RADIO_INFO info; // Create a struct to hold the radio info
  229. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  230. if(tea5767_get_radio_info(buffer, &info)) {
  231. snprintf(
  232. frequency_display,
  233. sizeof(frequency_display),
  234. "Frequency: %.1f MHz",
  235. (double)info.frequency);
  236. float tolerance = 0.05f;
  237. float diff = stations[current_station_index].frequency - info.frequency;
  238. if(fabsf(diff) < tolerance) {
  239. snprintf(
  240. station_display,
  241. sizeof(station_display),
  242. "%s",
  243. stations[current_station_index].name);
  244. } else {
  245. station_display[0] = '\0';
  246. }
  247. snprintf(
  248. volume_display,
  249. sizeof(volume_display),
  250. "Status: %s %s",
  251. info.muted ? "Playing" : "Muted",
  252. info.stereo ? "(Mono)" : "(Stereo)");
  253. snprintf(
  254. signal_display,
  255. sizeof(signal_display),
  256. "Signal: %d (%s)",
  257. info.signalLevel,
  258. info.signalQuality);
  259. } else {
  260. // Display error message if TEA5767 is not detected
  261. snprintf(frequency_display, sizeof(frequency_display), "TEA5767 Not Detected");
  262. snprintf(signal_display, sizeof(signal_display), "Pin 15 = SDA | Pin 16 = SLC");
  263. // Reset frequency_display and volume_display to blank
  264. station_display[0] = '\0';
  265. volume_display[0] = '\0';
  266. }
  267. // Display the stored values on the 5th loop
  268. canvas_set_font(canvas, FontPrimary);
  269. canvas_draw_str(canvas, 10, 20, station_display);
  270. canvas_set_font(canvas, FontSecondary);
  271. canvas_draw_str(canvas, 10, 30, frequency_display);
  272. canvas_draw_str(canvas, 10, 40, volume_display);
  273. canvas_draw_str(canvas, 10, 49, signal_display);
  274. }
  275. // Allocate memory for the application
  276. MyApp* my_app_alloc() {
  277. MyApp* app = (MyApp*)malloc(sizeof(MyApp));
  278. Gui* gui = furi_record_open(RECORD_GUI);
  279. // Initialize the view dispatcher
  280. app->view_dispatcher = view_dispatcher_alloc();
  281. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  282. // Initialize the submenu
  283. app->submenu = submenu_alloc();
  284. submenu_add_item(
  285. app->submenu, "Listen Now", MyAppSubmenuIndexFlipTheWorld, my_app_submenu_callback, app);
  286. //submenu_add_item(app->submenu, "Config", MyAppSubmenuIndexConfigure, my_app_submenu_callback, app);
  287. submenu_add_item(app->submenu, "About", MyAppSubmenuIndexAbout, my_app_submenu_callback, app);
  288. view_set_previous_callback(submenu_get_view(app->submenu), my_app_navigation_exit_callback);
  289. view_dispatcher_add_view(
  290. app->view_dispatcher, MyAppViewSubmenu, submenu_get_view(app->submenu));
  291. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewSubmenu);
  292. // Initialize the variable item list for configuration
  293. app->variable_item_list_config = variable_item_list_alloc();
  294. variable_item_list_reset(app->variable_item_list_config);
  295. // Add frequency configuration
  296. VariableItem* frequency_item = variable_item_list_add(
  297. app->variable_item_list_config, "Freq (MHz)", NUM_STATIONS, my_app_frequency_change, app);
  298. uint32_t current_station_index = 0;
  299. variable_item_set_current_value_index(frequency_item, current_station_index);
  300. // Add volume configuration
  301. VariableItem* volume_item = variable_item_list_add(
  302. app->variable_item_list_config,
  303. "Volume",
  304. COUNT_OF(volume_values),
  305. my_app_volume_change,
  306. app);
  307. uint8_t volume_index = 0;
  308. variable_item_set_current_value_index(volume_item, volume_index);
  309. view_set_previous_callback(
  310. variable_item_list_get_view(app->variable_item_list_config),
  311. my_app_navigation_submenu_callback);
  312. view_dispatcher_add_view(
  313. app->view_dispatcher,
  314. MyAppViewConfigure,
  315. variable_item_list_get_view(app->variable_item_list_config));
  316. // Initialize the view for flipping the world
  317. app->view_flip_the_world = view_alloc();
  318. view_set_draw_callback(app->view_flip_the_world, my_app_view_draw_callback);
  319. view_set_input_callback(app->view_flip_the_world, my_app_view_input_callback);
  320. view_set_previous_callback(app->view_flip_the_world, my_app_navigation_submenu_callback);
  321. view_allocate_model(app->view_flip_the_world, ViewModelTypeLockFree, sizeof(MyModel));
  322. MyModel* model = view_get_model(app->view_flip_the_world);
  323. model->current_station_index = current_station_index;
  324. model->volume_index = volume_index;
  325. view_dispatcher_add_view(
  326. app->view_dispatcher, MyAppViewFlipTheWorld, app->view_flip_the_world);
  327. // Initialize the widget for displaying information about the app
  328. app->widget_about = widget_alloc();
  329. widget_add_text_scroll_element(
  330. app->widget_about,
  331. 0,
  332. 0,
  333. 128,
  334. 64,
  335. "FM Radio (v1.1)\n"
  336. "---\n"
  337. "Created By Coolshrimp\n\n"
  338. "Up = Preset Up\n"
  339. "Down = Preset Down\n"
  340. "Left = Seek Down\n"
  341. "Right = Seek Up\n"
  342. "OK = Toggle Mute");
  343. view_set_previous_callback(
  344. widget_get_view(app->widget_about), my_app_navigation_submenu_callback);
  345. view_dispatcher_add_view(
  346. app->view_dispatcher, MyAppViewAbout, widget_get_view(app->widget_about));
  347. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  348. #ifdef BACKLIGHT_ALWAYS_ON
  349. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  350. #endif
  351. return app;
  352. }
  353. // Free memory used by the application
  354. void my_app_free(MyApp* app) {
  355. #ifdef BACKLIGHT_ALWAYS_ON
  356. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  357. #endif
  358. furi_record_close(RECORD_NOTIFICATION);
  359. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewAbout);
  360. widget_free(app->widget_about);
  361. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  362. view_free(app->view_flip_the_world);
  363. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewConfigure);
  364. variable_item_list_free(app->variable_item_list_config);
  365. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewSubmenu);
  366. submenu_free(app->submenu);
  367. view_dispatcher_free(app->view_dispatcher);
  368. furi_record_close(RECORD_GUI);
  369. free(app);
  370. }
  371. // Main function to start the application
  372. int32_t my_fm_radio(void* p) {
  373. UNUSED(p);
  374. MyApp* app = my_app_alloc();
  375. view_dispatcher_run(app->view_dispatcher);
  376. my_app_free(app);
  377. return 0;
  378. }