make progress bar narrower, add elapsed and duration display, add battery queries and display

This commit is contained in:
Sundog Jones 2020-07-21 10:00:02 -07:00
parent aa7201fc2a
commit f5945fbe75

View File

@ -6,6 +6,7 @@ import struct
import smbus
import sys
import musicpd
from math import floor
class SoundslabDisplay:
def __init__(self):
@ -28,11 +29,27 @@ class SoundslabDisplay:
self.player_client = musicpd.MPDClient()
self.player_client.connect()
def getBatteryState(self):
bus = smbus.SMBus(1)
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 78.125 / 1000000
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped / 256
return (voltage, capacity)
def updateOverlay(self, battery_level_percent):
# initialize overlay
im_overlay = Image.new("RGBA", self.screen_size, (0, 0, 0, 0))
font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/Hack-Bold.ttf', size=18)
small_font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/Hack-Regular.ttf', size=12)
overlay = ImageDraw.Draw(im_overlay)
# draw four rects on edges of screen_size
@ -52,12 +69,26 @@ class SoundslabDisplay:
overlay.text((10, 0), track_and_queue, font=font, fill=self.fg_color)
# add battery level
battery_display = '{:02.2f}'.format(battery_level_percent) + '%'
overlay.text((self.screen_size[0] - 70, 0), battery_display, font=font, fill=self.fg_color)
(voltage, capacity) = self.getBatteryState()
battery_display = '{:02.2f}'.format(capacity) + '%'
(voltage2, capacity2) = self.getBatteryState()
battery_display_offset = 80
if voltage2 > voltage:
# we're probably charging!
battery_display = 'Chrg ' + battery_display
battery_display_offset = 130
overlay.text((self.screen_size[0] - battery_display_offset, 0), battery_display, font=font, fill=self.fg_color)
# add progress meter
overlay.rectangle([(10, self.screen_size[1] - 15), (self.screen_size[0] - 10, self.screen_size[1] - 5)], fill=(0, 0, 0, 0), outline=self.fg_color, width=1)
overlay.rectangle([(10, self.screen_size[1] - 15), (int((self.screen_size[0] - 20) * track_progress_percent) + 10, self.screen_size[1] - 5)], fill=self.fg_color, outline=self.fg_color, width=1)
overlay.rectangle([(70, self.screen_size[1] - 15), (self.screen_size[0] - 70, self.screen_size[1] - 5)], fill=(0, 0, 0, 0), outline=self.fg_color, width=1)
overlay.rectangle([(70, self.screen_size[1] - 15), (int((self.screen_size[0] - 140) * track_progress_percent) + 70, self.screen_size[1] - 5)], fill=self.fg_color, outline=self.fg_color, width=1)
# add playhead position and song duration displays at beginning and end of progress meter
(time_width, time_height) = overlay.textsize(text='00:00', font=small_font)
playhead = str(floor(float(current_status['elapsed']) / 60)) + ":" + "{:02d}".format(floor(float(current_status['elapsed']) % 60))
duration = str(floor(float(current_status['duration']) / 60)) + ":" + "{:02d}".format(floor(float(current_status['duration']) % 60))
overlay.text((5, self.screen_size[1] - 20 + floor(time_height / 2)), playhead, font=small_font, fill=self.fg_color)
overlay.text((self.screen_size[0] - 5 - time_width, self.screen_size[1] - 20 + floor(time_height / 2)), duration, font=small_font, fill=self.fg_color)
self.current_overlay = im_overlay.copy()