Skip to content
Snippets Groups Projects
Commit b74393af authored by ft-Demo's avatar ft-Demo
Browse files

commit by robo pro coding

parents
No related branches found
No related tags found
No related merge requests found
{"uuid":"0a76a88f-4cdb-4f9d-8a94-da94e842a34b","name":"test_RX_i2c_device_template","mode":"ADVANCED","version":"1.0","controller":"RX"}
\ No newline at end of file
<xml xmlns="https://developers.google.com/blockly/xml" version="16">
<block type="rx_controller" id="rLS72=DDo(lk9vtgvJe?" x="81" y="37">
<mutation>
<arg type="Input" name="Input-0" value="-"/>
<arg type="Output" name="Output-0" value="-"/>
<arg type="Motor" name="Motor-0" value="-"/>
<arg type="I2C" name="I2C-0" value="-"/>
</mutation>
<field name="controller_name">RX</field>
<field name="Input-0">-</field>
<field name="Output-0">-</field>
<field name="Motor-0">-</field>
<field name="I2C-0">-</field>
</block>
</xml>
\ No newline at end of file
import asyncio
import fischertechnik.factories as rx_factory
from fischertechnik.logging import log as print
async def init(controller):
rx_factory.init_controller_factory()
<xml xmlns="https://developers.google.com/blockly/xml" version="16">
<variables>
<variable id="[HKS4(#X?SYWL^J5h6g_">i2cAdr</variable>
<variable id="i}2@Lo4Jj6^=z5w)nF#P">write_buffer</variable>
<variable id="XG0}H+Qa?Eq+T/-f$.Ig">read_length</variable>
<variable id="xif@_y%QFAVZ:W*vU}(`">i2cList</variable>
<variable id=":]ae2S[%IC#[WPCgbx%(">read_buffer</variable>
</variables>
<block type="util_python_imports" id="BuNdg{crGi4?b.2~{7~v" x="0" y="0">
<field name="value">import board&amp;#10;import busio&amp;#10;from adafruit_bus_device.i2c_device import I2CDevice</field>
</block>
<block type="start_block" id="7s|$pr*xo|Z,AfuHs:~," deletable="false" x="0" y="147">
<statement name="statement">
<block type="util_python" id="h9BK3DENhX`}qv`{TWOi">
<field name="value">global I2C_ADDRESS, I2C_FREQ&amp;#10;I2C_ADDRESS = 0x39&amp;#10;I2C_FREQ = 400000</field>
<next>
<block type="variables_set" id="GA5Xsw0+Mxs`x#b*,L(,">
<field name="VAR" id="xif@_y%QFAVZ:W*vU}(`">i2cList</field>
<value name="VALUE">
<block type="procedures_callreturn" id="ZgcacQbNpYur`7@%+$*b">
<mutation name="i2cScan"/>
</block>
</value>
<next>
<block type="text_print" id="mEMZn#djI|v}b+HEq3Dk">
<value name="TEXT">
<shadow type="text" id="Qo5E;81`j$rlyO7S;OGQ">
<field name="TEXT"/>
</shadow>
<block type="variables_get" id="xw]EJ535~-ww0}9u7n_Z">
<field name="VAR" id="xif@_y%QFAVZ:W*vU}(`">i2cList</field>
</block>
</value>
<next>
<block type="loop_endless" id="^x^S51cswn:O`*PQ]4y`"/>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defreturn" id="ecAbK;fN,?_e`Xaqdpv," x="0" y="458">
<field name="NAME">i2cScan</field>
<statement name="STACK">
<block type="util_python" id="Si.f-UPynZ9}TvQbdhT%">
<field name="value">#https://learn.adafruit.com/circuitpython-essentials/circuitpython-i2c&amp;#10;i2c = busio.I2C(board.SCL1, board.SDA1, frequency=400000)&amp;#10;while not i2c.try_lock():&amp;#10; pass&amp;#10;try:&amp;#10; #print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()],)&amp;#10; i2cList = i2c.scan()&amp;#10;finally: # unlock the i2c bus&amp;#10; i2c.unlock()&amp;#10;i2c.deinit()</field>
</block>
</statement>
<value name="RETURN">
<block type="variables_get" id="9V-sF[F@;?(Ojh@bN~vo">
<field name="VAR" id="xif@_y%QFAVZ:W*vU}(`">i2cList</field>
</block>
</value>
</block>
<block type="procedures_defnoreturn" id="b9}aT{G=Qe~2BTS;I{/K" inline="false" x="0" y="821">
<mutation>
<arg name="i2cAdr" varid="[HKS4(#X?SYWL^J5h6g_" argid="NWOx%:B*R@5qS{hwu}*6"/>
<arg name="write_buffer" varid="i}2@Lo4Jj6^=z5w)nF#P" argid="*^1{:*NQ;a~JjR]tLC0["/>
</mutation>
<field name="NAME">i2cWriteBuffer</field>
<field name="NWOx%:B*R@5qS{hwu}*6">i2cAdr</field>
<field name="*^1{:*NQ;a~JjR]tLC0[">write_buffer</field>
<statement name="STACK">
<block type="util_python" id="|*]W*YFeS8:]EZ?qv9:[">
<field name="value">with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:&amp;#10; with I2CDevice(i2c, i2cAdr) as device:&amp;#10; device.write(write_buffer)</field>
</block>
</statement>
</block>
<block type="procedures_defreturn" id="}UjMG~j-oVgtEs3.*C*p" x="0" y="1069">
<mutation>
<arg name="i2cAdr" varid="[HKS4(#X?SYWL^J5h6g_" argid="RlPBDL2._vnl$hOf*e_y"/>
<arg name="read_length" varid="XG0}H+Qa?Eq+T/-f$.Ig" argid=";l@;|?3O8#{w#P1a+uc@"/>
</mutation>
<field name="NAME">i2cReadBuffer</field>
<field name="RlPBDL2._vnl$hOf*e_y">i2cAdr</field>
<field name=";l@;|?3O8#{w#P1a+uc@">read_length</field>
<statement name="STACK">
<block type="util_python" id="-zX6j-.=tC-Y!f~]PQmv">
<field name="value">read_buffer = bytearray(read_length)&amp;#10;with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:&amp;#10; with I2CDevice(i2c, i2cAdr) as device:&amp;#10; device.readinto(read_buffer)</field>
</block>
</statement>
<value name="RETURN">
<block type="variables_get" id="R=mQwCp56lG95d|nd$/N">
<field name="VAR" id=":]ae2S[%IC#[WPCgbx%(">read_buffer</field>
</block>
</value>
</block>
<block type="procedures_defreturn" id="IQ-R}v]5bMHD39iwQF+|" x="0" y="1358">
<mutation>
<arg name="i2cAdr" varid="[HKS4(#X?SYWL^J5h6g_" argid="O.Le#]tX01s*mD=s2?cY"/>
<arg name="write_buffer" varid="i}2@Lo4Jj6^=z5w)nF#P" argid=",PWC!Et~WNrW[igor[qw"/>
<arg name="read_length" varid="XG0}H+Qa?Eq+T/-f$.Ig" argid="1y^K}wtnYK,6.[Ivw^gS"/>
</mutation>
<field name="NAME">i2cWriteReadBuffer</field>
<field name="O.Le#]tX01s*mD=s2?cY">i2cAdr</field>
<field name=",PWC!Et~WNrW[igor[qw">write_buffer</field>
<field name="1y^K}wtnYK,6.[Ivw^gS">read_length</field>
<statement name="STACK">
<block type="util_python" id="tDN-3|f.qE^KaO-I.`:c">
<field name="value">read_buffer = bytearray(read_length)&amp;#10;with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:&amp;#10; with I2CDevice(i2c, i2cAdr) as device:&amp;#10; device.write_then_readinto(write_buffer, read_buffer)</field>
</block>
</statement>
<value name="RETURN">
<block type="variables_get" id="[.6tZ$KS|O9Ic15@1j,N">
<field name="VAR" id=":]ae2S[%IC#[WPCgbx%(">read_buffer</field>
</block>
</value>
</block>
</xml>
\ No newline at end of file
import asyncio
import board
import busio
import user.lib.controller as controller
from adafruit_bus_device.i2c_device import I2CDevice
from fischertechnik.logging import log as print
i2cAdr = None
write_buffer = None
read_length = None
i2cList = None
read_buffer = None
async def i2cScan():
global i2cAdr, write_buffer, read_length, i2cList, read_buffer
#https://learn.adafruit.com/circuitpython-essentials/circuitpython-i2c
i2c = busio.I2C(board.SCL1, board.SDA1, frequency=400000)
while not i2c.try_lock():
pass
try:
#print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()],)
i2cList = i2c.scan()
finally: # unlock the i2c bus
i2c.unlock()
i2c.deinit()
return i2cList
async def i2cWriteBuffer(i2cAdr, write_buffer):
global read_length, i2cList, read_buffer
with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:
with I2CDevice(i2c, i2cAdr) as device:
device.write(write_buffer)
async def i2cReadBuffer(i2cAdr, read_length):
global write_buffer, i2cList, read_buffer
read_buffer = bytearray(read_length)
with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:
with I2CDevice(i2c, i2cAdr) as device:
device.readinto(read_buffer)
return read_buffer
async def i2cWriteReadBuffer(i2cAdr, write_buffer, read_length):
global i2cList, read_buffer
read_buffer = bytearray(read_length)
with busio.I2C(board.SCL1, board.SDA1, frequency=400000) as i2c:
with I2CDevice(i2c, i2cAdr) as device:
device.write_then_readinto(write_buffer, read_buffer)
return read_buffer
async def run(instance):
global i2cAdr, write_buffer, read_length, i2cList, read_buffer
await controller.init(instance)
global I2C_ADDRESS, I2C_FREQ
I2C_ADDRESS = 0x39
I2C_FREQ = 400000
i2cList = await i2cScan()
print(i2cList)
while True:
await asyncio.sleep(0.01)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment