š Laptop Battery Checker by Nick #SummerSideProjects #1 #2021
Welcome to Nickās first #SummerSideProject, today we have the highly anticipated background macbook laptop battery checker in 55 lines of code š. In the future, we hope to see more come to life š , or for him to bring back some of his old ones. š
Whatās the problem?
My macbook is often in the āchargingā state but is actually not charging,
and sometimes I just forget to plug in my charger š¤¦. This causes my laptop to run out of battery while Iām playing video games such as League of Legends or Steam games š. Unfortunately this has happened multiple timesā¦
How do we solve this?
Of course, the answer is letās code something to solve this. So weāve created a script thatās always monitoring the battery level since we definitely canāt trust the charging symbol in the macās user interface. Weāll make my laptop say ābattery is drainingā if the battery is not increasing, and ācharger is not plugged inā if I didnāt even plug in my charger.
After coding this up, we just need to run one command to make sure that our battery is always being checked when we are gaming:
nohup ./batteryd.py -a Steam|LeagueClient >/dev/null 2>&1 &
Whereās your code?
We had to google to figure out how to:
- Get the battery stats from the computer, and determine if the battery is charging or not,
- Determine if an application is running or not because I only want to this script to check the battery if Iām gaming,
- Get the laptop to talk to us.
Code is at https://github.com/nickwu241/battery-checker or see the gist below:
Whatās next?
Next up on #SummerSideProjects is a surprise so subscribe to Nick Codes on YouTube or follow nickwu241 on Github to be the first to the fun š #newyearnewnick