2019-10-02 19:01:07 +02:00
# Based on UPS Lite v1.1 from https://github.com/xenDE
2024-01-20 23:54:32 +01:00
# Made specifically to address the problems caused by the hardware changes in 1.3. Oh yeah I also removed the auto-shutdown feature because it's kind of broken.
2019-10-02 19:01:07 +02:00
#
2024-01-20 23:54:32 +01:00
# To setup, see page six of this manual to see how to enable i2c:
# https://github.com/linshuqin329/UPS-Lite/blob/master/UPS-Lite_V1.3_CW2015/Instructions%20for%20UPS-Lite%20V1.3.pdf
2019-10-02 19:01:07 +02:00
#
2024-01-20 23:54:32 +01:00
# Follow page seven, install the dependencies (python-smbus) and copy this script over for later use:
# https://github.com/linshuqin329/UPS-Lite/blob/master/UPS-Lite_V1.3_CW2015/UPS_Lite_V1.3_CW2015.py
2019-10-02 19:01:07 +02:00
#
2024-01-20 23:54:32 +01:00
# Now, install this plugin by copying this to the 'available-plugins' folder in your pwnagotchi, install and enable the plugin with the commands:
# sudo pwnagotchi plugins install upslite_plugin_1_3
# sudo pwnagotchi plugins enable upslite_plugin_1_3
2020-08-29 14:40:44 +01:00
#
2024-01-20 23:54:32 +01:00
# Now restart raspberry pi. Once back up ensure upslite_plugin_1_3 plugin is turned on in the WebUI. If there is still '0%' on your battery meter
# run the script we saved earlier and ensure that the pwnagotchi is plugged in both at the battery and the raspberry pi. The script should start trying to
# read the battery, and should be successful once there's a USB cable running power to the battery supply.
2019-12-19 19:37:16 +01:00
import logging
2019-10-02 19:01:07 +02:00
import struct
2021-04-18 17:11:04 +02:00
import RPi . GPIO as GPIO
import pwnagotchi
import pwnagotchi . plugins as plugins
import pwnagotchi . ui . fonts as fonts
2019-10-02 19:01:07 +02:00
from pwnagotchi . ui . components import LabeledValue
from pwnagotchi . ui . view import BLACK
2024-01-20 23:54:32 +01:00
CW2015_ADDRESS = 0X62
CW2015_REG_VCELL = 0X02
CW2015_REG_SOC = 0X04
CW2015_REG_MODE = 0X0A
2019-10-02 19:01:07 +02:00
# TODO: add enable switch in config.yml an cleanup all to the best place
class UPS :
def __init__ ( self ) :
# only import when the module is loaded and enabled
import smbus
# 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
self . _bus = smbus . SMBus ( 1 )
def voltage ( self ) :
try :
2024-01-20 23:54:32 +01:00
read = self . _bus . read_word_data ( CW2015_ADDRESS , CW2015_REG_VCELL )
2019-10-02 19:01:07 +02:00
swapped = struct . unpack ( " <H " , struct . pack ( " >H " , read ) ) [ 0 ]
return swapped * 1.25 / 1000 / 16
except :
return 0.0
def capacity ( self ) :
try :
address = 0x36
2024-01-20 23:54:32 +01:00
read = self . _bus . read_word_data ( CW2015_ADDRESS , CW2015_REG_SOC )
2019-10-02 19:01:07 +02:00
swapped = struct . unpack ( " <H " , struct . pack ( " >H " , read ) ) [ 0 ]
return swapped / 256
except :
return 0.0
2020-08-29 14:40:44 +01:00
def charging ( self ) :
try :
GPIO . setmode ( GPIO . BCM )
GPIO . setup ( 4 , GPIO . IN )
2021-04-18 17:11:04 +02:00
return ' + ' if GPIO . input ( 4 ) == GPIO . HIGH else ' - '
2020-08-29 14:40:44 +01:00
except :
2021-04-18 17:11:04 +02:00
return ' - '
2020-08-29 14:40:44 +01:00
2019-10-02 19:01:07 +02:00
2019-11-01 13:51:45 +01:00
class UPSLite ( plugins . Plugin ) :
2024-01-20 23:54:32 +01:00
__author__ = ' marbasec '
__version__ = ' 1.3.0 '
2019-11-01 13:51:45 +01:00
__license__ = ' GPL3 '
2024-01-20 23:54:32 +01:00
__description__ = ' A plugin that will add a voltage indicator for the UPS Lite v1.3 '
2019-10-02 19:01:07 +02:00
2019-11-01 13:51:45 +01:00
def __init__ ( self ) :
self . ups = None
2019-10-02 19:01:07 +02:00
2019-11-01 13:51:45 +01:00
def on_loaded ( self ) :
self . ups = UPS ( )
2019-10-02 19:01:07 +02:00
2019-11-01 13:51:45 +01:00
def on_ui_setup ( self , ui ) :
2024-01-20 23:54:32 +01:00
ui . add_element ( ' ups ' , LabeledValue ( color = BLACK , label = ' UPS ' , value = ' 0 % ' , position = ( ui . width ( ) / 2 + 15 , 0 ) ,
2019-11-01 13:51:45 +01:00
label_font = fonts . Bold , text_font = fonts . Medium ) )
2019-10-02 19:01:07 +02:00
2019-12-16 18:50:40 +01:00
def on_unload ( self , ui ) :
with ui . _lock :
ui . remove_element ( ' ups ' )
2019-11-01 13:51:45 +01:00
def on_ui_update ( self , ui ) :
2019-12-19 19:37:16 +01:00
capacity = self . ups . capacity ( )
2020-08-29 14:40:44 +01:00
charging = self . ups . charging ( )
ui . set ( ' ups ' , " %2i %s " % ( capacity , charging ) )