radio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. // Function prototypes for forward declarations
  91. void elements_button_top_left(Canvas* canvas, const char* str);
  92. void elements_button_top_right(Canvas* canvas, const char* str);
  93. //lib can only do bottom left/right
  94. void elements_button_top_left(Canvas* canvas, const char* str) {
  95. const uint8_t button_height = 10; // Define the height of the button
  96. const uint8_t vertical_offset = 2; // Define the vertical offset of the text
  97. const uint8_t horizontal_offset = 2; // Define the horizontal offset of the text
  98. const uint8_t string_width = canvas_string_width(canvas, str);
  99. const Icon* icon = &I_ButtonUp;
  100. const uint8_t icon_h_offset = 2;
  101. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  102. const uint8_t icon_v_offset = icon->height + vertical_offset;
  103. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  104. const uint8_t x = 0;
  105. const uint8_t y = 0 + button_height;
  106. canvas_draw_box(canvas, x, y - button_height, button_width, button_height); // Draw the button
  107. canvas_draw_line(
  108. canvas,
  109. x + button_width + 0,
  110. y - button_height,
  111. x + button_width + 0,
  112. y - 1); // Draw the button border
  113. canvas_draw_line(
  114. canvas,
  115. x + button_width + 1,
  116. y - button_height,
  117. x + button_width + 1,
  118. y - 2); // Draw the button border
  119. canvas_draw_line(
  120. canvas,
  121. x + button_width + 2,
  122. y - button_height,
  123. x + button_width + 2,
  124. y - 3); // Draw the button border
  125. canvas_invert_color(canvas); // Invert the color of the text and icon
  126. canvas_draw_str(
  127. canvas,
  128. x + horizontal_offset + icon_width_with_offset,
  129. y - vertical_offset,
  130. str); // Draw the text
  131. canvas_draw_icon(canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonUp);
  132. canvas_invert_color(canvas); // Invert the color of the text and icon
  133. }
  134. void elements_button_top_right(Canvas* canvas, const char* str) {
  135. const uint8_t button_height = 10;
  136. const uint8_t vertical_offset = 2;
  137. const uint8_t horizontal_offset = 2;
  138. const uint8_t string_width = canvas_string_width(canvas, str);
  139. const Icon* icon = &I_ButtonUp;
  140. const uint8_t icon_h_offset = 2;
  141. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  142. const uint8_t icon_v_offset = icon->height + vertical_offset;
  143. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  144. const uint8_t x = canvas_width(canvas);
  145. const uint8_t y = 0 + button_height;
  146. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  147. canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
  148. canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
  149. canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
  150. canvas_invert_color(canvas);
  151. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  152. canvas_draw_icon_ex(
  153. canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonUp, IconRotation180);
  154. canvas_invert_color(canvas);
  155. }
  156. // Enumerations for submenu and view indices
  157. typedef enum {
  158. MyAppSubmenuIndexConfigure,
  159. MyAppSubmenuIndexFlipTheWorld,
  160. MyAppSubmenuIndexAbout,
  161. } MyAppSubmenuIndex;
  162. typedef enum {
  163. MyAppViewSubmenu,
  164. MyAppViewConfigure,
  165. MyAppViewFlipTheWorld,
  166. MyAppViewAbout,
  167. } MyAppView;
  168. // Define a struct to hold the application's components
  169. typedef struct {
  170. ViewDispatcher* view_dispatcher;
  171. NotificationApp* notifications;
  172. Submenu* submenu;
  173. VariableItemList* variable_item_list_config;
  174. View* view_flip_the_world;
  175. Widget* widget_about;
  176. } MyApp;
  177. // Define a model struct for your application
  178. typedef struct {
  179. uint32_t current_station_index;
  180. uint8_t volume_index;
  181. } MyModel;
  182. // Callback for navigation events
  183. uint32_t my_app_navigation_exit_callback(void* context) {
  184. UNUSED(context);
  185. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  186. tea5767_sleep(buffer); // Call the tea5767_sleep function, passing the buffer as an argument
  187. return VIEW_NONE;
  188. }
  189. // Callback for navigating to the submenu
  190. uint32_t my_app_navigation_submenu_callback(void* context) {
  191. UNUSED(context);
  192. return MyAppViewSubmenu;
  193. }
  194. // Callback for handling submenu selections
  195. void my_app_submenu_callback(void* context, uint32_t index) {
  196. MyApp* app = (MyApp*)context;
  197. switch(index) {
  198. case MyAppSubmenuIndexConfigure:
  199. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewConfigure);
  200. break;
  201. case MyAppSubmenuIndexFlipTheWorld:
  202. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  203. break;
  204. case MyAppSubmenuIndexAbout:
  205. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewAbout);
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. bool my_app_view_input_callback(InputEvent* event, void* context) {
  212. UNUSED(context);
  213. if(event->type == InputTypeShort && event->key == InputKeyLeft) {
  214. tea5767_seekDown();
  215. current_volume = 0;
  216. return true; // Event was handled
  217. } else if(event->type == InputTypeShort && event->key == InputKeyRight) {
  218. tea5767_seekUp();
  219. current_volume = 0;
  220. return true; // Event was handled
  221. } else if(event->type == InputTypeShort && event->key == InputKeyOk) {
  222. if(current_volume == 0) {
  223. tea5767_MuteOn();
  224. current_volume = 1;
  225. } else {
  226. tea5767_MuteOff();
  227. current_volume = 0;
  228. }
  229. return true; // Event was handled
  230. } else if(event->type == InputTypeShort && event->key == InputKeyUp) {
  231. // Increment the current station index and loop back if at the end
  232. current_station_index = (current_station_index + 1) % NUM_STATIONS;
  233. // Set the new frequency from the stations array
  234. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  235. current_volume = 0;
  236. return true; // Event was handled
  237. } else if(event->type == InputTypeShort && event->key == InputKeyDown) {
  238. // Decrement the current station index and loop back if at the beginning
  239. if(current_station_index == 0) {
  240. current_station_index = NUM_STATIONS - 1;
  241. } else {
  242. current_station_index--;
  243. }
  244. // Set the new frequency from the stations array
  245. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  246. current_volume = 0;
  247. return true; // Event was handled
  248. }
  249. return false; // Event was not handled
  250. }
  251. // Callback for handling frequency changes
  252. void my_app_frequency_change(VariableItem* item) {
  253. MyApp* app = variable_item_get_context(item);
  254. uint8_t index = variable_item_get_current_value_index(item);
  255. MyModel* model = view_get_model(app->view_flip_the_world);
  256. model->current_station_index = index;
  257. // Display the selected frequency value as text
  258. char frequency_display[16];
  259. snprintf(
  260. frequency_display,
  261. sizeof(frequency_display),
  262. "%.1f MHz",
  263. (double)stations[(int)index].frequency);
  264. variable_item_set_current_value_text(item, frequency_display);
  265. }
  266. // Callback for handling volume changes
  267. void my_app_volume_change(VariableItem* item) {
  268. MyApp* app = variable_item_get_context(item);
  269. uint8_t index = variable_item_get_current_value_index(item);
  270. variable_item_set_current_value_text(
  271. item, volume_names[index]); // Display the selected volume as text
  272. MyModel* model = view_get_model(app->view_flip_the_world);
  273. model->volume_index = index;
  274. }
  275. // Declare local buffers for text display
  276. char frequency_display[64];
  277. char station_display[256];
  278. char signal_display[64];
  279. char volume_display[32];
  280. // Callback for drawing the view
  281. void my_app_view_draw_callback(Canvas* canvas, void* model) {
  282. (void)model;
  283. // Draw strings on the canvas
  284. canvas_set_font(canvas, FontPrimary);
  285. canvas_draw_str(canvas, 33, 10, "FM Radio");
  286. canvas_draw_icon(canvas, 83, 0, &I_RadioSmall);
  287. // Draw button prompts
  288. canvas_set_font(canvas, FontSecondary);
  289. elements_button_top_left(canvas, "Pre");
  290. elements_button_top_right(canvas, "Pre");
  291. elements_button_left(canvas, "Scan-");
  292. elements_button_center(canvas, "Mute");
  293. elements_button_right(canvas, "Scan+");
  294. struct RADIO_INFO info; // Create a struct to hold the radio info
  295. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  296. if(tea5767_get_radio_info(buffer, &info)) {
  297. snprintf(
  298. frequency_display,
  299. sizeof(frequency_display),
  300. "Frequency: %.1f MHz",
  301. (double)info.frequency);
  302. float tolerance = 0.05f;
  303. float diff = stations[current_station_index].frequency - info.frequency;
  304. if(fabsf(diff) < tolerance) {
  305. snprintf(
  306. station_display,
  307. sizeof(station_display),
  308. "%s",
  309. stations[current_station_index].name);
  310. } else {
  311. station_display[0] = '\0';
  312. }
  313. snprintf(
  314. volume_display,
  315. sizeof(volume_display),
  316. "Status: %s %s",
  317. info.muted ? "Playing" : "Muted",
  318. info.stereo ? "(Mono)" : "(Stereo)");
  319. snprintf(
  320. signal_display,
  321. sizeof(signal_display),
  322. "Signal: %d (%s)",
  323. info.signalLevel,
  324. info.signalQuality);
  325. } else {
  326. // Display error message if TEA5767 is not detected
  327. snprintf(frequency_display, sizeof(frequency_display), "TEA5767 Not Detected");
  328. snprintf(signal_display, sizeof(signal_display), "Pin 15 = SDA | Pin 16 = SLC");
  329. // Reset frequency_display and volume_display to blank
  330. station_display[0] = '\0';
  331. volume_display[0] = '\0';
  332. }
  333. // Display the stored values on the 5th loop
  334. canvas_set_font(canvas, FontPrimary);
  335. canvas_draw_str(canvas, 10, 20, station_display);
  336. canvas_set_font(canvas, FontSecondary);
  337. canvas_draw_str(canvas, 10, 30, frequency_display);
  338. canvas_draw_str(canvas, 10, 40, volume_display);
  339. canvas_draw_str(canvas, 10, 49, signal_display);
  340. }
  341. // Allocate memory for the application
  342. MyApp* my_app_alloc() {
  343. MyApp* app = (MyApp*)malloc(sizeof(MyApp));
  344. Gui* gui = furi_record_open(RECORD_GUI);
  345. // Initialize the view dispatcher
  346. app->view_dispatcher = view_dispatcher_alloc();
  347. view_dispatcher_enable_queue(app->view_dispatcher);
  348. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  349. // Initialize the submenu
  350. app->submenu = submenu_alloc();
  351. submenu_add_item(
  352. app->submenu, "Listen Now", MyAppSubmenuIndexFlipTheWorld, my_app_submenu_callback, app);
  353. //submenu_add_item(app->submenu, "Config", MyAppSubmenuIndexConfigure, my_app_submenu_callback, app);
  354. submenu_add_item(app->submenu, "About", MyAppSubmenuIndexAbout, my_app_submenu_callback, app);
  355. view_set_previous_callback(submenu_get_view(app->submenu), my_app_navigation_exit_callback);
  356. view_dispatcher_add_view(
  357. app->view_dispatcher, MyAppViewSubmenu, submenu_get_view(app->submenu));
  358. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewSubmenu);
  359. // Initialize the variable item list for configuration
  360. app->variable_item_list_config = variable_item_list_alloc();
  361. variable_item_list_reset(app->variable_item_list_config);
  362. // Add frequency configuration
  363. VariableItem* frequency_item = variable_item_list_add(
  364. app->variable_item_list_config, "Freq (MHz)", NUM_STATIONS, my_app_frequency_change, app);
  365. uint32_t current_station_index = 0;
  366. variable_item_set_current_value_index(frequency_item, current_station_index);
  367. // Add volume configuration
  368. VariableItem* volume_item = variable_item_list_add(
  369. app->variable_item_list_config,
  370. "Volume",
  371. COUNT_OF(volume_values),
  372. my_app_volume_change,
  373. app);
  374. uint8_t volume_index = 0;
  375. variable_item_set_current_value_index(volume_item, volume_index);
  376. view_set_previous_callback(
  377. variable_item_list_get_view(app->variable_item_list_config),
  378. my_app_navigation_submenu_callback);
  379. view_dispatcher_add_view(
  380. app->view_dispatcher,
  381. MyAppViewConfigure,
  382. variable_item_list_get_view(app->variable_item_list_config));
  383. // Initialize the view for flipping the world
  384. app->view_flip_the_world = view_alloc();
  385. view_set_draw_callback(app->view_flip_the_world, my_app_view_draw_callback);
  386. view_set_input_callback(app->view_flip_the_world, my_app_view_input_callback);
  387. view_set_previous_callback(app->view_flip_the_world, my_app_navigation_submenu_callback);
  388. view_allocate_model(app->view_flip_the_world, ViewModelTypeLockFree, sizeof(MyModel));
  389. MyModel* model = view_get_model(app->view_flip_the_world);
  390. model->current_station_index = current_station_index;
  391. model->volume_index = volume_index;
  392. view_dispatcher_add_view(
  393. app->view_dispatcher, MyAppViewFlipTheWorld, app->view_flip_the_world);
  394. // Initialize the widget for displaying information about the app
  395. app->widget_about = widget_alloc();
  396. widget_add_text_scroll_element(
  397. app->widget_about,
  398. 0,
  399. 0,
  400. 128,
  401. 64,
  402. "FM Radio (v1.1)\n"
  403. "---\n"
  404. "Created By Coolshrimp\n\n"
  405. "Up = Preset Up\n"
  406. "Down = Preset Down\n"
  407. "Left = Seek Down\n"
  408. "Right = Seek Up\n"
  409. "OK = Toggle Mute");
  410. view_set_previous_callback(
  411. widget_get_view(app->widget_about), my_app_navigation_submenu_callback);
  412. view_dispatcher_add_view(
  413. app->view_dispatcher, MyAppViewAbout, widget_get_view(app->widget_about));
  414. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  415. #ifdef BACKLIGHT_ALWAYS_ON
  416. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  417. #endif
  418. return app;
  419. }
  420. // Free memory used by the application
  421. void my_app_free(MyApp* app) {
  422. #ifdef BACKLIGHT_ALWAYS_ON
  423. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  424. #endif
  425. furi_record_close(RECORD_NOTIFICATION);
  426. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewAbout);
  427. widget_free(app->widget_about);
  428. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  429. view_free(app->view_flip_the_world);
  430. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewConfigure);
  431. variable_item_list_free(app->variable_item_list_config);
  432. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewSubmenu);
  433. submenu_free(app->submenu);
  434. view_dispatcher_free(app->view_dispatcher);
  435. furi_record_close(RECORD_GUI);
  436. free(app);
  437. }
  438. // Main function to start the application
  439. int32_t my_fm_radio(void* p) {
  440. UNUSED(p);
  441. MyApp* app = my_app_alloc();
  442. view_dispatcher_run(app->view_dispatcher);
  443. my_app_free(app);
  444. return 0;
  445. }