Skip to content

Commit c152304

Browse files
pandiannanolbuque
authored andcommitted
libs/unit: Code correction and add docs.
Signed-off-by: Pandian Nano <[email protected]>
1 parent 1cf6228 commit c152304

File tree

7 files changed

+278
-48
lines changed

7 files changed

+278
-48
lines changed

docs/en/refs/unit.kmeter_iso.ref

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. |KmeterISOUnit| image:: https://static-cdn.m5stack.com/resource/docs/products/unit/KMeterISO%20Unit/img-636d2fa0-a416-48b9-a667-57defaa0291e.webp
2+
:target: https://docs.m5stack.com/en/unit/KMeterISO%20Unit
3+
:height: 200px
4+
:width: 200px
5+
6+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/example.png
7+
8+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/init.png
9+
10+
.. |get_thermocouple_temperature.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_kmeter_thermo.png
11+
12+
.. |get_internal_temperature.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_kmeter_internal.png
13+
14+
.. |is_ready.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_data_available_status.png
15+
16+
.. |get_thermocouple_temperature_string.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_kmeter_thermo_string.png
17+
18+
.. |get_internal_temperature_string.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_kmeter_internal_string.png
19+
20+
.. |get_device_spec.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/get_device_spec.png
21+
22+
.. |set_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/kmeteriso/set_i2c_address.png
23+
24+
.. |cores3_kmeteriso_example.m5f2| raw:: html
25+
26+
<a href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/kmeteriso/cores3_kmeteriso_example.m5f2" target="_blank">cores3_kmeteriso_example.m5f2</a>

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Unit
3939
joystick.rst
4040
joystick2.rst
4141
key.rst
42+
kmeter_iso.rst
4243
kmeter.rst
4344
lcd.rst
4445
light.rst

docs/en/units/kmeter_iso.rst

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
Kmeter ISO Unit
2+
===============
3+
4+
.. include:: ../refs/unit.kmeter_iso.ref
5+
6+
7+
Supported Products:
8+
9+
10+
|KmeterISOUnit|
11+
12+
13+
Micropython Example::
14+
15+
import os, sys, io
16+
import M5
17+
from M5 import *
18+
from hardware import *
19+
from unit import KMeterISOUnit
20+
import time
21+
22+
23+
M5.begin()
24+
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
25+
kmeter_iso_0 = KMeterISOUnit(i2c0, 0x66)
26+
while True:
27+
if kmeteriso_0.is_ready():
28+
print(kmeteriso_0.get_thermocouple_temperature(0))
29+
print(kmeteriso_0.get_internal_temperature(0))
30+
time.sleep_ms(250)
31+
32+
33+
UIFLOW2 Example:
34+
35+
|example.png|
36+
37+
38+
.. only:: builder_html
39+
40+
|cores3_kmeteriso_example.m5f2|
41+
42+
43+
class KmeterISOUnit
44+
-------------------
45+
46+
Constructors
47+
------------
48+
49+
.. class:: KmeterISOUnit(i2c, address=0x66)
50+
51+
:param object i2c: the I2C object.
52+
:param int address: 0x08 ~ 0x77.
53+
54+
UIFLOW2:
55+
56+
|init.png|
57+
58+
59+
Methods
60+
-------
61+
62+
.. method:: KmeterISOUnit.get_thermocouple_temperature(scale=0) -> float
63+
64+
Get the temperature of the thermocouple in the KmeterISO Unit. Returns a float value.
65+
66+
``scale`` accepts values of :py:data:`KmeterISO.CELSIUS` or :py:data:`KmeterISO.FAHRENHEIT`.
67+
68+
UIFLOW2:
69+
70+
|get_thermocouple_temperature.png|
71+
72+
73+
.. method:: KmeterISOUnit.get_internal_temperature(scale=0) -> float
74+
75+
Get the internal temperature of the KmeterISO Unit. Returns a float value.
76+
77+
``scale`` accepts values of :py:data:`KmeterISO.CELSIUS` or :py:data:`KmeterISO.FAHRENHEIT`.
78+
79+
UIFLOW2:
80+
81+
|get_internal_temperature.png|
82+
83+
84+
.. method:: KmeterISOUnit.is_ready() -> bool
85+
86+
Check if the measurement result is ready.
87+
88+
UIFLOW2:
89+
90+
|is_ready.png|
91+
92+
93+
.. method:: KmeterISOUnit.get_thermocouple_temperature_string(scale=0) -> str
94+
95+
Get the temperature of the thermocouple in the KmeterISO Unit as a string with a sign.
96+
97+
``scale`` accepts values of :py:data:`KmeterISO.CELSIUS` or :py:data:`KmeterISO.FAHRENHEIT`.
98+
99+
UIFLOW2:
100+
101+
|get_thermocouple_temperature_string.png|
102+
103+
104+
.. method:: KmeterISOUnit.get_internal_temperature_string(scale=0) -> str
105+
106+
Get the internal temperature of the KmeterISO Unit as a string with a sign.
107+
108+
``scale`` accepts values of :py:data:`KmeterISO.CELSIUS` or :py:data:`KmeterISO.FAHRENHEIT`.
109+
110+
UIFLOW2:
111+
112+
|get_internal_temperature_string.png|
113+
114+
115+
.. method:: KmeterISOUnit.get_device_spec(mode) -> int
116+
117+
Get the firmware version of the KmeterISO Unit. Returns an integer version number.
118+
119+
:param int mode:
120+
121+
==== ==========
122+
int mode
123+
0xFE firmware version
124+
0xFF i2c address
125+
==== ==========
126+
127+
UIFLOW2:
128+
129+
|get_device_spec.png|
130+
131+
132+
.. method:: KmeterISOUnit.set_i2c_address(address) -> int
133+
134+
Set the i2c address can be changed by the user and this address should be between 0x08 and 0x77.
135+
136+
UIFLOW2:
137+
138+
|set_i2c_address.png|
139+
140+
141+
Constants
142+
---------
143+
144+
.. data:: KmeterISOUnit.CELSIUS
145+
:type: int
146+
147+
Celsius scale.
148+
149+
150+
.. data:: KmeterISOUnit.FAHRENHEIT
151+
:type: int
152+
153+
Fahrenheit scale.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.1.3","type":"cores3","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__cores3_screen","createTime":1726021027986,"x":0,"y":0,"width":320,"height":240,"backgroundColor":"#222222","size":0,"isSelected":true}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","touch","als","mic","i2c"]},{"unit":["unit_kmeteriso"]}],"units":[{"type":"unit_kmeteriso","name":"kmeteriso_0","portList":["A","PAHUB","Custom"],"portType":"A","userPort":[22,21],"id":"sQFu3BbQ6qv`abY6","createTime":1726021043442,"bus":"i2c0","pahubPortList":[0,1,2,3,4,5],"pahubPort":0,"initBlockId":"*QLj7tTTt;H/-{TYd^.e"}],"hats":[],"bases":[],"i2cs":[{"id":"i2c0","portType":"A","userPort":[22,21],"freq":"100000","blockId":"y71jIN++@lrYS0TuLd{y"}],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"-110\" y=\"-190\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"i2c_init\" id=\"y71jIN++@lrYS0TuLd{y\"><field name=\"NAME\">0</field><field name=\"FREQ\">100000</field><value name=\"SCL\"><shadow type=\"math_number\" id=\"DCQ;Ko-v{=OaS=!JqQcW\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value><value name=\"SDA\"><shadow type=\"math_number\" id=\"M(n{x(V)aaqkK}SSC+a8\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">2</field></shadow></value><next><block type=\"unit_kmeteriso_init\" id=\"*QLj7tTTt;H/-{TYd^.e\"><field name=\"NAME\">kmeteriso_0</field><value name=\"ADDR\"><shadow type=\"math_number\" id=\"Z_C0/O~=[^|qMty!H#7.\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0x66</field></shadow></value></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"-110\" y=\"10\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"controls_if\" id=\"wsVxfndjUMz]X4r8yla_\"><value name=\"IF0\"><block type=\"unit_kmeteriso_get_data_available_status\" id=\"^/A*k(pGcpS9qja*9S+T\"><field name=\"NAME\">kmeteriso_0</field></block></value><statement name=\"DO0\"><block type=\"text_print\" id=\":g#bl~:QJo07}7,NU78x\"><value name=\"TEXT\"><shadow type=\"text\" id=\"tI%5~8x^oR6gnD!=W=Dg\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"unit_kmeteriso_get_thermo\" id=\"/=z6s!^RqFY(.#Gq2|A-\"><field name=\"NAME\">kmeteriso_0</field><field name=\"OPTION\">0</field></block></value><next><block type=\"text_print\" id=\"WM#xtwocnx5ZpSu!Q.dI\"><value name=\"TEXT\"><shadow type=\"text\" id=\"tI%5~8x^oR6gnD!=W=Dg\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"unit_kmeteriso_get_internal\" id=\"0=OF7O2a:1f=VZT4^pIy\"><field name=\"NAME\">kmeteriso_0</field><field name=\"OPTION\">0</field></block></value></block></next></block></statement><next><block type=\"time_sleep_millisecond\" id=\"HoZNLQ#5*A$I`:6kQst}\"><value name=\"MS\"><shadow type=\"math_number\" id=\"NLYRJhri;1YrD2:AD*w0\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">250</field></shadow></value><next><block type=\"system_m5_update\" id=\"system_m5_update\"></block></next></block></next></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":320,"height":240,"scale":0.78,"screenName":"","blockId":"","screenColorType":0,"id":"builtin","createTime":1726021027981}],"logicWhenNum":0,"customList":[]}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
6+
import os, sys, io
7+
import M5
8+
from M5 import *
9+
from hardware import *
10+
from unit import KMeterISOUnit
11+
import time
12+
13+
14+
i2c0 = None
15+
kmeteriso_0 = None
16+
17+
18+
def setup() -> None:
19+
global i2c0, kmeteriso_0
20+
21+
M5.begin()
22+
Widgets.fillScreen(0x222222)
23+
24+
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
25+
kmeteriso_0 = KMeterISOUnit(i2c0, 0x66)
26+
27+
28+
def loop() -> None:
29+
global i2c0, kmeteriso_0
30+
if kmeteriso_0.is_ready():
31+
print(kmeteriso_0.get_thermocouple_temperature(0))
32+
print(kmeteriso_0.get_internal_temperature(0))
33+
time.sleep_ms(250)
34+
M5.update()
35+
36+
37+
if __name__ == "__main__":
38+
try:
39+
setup()
40+
while True:
41+
loop()
42+
except (Exception, KeyboardInterrupt) as e:
43+
try:
44+
from utility import print_error_msg
45+
46+
print_error_msg(e)
47+
except ImportError:
48+
print("please update to latest firmware")

m5stack/libs/unit/kmeter_iso.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,72 @@
2323

2424

2525
class KMeterISOUnit:
26+
CELSIUS = 0
27+
FAHRENHEIT = 1
28+
2629
def __init__(self, i2c: I2C | PAHUBUnit, address: int | list | tuple = KMETER_ADDR):
27-
"""! Kmeter Initialize Function
28-
addr: 1 ~ 127
29-
"""
30+
#! Kmeter Initialize Function
31+
#! @param address: 0x08 ~ 0x77
3032
self.kmeter_i2c = i2c
31-
if address >= 0x01 and address <= 0x7F:
33+
if address >= 0x08 and address <= 0x77:
3234
self.unit_addr = address
3335
if self.unit_addr not in self.kmeter_i2c.scan():
3436
raise UnitError("Kmeter iso unit maybe not connect")
3537

36-
def get_kmeter_thermo(self, temp=0):
37-
"""! get thermocouple temperature value."""
38-
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_CELSIUS_REG + (4 * temp), 4)
38+
def get_thermocouple_temperature(self, scale=CELSIUS) -> float:
39+
#! get thermocouple temperature value.
40+
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_CELSIUS_REG + (4 * scale), 4)
3941
return round((self.int_convert(buff) / 100), 2)
4042

41-
def get_kmeter_internal(self, temp=0):
42-
"""! get internal temperature value."""
43-
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_INT_CELSIUS_REG + (4 * temp), 4)
43+
def get_internal_temperature(self, scale=CELSIUS) -> float:
44+
#! get internal temperature value.
45+
buff = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_INT_CELSIUS_REG + (4 * scale), 4)
4446
return round(self.int_convert(buff) / 100, 2)
4547

46-
@property
47-
def get_data_available_status(self):
48-
"""! get temperature measurement value is ready?"""
48+
def is_ready(self) -> bool:
49+
#! get temperature measurement value is ready?
4950
status = self.kmeter_i2c.readfrom_mem(self.unit_addr, TEMP_READY_REG, 1)[0]
5051
return False if status else True
5152

52-
def get_kmeter_thermo_string(self, temp=0):
53-
"""! get thermocouple temperature string value."""
53+
def get_thermocouple_temperature_string(self, scale=CELSIUS) -> str:
54+
#! get thermocouple temperature string value.
5455
return self.kmeter_i2c.readfrom_mem(
55-
self.unit_addr, TEMP_STR_CELSIUS_REG + (temp * 0x10), 1
56+
self.unit_addr, TEMP_STR_CELSIUS_REG + (scale * 0x10), 1
5657
).decode() + str(
5758
float(
5859
self.kmeter_i2c.readfrom_mem(
59-
self.unit_addr, TEMP_STR_CELSIUS_REG + (temp * 0x10), 8
60+
self.unit_addr, TEMP_STR_CELSIUS_REG + (scale * 0x10), 8
6061
).decode()
6162
)
6263
)
6364

64-
def get_kmeter_internal_string(self, temp=0):
65-
"""! get internal temperature string value."""
65+
def get_internal_temperature_string(self, scale=CELSIUS) -> str:
66+
#! get internal temperature string value.
6667
return self.kmeter_i2c.readfrom_mem(
67-
self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (temp * 0x10), 1
68+
self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (scale * 0x10), 1
6869
).decode() + str(
6970
float(
7071
self.kmeter_i2c.readfrom_mem(
71-
self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (temp * 0x10), 8
72+
self.unit_addr, TEMP_INTSTR_CELSIUS_REG + (scale * 0x10), 8
7273
).decode()
7374
)
7475
)
7576

76-
def get_device_spec(self, mode) -> int:
77-
"""! Get device firmware version and i2c address.
78-
mode: 0xFE and 0xFF
79-
"""
77+
def get_device_info(self, mode) -> int:
78+
#! Get device firmware version and i2c address.
79+
#! @param mode: 0xFE and 0xFF
8080
if mode >= FIRM_VER_REG and mode <= I2C_ADDR_REG:
8181
return self.kmeter_i2c.readfrom_mem(self.unit_addr, mode, 1)[0]
8282

83-
def set_i2c_address(self, addr) -> None:
84-
"""! Set i2c address.
85-
addr: 1 to 127
86-
"""
87-
if addr >= 0x01 and addr <= 0x7F:
88-
if addr != self.unit_addr:
89-
self.kmeter_i2c.writeto_mem(self.unit_addr, I2C_ADDR_REG, struct.pack("b", addr))
90-
self.unit_addr = addr
83+
def set_i2c_address(self, address=0x66) -> None:
84+
#! Set i2c address.
85+
#! @param address: 0x08 ~ 0x77
86+
if address >= 0x08 and address <= 0x77:
87+
if address != self.unit_addr:
88+
self.kmeter_i2c.writeto_mem(
89+
self.unit_addr, I2C_ADDR_REG, struct.pack("b", address)
90+
)
91+
self.unit_addr = address
9192
time.sleep_ms(200)
9293

9394
def int_convert(self, value):

0 commit comments

Comments
 (0)