radio.c 15 KB

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