Skip to content

Commit d81f265

Browse files
committed
waitting for doc add
1 parent a3f0004 commit d81f265

File tree

10 files changed

+732
-524
lines changed

10 files changed

+732
-524
lines changed

docs/en/refs/unit.miniscale.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. |MINISCALE| image:: https://static-cdn.m5stack.com/resource/docs/products/unit/Unit-Mini%20Scales/img-b9cc6e44-bd3c-4256-bd72-5fb45423483c.webp
2+
:target: https://docs.m5stack.com/en/unit/Unit-Mini%20Scales
3+
:height: 200px
4+
:width: 200 px

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Unit
1919
relay.rst
2020
op90.rst
2121
op180.rst
22+
miniscale.rst

docs/en/units/miniscale.rst

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
Miniscale Unit
2+
==============
3+
4+
.. include:: ../refs/unit.adc.ref
5+
6+
The ``Miniscale`` class is designed for interfacing with a mini scale weight sensor, which includes a HX711 22-bit ADC. This sensor is capable of measuring weight and also includes additional functionalities like LED control and various filters.
7+
8+
Support the following products:
9+
10+
11+
|ADC|
12+
13+
14+
15+
Micropython Example::
16+
17+
import os, sys, io
18+
import M5
19+
from M5 import *
20+
import time
21+
from unit import MiniScaleUnit
22+
23+
i2c = I2C(0, scl=Pin(1), sda=Pin(2), freq=400000)
24+
scale = MiniScaleUnit(i2c)
25+
scale.setLed(255, 0, 0)
26+
print(miniscale.weight)
27+
28+
29+
UIFLOW2 Example:
30+
31+
|example.svg|
32+
33+
34+
.. only:: builder_html
35+
36+
37+
38+
Creating a Miniscale Object
39+
---------------------------
40+
41+
To create a new ``Miniscale`` object, initialize it with the following parameters:
42+
43+
.. code-block:: python
44+
45+
miniscale = unit.get(unit.MINISCALE, unit.PORTA)
46+
47+
Methods and Properties
48+
----------------------
49+
50+
adc (Property)
51+
^^^^^^^^^^^^^^
52+
53+
Returns the raw ADC value.
54+
55+
.. code-block:: python
56+
57+
adc_value = miniscale.adc
58+
59+
weight (Property)
60+
^^^^^^^^^^^^^^^^^
61+
62+
Returns the weight in grams.
63+
64+
.. code-block:: python
65+
66+
weight_value = miniscale.weight
67+
68+
button (Property)
69+
^^^^^^^^^^^^^^^^^
70+
71+
Returns the button state. True: pressing, False: Not pressing.
72+
73+
.. code-block:: python
74+
75+
button_state = miniscale.button
76+
77+
setLed
78+
^^^^^^
79+
80+
Sets the RGB LED color.
81+
82+
.. code-block:: python
83+
84+
miniscale.setLed(r, g, b)
85+
86+
- ``r``: Red value (0 - 255).
87+
- ``g``: Green value (0 - 255).
88+
- ``b``: Blue value (0 - 255).
89+
90+
Reset
91+
^^^^^
92+
93+
Resets sensor.
94+
95+
.. code-block:: python
96+
97+
miniscale.reset()
98+
99+
calibration
100+
^^^^^^^^^^^
101+
102+
Calibrates the MiniScale sensor.
103+
104+
.. code-block:: python
105+
106+
miniscale.calibration(weight1_g, weight1_adc, weight2_g, weight2_adc)
107+
108+
- ``weight1_g``: Weight1 in grams.
109+
- ``weight1_adc``: Weight1 in ADC value.
110+
- ``weight2_g``: Weight2 in grams.
111+
- ``weight2_adc``: Weight2 in ADC value.
112+
113+
calibration steps:
114+
115+
1. Reset sensor;
116+
2. Get adc, this is weight1_adc (should be zero). And weight1_g is also 0.
117+
3. Put some weight on it, then get adc, this is weight2_adc. And weight2_g is weight in gram you put on it.
118+
119+
setLowPassFilter
120+
^^^^^^^^^^^^^^^^
121+
122+
Enables or disables the low pass filter.
123+
124+
.. code-block:: python
125+
126+
miniscale.setLowPassFilter(enabled)
127+
128+
- ``enabled``: Boolean to enable or disable the filter.
129+
130+
getLowPassFilter
131+
^^^^^^^^^^^^^^^^
132+
133+
Returns the status of the low pass filter (enabled or not).
134+
135+
.. code-block:: python
136+
137+
filter_status = miniscale.getLowPassFilter()
138+
139+
setAverageFilterLevel
140+
^^^^^^^^^^^^^^^^^^^^^
141+
142+
Sets the level of the average filter.
143+
144+
.. code-block:: python
145+
146+
miniscale.setAverageFilterLevel(level)
147+
148+
- ``level``: Level of the average filter (0 - 50). Larger value for smoother result but more latency
149+
150+
getAverageFilterLevel
151+
^^^^^^^^^^^^^^^^^^^^^
152+
153+
Returns the level of the average filter.
154+
155+
.. code-block:: python
156+
157+
filter_level = miniscale.getAverageFilterLevel()
158+
159+
setEMAFilterAlpha
160+
^^^^^^^^^^^^^^^^^
161+
162+
Sets the alpha value for the EMA filter.
163+
164+
The EMA (Exponential Moving Average) filter is more sensitive to changes in data compared to the average filter.
165+
166+
.. code-block:: python
167+
168+
miniscale.setEMAFilterAlpha(alpha)
169+
170+
- ``alpha``: Alpha value for the EMA filter (0 - 99). Smaller value for smoother result but more latency
171+
172+
getEMAFilterAlpha
173+
^^^^^^^^^^^^^^^^^
174+
175+
Returns the alpha value for the EMA filter.
176+
177+
.. code-block:: python
178+
179+
alpha_value = miniscale.getEMAFilterAlpha()
180+
181+
Filter Compare
182+
^^^^^^^^^^^^^^
183+
184+
.. image:: C:\Users\tongy\OneDrive\work\M5\uiflow\image\filter.png

m5stack/libs/driver/mlx90640.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
class RefreshRate: # pylint: disable=too-few-public-methods
2929
"""Enum-like class for MLX90640's refresh rate"""
30+
3031
REFRESH_0_5_HZ = 0b000 # 0.5Hz
3132
REFRESH_1_HZ = 0b001 # 1Hz
3233
REFRESH_2_HZ = 0b010 # 2Hz
@@ -64,7 +65,7 @@ class MLX90640: # pylint: disable=too-many-instance-attributes
6465
cpOffset = [0] * 2
6566
ilChessC = [0] * 3
6667
brokenPixels = []
67-
outlierPixels = []
68+
outlierPixels = []
6869
cpKta = 0
6970
cpKv = 0
7071

@@ -172,9 +173,7 @@ def _GetVdd(self, frameData: List[int]) -> int:
172173
vdd -= 65536
173174

174175
resolutionRAM = (frameData[832] & 0x0C00) >> 10
175-
resolutionCorrection = math.pow(2, self.resolutionEE) / math.pow(
176-
2, resolutionRAM
177-
)
176+
resolutionCorrection = math.pow(2, self.resolutionEE) / math.pow(2, resolutionRAM)
178177
vdd = (resolutionCorrection * vdd - self.vdd25) / self.kVdd + 3.3
179178

180179
return vdd
@@ -224,15 +223,11 @@ def _CalculateTo(
224223
irDataCP[i] *= gain
225224

226225
irDataCP[0] -= (
227-
self.cpOffset[0]
228-
* (1 + self.cpKta * (ta - 25))
229-
* (1 + self.cpKv * (vdd - 3.3))
226+
self.cpOffset[0] * (1 + self.cpKta * (ta - 25)) * (1 + self.cpKv * (vdd - 3.3))
230227
)
231228
if mode == self.calibrationModeEE:
232229
irDataCP[1] -= (
233-
self.cpOffset[1]
234-
* (1 + self.cpKta * (ta - 25))
235-
* (1 + self.cpKv * (vdd - 3.3))
230+
self.cpOffset[1] * (1 + self.cpKta * (ta - 25)) * (1 + self.cpKv * (vdd - 3.3))
236231
)
237232
else:
238233
irDataCP[1] -= (
@@ -269,11 +264,7 @@ def _CalculateTo(
269264

270265
kta = self.kta[pixelNumber] / ktaScale
271266
kv = self.kv[pixelNumber] / kvScale
272-
irData -= (
273-
self.offset[pixelNumber]
274-
* (1 + kta * (ta - 25))
275-
* (1 + kv * (vdd - 3.3))
276-
)
267+
irData -= self.offset[pixelNumber] * (1 + kta * (ta - 25)) * (1 + kv * (vdd - 3.3))
277268

278269
if mode != self.calibrationModeEE:
279270
irData += (
@@ -298,9 +289,7 @@ def _CalculateTo(
298289
To = (
299290
math.sqrt(
300291
math.sqrt(
301-
irData
302-
/ (alphaCompensated * (1 - self.ksTo[1] * 273.15) + Sx)
303-
+ taTr
292+
irData / (alphaCompensated * (1 - self.ksTo[1] * 273.15) + Sx) + taTr
304293
)
305294
)
306295
- 273.15
@@ -524,9 +513,7 @@ def _ExtractAlphaParameters(self) -> None:
524513
alphaTemp[p] -= 64
525514
alphaTemp[p] *= 1 << accRemScale
526515
alphaTemp[p] += (
527-
alphaRef
528-
+ (accRow[i] << accRowScale)
529-
+ (accColumn[j] << accColumnScale)
516+
alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale)
530517
)
531518
alphaTemp[p] /= math.pow(2, alphaScale)
532519
alphaTemp[p] -= self.tgc * (self.cpAlpha[0] + self.cpAlpha[1]) / 2
@@ -593,13 +580,12 @@ def _ExtractOffsetParameters(self) -> None:
593580
self.offset[p] -= 64
594581
self.offset[p] *= 1 << occRemScale
595582
self.offset[p] += (
596-
offsetRef
597-
+ (occRow[i] << occRowScale)
598-
+ (occColumn[j] << occColumnScale)
583+
offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale)
599584
)
600585
del occRow
601586
del occColumn
602587
gc.collect()
588+
603589
def _ExtractKtaPixelParameters(self) -> None: # pylint: disable=too-many-locals
604590
# extract KtaPixel
605591
gc.collect()
@@ -743,11 +729,7 @@ def _ExtractDeviatingPixels(self) -> None:
743729
# pylint: disable=too-many-branches
744730
pixCnt = 0
745731

746-
while (
747-
(pixCnt < 768)
748-
and (len(self.brokenPixels) < 5)
749-
and (len(self.outlierPixels) < 5)
750-
):
732+
while (pixCnt < 768) and (len(self.brokenPixels) < 5) and (len(self.outlierPixels) < 5):
751733
if eeData[pixCnt + 64] == 0:
752734
self.brokenPixels.append(pixCnt)
753735
elif (eeData[pixCnt + 64] & 0x0001) != 0:
@@ -818,7 +800,12 @@ def _I2CWriteWord(self, writeAddress: int, data: int) -> None:
818800
# return -2
819801

820802
def _I2CReadWords(
821-
self, addr: int, buffer: Union[int, List[int]], *, end: Optional[int] = None, ) -> None:
803+
self,
804+
addr: int,
805+
buffer: Union[int, List[int]],
806+
*,
807+
end: Optional[int] = None,
808+
) -> None:
822809
if end is None:
823810
readwords = len(buffer)
824811
else:
@@ -834,10 +821,10 @@ def _I2CReadWords(
834821
time.sleep_ms(1)
835822
inbuf = memoryview(bytearray(2 * readwords))
836823
self.i2c_device.readfrom_into(self.i2c_addr, inbuf)
837-
outdata = list(struct.unpack('>' + 'H' * readwords, inbuf))
824+
outdata = list(struct.unpack(">" + "H" * readwords, inbuf))
838825
for i in range(0, len(outdata)):
839826
buffer[i] = outdata[i]
840827

841828
del inbuf
842829
del outdata
843-
gc.collect()
830+
gc.collect()

0 commit comments

Comments
 (0)