-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrightpi.py
68 lines (55 loc) · 1.87 KB
/
brightpi.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
60
61
62
63
64
65
66
67
#!/usr/bin/python
#
# Library controls the leds on the bright pi circuit
# https://www.pi-supply.com/product/bright-pi-bright-white-ir-camera-light-raspberry-pi/
#
# this circuit uses the Semtech SC620 LED driver chip
# Datasheet: http://www.semtech.com/images/datasheet/sc620.pdf
#
# Additional documentation: https://www.pi-supply.com/bright-pi-v1-0-code-examples/
#
# White LEDs:
# 2, 4, 5, 7
# IR LEDs (in pairs)
# 1, 3, 6, 8
#
import smbus
bus = smbus.SMBus(1)
DEVICE_ADDRESS = 0x70
LED_CONTROL_ALL_WHITE = 0x5a
LED_CONTROL_ALL_IR = 0xa5
LED_GAIN_REGISTER = 0x09
def switch_white_leds_on():
bus.write_byte_data(DEVICE_ADDRESS, 0x00, LED_CONTROL_ALL_WHITE)
def switch_ir_leds_on():
bus.write_byte_data(DEVICE_ADDRESS, 0x00, LED_CONTROL_ALL_IR)
def switch_leds_off():
bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0x00)
def get_led_states():
return bus.read_byte_data(DEVICE_ADDRESS, 0x00)
def get_led_state(led):
if led >= 1 and led <= 8:
states = get_led_states()
if (states >> led - 1) & 0x01:
return True
else:
return False
def set_led_state(led, state):
if led >= 1 and led <= 8:
states = get_led_states()
if state:
bus.write_byte_data(DEVICE_ADDRESS, 0x00, states | (0x01 << led - 1))
else:
bus.write_byte_data(DEVICE_ADDRESS, 0x00, states & ~(0x01 << led - 1))
def set_gain(gain):
# gain is a 4 bit value
if gain >= 0 and gain <= 15:
bus.write_byte_data(DEVICE_ADDRESS, LED_GAIN_REGISTER, gain)
def set_default_gain():
# default gain is 0b1000
bus.write_byte_data(DEVICE_ADDRESS, LED_GAIN_REGISTER, 0b1000)
def dim_led(led, value):
# there are 8 LEDs (1-8) and
# dim values range between 0 and 50
if led >= 1 and led <= 8 and value >= 0 and value <= 50:
bus.write_byte_data(DEVICE_ADDRESS, led, value)