# Pixelbook Charge Control

## Goal
This is a small project I hacked together to preserve the battery of a
Pixelbook running vanilla Linux (in my case Debian).

What I want to achieve is that the battery is not constantly being charged
to and held at 100%, which will quickly destroy the battery.

The Pixelbook Linux runtime contains a tool called "ectool" which can
control the charge control code embedded in the EC. Newer EC version also
allow for a "battery sustainer" pretty much implementing what I want but the
Pixelbook (aka Eve) EC is too old to use this.

So I came up with a bash script that can run in teh background.

For it to work you need the "ectool" in Linux. I tried to compile the ectool
from source under Debian but failed. So I took the alternative path and used
teh ChromeOS binary. But this will also not run diretcly under normal Linux
due to libc version conflicts. So I copied all necessary files one by one
and created a subdir that one can chroot into to execute the ectool:

```
# tar tvf pixelbook-bin.tar.bz2 
drwxr-xr-x root/root         0 2022-11-14 06:19 pixelbook-bin/
drwxr-xr-x root/root         0 2022-11-14 06:40 pixelbook-bin/sbin/
-rwxr-xr-x root/root   1035536 2022-11-14 06:34 pixelbook-bin/sbin/ldconfig
-rwxr-xr-x root/root     38408 2022-11-14 06:40 pixelbook-bin/sbin/ec_sb_firmware_update
-rwxr-xr-x root/root    210192 2022-11-14 06:20 pixelbook-bin/sbin/ectool
-rwx------ root/root      5960 2022-11-14 06:40 pixelbook-bin/sbin/ec_battery_wa
-rwxr-xr-x root/root     10536 2022-11-14 06:40 pixelbook-bin/sbin/ec_parse_panicinfo
drwxr-xr-x root/root         0 2022-11-14 06:37 pixelbook-bin/lib64/
-rwxr-xr-x root/root   2060112 2022-11-14 06:19 pixelbook-bin/lib64/libc-2.33.so
-rwxr-xr-x root/root    222152 2022-11-14 06:19 pixelbook-bin/lib64/ld-2.33.so
-rwxr-xr-x root/root    138360 2022-11-14 06:28 pixelbook-bin/lib64/libpthread.so.0
-rwxr-xr-x root/root     42096 2022-11-14 06:27 pixelbook-bin/lib64/libftdi1.so.2
-rwxr-xr-x root/root    222152 2022-11-14 06:19 pixelbook-bin/lib64/ld-linux-x86-64.so.2
-rwxr-xr-x root/root    112456 2022-11-14 06:37 pixelbook-bin/lib64/libudev.so.1
-rwxr-xr-x root/root     82520 2022-11-14 06:36 pixelbook-bin/lib64/libusb-1.0.so.0
-rwxr-xr-x root/root   2060112 2022-11-14 06:19 pixelbook-bin/lib64/libc.so.6
```

This needs to be placed into some known folder. I currently put this in the
root user's home dir. Then I added `~root/bin/` to teh root user's PATH so
that scripts placed in `~root/bin/` will be found.

In `~root/bin/` I first placed a simple script that will chroot exec the
real ectool:

```
#!/bin/sh
chroot /root/pixelbook-bin /sbin/ectool $*
```

Somewhat crude but works.

The second script in `~root/bin/` is the one controlling the charge control
then, currently called 'do_not_charge'. It will by default disable charging
under almost any circumstance, except the battery state of charge (SOC) has
dropped below 30%. Only then it will allow the charger to become or remain
active until SOF is > 80% again. In other words it will stop charging at
80%.

With this I still get more than enough charge in the Pixelbook for all my
tasks but on the other hand prevent teh battery from premature aging. Each
charge cycle means wear to the battery.

Please note that all this of course only works as long as the operating
system is running. Each time power is disconnected and reconnected the EC
reset the charge logic. So if the laptop is in suspend and you disconnect
and reconnect the charger it will again charge to 100% no matter what.

Enjoy!
Feedback welcome.
