|
39 | 39 | # Import Python Imaging Library
|
40 | 40 | from PIL import Image, ImageDraw, ImageFont
|
41 | 41 |
|
| 42 | +# Signal/sys are used to catch SIGINT. |
| 43 | +import signal |
| 44 | +import sys |
| 45 | + |
| 46 | +# URL of Pi Hole |
42 | 47 | api_url = 'http://localhost/admin/api.php'
|
43 | 48 |
|
| 49 | +# Load nice silkscreen font |
| 50 | +font = ImageFont.truetype('fonts/slkscr.ttf', 8) |
| 51 | + |
44 | 52 | # Create the I2C interface.
|
45 | 53 | i2c = busio.I2C(SCL, SDA)
|
46 | 54 |
|
| 55 | +# Blanks the Pi OLED display. |
| 56 | +def blank_screen(display): |
| 57 | + display.fill(0) |
| 58 | + display.show() |
| 59 | + |
| 60 | +def SIGINT(sig, frame): |
| 61 | + print('Ctrl+C detected. Quitting.') |
| 62 | + # Clear display. |
| 63 | + blank_screen(disp) |
| 64 | + sys.exit(0) |
| 65 | + |
| 66 | +# Catch SIGINT (ctrl-c) and blank the screen. |
| 67 | +# This ensures that the screen will be blank when we exit. |
| 68 | +signal.signal(signal.SIGINT, SIGINT) |
| 69 | + |
47 | 70 | # Create the SSD1306 OLED class.
|
48 | 71 | # The first two parameters are the pixel width and pixel height. Change these
|
49 | 72 | # to the right size for your display!
|
|
55 | 78 | DISPLAY_OFF = 50 # off time in seconds
|
56 | 79 |
|
57 | 80 | # Clear display.
|
58 |
| -disp.fill(0) |
59 |
| -disp.show() |
| 81 | +blank_screen(disp) |
60 | 82 |
|
61 | 83 | # Create blank image for drawing.
|
62 | 84 | # Make sure to create image with mode '1' for 1-bit color.
|
|
79 | 101 | # for drawing shapes.
|
80 | 102 | x = 0
|
81 | 103 |
|
82 |
| -# Load nice silkscreen font |
83 |
| -font = ImageFont.truetype('/home/pi/slkscr.ttf', 8) |
84 |
| - |
85 | 104 | while True:
|
86 | 105 | # Draw a black filled box to clear the image.
|
87 | 106 | draw.rectangle((0, 0, width, height), outline=0, fill=0)
|
|
131 | 150 | disp.image(image)
|
132 | 151 | disp.show()
|
133 | 152 | time.sleep(DISPLAY_ON)
|
134 |
| - disp.fill(0) |
135 |
| - disp.show() |
| 153 | + # Clear display. |
| 154 | + blank_screen(disp) |
136 | 155 | time.sleep(DISPLAY_OFF)
|
0 commit comments