-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCP4151_0.py
More file actions
56 lines (47 loc) · 1.26 KB
/
Copy pathMCP4151_0.py
File metadata and controls
56 lines (47 loc) · 1.26 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
#!/usr/bin/env python
import spidev
import math
PotiValues = [[10, 14],
[9.5, 15],
[9, 16],
[8.5, 17],
[8, 18],
[7.5, 19],
[7, 20],
[6.5, 21],
[6, 22],
[5.5, 24],
[5, 26],
[4.5, 30],
[4, 33],
[3.5, 38],
[3, 45],
[2.5, 54],
[2, 68],
[1.5, 90],
[1, 136],
[0.5, 255]
]
class MCP4151_0(object):
def __init__(self):
self.spi = spidev.SpiDev()
self.spi.open(0, 0)
self.spi.max_speed_hz = 976000
self.write_pot(12)
def scan_val(self, input):
i = 0
result = -1
elem_to_find = 2
while i < len(PotiValues):
if PotiValues[i][0] == input:
result = PotiValues[i][1]
i += 1
return result
def write_pot(self, input):
msb = input >> 8
lsb = input & 0xFF
self.spi.xfer([msb, lsb])
def write_range(self, input):
PotVal = self.scan_val(input)
if PotVal != -1:
self.write_pot(PotVal)