Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

i2ctarget: Add deinit() to I2CTargetRequest; remove close()#10366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dhalbert merged 2 commits intoadafruit:mainfromdhalbert:i2ctargetrequest-deinit
May 21, 2025

Conversation

dhalbert
Copy link
Collaborator

Removes visibleI2CTargetRequest.close() method. AddsI2CTargetRequest.deinit(). Doesclose() anddeinit() inI2CTargetRequest.__exit__(). MakesI2CTargetRequest have a finalizer:__del__() callsdeinit().

I tried testing with the more extensive example in theI2CTarget documentation, which tries to write and then read a value to an I2C register. It did not work, but I could not get that example to work with 8.2.9 or 9.2.3 either.

@Neradoc if you or someone else would like to test this with some previously-working code, I would be grateful.

@dhalbertdhalbertforce-pushed thei2ctargetrequest-deinit branch from17e24b8 to7d3dd30CompareMay 21, 2025 17:00
@Neradoc
Copy link

Neradoc commentedMay 21, 2025
edited
Loading

The following test code triggers the issue in 9.2.7 and works as expected with this PR.

Controller code (running on a tiny2350)

importboardimportrandomimporttimefromadafruit_bus_device.i2c_deviceimportI2CDevicei2c=board.STEMMA_I2C()device=I2CDevice(i2c,0x40)buffer=bytearray(1)whileTrue:try:withdevice:device.readinto(buffer)print(f"device responded with #{buffer[0]:02X}")time.sleep(1)withdevice:buffer[0]=random.randint(0,255)device.write(buffer)print(f"wrote to device: #{buffer[0]:02X}")time.sleep(1)exceptOSError:# target resetting or in errortime.sleep(1)

Target code (pro micro 2040) - that's the code being tested.

importboardfromi2ctargetimportI2CTargetmemory=0x55withI2CTarget(board.SCL,board.SDA, (0x40,))asdevice:whileTrue:i2c_target_request=device.request()ifnoti2c_target_request:continuewithi2c_target_request:ifi2c_target_request.is_read:print(f"read request: #{memory:02X}")buffer=memory.to_bytes(1)i2c_target_request.write(buffer)else:data=i2c_target_request.read(1)print(f"write request: #{data[0]:02X}")memory=data[0]

Could we have the call tocommon_hal_i2ctarget_i2c_target_close in deinit ? It is possible (though ill advised) to not use the context manager and call deinit manually, in which case I believe close wouldn't be called.
And then we could remove the custom__exit__.

@dhalbert
Copy link
CollaboratorAuthor

Could we have the call tocommon_hal_i2ctarget_i2c_target_close in deinit ? It is possible (though ill advised) to not use the context manager and call deinit manually, in which case I believe close wouldn't be called.
And then we could remove the custom__exit__.

I considered that, but wasn't sure if there was another use case. But I think not. I'll do that, yes, thanks.

@dhalbertdhalbertforce-pushed thei2ctargetrequest-deinit branch from7306e5e to8da8e31CompareMay 21, 2025 20:46
@dhalbertdhalbert requested a review fromNeradocMay 21, 2025 21:05
@dhalbertdhalbert marked this pull request as ready for reviewMay 21, 2025 21:06
Copy link

@NeradocNeradoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Runs fine, looks good.

@dhalbert
Copy link
CollaboratorAuthor

I will open a separate issue to fix the example.

@dhalbertdhalbert merged commitc6e4d72 intoadafruit:mainMay 21, 2025
197 checks passed
@dhalbertdhalbert deleted the i2ctargetrequest-deinit branchMay 21, 2025 22:05
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@NeradocNeradocNeradoc approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

i2ctarget error with missing deinit for request
2 participants
@dhalbert@Neradoc

[8]ページ先頭

©2009-2025 Movatter.jp