radio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. {90.3, "Ici Musique"},
  53. {91.1, "JAZZ.FM91"},
  54. {92.5, "KiSS Radio"},
  55. {93.5, "93.5 Today"},
  56. {94.1, "CBC Music"},
  57. {95.9, "KX96"},
  58. {96.3, "Classical 96.3"},
  59. {97.3, "Boom 97.3"},
  60. {98.1, "CHFI"},
  61. {98.7, "Flow 98.7"},
  62. {99.1, "CBC Radio 1"},
  63. {99.9, "99.9 Toronto"},
  64. {100.7, "CHIN 100.7"},
  65. {101.3, "CMR Diversity"},
  66. {101.3, "CMR Hindi Urdu"},
  67. {101.3, "CMR Punjabi"},
  68. {101.3, "CMR Tamil"},
  69. {102.1, "102.1 Edge"},
  70. {103.5, "Z103.5"},
  71. {103.9, "Proud FM"},
  72. {104.5, "CHUM"},
  73. {105.5, "VIBE1055 FM"},
  74. {106.5, "106.5 Elmnt"},
  75. {107.1, "Q107"}
  76. };
  77. // Function prototypes for forward declarations
  78. void elements_button_top_left(Canvas* canvas, const char* str);
  79. void elements_button_top_right(Canvas* canvas, const char* str);
  80. //lib can only do bottom left/right
  81. void elements_button_top_left(Canvas* canvas, const char* str) {
  82. const uint8_t button_height = 10; // Define the height of the button
  83. const uint8_t vertical_offset = 2; // Define the vertical offset of the text
  84. const uint8_t horizontal_offset = 2; // Define the horizontal offset of the text
  85. const uint8_t string_width = canvas_string_width(canvas, str);
  86. const Icon* icon = &I_ButtonUp;
  87. const uint8_t icon_h_offset = 2;
  88. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  89. const uint8_t icon_v_offset = icon->height + vertical_offset;
  90. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  91. const uint8_t x = 0;
  92. const uint8_t y = 0 + button_height;
  93. canvas_draw_box(canvas, x, y - button_height, button_width, button_height); // Draw the button
  94. canvas_draw_line(canvas, x + button_width + 0, y - button_height, x + button_width + 0, y - 1); // Draw the button border
  95. canvas_draw_line(canvas, x + button_width + 1, y - button_height, x + button_width + 1, y - 2); // Draw the button border
  96. canvas_draw_line(canvas, x + button_width + 2, y - button_height, x + button_width + 2, y - 3); // Draw the button border
  97. canvas_invert_color(canvas); // Invert the color of the text and icon
  98. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonUp); // Draw the icon
  99. canvas_draw_str(canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str); // Draw the text
  100. canvas_invert_color(canvas); // Invert the color of the text and icon
  101. }
  102. void elements_button_top_right(Canvas* canvas, const char* str) {
  103. const uint8_t button_height = 10;
  104. const uint8_t vertical_offset = 2;
  105. const uint8_t horizontal_offset = 2;
  106. const uint8_t string_width = canvas_string_width(canvas, str);
  107. const Icon* icon = &I_ButtonDown;
  108. const uint8_t icon_h_offset = 2;
  109. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  110. const uint8_t icon_v_offset = icon->height + vertical_offset;
  111. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  112. const uint8_t x = canvas_width(canvas);
  113. const uint8_t y = 0 + button_height;
  114. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  115. canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
  116. canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
  117. canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
  118. canvas_invert_color(canvas);
  119. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  120. canvas_draw_icon(
  121. canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonDown);
  122. canvas_invert_color(canvas);
  123. }
  124. // Enumerations for submenu and view indices
  125. typedef enum {
  126. MyAppSubmenuIndexConfigure,
  127. MyAppSubmenuIndexFlipTheWorld,
  128. MyAppSubmenuIndexAbout,
  129. } MyAppSubmenuIndex;
  130. typedef enum {
  131. MyAppViewSubmenu,
  132. MyAppViewConfigure,
  133. MyAppViewFlipTheWorld,
  134. MyAppViewAbout,
  135. } MyAppView;
  136. // Define a struct to hold the application's components
  137. typedef struct {
  138. ViewDispatcher* view_dispatcher;
  139. NotificationApp* notifications;
  140. Submenu* submenu;
  141. VariableItemList* variable_item_list_config;
  142. View* view_flip_the_world;
  143. Widget* widget_about;
  144. } MyApp;
  145. // Define a model struct for your application
  146. typedef struct {
  147. uint32_t current_station_index;
  148. uint8_t volume_index;
  149. } MyModel;
  150. // Callback for navigation events
  151. uint32_t my_app_navigation_exit_callback(void* context) {
  152. UNUSED(context);
  153. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  154. tea5767_sleep(buffer); // Call the tea5767_sleep function, passing the buffer as an argument
  155. return VIEW_NONE;
  156. }
  157. // Callback for navigating to the submenu
  158. uint32_t my_app_navigation_submenu_callback(void* context) {
  159. UNUSED(context);
  160. return MyAppViewSubmenu;
  161. }
  162. // Callback for handling submenu selections
  163. void my_app_submenu_callback(void* context, uint32_t index) {
  164. MyApp* app = (MyApp*)context;
  165. switch(index) {
  166. case MyAppSubmenuIndexConfigure:
  167. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewConfigure);
  168. break;
  169. case MyAppSubmenuIndexFlipTheWorld:
  170. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  171. break;
  172. case MyAppSubmenuIndexAbout:
  173. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewAbout);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. bool my_app_view_input_callback(InputEvent* event, void* context) {
  180. UNUSED(context);
  181. if(event->type == InputTypeShort && event->key == InputKeyLeft) {
  182. tea5767_seekDown();
  183. current_volume = 0;
  184. return true; // Event was handled
  185. } else if(event->type == InputTypeShort && event->key == InputKeyRight) {
  186. tea5767_seekUp();
  187. current_volume = 0;
  188. return true; // Event was handled
  189. } else if(event->type == InputTypeShort && event->key == InputKeyOk) {
  190. if(current_volume == 0) {
  191. tea5767_MuteOn();
  192. current_volume = 1;
  193. } else {
  194. tea5767_MuteOff();
  195. current_volume = 0;
  196. }
  197. return true; // Event was handled
  198. } else if(event->type == InputTypeShort && event->key == InputKeyUp) {
  199. // Increment the current station index and loop back if at the end
  200. current_station_index = (current_station_index + 1) % NUM_STATIONS;
  201. // Set the new frequency from the stations array
  202. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  203. current_volume = 0;
  204. return true; // Event was handled
  205. } else if(event->type == InputTypeShort && event->key == InputKeyDown) {
  206. // Decrement the current station index and loop back if at the beginning
  207. if(current_station_index == 0) {
  208. current_station_index = NUM_STATIONS - 1;
  209. } else {
  210. current_station_index--;
  211. }
  212. // Set the new frequency from the stations array
  213. tea5767_SetFreqMHz(stations[current_station_index].frequency);
  214. current_volume = 0;
  215. return true; // Event was handled
  216. }
  217. return false; // Event was not handled
  218. }
  219. // Callback for handling frequency changes
  220. void my_app_frequency_change(VariableItem* item) {
  221. MyApp* app = variable_item_get_context(item);
  222. uint8_t index = variable_item_get_current_value_index(item);
  223. MyModel* model = view_get_model(app->view_flip_the_world);
  224. model->current_station_index = index;
  225. // Display the selected frequency value as text
  226. char frequency_display[16];
  227. snprintf(frequency_display, sizeof(frequency_display), "%.1f MHz", (double)stations[(int)index].frequency);
  228. variable_item_set_current_value_text(item, frequency_display);
  229. }
  230. // Callback for handling volume changes
  231. void my_app_volume_change(VariableItem* item) {
  232. MyApp* app = variable_item_get_context(item);
  233. uint8_t index = variable_item_get_current_value_index(item);
  234. variable_item_set_current_value_text(
  235. item, volume_names[index]); // Display the selected volume as text
  236. MyModel* model = view_get_model(app->view_flip_the_world);
  237. model->volume_index = index;
  238. }
  239. // Declare local buffers for text display
  240. char frequency_display[64];
  241. char station_display[256];
  242. char signal_display[64];
  243. char volume_display[32];
  244. // Callback for drawing the view
  245. void my_app_view_draw_callback(Canvas* canvas, void* model) {
  246. (void)model;
  247. // Draw strings on the canvas
  248. canvas_set_font(canvas, FontPrimary);
  249. canvas_draw_str(canvas, 33, 10, "FM Radio");
  250. canvas_draw_icon(canvas,83, 0, &I_RadioSmall);
  251. // Draw button prompts
  252. canvas_set_font(canvas, FontSecondary);
  253. elements_button_top_left(canvas, "Pre"); elements_button_top_right(canvas, "Pre");
  254. elements_button_left(canvas, "Scan-"); elements_button_center(canvas, "Mute"); elements_button_right(canvas, "Scan+");
  255. struct RADIO_INFO info; // Create a struct to hold the radio info
  256. uint8_t buffer[5]; // Create a buffer to hold the TEA5767 register values
  257. if(tea5767_get_radio_info(buffer, &info)) {
  258. snprintf(frequency_display, sizeof(frequency_display), "Frequency: %.1f MHz", (double)info.frequency);
  259. float tolerance = 0.05f;
  260. float diff = stations[current_station_index].frequency - info.frequency;
  261. if(fabsf(diff) < tolerance) {snprintf(station_display, sizeof(station_display), "%s", stations[current_station_index].name);}
  262. else {station_display[0] = '\0';}
  263. snprintf(volume_display, sizeof(volume_display), "Status: %s %s", info.muted ? "Playing" : "Muted", info.stereo ? "(Mono)" : "(Stereo)");
  264. snprintf(signal_display, sizeof(signal_display), "Signal: %d (%s)", info.signalLevel, info.signalQuality);
  265. } else {
  266. // Display error message if TEA5767 is not detected
  267. snprintf(frequency_display, sizeof(frequency_display), "TEA5767 Not Detected");
  268. snprintf(signal_display, sizeof(signal_display), "Pin 15 = SDA | Pin 16 = SLC");
  269. // Reset frequency_display and volume_display to blank
  270. station_display[0] = '\0';
  271. volume_display[0] = '\0';
  272. }
  273. // Display the stored values on the 5th loop
  274. canvas_set_font(canvas, FontPrimary);
  275. canvas_draw_str(canvas, 10, 20, station_display);
  276. canvas_set_font(canvas, FontSecondary);
  277. canvas_draw_str(canvas, 10, 30, frequency_display);
  278. canvas_draw_str(canvas, 10, 40, volume_display);
  279. canvas_draw_str(canvas, 10, 49, signal_display);
  280. }
  281. // Allocate memory for the application
  282. MyApp* my_app_alloc() {
  283. MyApp* app = (MyApp*)malloc(sizeof(MyApp));
  284. Gui* gui = furi_record_open(RECORD_GUI);
  285. // Initialize the view dispatcher
  286. app->view_dispatcher = view_dispatcher_alloc();
  287. view_dispatcher_enable_queue(app->view_dispatcher);
  288. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  289. // Initialize the submenu
  290. app->submenu = submenu_alloc();
  291. submenu_add_item(app->submenu, "Listen Now", MyAppSubmenuIndexFlipTheWorld, my_app_submenu_callback, app);
  292. //submenu_add_item(app->submenu, "Config", MyAppSubmenuIndexConfigure, my_app_submenu_callback, app);
  293. submenu_add_item(app->submenu, "About", MyAppSubmenuIndexAbout, my_app_submenu_callback, app);
  294. view_set_previous_callback(submenu_get_view(app->submenu), my_app_navigation_exit_callback);
  295. view_dispatcher_add_view(app->view_dispatcher, MyAppViewSubmenu, submenu_get_view(app->submenu));
  296. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewSubmenu);
  297. // Initialize the variable item list for configuration
  298. app->variable_item_list_config = variable_item_list_alloc();
  299. variable_item_list_reset(app->variable_item_list_config);
  300. // Add frequency configuration
  301. VariableItem* frequency_item = variable_item_list_add(app->variable_item_list_config, "Freq (MHz)", NUM_STATIONS, my_app_frequency_change, app);
  302. uint32_t current_station_index = 0;
  303. variable_item_set_current_value_index(frequency_item, current_station_index);
  304. // Add volume configuration
  305. VariableItem* volume_item = variable_item_list_add(app->variable_item_list_config, "Volume", COUNT_OF(volume_values), my_app_volume_change, app);
  306. uint8_t volume_index = 0;
  307. variable_item_set_current_value_index(volume_item, volume_index);
  308. view_set_previous_callback(variable_item_list_get_view(app->variable_item_list_config),my_app_navigation_submenu_callback);
  309. view_dispatcher_add_view(app->view_dispatcher, MyAppViewConfigure, variable_item_list_get_view(app->variable_item_list_config));
  310. // Initialize the view for flipping the world
  311. app->view_flip_the_world = view_alloc();
  312. view_set_draw_callback(app->view_flip_the_world, my_app_view_draw_callback);
  313. view_set_input_callback(app->view_flip_the_world, my_app_view_input_callback);
  314. view_set_previous_callback(app->view_flip_the_world, my_app_navigation_submenu_callback);
  315. view_allocate_model(app->view_flip_the_world, ViewModelTypeLockFree, sizeof(MyModel));
  316. MyModel* model = view_get_model(app->view_flip_the_world);
  317. model->current_station_index = current_station_index;
  318. model->volume_index = volume_index;
  319. view_dispatcher_add_view(app->view_dispatcher, MyAppViewFlipTheWorld, app->view_flip_the_world);
  320. // Initialize the widget for displaying information about the app
  321. app->widget_about = widget_alloc();
  322. widget_add_text_scroll_element(app->widget_about, 0, 0, 128, 64,
  323. "FM Radio (v1.1)\n"
  324. "---\n"
  325. "Created By Coolshrimp\n\n"
  326. "Up = Preset Up\n"
  327. "Down = Preset Down\n"
  328. "Left = Seek Down\n"
  329. "Right = Seek Up\n"
  330. "OK = Toggle Mute");
  331. view_set_previous_callback(widget_get_view(app->widget_about), my_app_navigation_submenu_callback);
  332. view_dispatcher_add_view(app->view_dispatcher, MyAppViewAbout, widget_get_view(app->widget_about));
  333. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  334. #ifdef BACKLIGHT_ALWAYS_ON
  335. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  336. #endif
  337. return app;
  338. }
  339. // Free memory used by the application
  340. void my_app_free(MyApp* app) {
  341. #ifdef BACKLIGHT_ALWAYS_ON
  342. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  343. #endif
  344. furi_record_close(RECORD_NOTIFICATION);
  345. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewAbout);
  346. widget_free(app->widget_about);
  347. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewFlipTheWorld);
  348. view_free(app->view_flip_the_world);
  349. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewConfigure);
  350. variable_item_list_free(app->variable_item_list_config);
  351. view_dispatcher_remove_view(app->view_dispatcher, MyAppViewSubmenu);
  352. submenu_free(app->submenu);
  353. view_dispatcher_free(app->view_dispatcher);
  354. furi_record_close(RECORD_GUI);
  355. free(app);
  356. }
  357. // Main function to start the application
  358. int32_t my_fm_radio(void* p) {
  359. UNUSED(p);
  360. MyApp* app = my_app_alloc();
  361. view_dispatcher_run(app->view_dispatcher);
  362. my_app_free(app);
  363. return 0;
  364. }