forked from hankhank10/music-screen-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhyperpixel_backlight.py
56 lines (44 loc) · 1.57 KB
/
hyperpixel_backlight.py
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
55
56
"""
Support class to control the backlight power on a HyperPixel display.
"""
import logging
import os
_LOGGER = logging.getLogger(__name__)
try:
import RPi.GPIO as GPIO
except ImportError:
GPIO = None
BACKLIGHT_PIN = 19
class Backlight():
def __init__(self, initial_value=False):
"""Initialize the backlight instance."""
self.power = None
if not GPIO:
self.active = False
_LOGGER.error("Backlight control not available, please ensure RPi.GPIO python3 package is installed")
return
GPIO.setwarnings(False)
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(BACKLIGHT_PIN, GPIO.OUT)
self.active = True
except RuntimeError:
self.active = False
username = os.environ.get('USER')
_LOGGER.error("Backlight control not available, please ensure '%s' is part of group 'gpio'.", username)
_LOGGER.error(" To add user to group: `sudo gpasswd -a %s gpio`", username)
else:
self.set_power(initial_value)
def set_power(self, new_state):
"""Control the backlight power of the HyperPixel display."""
if not self.active:
return
if new_state is False and self.power:
_LOGGER.debug("Going idle, turning backlight off")
self.power = new_state
GPIO.output(BACKLIGHT_PIN, new_state)
def cleanup(self):
"""Return the GPIO setup to initial state."""
if self.active:
GPIO.output(BACKLIGHT_PIN, True)
GPIO.cleanup()