Repository navigation
Expand file tree
/
Copy pathpitouch.py
More file actions
123 lines (103 loc) · 3.04 KB
/
Copy pathpitouch.py
File metadata and controls
123 lines (103 loc) · 3.04 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from time import sleep
from smbus import SMBus
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
bus = 0
class PiTouch:
def __init__(self):
if GPIO.RPI_REVISION == 1: # Although PiTouch doesn't fit, keep for compatibility
i2c_bus = 0
elif GPIO.RPI_REVISION == 2: # As well as the rev 2
i2c_bus = 1
elif GPIO.RPI_REVISION == 3: # For model B+
i2c_bus = 1
else:
print "Unable to determine Raspberry Pi revision."
exit
self.bus = SMBus(i2c_bus)
self.bus.write_byte_data(0x40, 0x00, 0x10)
self.bus.write_byte_data(0x40, 0xFE, 0x00)
sleep(0.01)
self.bus.write_byte_data(0x40, 0x00, 0x00)
self.bus.write_byte_data(0x40, 0x01, 0x04)
sleep(0.01)
## Fin init PCA
GPIO.setup(11,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def brightness(self,input):
if input == 0:
return 4096
else:
return (input - 0) * (4094 - 0) // (100 - 0) + 1
def light(self,chan,light):
light = self.brightness(light)
self.bus.write_byte_data(0x40, chan, light & 0xFF)
self.bus.write_byte_data(0x40, chan+1, light >> 8)
self.bus.write_byte_data(0x40, chan+2, 0 & 0xFF)
self.bus.write_byte_data(0x40, chan+3, 0 >> 8)
def green(self,light,pad=0):
#3, 0, 10, 13
if pad == 0:
self.light(0x06+(4*3),light)
self.light(0x06+(4*0),light)
self.light(0x06+(4*10),light)
self.light(0x06+(4*13),light)
elif pad == 1:
self.light(0x06+(4*3),light)
elif pad == 2:
self.light(0x06+(4*0),light)
elif pad == 3:
self.light(0x06+(4*10),light)
elif pad == 4:
self.light(0x06+(4*13),light)
def blue(self,light,pad=0):
#4, 1, 11, 14
if pad == 0:
self.light(0x06+(4*4),light)
self.light(0x06+(4*1),light)
self.light(0x06+(4*11),light)
self.light(0x06+(4*14),light)
elif pad == 1:
self.light(0x06+(4*4),light)
elif pad == 2:
self.light(0x06+(4*1),light)
elif pad == 3:
self.light(0x06+(4*11),light)
elif pad == 4:
self.light(0x06+(4*14),light)
def red(self,light,pad=0):
#5, 2, 12, 15
if pad == 0:
self.light(0x06+(4*5),light)
self.light(0x06+(4*2),light)
self.light(0x06+(4*12),light)
self.light(0x06+(4*15),light)
elif pad == 1:
self.light(0x06+(4*5),light)
elif pad == 2:
self.light(0x06+(4*2),light)
elif pad == 3:
self.light(0x06+(4*12),light)
elif pad == 4:
self.light(0x06+(4*15),light)
def all(self,light):
light = self.brightness(light)
self.bus.write_byte_data(0x40, 0xFA, light & 0xFF)
self.bus.write_byte_data(0x40, 0xFB, light >> 8)
self.bus.write_byte_data(0x40, 0xFC, 0 & 0xFF)
self.bus.write_byte_data(0x40, 0xFD, 0 >> 8)
def read(self):
readpad = self.bus.read_byte_data(0x1b, 0x03)
if readpad == 8:
return 1
if readpad == 16:
return 2
if readpad == 4:
return 3
if readpad == 2:
return 4
def touch(self):
while True:
if GPIO.input(11) == 0:
read = self.read()
if 4 >= read > 0:
return self.read()