subbrute_radio_device_loader.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <furi/core/check.h>
  2. #include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
  3. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  4. #include "subbrute_radio_device_loader.h"
  5. #define TAG "SubBruteRadioDeviceLoader"
  6. static void subbrute_radio_device_loader_power_on() {
  7. uint8_t attempts = 5;
  8. while(--attempts > 0) {
  9. if(furi_hal_power_enable_otg()) {
  10. break;
  11. }
  12. }
  13. if(attempts == 0) {
  14. if(furi_hal_power_get_usb_voltage() < 4.5f) {
  15. FURI_LOG_E(
  16. TAG,
  17. "Error power otg enable. BQ2589 check otg fault = %d",
  18. furi_hal_power_check_otg_fault() ? 1 : 0);
  19. }
  20. }
  21. }
  22. static void subbrute_radio_device_loader_power_off() {
  23. if(furi_hal_power_is_otg_enabled()) {
  24. furi_hal_power_disable_otg();
  25. }
  26. }
  27. bool subbrute_radio_device_loader_is_connect_external(const char* name) {
  28. bool is_connect = false;
  29. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  30. if(!is_otg_enabled) {
  31. subbrute_radio_device_loader_power_on();
  32. }
  33. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  34. if(device) {
  35. is_connect = subghz_devices_is_connect(device);
  36. }
  37. if(!is_otg_enabled) {
  38. subbrute_radio_device_loader_power_off();
  39. }
  40. return is_connect;
  41. }
  42. const SubGhzDevice* subbrute_radio_device_loader_set(
  43. const SubGhzDevice* current_radio_device,
  44. SubGhzRadioDeviceType radio_device_type) {
  45. const SubGhzDevice* radio_device;
  46. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  47. subbrute_radio_device_loader_is_connect_external(SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  48. subbrute_radio_device_loader_power_on();
  49. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  50. subghz_devices_begin(radio_device);
  51. } else if(current_radio_device == NULL) {
  52. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  53. } else {
  54. subbrute_radio_device_loader_end(current_radio_device);
  55. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  56. }
  57. return radio_device;
  58. }
  59. void subbrute_radio_device_loader_end(const SubGhzDevice* radio_device) {
  60. furi_assert(radio_device);
  61. subbrute_radio_device_loader_power_off();
  62. if(radio_device != subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME)) {
  63. subghz_devices_end(radio_device);
  64. }
  65. }