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()
  • edited February 2023
    I'm still having that weird "block cursor in battery icon" sometimes, and I have no idea where it's coming from... I keep intending to dig into the qt docs, but I haven't had time lately... Has anybody else seen anything like this?
    Chris
  • @ChrisInTn We uploaded it again. Now you should see 4 . ZIP files and 2. PDF files in it through the link above.
  • Ummm... I'm a little confused. What link above? I pulled the code from the github. No zip files or pdf files.

    I think I waited too long between checking messages. Now I'm really lost.

    Besides, apparently I'm the only one seeing this goofy quarter-character block missing in my icon. Must be something strange about my setup.

  • Anyone who has booted from 64-bit Pi OS may not find the battery icon appearing. It runs but the icon doesn't show up. I've only juts got my Crowpi L and someone at a computer museum solved it. Add a delay to it being added to the tray as the 64-bit OS loads the Python code faster than the desktop is fully initialised. Insert the code just before :

    tray = QSystemTrayIcon()

    Hope this helps someone.


    # Add delay for desktop to initialise for 64-bit Pi OS


    import time

    time.sleep(10)

Sign In or Register to comment.