RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    Wireless Battery Indicator (PS3)

    Scheduled Pinned Locked Moved Ideas and Development
    batterywirelessps3
    22 Posts 11 Posters 9.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Heyoeyo
      last edited by Heyoeyo

      Though it's far from ideal, I've set up a simple battery indicator script which can be added to the RetroPie settings menu in ES. I couldn't find anything similar after a bit of searching, so sorry if this has already been done!
      It only works for PS3 controllers, but for anyone interested, here's the code:

      #!/bin/bash
      
      # Specify sixad log file path
      logPath='/var/log/sixad'
      
      # Create array of controller position names
      ctrlPosition=('First' 'Second' 'Third' 'Fourth')
      ctrlIndex=0
      
      # Create array to store battery levels
      batLevels=('dead...' 'very 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 PS3 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
      }
      

      To use it, copy the code above into a file and save it (e.g. batterycheck.sh) then place it in:
      /home/pi/RetroPie/retropiemenu/
      Restart ES and it should automagically appear in the RetroPie settings menu. If you want to get fancy and add a menu icon and/or description, you can edit the following file:
      /opt/retropie/configs/all/emulationstation/gamelists/retropie/gamelist.xml

      How it works:
      There's a log file (located at /var/log/sixad) that contains information about interfacing with PS3 controllers. This log file contains a little message about the controller battery level whenever a controller is first connected. The script above just searches for the most recent battery messages, checks which controllers are still connected (using PIDs) and then lists the battery value as a percentage in a message box.

      Problems:

      • It only reports the battery level when the controller was first connected, not the current level
      • It only works for PS3 controllers (that's all I have for testing), though a similar approach would be possible for controllers that have similar behaviour and log files (or using the syslog file)
      • It's very fragile/inelegant! If the formatting of the log file is ever changed, it could break the script
      • (nitpicky) It reports the battery levels based on the order the controllers were connected, not by the actual player order (i.e. it may not match the controller LEDs if other controller types are connected)
      • (nitpicky) It's only accessible from the RetroPie settings menu. It would be nice to have a little pop-up image to show the battery level when it connects (as with the actual PS3/PS4 UI). It may be easy-ish to do with python, but I can't figure out how to draw things over top of ES...

      I'm fairly new to Linux/RetroPie, so figuring out where the useful/important files are is a bit of a struggle. I'd appreciate any advice/guidance about how to access the battery state more directly, if possible. I'm also open to suggestions for how to clean up the code.
      Thanks for reading this far!

      EDIT:
      Here's some screenshots of how it looks:
      0_1505938385813_menuExample.png

      0_1505938395790_dialogExample.png

      1 Reply Last reply Reply Quote 4
      • C
        c0nsole-guy
        last edited by

        Very cool idea! I'll give it a go tonight with my older PS3 controller. Will share feedback.

        1 Reply Last reply Reply Quote 0
        • H
          Heyoeyo
          last edited by

          Great, thanks!

          I should probably note that this script relies on sixad handling the connection to the controller. So if another connection method is used (as in the recent 'DualShock 3/4 testers' thread for example) the script would probably have to be modified.

          1 Reply Last reply Reply Quote 1
          • herb_fargusH
            herb_fargus administrators
            last edited by

            Cool. My battery indicator is: hey why doesn't my controller work anymore? Oh. Batteries.

            If you read the documentation it will answer 99% of your questions: https://retropie.org.uk/docs/

            Also if you want a solution to your problems read this first: https://retropie.org.uk/forum/topic/3/read-this-first

            1 Reply Last reply Reply Quote 2
            • H
              Heyoeyo
              last edited by

              Haha, yep I've had the same experience!
              I'd like to add a warning for low batteries if I can find a way to check them after the controller has already connected. I think it's possible with a (clumsy) hack to one of the sixad files, but hopefully I can find a simpler way to do it.

              1 Reply Last reply Reply Quote 0
              • H
                Heyoeyo
                last edited by Heyoeyo

                Quick update:

                So far, I haven't been able to find a nice way of obtaining battery level information after the controller initially connects. However, I did manage to find a way to display a pop-up indicator image (over top of emulation station) when the controller first connects, thanks to AndrewFromMelbourne's raspidmx library. I think this is probably preferable to the system I had before, even without the up-to-date battery level information. Here's what it looks like (on top of the minimal theme):

                0_1506814792759_batteryExample_lowRes.png

                When a controller is connected, the icon slides in from the left, pauses for a moment and then slides out (example). The timing, positioning and icon graphics can all be changed. It works in more or less the same way as the previous script (checking the sixad log).

                Getting this running is a bit more involved than the menu script from my previous post. If anyone is interested, I've set up a github page with instructions for 'installing' the required code:
                https://github.com/heyoeyo/batteryCheck

                An additional thank you goes to this thread discussing the layering issue. That problem was driving me insane haha.

                Edit: Added link to example of the display animation

                1 Reply Last reply Reply Quote 2
                • P
                  psyke83 Global Moderator
                  last edited by

                  The next kernel firmware will have support for rumble & battery information in the sony-hid driver (which will be used if you don't install the ps3controller driver).

                  You will be able to query via:
                  /sys/class/power_supply/sony_controller_battery_xx:xx:xx:xx:xx:xx/capacity

                  Unfortunately, the native driver has one big downside in that it ignores the bluetooth IdleTimeout setting. If you leave your Pi on 24/7 and forget to manually turn off the controller, it will stay paired indefinitely and drain battery quite quickly.

                  1 Reply Last reply Reply Quote 0
                  • P
                    psyke83 Global Moderator
                    last edited by psyke83

                    Here's a very rough way to read the current battery status when ps3controller is used. Feel free to clean it up and incorporate it in your stuff:

                    #!/bin/bash
                    
                    function read_sixaxis_battery () {
                    while read -r line; do
                      if [[ "$line" == *"L2CAP"* ]]; then
                         read -r line
                         read -r line
                         echo "$line" | awk '{ print $12 }'
                         return
                      fi
                    done
                    }
                    
                    hcidump -x | read_sixaxis_battery &
                    sleep 5
                    pkill hcidump
                    

                    You need to install bluez-hcidump and run the script with root privileges.

                    If you want to incorporate it into python, here's what you're looking for from the output:

                    hcidump -x
                    > ACL data: handle 11 flags 0x02 dlen 54
                        L2CAP(d): cid 0x0041 len 50 [psm 0]
                          A1 01 00 00 00 00 00 87 82 7E 7D 00 00 00 00 00 00 00 00 00
                          00 00 00 00 00 00 00 00 00 00 03 05 16 00 00 00 00 33 FD 77
                          01 C0 02 09 01 F1 01 93 00 02
                    

                    The battery value is the 31st in the array, shown as 05 in the above log.

                    1 Reply Last reply Reply Quote 1
                    • H
                      Heyoeyo
                      last edited by

                      Awesome, thanks!

                      I'll give this a try and let you know how it goes!

                      1 Reply Last reply Reply Quote 0
                      • Z
                        zerojay
                        last edited by

                        Anyone have this functional at all right now?

                        H 1 Reply Last reply Reply Quote 0
                        • retroprogrammerR
                          retroprogrammer
                          last edited by

                          can we incorporate it into other controllers

                          Here is me converting the original Xbox for a case:
                          https://retropie.org.uk/forum/topic/15232/converting-original-xbox-into-case-for-raspberrypi/15
                          Check out my GitHub: https://github.com/retro-programmer

                          1 Reply Last reply Reply Quote 0
                          • E
                            ezra84
                            last edited by

                            Very cool feature!

                            Would be fantastic if the indicator could function similarly as the overheating or power shortage icons on the top right of the screen.

                            1 Reply Last reply Reply Quote 0
                            • H
                              Heyoeyo @zerojay
                              last edited by Heyoeyo

                              @zerojay said in Wireless Battery Indicator (PS3):

                              Anyone have this functional at all right now?

                              I haven't had a chance to use the newer hcidump method since I gifted my pi to a friend a while back. Now I have another one, so I might give it a shot this weekend!

                              @retroprogrammer said in Wireless Battery Indicator (PS3):

                              can we incorporate it into other controllers

                              Maybe, depending on how the other controllers work/connect to the pi. I get the feeling it's not a standardised system, so it probably needs to be tailored to each controller type (though once each implementation is figured out, a single script could check for all known types).

                              @ezra84 said in Wireless Battery Indicator (PS3):

                              Very cool feature!

                              Would be fantastic if the indicator could function similarly as the overheating or power shortage icons on the top right of the screen.

                              Do you mean a similar look/graphics of the icons, or similar in the sense of having a (low battery) warning?

                              E 1 Reply Last reply Reply Quote 0
                              • Z
                                zerojay
                                last edited by

                                @Heyoeyo Let me know if you need anyone to help with testing.

                                1 Reply Last reply Reply Quote 0
                                • E
                                  ezra84 @Heyoeyo
                                  last edited by

                                  @heyoeyo I mean in the sense of having a low battery warning. That should be more important than the looks, but of course it would be nice if it can be implemented in the same style as the other warning icons.

                                  1 Reply Last reply Reply Quote 0
                                  • L
                                    Lore88
                                    last edited by

                                    Hello,
                                    i tried to install this https://github.com/heyoeyo/batteryCheck
                                    on my Raspberry Pi3 running Retropie v4.3 but once i try to run batteryCheck.py i obtain the following error

                                    Traceback (most recent call last):
                                    File "batCheck.py", line 98, in <module>
                                    time_stamp = os.stat(logPath).st_mtime;
                                    OSError: [Errno 2] No such file or directory: '/var/log/sixad'

                                    infact /var/log/sixad does not exists

                                    S 1 Reply Last reply Reply Quote 0
                                    • E
                                      eviltrooper
                                      last edited by

                                      dont work with latest retropie Version :(

                                      1 Reply Last reply Reply Quote 0
                                      • madmodder123M
                                        madmodder123
                                        last edited by

                                        If someone wants to get real fancy they could use pngview to make something like this : https://github.com/d-rez/gbz_overlay

                                        1 Reply Last reply Reply Quote 1
                                        • S
                                          silverbullet @Lore88
                                          last edited by

                                          @Heyoeyo do you know if the BatteryCheck script can be updated to work on the newer release of RetroPie?

                                          madmodder123M 1 Reply Last reply Reply Quote 0
                                          • madmodder123M
                                            madmodder123 @silverbullet
                                            last edited by

                                            @silverbullet
                                            You would have to check the log files for whatever PS3 driver is running by default and then possible edit the lines of code it is searching for.
                                            /var/log/sixad is the old one for the sixad driver, not sure where the other drivers logs are at.

                                            S 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            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.