cornishsteve
Posts: 6
Joined: Wed May 22, 2013 10:53 am
Location: Cornwall, UK
Contact: Website

Python + PiGlow Programming Help

Tue Aug 27, 2013 7:26 pm

Hi all

I'm throwing myself at the mercy of the forum as i'm failing miserably to write a python script to control my shiney new PiGlow from Pimoroni.

I'm a complete beginner at programming but I'm trying to create a script that I can run at system boot that cycles the LED's on my PiBow at a rate dependent on current CPU load.
EG:
CPU Load = 0-10% LED's change / cycles with a sleep time of 1.0
CPU Load = 20-40% LED's change / cycles with a sleep time of 0.7
CPU Load = 50-60% LED's change / cycles with a sleep time of 0.4
CPU Load = 70-90% LED's change / cycles with a sleep time of 0.2
CPU Load = 91-100% LED's change / cycles with a sleep time of 0.1

Here is a link to the PiGlow board to get a better idea of the product / LED layout for anyone interested :)

http://shop.pimoroni.com/products/piglow

I've found some example python code online, courtesy of Boeeerb and am using that as my starting point.

I'd like to create a function / procedure (not 100% sure thats the correct term - sorry, but have no idea how to do this) for the code that controls the LED cycle command and use variables to change the cycle speed and LED intensity.

This is what I have so far:

Code: Select all

#!/usr/bin/python

from piglow import PiGlow
from time import sleep
import psutil

piglow = PiGlow()
cpu = psutil.cpu_percent()
brghtness = 1
speed = 1.0
  
while true:
    piglow.white('brightness')
    sleep('speed')
    piglow.blue('brightness')
    sleep('speed')
    piglow.green('brightness')
    sleep('speed')
    piglow.yellow('brightness')
    sleep('speed')
    piglow.orange('brightness')
    sleep('speed')
    piglow.red('brightness')
    sleep('speed')
    piglow.red(0)
    sleep('speed')
    piglow.orange(0)
    sleep('speed')
    piglow.yellow(0)
    sleep('speed')
    piglow.green(0)
    sleep('speed')
    piglow.blue(0)
    sleep('speed')
    piglow.white(0)
    sleep(0.1)
I'd really appreciate any assistance with creating the function / procedure and changing the while true statement to an elseif statement based on cpu usage.

Thanks
Steve.
CornishSteve
http://cornishgeek.co.uk

evansg515
Posts: 7
Joined: Wed May 15, 2013 10:24 am

Re: Python + PiGlow Programming Help

Wed Aug 28, 2013 11:23 am

Steve,

The psutil function cpu_percent() will return a float that corresponds to your cpu usage ranging from 0-100. You already have that stored in the variable cpu however your cpu value will never change as it is only calculated once (when the script runs). That variable needs to be assigned everytime the loop runs (put it within the while True loop).

Then you need to run a comparison on that value, so:

Code: Select all

if cpu < 5:        
  piglow.white(20)
elif cpu < 20:
  piglow.white(20)        
  piglow.blue(20)
........... etc
all the way to 90% (ish) then a final else statment to catch the last 10%

Code: Select all

.....
else:
  piglow.all(20)
There is an example held here: https://github.com/Boeeerb/PiGlow/blob/ ... les/cpu.py

I wrote a script to make the PiGlow "pulse" from the center outward and then back in. I'll be putting it up on my blog soon.

Gareth
Gareth Evans

@pukkapi
www.pukkapi.co.uk

cornishsteve
Posts: 6
Joined: Wed May 22, 2013 10:53 am
Location: Cornwall, UK
Contact: Website

Re: Python + PiGlow Programming Help

Thu Aug 29, 2013 10:12 am

Thanks guys for all your replies.

I've also been working with Jon @ Pimoroni and have come up with the following code:

Code: Select all

#!/usr/bin/python

from piglow import PiGlow
from time import sleep
import psutil

piglow = PiGlow()

# start at white
colour = 1

while True:
    # returns a value from 0 - 1 representing current CPU load
    cpu = psutil.cpu_percent()

    # now we need to scale that CPU level to the range 0 - 255 for PiGlow brightness
    brightness = max( (int)(cpu * 255), 1)

    # pick a speed that gets faster as the CPU usage increases
    speed = max ( 1.2 - cpu, 0.1 )

    # turn everything off, let's get ready to rumble
    piglow.all(0)

    # turn on the next "ring" of colour
    piglow.colour(colour, brightness)

    # sleep for a bit, i'm weary...
    sleep(speed)

    # increment to the next colour (reset to white if we're already on red)
    colour = colour + 1 if colour < 6 else 1
I've posted the whole thing on github so please feel free to download / fork from there if you wish:

https://github.com/CornishSteve/PiGlow-CPU-Monitor

Just need to work on adding this to the init.d as a daemon and then I'll be happy :)

Cheers

Steve.
CornishSteve
http://cornishgeek.co.uk

Electrodude
Posts: 1
Joined: Wed Apr 10, 2019 1:01 am

Re: Python + PiGlow Programming Help

Wed Apr 10, 2019 1:08 am

Steve,

I've done the 'dependent' items you listed at GitHub, but what do I do or how do I make use of the .py files? There's two of them, and I'm not sure what to do with them or where to copy them, etc.

I hope to get the PiGlow cpu monitor going soon!

Thanks!
Electrodude

Return to “Python”