Skip to content

Commit 81c03bd

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/unit: Roller485 compatible other i2c unit.
Signed-off-by: Tinyu <[email protected]>
1 parent abe94d7 commit 81c03bd

11 files changed

+647
-365
lines changed

docs/en/refs/unit.roller485.ref

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:target: https://docs.m5stack.com/en/unit/Unit-Roller485
44
:height: 200px
55
:width: 200px
6-
#base
6+
77
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/init.png
88
.. |set_motor_output_state.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/set_motor_output_state.png
99
.. |get_motor_output_state.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/get_motor_output_state.png
@@ -54,32 +54,34 @@
5454
.. |set_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/set_i2c_address.png
5555
.. |get_i2c_address.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/get_i2c_address.png
5656

57-
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/example.png
57+
.. |i2c_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/i2c_example.png
58+
.. |rs485_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/rs485_example.png
59+
.. |rs485_i2c_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/rs485_i2c_example.png
5860

59-
#i2c
60-
.. |init2.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/init.png
61-
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/read.png
62-
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/write.png
61+
.. |roller485_i2c_fire_example.m5f2| raw:: html
6362

64-
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/example.png
63+
<a
64+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/roller485/roller485_i2c_fire_example.m5f2"
65+
target="_blank"
66+
>
67+
roller485_i2c_fire_example.m5f2
68+
</a>
6569

66-
#485
67-
.. |init3.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/init.png
68-
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/read.png
69-
.. |create_frame.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/create_frame.png
70-
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/write.png
71-
.. |send_command.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/send_command.png
72-
.. |read_response.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/read_response.png
73-
.. |_crc8.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/_crc8.png
70+
.. |roller485_485_fire_example.m5f2| raw:: html
7471

72+
<a
73+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/roller485/roller485_485_fire_example.m5f2"
74+
target="_blank"
75+
>
76+
roller485_485_fire_example.m5f2
77+
</a>
7578

76-
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/example.png
7779

78-
#485->i2c
79-
.. |init4.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/init.png
80-
.. |read_i2c_slave.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/read_i2c_slave.png
81-
.. |write_i2c_slave.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/write_i2c_slave.png
82-
.. |read_i2c_slave_register.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/read_i2c_slave_register.png
83-
.. |write_i2c_slave_register.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/write_i2c_slave_register.png
80+
.. |roller485_485toi2c_fire_example.m5f2| raw:: html
8481

85-
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/roller485/example.png
82+
<a
83+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/roller485/roller485_485toi2c_fire_example.m5f2"
84+
target="_blank"
85+
>
86+
roller485_485toi2c_fire_example.m5f2
87+
</a>

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Unit
6565
relay.rst
6666
relay4.rst
6767
rgb.rst
68+
roller485.rst
6869
scroll.rst
6970
ssr.rst
7071
synth.rst

docs/en/units/roller485.rst

Lines changed: 95 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,82 @@
11

2-
RollerBase
2+
Roller485
33
==========
44

55
.. include:: ../refs/unit.roller485.ref
66

77
Support the following products:
88

9-
|RollerBase|
9+
|Roller485|
1010

11-
Micropython Example::
11+
Roller485 I2C Example:
1212

13-
import os, sys, io
14-
import M5
15-
from M5 import *
13+
.. literalinclude:: ../../../examples/unit/roller485/roller485_i2c_fire_example.py
14+
:language: python
15+
:linenos:
1616

17+
Roller485 I2C UIFLOW2 Example:
1718

19+
|i2c_example.png|
1820

19-
UIFLOW2 Example:
21+
.. only:: builder_html
22+
23+
|roller485_i2c_fire_example.m5f2|
24+
25+
Roller485 RS485 Example:
2026

21-
|example.png|
27+
.. literalinclude:: ../../../examples/unit/roller485/roller485_485_fire_example.py
28+
:language: python
29+
:linenos:
30+
31+
Roller485 RS485 UIFLOW2 Example:
32+
33+
|rs485_example.png|
2234

2335
.. only:: builder_html
2436

25-
class RollerBase
26-
----------------
37+
|roller485_485_fire_example.m5f2|
38+
39+
Roller485 RS485ToI2C Example:
40+
41+
.. literalinclude:: ../../../examples/unit/roller485/roller485_485toi2c_fire_example.py
42+
:language: python
43+
:linenos:
44+
45+
Roller485 RS485ToI2C UIFLOW2 Example:
46+
47+
|rs485_i2c_example.png|
48+
49+
.. only:: builder_html
50+
51+
|roller485_485toi2c_fire_example.m5f2|
52+
53+
54+
class Roller485Unit
55+
-------------------
2756

2857
Constructors
2958
------------
3059

31-
.. class:: RollerBase()
60+
.. class:: Roller485Unit(bus, address, mode)
61+
62+
Initialize the Roller485Unit object based on communication mode.
63+
64+
:param bus: The I2C/RS485 bus instance.
65+
:param address: The motor's RS485 address. Defaults to _ROLLER485_RS485_ADDR.
66+
:param mode: The Roller485 communication mode.
3267

3368
UIFLOW2:
3469

3570
|init.png|
3671

72+
class RollerBase
73+
----------------
74+
75+
Constructors
76+
------------
77+
78+
.. class:: RollerBase()
79+
3780

3881
Methods
3982
-------
@@ -522,7 +565,7 @@ Methods
522565

523566

524567
class RollerI2C(RollerBase)
525-
---------------
568+
---------------------------
526569

527570
Constructors
528571
------------
@@ -550,23 +593,16 @@ Methods
550593
:param length: The number of bytes to read.
551594
:return: The data read from the device as a bytes object.
552595

553-
UIFLOW2:
554-
555-
|read.png|
556-
557596
.. method:: RollerI2C.write(register, bytes) -> None
558597

559598
Write data to a specified register on the I2C device.
560599

561600
:param register: The name of the register to write to.
562601
:param bytes: The data to write to the register as a bytes object.
563602

564-
UIFLOW2:
565-
566-
|write.png|
567603

568604
class Roller485(RollerBase)
569-
---------------
605+
----------------------------
570606

571607
Constructors
572608
------------
@@ -578,11 +614,6 @@ Constructors
578614
:param bus: The RS485 bus instance.
579615
:param int address: The motor's RS485 address. Defaults to _ROLLER485_RS485_ADDR.
580616

581-
UIFLOW2:
582-
583-
|init1.png|
584-
585-
586617
Methods
587618
-------
588619

@@ -593,9 +624,6 @@ Methods
593624
:param register: The name of the register to read from.
594625
:param length: The number of bytes to read.
595626

596-
UIFLOW2:
597-
598-
|read.png|
599627

600628
.. method:: Roller485.create_frame(cmd, motor_id, *datas) -> None
601629

@@ -605,10 +633,6 @@ Methods
605633
:param motor_id: The ID of the motor.
606634
:param datas: Additional data bytes to include in the frame.
607635

608-
UIFLOW2:
609-
610-
|create_frame.png|
611-
612636
.. method:: Roller485.write(register, bytes) -> bool
613637

614638
Write data to a specified register via RS485.
@@ -617,10 +641,6 @@ Methods
617641
:param bytes: The data to write to the register as a bytes object.
618642
:return: The response after writing the data.
619643

620-
UIFLOW2:
621-
622-
|write.png|
623-
624644
.. method:: Roller485.send_command(cmd, id, data, buf_len) -> bool
625645

626646
Send a command via RS485.
@@ -630,9 +650,6 @@ Methods
630650
:param data: The data to send along with the command.
631651
:param buf_len: The length of the buffer.
632652

633-
UIFLOW2:
634-
635-
|send_command.png|
636653

637654
.. method:: Roller485.read_response(cmd, id) -> tuple[Literal[True], Any]
638655

@@ -642,91 +659,87 @@ Methods
642659
:param id: The motor ID.
643660
:return: A tuple (success, response). Success is True if the response is valid, and response is the data read.
644661

645-
UIFLOW2:
646-
647-
|read_response.png|
648-
649662
.. method:: Roller485._crc8(buffer) -> int
650663

651664
Calculate CRC8 checksum.
652665

653666
:param buffer: The data buffer to compute the checksum for.
654667
:return: The computed CRC8 value.
655668

656-
UIFLOW2:
657-
658-
|_crc8.png|
659-
660669

661670
class Roller485ToI2CBus(RollerBase)
662-
---------------
671+
-----------------------------------
663672

664673
Constructors
665674
------------
666675

667-
.. class:: Roller485ToI2CBus(bus, address=_ROLLER485_RS485_ADDR, i2c_address=0x64)
676+
.. class:: Roller485ToI2CBus(bus, address, mode)
668677

669678
Initialize the Roller485ToI2CBus object.
670679

671680
:param bus: The RS485 bus instance.
672681
:param address: The motor's RS485 address. Defaults to _ROLLER485_RS485_ADDR.
673-
:param i2c_address: The I2C address of the slave device. Defaults to 0x64.
674-
675-
UIFLOW2:
676-
677-
|init2.png|
678-
679682

680683
Methods
681684
-------
682685

683-
.. method:: Roller485ToI2CBus.read_i2c_slave(length) -> tuple[Literal[True], Any]
686+
.. method:: Roller485ToI2CBus.readfrom_mem(addr, mem_addr, nbytes) -> bytes
684687

685-
Read data from the I2C slave device via RS485.
688+
Read data from a specific register of the I2C slave device.
686689

687-
:param length: The number of bytes to read.
688-
:return: The data read from the I2C slave.
690+
:param addr: The I2C slave address to read from.
691+
:param mem_addr: Memory register address.
692+
:param nbytes: The number of bytes to read.
693+
:return: The data read from the register.
689694
:throws Exception: If the read operation fails.
690695

691-
UIFLOW2:
696+
.. method:: Roller485ToI2CBus.readfrom_mem_into(addr, mem_addr, buf)
692697

693-
|read_i2c_slave.png|
698+
Read data from a specific register of the I2C slave device.
694699

695-
.. method:: Roller485ToI2CBus.write_i2c_slave(byte_list, stop_bit) -> Literal[True]
700+
:param addr: The I2C slave address to read from.
701+
:param mem_addr: Memory register address.
702+
:param buf: Buffer to store the data.
696703

697-
Write data to the I2C slave device via RS485.
704+
.. method:: Roller485ToI2CBus.writeto_mem(addr, mem_addr, buf) -> Literal[True]
698705

699-
:param byte_list: The data bytes to write.
700-
:param stop_bit: Whether to send a stop bit after writing.
706+
Write data to a specific register of the I2C slave device.
707+
708+
:param addr: The I2C slave address to write to.
709+
:param mem_addr: Memory register address.
710+
:param buf: The data bytes to write.
701711
:return: True if the write operation is successful.
702712
:throws Exception: If the write operation fails.
703713

704-
UIFLOW2:
705-
706-
|write_i2c_slave.png|
707-
708-
.. method:: Roller485ToI2CBus.read_i2c_slave_register(register, length) -> bytes
714+
.. method:: Roller485ToI2CBus.readfrom(addr, nbytes) -> bytes
709715

710-
Read data from a specific register of the I2C slave device.
716+
Read data from the I2C slave device via RS485.
711717

712-
:param register: The register address to read from.
713-
:param length: The number of bytes to read.
714-
:return: The data read from the register.
718+
:param addr: The I2C slave address to read from.
719+
:param nbytes: The number of bytes to read.
720+
:return: The data read from the I2C slave.
715721
:throws Exception: If the read operation fails.
716722

717-
UIFLOW2:
723+
.. method:: Roller485ToI2CBus.readfrom_into(addr, buf)
718724

719-
|read_i2c_slave_register.png|
725+
Read data from the I2C slave device via RS485.
720726

721-
.. method:: Roller485ToI2CBus.write_i2c_slave_register(register, byte_list) -> Literal[True]
727+
:param addr: I2C device address.
728+
:param buf: Buffer to store the data.
722729

723-
Write data to a specific register of the I2C slave device.
730+
.. method:: Roller485ToI2CBus.writeto(addr, buf, stop) -> Literal[True]
724731

725-
:param register: The register address to write to.
726-
:param byte_list: The data bytes to write.
732+
Write data to the I2C slave device via RS485.
733+
734+
:param addr: The I2C slave address to write to.
735+
:param buf: The data bytes to write.
736+
:param stop: Whether to send a stop bit after writing.
727737
:return: True if the write operation is successful.
728738
:throws Exception: If the write operation fails.
729739

730-
UIFLOW2:
731740

732-
|write_i2c_slave_register.png|
741+
.. method:: Roller485ToI2CBus.scan(addr, buf, stop) -> List
742+
743+
Scan for I2C devices on the bus.
744+
745+
:return: A list of addresses of the found I2C devices.

examples/unit/roller485/roller485_485_fire_example.m5f2

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)