1
1
# -*- encoding: utf-8 -*-
2
- '''
2
+ """
3
3
@File : _miniscale.py
4
4
@Time : 2023/12/19
5
5
@Author : TONG YIHAN
6
6
7
7
@License : (C)Copyright 2015-2023, M5STACK
8
8
@Desc : This sensor is capable of measuring weight and also includes additional functionalities like LED control and various filters.
9
- '''
9
+ """
10
10
11
11
# Import necessary libraries
12
12
from machine import I2C
13
13
from .pahub import PAHUBUnit
14
14
from .unit_helper import UnitError
15
- import time
16
15
import struct
17
- try :
16
+ import sys
17
+
18
+ if sys .platform != "esp32" :
18
19
from typing import Union
19
- except ImportError :
20
- pass
21
-
22
- class Miniscale :
23
- """! MiniScale is a weight sensor, includes a hx711 22bit ADC.
24
-
25
- """
26
- def __init__ (self , i2c : Union [I2C , PAHUBUnit ], addr = 0x26 ):
20
+
21
+
22
+ class MiniScaleUnit :
23
+ """! MiniScale is a weight sensor, includes a hx711 22bit ADC."""
24
+
25
+ def __init__ (self , i2c : Union [I2C , PAHUBUnit ], addr = 0x26 ):
27
26
self .i2c = i2c
28
27
self .addr = addr
29
28
self ._available ()
30
29
31
30
def _available (self ):
32
31
"""! Check if sensor is available on the I2C bus.
33
-
32
+
34
33
Raises:
35
34
UnitError: If the sensor is not found.
36
35
"""
37
36
if not (self .i2c_addr in self .i2c .scan ()):
38
37
raise UnitError ("MiniScale Unit not found." )
39
-
38
+
40
39
@property
41
40
def adc (self ) -> int :
42
41
"""! Get the ADC value."""
43
42
data = self .i2c .readfrom_mem (self .addr , 0x00 , 4 )
44
- return struct .unpack ('<I' , data )[0 ]
45
-
43
+ return struct .unpack ("<I" , data )[0 ]
44
+
46
45
@property
47
46
def weight (self ) -> float :
48
47
"""! Get the weight in grams."""
49
48
data = self .i2c .readfrom_mem (self .addr , 0x10 , 4 )
50
- return struct .unpack ('<f' , data )[0 ]
51
-
49
+ return struct .unpack ("<f" , data )[0 ]
50
+
52
51
@property
53
52
def button (self ) -> bool :
54
53
"""! Get the button state."""
55
54
data = self .i2c .readfrom_mem (self .addr , 0x20 , 1 )
56
- return struct .unpack ('b' , data )[0 ] == 0
57
-
55
+ return struct .unpack ("b" , data )[0 ] == 0
56
+
58
57
def setLed (self , r : int , g : int , b : int ):
59
58
"""! Set the RGB LED color.
60
59
@@ -65,10 +64,8 @@ def setLed(self, r: int, g: int, b: int):
65
64
self .i2c .writeto_mem (self .addr , 0x30 , bytes ([r , g , b ]))
66
65
67
66
def reset (self ):
68
- """! Reset weight value to zero
69
-
70
- """
71
- self .i2c .writeto_mem (self .addr , 0x50 , b'\x01 ' )
67
+ """! Reset weight value to zero"""
68
+ self .i2c .writeto_mem (self .addr , 0x50 , b"\x01 " )
72
69
73
70
def calibration (self , weight1_g : float , weight1_adc : int , weight2_g : float , weight2_adc : int ):
74
71
"""! Calibration the MiniScale sensor.
@@ -78,63 +75,57 @@ def calibration(self, weight1_g: float, weight1_adc: int, weight2_g: float, weig
78
75
(3) step 3: Put 100g weight on it, and get RawADC, this is RawADC_100g
79
76
(4) step 4: Calculate the value of GAP = (RawADC_100g-RawADC0g) / 100
80
77
(5) step 5: Write GAP value to the unit Via I2C
81
-
78
+
82
79
@param weight1_g Weight1 in grams.
83
80
@param weight1_adc Weight1 in ADC.
84
81
@param weight2_g Weight2 in grams.
85
82
@param weight2_adc Weight2 in ADC.
86
83
"""
87
-
84
+
88
85
gap = (weight2_adc - weight1_adc ) / (weight2_g - weight1_g )
89
- self .i2c .writeto_mem (self .addr , 0x40 , struct .pack ('<f' , gap ))
86
+ self .i2c .writeto_mem (self .addr , 0x40 , struct .pack ("<f" , gap ))
90
87
91
88
def setLowPassFilter (self , enabled : bool ):
92
89
"""! Set low pass filter enabled or not
93
-
90
+
94
91
@param enabled Enable filter or not
95
92
"""
96
93
if enabled :
97
- self .i2c .writeto_mem (self .addr , 0x80 , b' \x01 ' )
94
+ self .i2c .writeto_mem (self .addr , 0x80 , b" \x01 " )
98
95
else :
99
- self .i2c .writeto_mem (self .addr , 0x80 , b' \x00 ' )
96
+ self .i2c .writeto_mem (self .addr , 0x80 , b" \x00 " )
100
97
101
98
def getLowPassFilter (self ) -> bool :
102
- """! Get low pass filter enabled or not
103
-
104
- """
99
+ """! Get low pass filter enabled or not"""
105
100
data = self .i2c .readfrom_mem (self .addr , 0x80 , 1 )
106
- return data == b' \x01 '
101
+ return data == b" \x01 "
107
102
108
103
def setAverageFilterLevel (self , level : int ):
109
104
"""! Set average filter level
110
-
105
+
111
106
@param level level of average filter, larger value for smoother result but more latency
112
107
"""
113
108
if level > 50 or level < 0 :
114
109
raise Exception ("Level for average filter must between 0 ~ 50" )
115
110
116
- self .i2c .writeto_mem (self .addr , 0x81 , struct .pack ('b' , level ))
117
-
111
+ self .i2c .writeto_mem (self .addr , 0x81 , struct .pack ("b" , level ))
112
+
118
113
def getAverageFilterLevel (self ) -> int :
119
- """! Get average filter level
120
-
121
- """
114
+ """! Get average filter level"""
122
115
data = self .i2c .readfrom_mem (self .addr , 0x81 , 1 )
123
- return struct .unpack ('b' , data )[0 ]
116
+ return struct .unpack ("b" , data )[0 ]
124
117
125
118
def setEMAFilterAlpha (self , alpha : int ):
126
119
"""! Set ema filter alpha
127
-
120
+
128
121
@param alpha alpha of ema filter, smaller value for smoother result but more latency
129
122
"""
130
123
if alpha > 99 or alpha < 0 :
131
124
raise Exception ("Alpha for EMA filter must between 0 ~ 99" )
132
125
133
- self .i2c .writeto_mem (self .addr , 0x82 , struct .pack ('b' , alpha ))
126
+ self .i2c .writeto_mem (self .addr , 0x82 , struct .pack ("b" , alpha ))
134
127
135
128
def getEMAFilterAlpha (self ) -> int :
136
- """! Get ema filter alpha
137
-
138
- """
129
+ """! Get ema filter alpha"""
139
130
data = self .i2c .readfrom_mem (self .addr , 0x82 , 1 )
140
- return struct .unpack ('b' , data )[0 ]
131
+ return struct .unpack ("b" , data )[0 ]
0 commit comments