-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbacklight.py
59 lines (47 loc) · 1.45 KB
/
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
57
58
59
#!/usr/bin/python
# -*- charset: utf-8 -*-
# This is a port of https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight
# (c) 2014 Helge Jung <[email protected]>
# Released unter the LGPL, like the original.
from smbus import SMBus
class Backlight(object):
REG_RED = 0x04 # pwm2
REG_GREEN = 0x03 # pwm1
REG_BLUE = 0x02 # pwm0
REG_MODE1 = 0x00
REG_MODE2 = 0x01
REG_OUTPUT = 0x08
def __init__(self, bus, address):
if not isinstance(bus, SMBus):
raise TypeError
self.bus = bus
self.address = int(address)
# initialize
self.set_register(self.REG_MODE1, 0)
self.set_register(self.REG_MODE2, 0)
# all LED control by PWM
self.set_register(self.REG_OUTPUT, 0xAA)
def set_register(self, addr, value):
self.bus.write_byte_data(self.address, addr, value)
def set_color(self, red, green, blue):
r = int(red)
g = int(green)
b = int(blue)
self.set_register(self.REG_RED, r)
self.set_register(self.REG_GREEN, g)
self.set_register(self.REG_BLUE, b)
if __name__ == '__main__':
import time
bus = SMBus(1)
light = Backlight(bus, 0x62)
light.set_color(255, 0, 0)
time.sleep(1)
light.set_color(0,255,0)
time.sleep(1)
light.set_color(0,0,255)
time.sleep(1)
light.set_color(255,0,255)
time.sleep(1)
light.set_color(255,255,255)
time.sleep(1)
light.set_color(0, 0, 128)