Backing up images with the Raspberry Pi

Pibrella

The Pibrella board belongs to the first generation of Raspberry Pi extensions. Therefore it is not an official HAT (Figure 1). With a price tag of about 25 Euro, it functions seamlessly with the current Raspberry Pi 3 however. There are three LEDs on board: red, yellow and green. There is also a buzzer and a button. The six additional connections can be used for switching larger loads. The board also has its own microUSB connection for power which the Raspberry Pi can also use.

Figure 1: The Raspberry Pi extension Pibrella offers switchable LEDs and a button for inputs.

Overall, the Pibrella makes a solid impression but it does have a downside: the button sits opposite the baseboard so that each press of the button exerts leverage on the entire board. You can lessen this by placing material between the HDMI jack and the Pibrella. A proven solution is a rolled-up piece of thin paper The paper acts as a kind of spring, moving in the opposite direction.

Pimoroni has developed a Python library for the Pibrella board. This is available for download from Github [4]. Run the commands from Listing 7 to install.

Listing 7

hat-server.py

01 #!/usr/bin/python
02
03 import sys
04 import socket
05 import os, os.path
06 import time
07
08 import pibrella
09
10 SOCKFILE = "/run/hat-server"
11
12 TIME_ON = 0.2
13 TIME_OFF = 0.2
14 FADE_ON = 0.2
15 FADE_OFF = 0.2
16
17 ts_start = 0
18
19 def boot_start():
20   pibrella.light.red.blink(TIME_ON, TIME_OFF)
21
22 def boot_end():
23   pibrella.light.red.off()
24   pibrella.light.green.on()
25
26 def action_start():
27   pibrella.light.green.off()
28   pibrella.light.yellow.pulse(FADE_ON, FADE_OFF, TIME_ON, TIME_OFF)
29
30 def action_end():
31   pibrella.light.green.on()
32   pibrella.light.yellow.off()
33
34 def halt_start():
35   pibrella.light.red.blink(TIME_ON, TIME_OFF)
36   pibrella.light.green.off()
37   pibrella.light.yellow.off()
38
39 def halt_end():
40   pibrella.light.red.off()
41
42 def btn_pressed(pin):
43   global ts_start
44   ts_start = time.time()
45   print "button pressed ..."
46
47 def btn_released(pin):
48   global ts_start
49   ts_end = time.time()
50   print "button released ..."
51   print "ts_start:", ts_start
52   print "duration:", ts_end-ts_start
53   if ts_end - ts_start > 2:
54     pibrella.buzzer.success()
55     os.system("halt -p &")
56   else:
57     pibrella.buzzer.fail()
58   ts_start = 0
59
60 def run_server():
61   if os.path.exists(SOCKFILE):
62     os.remove(SOCKFILE)
63   print "Opening socket..."
64
65   server = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
66   server.bind(SOCKFILE)
67   server.listen(5)
68
69   print "Listening..."
70   while True:
71     conn, addr = server.accept()
72     print ,accepted connection'
73     while True:
74       data = conn.recv(2)
75       if not data:
76         break
77       elif data == "BS":
78         boot_start()
79       elif data == "BE":
80         boot_end()
81       elif data == "AS":
82         action_start()
83       elif data == "AE":
84         action_end()
85       elif data == "HS":
86         halt_start()
87       elif data == "HE":
88         halt_end()
89         print "Shutting down..."
90         server.close()
91         os.remove(SOCKFILE)
92         return
93
94 pibrella.button.pressed(btn_pressed)
95 pibrella.button.released(btn_released)
96 run_server()

This library just manipulates the GPIO pins on the Raspberry Pi. This means that you can use the board with your chosen programming language. The functions simplify operation greatly. They include switching on and off of the LEDs, various blinking combinations, button reaction, and even use of the buzzer for playing melodies. The example code on Github can be used to get started, even if you are not experienced in Python.

Status Machine

You will only use some of the available functions for your image tank. The current status of the system is displayed via the LEDs. At first the system boots, and the red LED lights up during the process. Then it idles, and the green LED lights up. If the Pi executes an action the yellow LED lights up. Finally, the system shuts down. Somewhat confusingly, this action is also designated by red. Events trigger the transition from one status to another. Figure 2 abbreviates these events with two letters of the alphabet, for instance BS is "Boot-Start". AS and AE designate "Action-Start" and "Action-End" respectively.

Figure 2: LED signals for particular conditions and mode changes on the Raspberry Pi image tank.

Listing 7 shows the HAT server. It queries a Unix socket in an endless loop (starting with line 88), reads the alphabet code described above and calls a corresponding function. In this way the boot_start() function starting with line 21 activates the red LED and causes it to flash. The function boot_end() starting with line 26 turns off the red flashing light diode and activates the green LED.

In order to make sure that the button also works, the program code will register two callbacks for the two events Button pressed and Button released during start up (starting with line 114). The HAT server will trigger a shut down when at least two seconds pass between these two events (lines 63 to 74).

The control program hatctl.py communicates with the HAT server from Listing 8. This is a very simple program that waits after the start until the HAT server socket is available (lines 18 to 22). Then the program writes the first argument blind to the socket (line 25). A hatctl.py AS triggers an Action-Start event. The HAT server switches off the green LED in the corresponding callback, and activates the slowly flashing yellow light.

Listing 8

hatctl.py

01 #!/usr/bin/python
02
03 import sys
04 import socket
05 import os, os.path
06 import time
07
08 SOCKFILE = "/run/hat-server"
09
10 if len(sys.argv) == 1:
11   sys.exit(3)
12 else:
13   for _ in range(100):
14     if os.path.exists(SOCKFILE):
15       break
16     else:
17       time.sleep(0.1)
18   sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
19   sock.connect(SOCKFILE)
20   sock.sendall(sys.argv[1])

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Pibrella board for Raspberry Pi

    Pibrella eliminates the need for a separate breadboard, jumper wires, and basic electronic components. This makes it a perfect board for prototyping and building both simple and advanced Raspberry Pi-based projects.