forked from DzikuVx/Raspberry_PCF8574
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcf8574_led_rotate.py
48 lines (34 loc) · 864 Bytes
/
pcf8574_led_rotate.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
import smbus
import time
# define I2C bus number
BUS_NUMBER = 1
# define device address
DEVICE_ADDR = 0x20
bus = smbus.SMBus(BUS_NUMBER)
# PULLUP all ports to enable button state readout
writeVal = 255
bus.write_byte(DEVICE_ADDR,writeVal)
ledCounter = 1
while 1==1:
#get current register state
inputVal = bus.read_byte(DEVICE_ADDR)
#shift counter bits left
writeVal = ledCounter << 4
#pullup input
writeVal = writeVal + 15
#negate bits 4-7 so only 1 led is lighted atm
writeVal =writeVal ^ 240
#write to register
bus.write_byte(DEVICE_ADDR,writeVal)
#multiply counter times 2
ledCounter = ledCounter << 1
#and cap at 16 (4 bits)
if ledCounter == 16:
ledCounter = 1
#check if any button is pressed
inputVal = inputVal & 15
#if presses <> 15 rotate led 3 times faster
if inputVal == 15:
time.sleep(0.3)
else:
time.sleep(0.1)