Skip to content

Commit b47bd36

Browse files
committed
libs/units: Merge branch 'develop-pandian' into develop.
Signed-off-by: lbuque <[email protected]>
2 parents 34ea43f + 49c0439 commit b47bd36

File tree

7 files changed

+470
-0
lines changed

7 files changed

+470
-0
lines changed

docs/en/refs/unit.weight_i2c.ref

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. |WEIGHT I2C| image:: https://static-cdn.m5stack.com/resource/docs/products/unit/Unit-Weight%20I2C/img-0634712b-2a27-48cf-894f-d6708a771d47.webp
2+
:target: https://docs.m5stack.com/en/unit/Unit-Weight%20I2C
3+
:height: 200px
4+
:width: 200 px
5+
6+
.. |init_i2c_address.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/init.svg
7+
8+
.. |get_adc_raw.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_adc_raw.svg
9+
10+
.. |get_weight_float.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_weight_float.svg
11+
12+
.. |get_weight_int.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_weight_int.svg
13+
14+
.. |get_weight_str.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_weight_str.svg
15+
16+
.. |set_reset_offset.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_reset_offset.svg
17+
18+
.. |set_calibration.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_calibration.svg
19+
20+
.. |set_lowpass_filter.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_lowpass_filter.svg
21+
22+
.. |get_lowpass_filter.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_lowpass_filter.svg
23+
24+
.. |set_average_filter_level.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_average_filter_level.svg
25+
26+
.. |get_average_filter_level.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_average_filter_level.svg
27+
28+
.. |set_ema_filter_alpha.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_ema_filter_alpha.svg
29+
30+
.. |get_ema_filter_alpha.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_ema_filter_alpha.svg
31+
32+
.. |set_i2c_address.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/set_i2c_address.svg
33+
34+
.. |get_device_spec.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/get_device_spec.svg
35+
36+
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/weight_i2c/example.svg

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Unit
2222
miniscale.rst
2323
dac2.rst
2424
lora_e220.rst
25+
weight_i2c.rst

docs/en/units/weight_i2c.rst

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
Weight I2C Unit
2+
===============
3+
4+
.. include:: ../refs/unit.weight_i2c.ref
5+
6+
The ``Weight I2C`` Unit is a weighing acquisition transmitter unit that adopts the "STM32+HX711 chip" solution to achieve 24-bit precision weight measurement through I2C communication and does not include a load cell sensor. This unit is capable of measuring weight and also includes various filters.
7+
8+
Support the following products:
9+
10+
11+
|WEIGHT I2C|
12+
13+
14+
15+
Micropython Example::
16+
17+
import os, sys, io
18+
import M5
19+
from M5 import *
20+
from hardware import *
21+
from unit import WEIGHT_I2CUnit
22+
import time
23+
24+
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
25+
weight_i2c0 = WEIGHT_I2CUnit(i2c0)
26+
print(weight_i2c_0.get_adc_raw)
27+
print(weight_i2c_0.get_weight_float)
28+
time.sleep_ms(100)
29+
30+
31+
UIFLOW2 Example:
32+
33+
|example.svg|
34+
35+
36+
.. only:: builder_html
37+
38+
39+
class WEIGHT_I2CUnit
40+
--------------------
41+
42+
Constructors
43+
---------------------------
44+
45+
.. class:: WEIGHT_I2CUnit(i2c0, 0x26)
46+
47+
Create an WEIGHT_I2CUnit object.
48+
49+
- ``I2C0`` is I2C Port.
50+
- ``0x26`` is default I2C address
51+
52+
53+
UIFLOW2:
54+
55+
|init_i2c_address.svg|
56+
57+
58+
Methods
59+
----------------------
60+
61+
.. method:: WEIGHT_I2CUnit.get_adc_raw
62+
63+
64+
Gets the raw adc value.
65+
66+
UIFLOW2:
67+
68+
|get_adc_raw.svg|
69+
70+
.. method:: WEIGHT_I2CUnit.get_weight_float
71+
72+
73+
Gets the weight in grams float value.
74+
75+
UIFLOW2:
76+
77+
|get_weight_float.svg|
78+
79+
.. method:: WEIGHT_I2CUnit.get_weight_int
80+
81+
82+
Gets the weight in grams int value.
83+
84+
UIFLOW2:
85+
86+
|get_weight_int.svg|
87+
88+
.. method:: WEIGHT_I2CUnit.get_weight_str
89+
90+
91+
Gets the weight in grams string value.
92+
93+
UIFLOW2:
94+
95+
|get_weight_str.svg|
96+
97+
.. method:: WEIGHT_I2CUnit.set_reset_offset()
98+
99+
Reset the offset value(Tare).
100+
101+
102+
UIFLOW2:
103+
104+
|set_reset_offset.svg|
105+
106+
.. method:: WEIGHT_I2CUnit.set_calibration(weight1_g, weight1_adc, weight2_g, weight2_adc)
107+
108+
109+
Calibrates the Load sensor.
110+
111+
- ``weight1_g``: Weight1 in grams.
112+
- ``weight1_adc``: Weight1 in ADC value.
113+
- ``weight2_g``: Weight2 in grams.
114+
- ``weight2_adc``: Weight2 in ADC value.
115+
116+
calibration steps:
117+
118+
1.Reset offset(Tare).
119+
2.Get the raw ADC value at no-load weight, this is the Raw ADC of zero weight in 0g.
120+
3.Put some weight on it, then get adc, this is the load weight adc value and the gram weight you put on it.
121+
122+
123+
UIFLOW2:
124+
125+
|set_calibration.svg|
126+
127+
.. method:: WEIGHT_I2CUnit.set_lowpass_filter(Enable)
128+
129+
Enable or disable the low pass filter.
130+
131+
132+
UIFLOW2:
133+
134+
|set_lowpass_filter.svg|
135+
136+
137+
.. method:: WEIGHT_I2CUnit.get_lowpass_filter
138+
139+
Returns the status of the low pass filter (enable or disable).
140+
141+
142+
UIFLOW2:
143+
144+
|get_lowpass_filter.svg|
145+
146+
.. method:: WEIGHT_I2CUnit.set_average_filter_level(level)
147+
148+
Sets the level of the average filter.
149+
150+
- ``level``: Level of the average filter (0 - 50). Larger value for smoother result but more latency
151+
152+
UIFLOW2:
153+
154+
|set_average_filter_level.svg|
155+
156+
.. method:: WEIGHT_I2CUnit.get_average_filter_level
157+
158+
Returns the level of the average filter.
159+
160+
UIFLOW2:
161+
162+
|get_average_filter_level.svg|
163+
164+
.. method:: WEIGHT_I2CUnit.set_ema_filter_alpha(alpha)
165+
166+
Sets the alpha value for the EMA filter.
167+
168+
The EMA (Exponential Moving Average) filter is more sensitive to changes in data compared to the average filter.
169+
170+
- ``alpha``: Alpha value for the EMA filter (0 - 99). Smaller value for smoother result but more latency
171+
172+
UIFLOW2:
173+
174+
|set_ema_filter_alpha.svg|
175+
176+
.. method:: WEIGHT_I2CUnit.get_ema_filter_alpha
177+
178+
Returns the alpha value for the EMA filter.
179+
180+
UIFLOW2:
181+
182+
|get_ema_filter_alpha.svg|
183+
184+
.. method:: WEIGHT_I2CUnit.set_i2c_address(address)
185+
186+
The i2c address can be changed by the user and this address should be between 0x01 and 0x7F.
187+
188+
- ``address``: range of address(0x01 - 0x7F).
189+
190+
UIFLOW2:
191+
192+
|set_i2c_address.svg|
193+
194+
.. method:: WEIGHT_I2CUnit.get_device_spec(info)
195+
196+
Get the firmware version details and I2c address of this device.
197+
198+
- ``info``: (0xFE, 0xFF)
199+
200+
UIFLOW2:
201+
202+
|get_device_spec.svg|

m5stack/libs/unit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
"RTC8563Unit": "rtc8563",
4949
"VMeterUnit": "vmeter",
5050
"AMeterUnit": "ameter",
51+
"WEIGHT_I2CUnit": "weight_i2c",
52+
"KMETER_ISOUnit": "kmeter_iso",
5153
}
5254

5355

m5stack/libs/unit/kmeter_iso.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from machine import I2C
2+
from .pahub import PAHUBUnit
3+
from .unit_helper import UnitError
4+
import struct
5+
import sys
6+
7+
if sys.platform != "esp32":
8+
from typing import Union
9+
10+
import time
11+
import struct
12+
13+
KMETER_ADDR = 0x66
14+
15+
TEMP_CELSIUS_REG = 0x00
16+
TEMP_FAHREN_REG = 0x04
17+
TEMP_INT_CELSIUS_REG = 0x10
18+
TEMP_INT_FAHREN_REG = 0x14
19+
TEMP_READY_REG = 0x20
20+
TEMP_STR_CELSIUS_REG = 0x30
21+
TEMP_STR_FAHREN_REG = 0x40
22+
TEMP_INTSTR_CELSIUS_REG = 0x50
23+
TEMP_INTSTR_FAHREN_REG = 0x60
24+
FIRM_VER_REG = 0xFE
25+
I2C_ADDR_REG = 0xFF
26+
27+
28+
class KMETER_ISOUnit:
29+
def __init__(self, i2c: Union[I2C, PAHUBUnit], addr=KMETER_ADDR):
30+
"""! Kmeter Initialize Function
31+
addr: 1 ~ 127
32+
"""
33+
self.kmeter_i2c = i2c
34+
if addr >= 0x01 and addr <= 0x7F:
35+
self.unit_addr = addr
36+
if not (self.unit_addr in self.kmeter_i2c.scan()):
37+
raise UnitError("Kmeter iso unit maybe not connect")
38+
39+
def get_kmeter_thermo(self, temp=0):
40+
"""! get thermocouple temperature value."""
41+
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_CELSIUS_REG + (4*temp), 4)
42+
return round((self.int_convert(buff)/100), 2)
43+
44+
def get_kmeter_internal(self, temp=0):
45+
"""! get internal temperature value."""
46+
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_INT_CELSIUS_REG + (4*temp), 4)
47+
return round(self.int_convert(buff)/100, 2)
48+
49+
@property
50+
def get_data_available_status(self):
51+
"""! get temperature measurement value is ready? """
52+
status = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_READY_REG, 1)[0]
53+
return False if status else True
54+
55+
def get_kmeter_thermo_string(self, temp=0):
56+
"""! get thermocouple temperature string value."""
57+
return self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_STR_CELSIUS_REG + (temp*0x10), 1).decode() + str(float(self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_STR_CELSIUS_REG + (temp*0x10), 8).decode()))
58+
59+
def get_kmeter_internal_string(self, temp=0):
60+
"""! get internal temperature string value."""
61+
return self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (temp*0x10), 1).decode()+str(float(self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (temp*0x10), 8).decode()))
62+
63+
def get_device_spec(self, mode) -> int:
64+
"""! Get device firmware version and i2c address.
65+
mode: 0xFE and 0xFF
66+
"""
67+
if mode >= FIRM_VER_REG and mode <= I2C_ADDR_REG:
68+
return self.kmeter_i2c.readfrom_mem(self.unit_addr, mode, 1)[0]
69+
70+
def set_i2c_address(self, addr) -> None:
71+
"""! Set i2c address.
72+
addr: 1 to 127
73+
"""
74+
if addr >= 0x01 and addr <= 0x7F:
75+
if addr != self.unit_addr:
76+
self.kmeter_i2c.writeto_mem(self.unit_addr, I2C_ADDR_REG, struct.pack('b', addr))
77+
self.unit_addr = addr
78+
time.sleep_ms(200)
79+
80+
def int_convert(self, value):
81+
return struct.unpack('<i', value)[0]
82+

m5stack/libs/unit/manifest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"rtc8563.py",
5151
"vmeter.py",
5252
"ameter.py",
53+
"weight_i2c.py",
54+
"kmeter_iso.py",
5355
),
5456
base_path="..",
5557
opt=0,

0 commit comments

Comments
 (0)