Show battery status of CrowPi L in Raspberry Pi OS

2»

Comments

  • Thanks Wintchan, I did it and it works now, I have this nice working blue icon on my desktop. The only annoying thing is that I lost the bottom line showing icons of essential programs in the process and I don't know how to get it back.
  • I didn't want to install QT bindings, so i made a simplified version that don't require to install any .deb package. Here it is :smile:
    #!/usr/bin/env python3 from guizero import App, Text from smbus import SMBus import time address = 0x03 power_reg = 0x35 bus = SMBus(1) def writeLevel(mesure): # convert the read value in a power number from -1 to 7 power_number = 0 power_Vol = (((mesure[1]*3.0)/1024)*260)/100 print(f'Charge={power_Vol:.1f}') if (power_Vol >= 7.2): power_number = 7 #icon 100% elif (power_Vol >= 7.032): power_number = 6 # icon 86% elif (power_Vol >= 6.852): power_number = 5 # icon 71% elif (power_Vol >= 6.684): power_number = 4 # icon 57% elif (power_Vol >= 6.516): power_number = 3 # icon 43% elif (power_Vol >= 6.348): power_number = 2 # icon 29% elif (power_Vol >= 6.168): power_number = 1 # icon 14% elif (power_Vol >= 6.024): power_number = 0 # icon 2% else: power_number = -1 # icon alert return(power_number) def getPowerValue(): # read the power data from I2C port power_data = 0 new_power_data = 0 flag = 0 read_power_init = {0,0,0,0,0,0} while(1): try: read_power_init = bus.read_i2c_block_data(address,power_reg, 6) if int(read_power_init[0]) != 170 or int(read_power_init[5]) != 85: print("Incomplete data acquisition exception, reread") time.sleep(0.5) continue break except: print("IIC read failed, reread") time.sleep(0.5) read_value_init = int(read_power_init[2] * 256 + read_power_init[3]) # High eight bits low eight bits if (int(read_power_init[1]) != 0): # charging data if (int(read_power_init[1]) >=3 and int(read_power_init[1]) < 0): # Abnormal charging data pass else: return([read_power_init[1], read_value_init]) flag = 0 else: power_data = new_power_data if ((power_data - read_value_init) >= 0 and flag > 1 and (power_data > read_value_init)): return([read_power_init[1], new_power_data]) new_power_data = read_value_init elif (flag <= 1): return([read_power_init[1], read_value_init]) new_power_data = read_value_init flag = flag + 1 # main resu = getPowerValue() #print(resu) level = writeLevel(resu) # status meaning if resu[0] == 1: state = 'charging' elif resu[0] == 0: if level >= 0: state = 'using' else: state = 'empty' else: state = 'unknown' app = App(title="Battery", width=300, height=20) text = Text(app, text=f'state={resu[0]} ({state}), level={level}/7', size=12) app.display()
Sign In or Register to comment.