Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Pr/2 #1

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Binary file added Instructions for UPS-Lite V1.2.pdf
Binary file not shown.
Binary file removed MAX17040.pdf
Binary file not shown.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

Binary file added UPS-Lite V1.2 使用说明.pdf
Binary file not shown.
Binary file removed UPS-Lite使用说明.pdf
Binary file not shown.
50 changes: 45 additions & 5 deletions UPS_Lite.py → UPS_Lite_org.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

#!/usr/bin/env python
import struct
import smbus
import sys
import time
import RPi.GPIO as GPIO



def readVoltage(bus):

"This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 2)
read = bus.read_word_data(address, 0X02)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
Expand All @@ -19,27 +20,66 @@ def readVoltage(bus):
def readCapacity(bus):
"This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 4)
read = bus.read_word_data(address, 0X04)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity


def QuickStart(bus):
address = 0x36
bus.write_word_data(address, 0x06,0x4000)


def PowerOnReset(bus):
address = 0x36
bus.write_word_data(address, 0xfe,0x0054)




GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4,GPIO.IN)

bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

PowerOnReset(bus)
QuickStart(bus)


print " "
print "Initialize the MAX17040 ......"


while True:

print "++++++++++++++++++++"
print "Voltage:%5.2fV" % readVoltage(bus)

print "Battery:%5i%%" % readCapacity(bus)



if readCapacity(bus) == 100:

print "Battery FULL"

if readCapacity(bus) < 20:
if readCapacity(bus) < 5:

print "Battery LOW"





if (GPIO.input(4) == GPIO.HIGH):

print "Power Adapter Plug In "

if (GPIO.input(4) == GPIO.LOW):

print "Power Adapter Unplug"


print "++++++++++++++++++++"
time.sleep(2)