44
55#include < zephyr/sys/poweroff.h>
66#include < zephyr/sys/reboot.h>
7+ #include < zephyr/shell/shell.h>
78
89#include < zephyr/pm/pm.h>
910#include < zephyr/pm/state.h>
@@ -558,4 +559,71 @@ void PowerManager::charge_task() {
558559 last_charging_state = charging_state;
559560}
560561
562+ int cmd_setup_fuel_gauge (const struct shell *shell, size_t argc, const char **argv) {
563+ ARG_UNUSED (argc);
564+ ARG_UNUSED (argv);
565+
566+ fuel_gauge.setup (power_manager._battery_settings );
567+
568+ power_manager.reboot ();
569+
570+ return 0 ;
571+ }
572+
573+ static int cmd_battery_info (const struct shell *shell, size_t argc, const char **argv) {
574+ ARG_UNUSED (argc);
575+ ARG_UNUSED (argv);
576+
577+ shell_print (shell, " ------------------ Battery Info ------------------" );
578+ // Battery fuel gauge status
579+ bat_status status = fuel_gauge.battery_status ();
580+ shell_print (shell, " Battery Status:" );
581+ shell_print (shell, " Present: %i, Full Charge: %i, Full Discharge: %i" ,
582+ status.BATTPRES , status.FC , status.FD );
583+
584+ // Basic measurements
585+ shell_print (shell, " Basic Measurements:" );
586+ shell_print (shell, " Voltage: %.3f V" , fuel_gauge.voltage ());
587+ shell_print (shell, " Temperature: %.1f °C" , fuel_gauge.temperature ());
588+ shell_print (shell, " Current: %.1f mA (avg: %.1f mA)" ,
589+ fuel_gauge.current (), fuel_gauge.average_current ());
590+ shell_print (shell, " State of Charge: %.1f%%" , fuel_gauge.state_of_charge ());
591+
592+ // Capacity info
593+ shell_print (shell, " Capacity Information:" );
594+ shell_print (shell, " Design Capacity: %.1f mAh" , fuel_gauge.design_cap ());
595+ shell_print (shell, " Full Charge Capacity: %.1f mAh" , fuel_gauge.capacity ());
596+ shell_print (shell, " Remaining Capacity: %.1f mAh" , fuel_gauge.remaining_cap ());
597+
598+ // Time estimates
599+ float ttf = fuel_gauge.time_to_full ();
600+ float tte = fuel_gauge.time_to_empty ();
601+ shell_print (shell, " Time Estimates:" );
602+ shell_print (shell, " Time to Full: %ih %02dmin" , (int )ttf / 60 , (int )ttf % 60 );
603+ shell_print (shell, " Time to Empty: %ih %02dmin" , (int )tte / 60 , (int )tte % 60 );
604+
605+ // Battery controller status
606+ battery_controller.exit_high_impedance ();
607+
608+ shell_print (shell, " Charging Information:" );
609+ uint16_t charging_state = battery_controller.read_charging_state () >> 6 ;
610+ shell_print (shell, " Charging State: %i" , charging_state);
611+ shell_print (shell, " Power Good: %i" , battery_controller.power_connected ());
612+
613+ struct chrg_state charge_ctrl = battery_controller.read_charging_control ();
614+ shell_print (shell, " Charge Control: enabled=%i, current=%.1f mA" ,
615+ charge_ctrl.enabled , charge_ctrl.mAh );
616+
617+ battery_controller.enter_high_impedance ();
618+
619+ return 0 ;
620+ }
621+
622+ SHELL_STATIC_SUBCMD_SET_CREATE (battery_cmd,
623+ SHELL_COND_CMD (CONFIG_SHELL, info, NULL , " Print battery info" , cmd_battery_info),
624+ SHELL_COND_CMD(CONFIG_SHELL, setup, NULL , " Setup fuel gauge" , cmd_setup_fuel_gauge),
625+ SHELL_SUBCMD_SET_END);
626+
627+ SHELL_CMD_REGISTER (power_manager, &battery_cmd, " Power Manager Commands" , NULL );
628+
561629PowerManager power_manager;
0 commit comments