Skip to content

Instantly share code, notes, and snippets.

@ItsVixano
Last active August 15, 2023 00:46
Show Gist options
  • Save ItsVixano/77358f86567c954c14f552b716c73a6f to your computer and use it in GitHub Desktop.
Save ItsVixano/77358f86567c954c14f552b716c73a6f to your computer and use it in GitHub Desktop.
Battery Charging Checker
#! /vendor/bin/sh
# Copyright (C) 2023 Giovanni Ricca
# SPDX-License-Identifier: GPL-3.0-or-later
# vars
BATTERY_LEVEL_NODE="/sys/class/power_supply/battery/capacity"
TIME_TO_FULL_NODE="/sys/class/power_supply/battery/time_to_full_now"
POWER_NOW_NODE="/sys/class/power_supply/battery/power_now"
CURRENT_NOW_NODE="/sys/class/power_supply/battery/current_now"
OUTPUT_FILE="battery_chart.txt"
# functions
battery_data() {
local unix_timestamp=$(date +%s)
local battery_level=$(cat "$BATTERY_LEVEL_NODE")
local time_to_full=$(cat "$TIME_TO_FULL_NODE")
local power_now=$(cat "$POWER_NOW_NODE")
local current_now=$(cat "$CURRENT_NOW_NODE")
echo "$unix_timestamp | $battery_level | $time_to_full | $power_now | $current_now"
}
# Clear the output file
echo -e "# Timestamp | Battery Level | Time to Full | Power Now | Current Now\n" > "$OUTPUT_FILE"
while true; do
battery_data >> "$OUTPUT_FILE"
sleep 5
if [ "$(cat $TIME_TO_FULL_NODE)" -eq 0 ]; then
# Log battery stats for the last time
battery_data >> "$OUTPUT_FILE"
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment