-
Notifications
You must be signed in to change notification settings - Fork 6
/
mcp3008.py
126 lines (109 loc) · 4.29 KB
/
mcp3008.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
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
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
RPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip,
as described in the datasheet.
https://www.adafruit.com/datasheets/MCP3008.pdf
Copyright (C) 2021 Luiz Eduardo Amaral <luizamaral306@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import spidev
# Modes Single
CH0 = 8 # single-ended CH0
CH1 = 9 # single-ended CH1
CH2 = 10 # single-ended CH2
CH3 = 11 # single-ended CH3
CH4 = 12 # single-ended CH4
CH5 = 13 # single-ended CH5
CH6 = 14 # single-ended CH6
CH7 = 15 # single-ended CH7
# Modes Diff
DF0 = 0 # differential CH0 = IN+ CH1 = IN-
DF1 = 1 # differential CH0 = IN- CH1 = IN+
DF2 = 2 # differential CH2 = IN+ CH3 = IN-
DF3 = 3 # differential CH2 = IN- CH3 = IN+
DF4 = 4 # differential CH4 = IN+ CH5 = IN-
DF5 = 5 # differential CH4 = IN- CH5 = IN+
DF6 = 6 # differential CH6 = IN+ CH7 = IN-
DF7 = 7 # differential CH6 = IN- CH7 = IN+
RESOLUTION = 1 << 10 # 10 bits resolution
class MCP3008(spidev.SpiDev):
'''
Object that listens the MCP3008 in the SPI port of the RPi.
Connects the object to the specified SPI device.
The initialization arguments are MCP3008(bus=0, device=0) where:
MCP3008(X, Y) will open /dev/spidev-X.Y, same as spidev.SpiDev.open(X, Y).
'''
def __init__(self, bus=0, device=0, max_speed_hz=976000):
self.bus = bus
self.device = device
self.open(self.bus, self.device)
self.modes = False
self.max_speed_hz = max_speed_hz
def __del__(self):
self.close()
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.close()
def __repr__(self):
return 'MCP3008 object at bus {0}, device {1}'.format(self.bus, self.device)
def __call__(self, norm=False):
return self.read(self.modes, norm)
@classmethod
def fixed(cls, modes, bus=0, device=0):
'''
Initializes the class with fixed modes, which turns the instance callable.
The modes argument is a list with the modes of operation to be read (e.g.
[mcp3008.CH0,mcp3008.Df0]).
When calling the instance the object will execute a reading of and return the
values (e.g. print instance()).
When calling the instance, you can pass the optional argument norm to
normalize
the data (e.g. print instance(5.2)).
'''
instance = cls(bus, device)
instance.modes = modes
return instance
def _read_single(self, mode):
'''
Returns the value of a single mode reading
'''
if not 0 <= mode <= 15:
raise IndexError('Outside the channels scope, please use: 0, 1 ..., 7')
request = [0x1, mode << 4, 0x0] # [start bit, configuration, listen space]
_, byte1, byte2 = self.xfer2(request)
value = (byte1%4 << 8) + byte2
return value
def read(self, modes, norm=False):
'''
Returns the raw value (0 ... 1024) of the reading.
The modes argument is a list with the modes of operation to be read (e.g.
[mcp3008.CH0,mcp3008.Df0]).
norm is a normalization factor, usually Vref.
'''
reading = []
for mode in modes:
reading.append(self._read_single(mode))
if norm:
return [float(norm)*value/RESOLUTION for value in reading]
else:
return reading
def read_all(self, norm=False):
'''
Returns a list with the readings of all the modes
Data Order:
[DF0, DF1, DF2, DF3, DF4, DF5, DF6, DF7,
CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7]
norm is a normalization factor, usually Vref.
'''
return self.read(range(16), norm)