Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

[IDEA AND START OF DEVELOPMENT]Battery Check For All Controllers



  • Hey guys, I am trying to make a script which basically tells you how much battery is left of your wireless controller, Because I don't have all controllers I am going to need some help.
    I got this idea off this post: https://retropie.org.uk/forum/topic/12945/wireless-battery-indicator-ps3
    if any one does have some screen shots of how sixad log file path: /var/log/sixad
    looks like please post :)

    But instead of getting the battery level from the sixad log file path: /var/log/sixad
    we get it from: /sys/class/power_supply/what_ever_controller_ac:fd:21:d1:7c:24/capacity

    Here is an example script:

    #!/bin/bash
    
    # Specify log file path
    logPath='sys/class/power_supply/what_ever_controller_ac:fd:21:d1:7c:24/capacity'
    
    # Create array of controller position names
    ctrlPosition=('First' 'Second' 'Third' 'Fourth')
    ctrlIndex=0
    
    # Create array to store battery levels
    batLevels=('dead' 'low' '25%' '50%' '75%' '100%')
    
    # Create a container to store message text
    dialogTxt=$""
    
    
    # Extract and loop through the most recent lines of the log file which contain the word 'Battery'
    tac $logPath | grep -m 1 -B1000 "Watching" | tac | grep "Battery" | 
    {
    while read -r line ; do
    
    	# Print out raw info (for debugging)
    	#echo "$line"
    
    	# Extract PID values from battery print-outs
    	chkPID=$(echo "$line" | grep -oP '(?<=[).*(?=]:)')
    
    	# Check if PID is still running. If so, get the battery value
    	if ps -p "$chkPID" > /dev/null ; then
    		# Extract battery value (0-5)
    		batIndex=$(echo "$line" | grep -oP '(?<=Battery 0).*(?=])')
    
    		# Append battery level text to any existing text
    		dialogTxt+=$"${ctrlPosition[ctrlIndex]} controller battery level is at ${batLevels[batIndex]}\n\n"
    
    		# Increment the controller index, in case there are multiple controller PIDs
    		((ctrlIndex++))
    	fi	
    	
    done
    
    
    # Check to see if no controllers were detected (ctrlIndex wasn't incremented above)
    if [[ "$ctrlIndex" -eq "0" ]] ; then
    	dialogTxt=$"No wireless controllers detected!"
    fi
    
    # Create message box containing battery info
    dialog --no-mouse --ok-label "OK" --backtitle "Battery Check" --msgbox "$dialogTxt" 12 60 2>&1 > /dev/tty
    }
    

    I practically copyed and pasted the https://retropie.org.uk/forum/topic/12945/wireless-battery-indicator-ps3 code that was there and made a few adjustments I didn't do
    to much[changed a few word ;)] as I am not the best with this type of stuff but will try my best!



  • the example does not work, I just put it their as well a 'example' for us to base of :)



  • Any one have any ideas?



  • @retroprogrammer
    Your suggestion does not work in general. The powerlevel-feature via /sys/class is a design in the joypad firmware and can change from company to company :(

    Maybe the hcidump-method introduced by @psyke83 is a more valueable way. If you want you can make a script collection to read out known controllers.



  • @cyperghost yeah, I will try to do that



Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.