Python stats.py Rotate Display

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
jhovak
 
Posts: 8
Joined: Tue Mar 18, 2014 11:36 am

Python stats.py Rotate Display

Post by jhovak »

I'm trying to use the stats.py example as in https://learn.adafruit.com/pi-hole-ad-b ... all-pioled. I'd like to be able to rotate between displaying the Pi-Hole information and the typical stats information. I'm able to display either or by commenting out the code, but I don't have enough experience to rotate “images”. I think I want something like the following:

draw = ImageDraw.Draw(imageA)
...
draw = ImageDraw.Draw(imageB)

disp.image(imageA)
disp.show()
time.sleep(DISPLAY_ON)
disp.fill(0)
disp.show()
time.sleep(DISPLAY_OFF)
disp.image(imageB)
disp.show()
time.sleep(DISPLAY_ON)
disp.fill(0)
disp.show()
time.sleep(DISPLAY_OFF)

But, I don’t understand how I’d accomplish this.

Thanks,
J

User avatar
jhovak
 
Posts: 8
Joined: Tue Mar 18, 2014 11:36 am

Re: Python stats.py Rotate Display

Post by jhovak »

To anyone interested, I've resolved this. In order to understand the drawing section better I reorganized the last section of code.

Revised code starting from the "while True:" statement:

Code: Select all

while True:

    # Shell scripts for system monitoring from here :
    # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "hostname -I | cut -d\' \' -f1 | tr -d \'\\n\'"
    IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "hostname | tr -d \'\\n\'"
    HOST = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "top -bn1 | grep load | awk " \
          "'{printf \"CPU Load: %.2f\", $(NF-2)}'"
    CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "free -m | awk 'NR==2{printf " \
          "\"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
    MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "df -h | awk '$NF==\"/\"{printf " \
          "\"Disk: %d/%dGB %s\", $3,$2,$5}'"
    Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")

    # Pi Hole data!
    try:
        r = requests.get(api_url)
        data = json.loads(r.text)
        DNSQUERIES = data['dns_queries_today']
        ADSBLOCKED = data['ads_blocked_today']
        CLIENTS = data['unique_clients']
    except KeyError:
        time.sleep(1)
        continue

    # Start A
    # Create blank image for drawing.
    # Make sure to create image with mode '1' for 1-bit color.
    imageA = Image.new('1', (width, height))

    # Get drawing object to draw on image.
    draw = ImageDraw.Draw(imageA)

    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    # Text A
    draw.text((x, top + 0), "IP: " + IP, font=font, fill=255)
    draw.text((x, top + 8),  CPU, font=font, fill=255)
    draw.text((x, top + 16), MemUsage, font=font, fill=255)
    draw.text((x, top + 25), Disk, font=font, fill=255)

    # Start B
    imageB = Image.new('1', (width, height))
    draw = ImageDraw.Draw(imageB)
    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    # Text B
    draw.text((x, top), "HOSTNAME: " + str(HOST), font=font, fill=255)
    draw.text((x, top + 8), "Ads Blocked: " + str(ADSBLOCKED), font=font, fill=255)
    draw.text((x, top + 16), "Clients:     " + str(CLIENTS), font=font, fill=255)
    draw.text((x, top + 24), "DNS Queries: " + str(DNSQUERIES), font=font, fill=255)

    # Display images.
    disp.image(imageA)
    disp.show()
    time.sleep(DISPLAY_ON)
    disp.fill(0)
    disp.show()
    time.sleep(DISPLAY_OFF)
    disp.image(imageB)
    disp.show()
    time.sleep(DISPLAY_ON)
    disp.fill(0)
    disp.show()
    time.sleep(DISPLAY_OFF)

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”