Skip to content

Commit f69b000

Browse files
Tinyu-Zhaolbuque
authored andcommitted
libs/module: Add AIN module.
Signed-off-by: Tinyu <[email protected]>
1 parent ac89982 commit f69b000

File tree

8 files changed

+374
-0
lines changed

8 files changed

+374
-0
lines changed

docs/en/module/ain4.rst

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

docs/en/module/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Module
44
.. toctree::
55
:maxdepth: 1
66

7+
ain4.rst
78
display.rst
89
dualkmeter.rst
910
pps.rst

docs/en/refs/module.ain4.ref

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. |AIN4Module| image:: https://static-cdn.m5stack.com/resource/docs/products/module/AIN4-20mA%20Module%2013.2/img-ddec758e-78ce-43e8-9af7-bc101f3dc175.webp
2+
:target: https://docs.m5stack.com/en/module/AIN4-20mA%20Module%2013.2
3+
:height: 200px
4+
:width: 200px
5+
6+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/init.png
7+
.. |get_adc_raw_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/get_adc_raw_value.png
8+
.. |get_current_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/get_current_value.png
9+
.. |set_cal_current.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/set_cal_current.png
10+
.. |get_firmware_version.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/get_firmware_version.png
11+
.. |get_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/get_i2c_address.png
12+
.. |set_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/set_i2c_address.png
13+
14+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/ain4_20ma/example.png
15+
16+
17+
.. |ain4_core2_example.m5f2| raw:: html
18+
19+
<a
20+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/ain4/ain4_core2_example.m5f2"
21+
target="_blank"
22+
>
23+
ain4_core2_example.m5f2
24+
</a>
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 Module 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":"CH2 Current:","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false},{"name":"label2","type":"label","layer":4,"screenId":"builtin","screenName":"","id":"yhiGANRD2@fI@vl+","createTime":1722588654631,"x":1,"y":131,"color":"#ffffff","backgroundColor":"#222222","text":"CH3 Current:","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false},{"name":"label3","type":"label","layer":5,"screenId":"builtin","screenName":"","id":"i=W!BPhcf6PxH!bP","createTime":1722588660375,"x":1,"y":164,"color":"#ffffff","backgroundColor":"#222222","text":"CH4 Current:","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","touch","mic"]},{"module":["ain4_20ma"]}],"units":[],"hats":[],"bases":[],"i2cs":[],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"70\" y=\"10\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"module_ain420ma_init\" id=\"C%hgN:0BWn2t,T/YYP2N\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"ADDR\"><shadow type=\"math_number\" id=\"LCFj6;^S|w2f@q^arHS!\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0x55</field></shadow></value><next><block type=\"module_ain420ma_set_cal_current\" id=\"4ON2p!=yka;Y:|ER6cD,\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"%%e?%x-Nz-L$bU56op^H\"><field name=\"VALUE\">1</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"Ue4uWT[fYZixlCf`j+ud\"><mutation max=\"20\" min=\"4\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">20</field></shadow></value><next><block type=\"module_ain420ma_set_cal_current\" id=\"MX2Y`tX6+sYL;Nk44m:N\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"eQ)^N/l7EH30qW%]4k98\"><field name=\"VALUE\">2</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"TJpuq2tL[78P$:[7R;Y%\"><mutation max=\"20\" min=\"4\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">20</field></shadow></value><next><block type=\"module_ain420ma_set_cal_current\" id=\"q_MEYYp]@j:2^0(kPPWa\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"jiN[Y%~%GFOZ4/,}JDaa\"><field name=\"VALUE\">3</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"=Lcn7hNw]gXb|2_lsL#/\"><mutation max=\"20\" min=\"4\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">20</field></shadow></value><next><block type=\"module_ain420ma_set_cal_current\" id=\"u#CecT734](~oY*a{?yY\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\".S2UAV7Fd*k[_tw69hDT\"><field name=\"VALUE\">4</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"[email protected]+Mj_;XK/5DFL\"><mutation max=\"20\" min=\"4\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">20</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"70\" y=\"330\"><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=\"module_ain420ma_get_current_value\" id=\"]ahpOd/knGnB`Aw4Sh;+\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"9{I7!`,CzP3K|OMG}77G\"><field name=\"VALUE\">1</field></shadow></value></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\">CH2 Current:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_ain420ma_get_current_value\" id=\"e2|Yo_rZ)G.))7EQ-5|_\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"gBegL_9hD)[E.DkZYUj!\"><field name=\"VALUE\">2</field></shadow></value></block></value></block></value><next><block type=\"label_set_text\" id=\"Mu[=KX0#FGTS*8Z?u{n(\"><field name=\"NAME\">label2</field><value name=\"TEXT\"><shadow type=\"text\" id=\"$%USq[P5^ytd1}1sGj3p\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"Mm$HaP.zil}b%UvsC7s3\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"ov)D`Gn.b74Fw{y/JHO3\"><field name=\"TEXT\">CH3 Current:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_ain420ma_get_current_value\" id=\"_3U{=*uQ*0IdIywaJthM\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"=tJi~YH[0MlVD*ztBDvj\"><field name=\"VALUE\">3</field></shadow></value></block></value></block></value><next><block type=\"label_set_text\" id=\"[-jPgxD3-R;p_6/)%]O_\"><field name=\"NAME\">label3</field><value name=\"TEXT\"><shadow type=\"text\" id=\"$%USq[P5^ytd1}1sGj3p\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"b*([P$*R-?IdVECI;TWe\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"(1Xq]u{W7?Q@CHq8ebl]\"><field name=\"TEXT\">CH4 Current:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_ain420ma_get_current_value\" id=\"sK`2671a^X]F:LP%eK5W\"><field name=\"NAME\">ain4_20ma_0</field><value name=\"CHANNEL\"><shadow type=\"module_ain420ma_option\" id=\"XOr+^:01rPV4-wyU%fLc\"><field name=\"VALUE\">4</field></shadow></value></block></value></block></value></block></next></block></next></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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 module import AIN4Module
9+
10+
title0 = None
11+
label0 = None
12+
label1 = None
13+
label2 = None
14+
label3 = None
15+
ain4_20ma_0 = None
16+
17+
18+
def setup():
19+
global title0, label0, label1, label2, label3, ain4_20ma_0
20+
21+
M5.begin()
22+
Widgets.fillScreen(0x222222)
23+
title0 = Widgets.Title("AIN 4-20mA Module Test", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
24+
label0 = Widgets.Label("CH1 Current:", 1, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
25+
label1 = Widgets.Label("CH2 Current:", 1, 96, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26+
label2 = Widgets.Label("CH3 Current:", 1, 131, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27+
label3 = Widgets.Label("CH4 Current:", 1, 164, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28+
29+
ain4_20ma_0 = AIN4Module(address=0x55)
30+
ain4_20ma_0.set_cal_current(1, 20)
31+
ain4_20ma_0.set_cal_current(2, 20)
32+
ain4_20ma_0.set_cal_current(3, 20)
33+
ain4_20ma_0.set_cal_current(4, 20)
34+
35+
36+
def loop():
37+
global title0, label0, label1, label2, label3, ain4_20ma_0
38+
M5.update()
39+
label0.setText(str((str("CH1 Current:") + str((ain4_20ma_0.get_current_value(1))))))
40+
label1.setText(str((str("CH2 Current:") + str((ain4_20ma_0.get_current_value(2))))))
41+
label2.setText(str((str("CH3 Current:") + str((ain4_20ma_0.get_current_value(3))))))
42+
label3.setText(str((str("CH4 Current:") + str((ain4_20ma_0.get_current_value(4))))))
43+
44+
45+
if __name__ == "__main__":
46+
try:
47+
setup()
48+
while True:
49+
loop()
50+
except (Exception, KeyboardInterrupt) as e:
51+
try:
52+
from utility import print_error_msg
53+
54+
print_error_msg(e)
55+
except ImportError:
56+
print("please update to latest firmware")

m5stack/libs/module/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import mbus
66

77
_attrs = {
8+
"AIN4Module": "ain4",
89
"DualKmeterModule": "dual_kmeter",
910
"Relay4Module": "relay_4",
1011
"Encoder4MotorModule": "encoder4_motor",

0 commit comments

Comments
 (0)