-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_blinkt_cpu_load.py
More file actions
executable file
·54 lines (43 loc) · 1.44 KB
/
my_blinkt_cpu_load.py
File metadata and controls
executable file
·54 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
import time
from sys import exit
import math
def floatRgb(mag, cmin, cmax):
""" Return a tuple of floats between 0 and 1 for R, G, and B. """
# Normalize to 0-1
try: x = float(mag-cmin)/(cmax-cmin)
except ZeroDivisionError: x = 0.5 # cmax == cmin
blue = min((max((4*(0.75-x), 0.)), 1.))
red = min((max((4*(x-0.25), 0.)), 1.))
green = min((max((4*math.fabs(x-0.5)-1., 0.)), 1.))
return red, green, blue
def rgb(mag, cmin, cmax):
""" Return a tuple of integers, as used in AWT/Java plots. """
red, green, blue = floatRgb(mag, cmin, cmax)
return int(red*255), int(green*255), int(blue*255)
def strRgb(mag, cmin, cmax):
""" Return a hex string, as used in Tk plots. """
return "#%02x%02x%02x" % rgb(mag, cmin, cmax)
try:
import psutil
except ImportError:
exit("This script requires the psutil module\nInstall with: sudo pip install psutil")
import blinkt
blinkt.set_clear_on_exit()
def show_graph(v):
v *= blinkt.NUM_PIXELS
for x in range(blinkt.NUM_PIXELS):
if x > v:
blinkt.set_pixel(x, 0, 0, 0)
elif x < blinkt.NUM_PIXELS/3.0:
blinkt.set_pixel(x, 0, 50, 0)
elif x < blinkt.NUM_PIXELS*2.0/3.0:
blinkt.set_pixel(x, 50, 25, 0)
elif x < blinkt.NUM_PIXELS*3.0/3.0:
blinkt.set_pixel(x, 50, 0, 0)
blinkt.show()
blinkt.set_brightness(0.04)
while True:
load = psutil.cpu_percent() / 100.0
show_graph(load)
time.sleep(0.2)