UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Guide to charge limiting your Fairphone 5

    Scheduled Pinned Locked Moved Fairphone 5
    4 Posts 2 Posters 60 Views 2 Watching
    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.
      • C Offline
        ChromiumOS-Guy
        last edited by ChromiumOS-Guy

        I AM NOT RESPONSIBLE FOR ANY DAMAGE WHATSOEVER TO THE EXTENT LAW PERMITS.

        EDIT: there is a edge case that lets the battery continue charging it seems easy to fix so I'll do so tomorrow, for now just use slow chargers. (or make sure to unplug/replug the phone after it stops charging from there it will work fine)

        I'll start by saying I have no idea if this works for Focal but I haven't tried so it may work.
        also this is a proof of concept you may want to refine this by using a udev rule or whatever else this is just how I got it working the first time so its a known good.

        essentially there is something called user_fcc my take is that the fcc part is "full charge capacity" you can essentially tell the device that it actually has a smaller/bigger battery with this, I did tests on this however and either its using different units then charge_full (which is how you get the battery capacity) or its something else entirely, the point is that if set to 0 battery stops charging if set to big number like 10^6 battery charges again.

        so... heres my setup I did for the proof of concept.

        first start by running:

        sudo mount -o remount,rw /
        

        this is to remount read-only root as read/write.

        make a file and name it charge-control.sh and put it in the /usr/bin directory ( /usr/bin/charge-control.sh)
        change THRESHOLD to whatever percent you want to stop charging at, I put it on 80%

        #!/usr/bin/env bash
        
        # Path to capacity file (adjust if needed)
        CAP_FILE="/sys/class/power_supply/battery/capacity"
        UFCC_FILE="/sys/class/power_supply/battery/user_fcc"
        FULL_CAP_FILE="/sys/class/power_supply/battery/charge_full"
        
        # Threshold to compare against (integer 0-100)
        THRESHOLD=80
        
        # Commands to run
        ON_GE="echo 0 > ${UFCC_FILE}" #echo Battery >= threshold: do X here
        ON_LT="echo `cat ${FULL_CAP_FILE}` > ${UFCC_FILE}" #echo Battery < threshold: do Y here
        
        # Trim whitespace and ensure integer
        capacity=$(tr -d ' \t\n\r' < "$CAP_FILE")
        if ! [[ $capacity =~ ^[0-9]+$ ]]; then
          echo "Error: capacity value is not an integer: '$capacity'" >&2
          exit 3
        fi
        
        # Compare as integers
        if (( capacity >= THRESHOLD )); then
          eval "$ON_GE"
        else
          eval "$ON_LT"
        fi
        
        exit 0
        

        make sure you set executable permissions sudo chmod +x /usr/bin/charge-control.sh

        now we make a system service to start this script, make a file called charge-control.service at /etc/systemd/system (/etc/systemd/system/charge-control.service)
        and put this into it

        [Unit]
        Description=Battery charge control script
        
        [Service]
        Type=oneshot
        ExecStart=/usr/bin/charge_control.sh
        User=root
        

        now we create a timer to run this every minute because we want it to enable/disable battery charging without us running the service manually.
        so create a file named charge-control.timer in the same directory (/etc/systemd/system/charge-control.timer)
        and put this into it

        [Unit]
        Description=Run charge_control every 1 minute
        
        [Timer]
        OnBootSec=1min
        OnUnitActiveSec=1min
        Persistent=false
        
        [Install]
        WantedBy=timers.target
        

        then enable the timer by running these commands:

        sudo systemctl daemon-reload
        sudo systemctl enable --now charge_control.timer
        

        you may also check that it is working with these commands if you want

        sudo systemctl status charge_control.timer
        sudo systemctl list-timers --all | grep charge_control
        sudo journalctl -u charge_control.service
        

        hope this helps y'all manage you're battery health better.

        G 1 Reply Last reply Reply Quote 0
        • G Online
          gpatel-fr @ChromiumOS-Guy
          last edited by

          @ChromiumOS-Guy

          thanks, may I ask why you are remounting the root fs r/w ? It's possible to create a systemd unit without it.

          C 1 Reply Last reply Reply Quote 0
          • C Offline
            ChromiumOS-Guy @gpatel-fr
            last edited by

            @gpatel-fr don't want the bash script in the user directory.
            this is because I mess with the user directory a lot, I didn't change it for the guide because this is a proof of concept and if I have a known good I will use it.

            I opened an Issue on this for indicator-upower fork, paulcarroty (the developer) is aware.
            and I am mostly counting on him to make this into something useful because he already has an app for it.

            G 1 Reply Last reply Reply Quote 0
            • G Online
              gpatel-fr @ChromiumOS-Guy
              last edited by

              @ChromiumOS-Guy

              I have looked at the kernel sources, the 'user_fcc' is a limit value for the charging current (in micro amperes). If you set it at 0, well, current_now goes to 0 (logical) and the battery charging indicator disappears. If you set it to infinity, well, the result is unclear to me. It would seem logical that the maximum amount that can be tolerated by the battery is set if user_fcc is higher.
              Since the user_fcc value is aimed at setting an artificial low value when people are connecting a high powered charged (fast charger), I'd be wary of setting a very high value myself. It could work or maybe have unfortunate consequences. Setting it to 0 seems safe though.

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