User manual
63
import RPi.GPIO as GPIO
import time
import os
g1 = 1; g2 = 10; g3 = 100; g4 = 500
GPIO.setmode(GPIO.BCM)
LED = [4,18,23]
for i in range(3):
GPIO.setup(LED[i], GPIO.OUT, initial=False)
print (“Ctrl+C exits the programme")
try:
while True:
s = os.statvfs('/')
f = s.f_bsize * s.f_bavail / 1000000
if f < g1:
x = "100"
elif f < g2:
x = "110"
elif f < g3:
x = "010"
elif f < g4:
x = "011"
else:
x = "001"
for i in range(3):
GPIO.output(LED[i], int(x[i]))
time.sleep(1.0)
except KeyboardInterrupt:
GPIO.cleanup()
When you run the programme the three LEDs will constantly show the free space on the memory card. Try it
out by copying large files over the network to the memory card and delete them. The indicator updates
automatically.
7.1.1 How does it work?
The programme uses the Python module os to calculate the free space, which provides the basic functions of
the operating system.
import os The module os as any other module needs to be imported at the beginning of the programme.
g1 = 1; g2 = 10; g3 = 100; g4 = 500 Those command lines define the limits of the free space areas, where
the indication is supposed to switch. The programme uses megabytes and not bytes, as such figures are
better comprehensible, to keep it simple, so to say. You can modify the limits at any time; however, the four
values must be arranged in ascending order according to size.