Skip to content

Commit ec9c2b7

Browse files
Tinyu-Zhaolbuque
authored andcommitted
libs/unit: Modify AIN unit api.
Signed-off-by: Tinyu <[email protected]>
1 parent f6e6adc commit ec9c2b7

File tree

6 files changed

+343
-30
lines changed

6 files changed

+343
-30
lines changed

docs/en/refs/unit.ain4.ref

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. |AIN4_20MAUnit| image:: https://static-cdn.m5stack.com/resource/docs/products/unit/AIN4-20mA%20Unit/img-a7fb07ad-7fd6-47f8-b333-2f55250f73a7.webp
2+
:target: https://docs.m5stack.com/en/unit/AIN4-20mA%20Unit
3+
:height: 200px
4+
:width: 200px
5+
6+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/init.png
7+
.. |get_adc_raw16_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_adc_raw16_value.png
8+
.. |get_4_20ma_current_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_4_20ma_current_value.png
9+
.. |get_adc_raw_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_adc_raw_value.png
10+
.. |get_current_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_current_value.png
11+
.. |set_cal_current.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/set_cal_current.png
12+
.. |get_device_spec.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_device_spec.png
13+
.. |get_firmware_version.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_firmware_version.png
14+
.. |get_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/get_i2c_address.png
15+
.. |set_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/set_i2c_address.png
16+
17+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/ain4_20ma/example.png
18+
19+
20+
.. |ain4_core2_example.m5f2| raw:: html
21+
22+
<a
23+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/ain4/ain4_core2_example.m5f2"
24+
target="_blank"
25+
>
26+
ain4_core2_example.m5f2
27+
</a>

docs/en/units/ain4.rst

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
2+
AIN4_20mA Unit
3+
==========
4+
5+
.. include:: ../refs/unit.ain4.ref
6+
7+
The following products are supported:
8+
9+
|AIN4_20MAUnit|
10+
11+
Micropython Example::
12+
13+
import os, sys, io
14+
import M5
15+
from M5 import *
16+
from hardware import *
17+
from unit import AIN4_20MAUnit
18+
19+
20+
21+
title0 = None
22+
label0 = None
23+
label1 = None
24+
i2c0 = None
25+
ain4_20ma_0 = None
26+
27+
28+
def setup():
29+
global title0, label0, label1, i2c0, ain4_20ma_0
30+
31+
M5.begin()
32+
Widgets.fillScreen(0x222222)
33+
title0 = Widgets.Title("AIN 4-20mA Unit Test", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu18)
34+
label0 = Widgets.Label("CH1 Current:", 1, 60, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
35+
label1 = Widgets.Label("CH1 ADC:", 1, 96, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
36+
37+
i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
38+
ain4_20ma_0 = AIN4_20MAUnit(i2c0, 0x55)
39+
ain4_20ma_0.set_cal_current(20)
40+
41+
42+
def loop():
43+
global title0, label0, label1, i2c0, ain4_20ma_0
44+
M5.update()
45+
label0.setText(str((str('CH1 Current:') + str((ain4_20ma_0.get_4_20ma_current_value())))))
46+
label1.setText(str((str('CH1 ADC:') + str((ain4_20ma_0.get_adc_raw16_value())))))
47+
48+
49+
if __name__ == '__main__':
50+
try:
51+
setup()
52+
while True:
53+
loop()
54+
except (Exception, KeyboardInterrupt) as e:
55+
try:
56+
from utility import print_error_msg
57+
print_error_msg(e)
58+
except ImportError:
59+
print("please update to latest firmware")
60+
61+
62+
63+
UIFLOW2 Example:
64+
65+
|example.png|
66+
67+
.. only:: builder_html
68+
69+
class AIN4_20MAUnit
70+
----------------
71+
72+
Constructors
73+
------------
74+
75+
.. class:: AIN4_20MAUnit(i2c, address)
76+
77+
Init I2C port & UNIT AIN 4-20mA I2C Address.
78+
79+
:param I2C i2c: I2C port to use.
80+
:param int|list|tuple address: I2C address of the Unit AIN4-20mA.
81+
82+
UIFLOW2:
83+
84+
|init.png|
85+
86+
87+
Methods
88+
-------
89+
90+
.. method:: AIN4_20MAUnit.get_adc_raw_value() -> int
91+
92+
Retrieves the raw ADC value from the channel.
93+
94+
:return: Raw ADC value as a 12-bit integer.
95+
96+
.. method:: AIN4_20MAUnit.get_adc_raw16_value() -> int
97+
98+
Retrieves the raw ADC value from the channel.
99+
100+
Note: This method will be removed in the next few versions, please use get_adc_raw_value()
101+
102+
:return: Raw ADC value as a 12-bit integer.
103+
104+
UIFLOW2:
105+
106+
|get_adc_raw16_value.png|
107+
108+
.. method:: AIN4_20MAUnit.get_current_value() -> float
109+
110+
Retrieves the current value (in mA) from the channel.
111+
112+
:return: Current value in milliamperes (mA).
113+
114+
.. method:: AIN4_20MAUnit.get_4_20ma_current_value() -> float
115+
116+
Retrieves the current value (in mA) from the channel.
117+
118+
Note: This method will be removed in the next few versions, please use get_current_value()
119+
120+
:return: Current value in milliamperes (mA).
121+
122+
UIFLOW2:
123+
124+
|get_4_20ma_current_value.png|
125+
126+
.. method:: AIN4_20MAUnit.set_cal_current(val)
127+
128+
Sets the calibration current for the specified channel.
129+
130+
:param int val: The calibration current value, ranging from 4 to 20 mA.
131+
132+
UIFLOW2:
133+
134+
|set_cal_current.png|
135+
136+
.. method:: AIN4_20MAUnit.get_device_spec() -> int
137+
138+
Retrieves the firmware version or i2c address of the AIN 4-20mA unit.
139+
140+
:return: Firmware version or I2C address.
141+
142+
:Note: This method will be removed in the next few versions, please use get_firmware_version() and get_i2c_address()
143+
144+
UIFLOW2:
145+
146+
|get_device_spec.png|
147+
148+
.. method:: AIN4_20MAUnit.get_firmware_version() -> int
149+
150+
Retrieves the firmware version of the AIN 4-20mA unit.
151+
152+
:return: Firmware version.
153+
154+
UIFLOW2:
155+
156+
|get_firmware_version.png|
157+
158+
.. method:: AIN4_20MAUnit.get_i2c_address() -> str
159+
160+
Retrieves the current I2C address of the AIN 4-20mA unit.
161+
162+
:return: I2C address as a string in hexadecimal format.
163+
164+
UIFLOW2:
165+
166+
|get_i2c_address.png|
167+
168+
.. method:: AIN4_20MAUnit.set_i2c_address(addr)
169+
170+
Sets a new I2C address for the AIN 4-20mA unit.
171+
172+
:param int addr: The new I2C address, must be between 0x08 and 0x77.
173+
174+
UIFLOW2:
175+
176+
|set_i2c_address.png|
177+
178+
179+
180+
181+

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unit
66

77
accel.rst
88
adc.rst
9+
ain4.rst
910
angle.rst
1011
bps.rst
1112
buzzer.rst
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.1.2","type":"core2","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__core2_screen","createTime":1722588338152,"x":0,"y":0,"width":320,"height":240,"backgroundColor":"#222222","size":0,"isSelected":true},{"name":"title0","type":"title","layer":1,"screenId":"builtin","screenName":"","id":"xF3#wimN7zBTj$xo","createTime":1722588629669,"x":0,"y":0,"color":"#ffffff","backgroundColor":"#0000FF","text":"AIN 4-20mA Unit Test","textOffset":3,"font":"Widgets.FONTS.DejaVu18","isSelected":false},{"name":"label0","type":"label","layer":2,"screenId":"builtin","screenName":"","id":"nPl5cDe^H=o0AQH1","createTime":1722588647294,"x":1,"y":60,"color":"#ffffff","backgroundColor":"#222222","text":"CH1 Current:","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false},{"name":"label1","type":"label","layer":3,"screenId":"builtin","screenName":"","id":"nW$!G2`fJOUBmFsi","createTime":1722588650250,"x":1,"y":96,"color":"#ffffff","backgroundColor":"#222222","text":"CH1 ADC:","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","touch","mic","i2c"]},{"unit":["unit_ain4_20ma"]}],"units":[{"type":"unit_ain4_20ma","name":"ain4_20ma_0","portList":["A","PAHUB","Custom"],"portType":"A","userPort":[22,21],"id":"n0G=HfSa8jgjYUZ0","createTime":1723616457732,"bus":"i2c0","pahubPortList":[0,1,2,3,4,5],"pahubPort":0,"initBlockId":"W{5czv}b}o5w1*pEhg^P"}],"hats":[],"bases":[],"i2cs":[{"id":"i2c0","portType":"A","userPort":[22,21],"freq":"100000","blockId":"~-mrVO25b#)1Pdr2:wn7"}],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"-10\" y=\"-30\"><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=\"~-mrVO25b#)1Pdr2:wn7\"><field name=\"NAME\">0</field><field name=\"FREQ\">100000</field><value name=\"SCL\"><shadow type=\"math_number\" id=\"[KAu9a}7:QJeWBAy5j:Y\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">33</field></shadow></value><value name=\"SDA\"><shadow type=\"math_number\" id=\"6WK7Qh6;IdHsA-6to/bD\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">32</field></shadow></value><next><block type=\"unit_ain4_20ma_init\" id=\"W{5czv}b}o5w1*pEhg^P\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"ADDR\"><shadow type=\"math_number\" id=\"DEuPeQBN$ojX;7uu1c%S\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0x55</field></shadow></value><next><block type=\"unit_ain4_20ma_set_cal_current\" id=\"FMuum#t!aLdl)52wq2+H\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"-Ttn[ORajwu?--oHu/KH\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></shadow></value></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"-10\" y=\"210\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"><next><block type=\"label_set_text\" id=\"$7P!AC$K|7Z$Na=n7e:(\"><field name=\"NAME\">label0</field><value name=\"TEXT\"><shadow type=\"text\" id=\"$%USq[P5^ytd1}1sGj3p\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"4zN;28Zq9a4r~FK%V_A(\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"SBF^w~gs5_XLTxz~/$O^\"><field name=\"TEXT\">CH1 Current:</field></shadow></value><value name=\"VALUE2\"><block type=\"unit_ain4_20ma_get_current_value\" id=\";qv*mf2nir^A1{F6%TZJ\"><field name=\"NAME\">ain4_20ma_0</field></block></value></block></value><next><block type=\"label_set_text\" id=\"E,!osA=dG0r,hQ@h;@8q\"><field name=\"NAME\">label1</field><value name=\"TEXT\"><shadow type=\"text\" id=\"$%USq[P5^ytd1}1sGj3p\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"4TTUnctF05=~-,bXbCl;\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"iWVAXbRw;=}s}Eo+ZoD8\"><field name=\"TEXT\">CH1 ADC:</field></shadow></value><value name=\"VALUE2\"><block type=\"unit_ain4_20ma_get_adc_raw16_value\" id=\"Lrkn^@*ZoW3vah/,]|7{\"><field name=\"NAME\">ain4_20ma_0</field></block></value></block></value></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":1722588338148}],"logicWhenNum":0,"customList":[]}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os, sys, io
6+
import M5
7+
from M5 import *
8+
from hardware import *
9+
from unit import AIN4_20MAUnit
10+
11+
12+
title0 = None
13+
label0 = None
14+
label1 = None
15+
i2c0 = None
16+
ain4_20ma_0 = None
17+
18+
19+
def setup():
20+
global title0, label0, label1, i2c0, ain4_20ma_0
21+
22+
M5.begin()
23+
Widgets.fillScreen(0x222222)
24+
title0 = Widgets.Title("AIN 4-20mA Unit Test", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
25+
label0 = Widgets.Label("CH1 Current:", 1, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26+
label1 = Widgets.Label("CH1 ADC:", 1, 96, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27+
28+
i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
29+
ain4_20ma_0 = AIN4_20MAUnit(i2c0, 0x55)
30+
ain4_20ma_0.set_cal_current(20)
31+
32+
33+
def loop():
34+
global title0, label0, label1, i2c0, ain4_20ma_0
35+
M5.update()
36+
label0.setText(str((str("CH1 Current:") + str((ain4_20ma_0.get_4_20ma_current_value())))))
37+
label1.setText(str((str("CH1 ADC:") + str((ain4_20ma_0.get_adc_raw16_value())))))
38+
39+
40+
if __name__ == "__main__":
41+
try:
42+
setup()
43+
while True:
44+
loop()
45+
except (Exception, KeyboardInterrupt) as e:
46+
try:
47+
from utility import print_error_msg
48+
49+
print_error_msg(e)
50+
except ImportError:
51+
print("please update to latest firmware")

0 commit comments

Comments
 (0)