battery_test_app.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "battery_test_app.h"
  2. #include <notification/notification_messages.h>
  3. void battery_test_dialog_callback(DialogExResult result, void* context) {
  4. furi_assert(context);
  5. BatteryTestApp* app = context;
  6. if(result == DialogExResultLeft) {
  7. view_dispatcher_stop(app->view_dispatcher);
  8. } else if(result == DialogExResultRight) {
  9. view_dispatcher_switch_to_view(app->view_dispatcher, BatteryTestAppViewBatteryInfo);
  10. }
  11. }
  12. uint32_t battery_test_exit_confirm_view() {
  13. return BatteryTestAppViewExitDialog;
  14. }
  15. static void battery_test_battery_info_update_model(void* context) {
  16. BatteryTestApp* app = context;
  17. power_get_info(app->power, &app->info);
  18. BatteryInfoModel battery_info_data = {
  19. .vbus_voltage = app->info.voltage_vbus,
  20. .gauge_voltage = app->info.voltage_gauge,
  21. .gauge_current = app->info.current_gauge,
  22. .gauge_temperature = app->info.temperature_gauge,
  23. .charge = app->info.charge,
  24. .health = app->info.health,
  25. };
  26. battery_info_set_data(app->battery_info, &battery_info_data);
  27. notification_message(app->notifications, &sequence_display_backlight_on);
  28. }
  29. BatteryTestApp* battery_test_alloc() {
  30. BatteryTestApp* app = malloc(sizeof(BatteryTestApp));
  31. // Records
  32. app->gui = furi_record_open(RECORD_GUI);
  33. app->power = furi_record_open(RECORD_POWER);
  34. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  35. // View dispatcher
  36. app->view_dispatcher = view_dispatcher_alloc();
  37. view_dispatcher_enable_queue(app->view_dispatcher);
  38. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  39. view_dispatcher_set_tick_event_callback(
  40. app->view_dispatcher, battery_test_battery_info_update_model, 500);
  41. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  42. // Views
  43. app->battery_info = battery_info_alloc();
  44. view_set_previous_callback(
  45. battery_info_get_view(app->battery_info), battery_test_exit_confirm_view);
  46. view_dispatcher_add_view(
  47. app->view_dispatcher,
  48. BatteryTestAppViewBatteryInfo,
  49. battery_info_get_view(app->battery_info));
  50. app->dialog = dialog_ex_alloc();
  51. dialog_ex_set_header(app->dialog, "Close Battery Test?", 64, 12, AlignCenter, AlignTop);
  52. dialog_ex_set_left_button_text(app->dialog, "Exit");
  53. dialog_ex_set_right_button_text(app->dialog, "Stay");
  54. dialog_ex_set_result_callback(app->dialog, battery_test_dialog_callback);
  55. dialog_ex_set_context(app->dialog, app);
  56. view_dispatcher_add_view(
  57. app->view_dispatcher, BatteryTestAppViewExitDialog, dialog_ex_get_view(app->dialog));
  58. battery_test_battery_info_update_model(app);
  59. view_dispatcher_switch_to_view(app->view_dispatcher, BatteryTestAppViewBatteryInfo);
  60. return app;
  61. }
  62. void battery_test_free(BatteryTestApp* app) {
  63. furi_assert(app);
  64. // Views
  65. view_dispatcher_remove_view(app->view_dispatcher, BatteryTestAppViewBatteryInfo);
  66. battery_info_free(app->battery_info);
  67. view_dispatcher_remove_view(app->view_dispatcher, BatteryTestAppViewExitDialog);
  68. dialog_ex_free(app->dialog);
  69. // View dispatcher
  70. view_dispatcher_free(app->view_dispatcher);
  71. // Records
  72. furi_record_close(RECORD_POWER);
  73. furi_record_close(RECORD_GUI);
  74. furi_record_close(RECORD_NOTIFICATION);
  75. free(app);
  76. }
  77. int32_t battery_test_app(void* p) {
  78. UNUSED(p);
  79. BatteryTestApp* app = battery_test_alloc();
  80. // Disable battery low level notification
  81. power_enable_low_battery_level_notification(app->power, false);
  82. view_dispatcher_run(app->view_dispatcher);
  83. power_enable_low_battery_level_notification(app->power, true);
  84. battery_test_free(app);
  85. return 0;
  86. }