-
Notifications
You must be signed in to change notification settings - Fork 0
/
hx710_spi.py
124 lines (96 loc) · 4 KB
/
hx710_spi.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
# MIT License
# Copyright (c) 2024
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from machine import idle
import time
class HX710:
def __init__(self, clock, data, spi, mode=1):
self.clock = clock
self.data = data
self.spi = spi
self.clock(0)
self.clock_25 = b'\xaa\xaa\xaa\xaa\xaa\xaa\x80'
self.clock_26 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa0'
self.clock_27 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa8'
self.clock = self.clock_25
self.lookup = (b'\x00\x01\x00\x00\x02\x03\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x04\x05\x00\x00\x06\x07\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x08\x09\x00\x00\x0a\x0b\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x0d\x00\x00'
b'\x0e\x0f')
self.in_data = bytearray(7)
self.OFFSET = 0
self.SCALE = 1
self.time_constant = 0.1
self.filtered = 0
self.set_gain(gain)
def set_mode(self, mode):
if mode == 1:
self.clock = self.clock_25
elif mode == 2:
self.clock = self.clock_26
elif mode == 3:
self.clock = self.clock_27
self.read()
self.filtered = self.read()
def read(self):
# wait for the device to get ready
for _ in range(500):
if self.data() == 0:
break
time.sleep_ms(1)
else:
raise OSError("Sensor does not respond")
# get the data and set channel and gain
self.spi.write_readinto(self.clock, self.in_data)
# pack the data into a single value
result = 0
for _ in range(6):
result = (result << 4) + self.lookup[self.in_data[_] & 0x55]
# return sign corrected result
return result - ((result & 0x800000) << 1)
def read_average(self, times=3):
sum = 0
for i in range(times):
sum += self.read()
return sum / times
def read_lowpass(self):
self.filtered += self.time_constant * (self.read() - self.filtered)
return self.filtered
def get_value(self):
return self.read_lowpass() - self.OFFSET
def get_units(self):
return self.get_value() / self.SCALE
def tare(self, times=15):
self.set_offset(self.read_average(times))
def set_scale(self, scale):
self.SCALE = scale
def set_offset(self, offset):
self.OFFSET = offset
def set_time_constant(self, time_constant=None):
if time_constant is None:
return self.time_constant
elif 0 < time_constant < 1.0:
self.time_constant = time_constant
def power_down(self):
self.clock.value(False)
self.clock.value(True)
def power_up(self):
self.clock.value(False)