Skip to content

Fix buffer protocol TypeError in I2C write operations for current MicroPython#4

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-buffer-protocol-exception
Draft

Fix buffer protocol TypeError in I2C write operations for current MicroPython#4
Copilot wants to merge 3 commits intomainfrom
copilot/fix-buffer-protocol-exception

Conversation

Copy link
Copy Markdown

Copilot AI commented Jan 14, 2026

Current MicroPython versions on ESP32 require i2c.writeto_mem() to receive bytes-like objects, not raw integers. The library was passing integers directly, causing TypeError: object with buffer protocol required.

Changes

  • lib/lidar.py: Wrap integer values in bytes([value]) before passing to writeto_mem()
  • .gitignore: Added to exclude Python bytecode cache

Technical Details

All _write() calls pass single-byte integer values (0x01, 0x02, register values, etc). Converting via bytes([value]) creates the required single-byte buffer:

def _write(self, addr, value):
-   self.i2c.writeto_mem(self.addr, addr, value)
+   self.i2c.writeto_mem(self.addr, addr, bytes([value]))

This maintains compatibility while satisfying the buffer protocol requirement in newer MicroPython releases.

Original prompt

This section details on the original issue you should resolve

<issue_title>library does throws exception with current micropyhton on esp32</issue_title>
<issue_description>library throws
TypeError: object with buffer protocol required
in current micropython version.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 14, 2026 18:08
…thod

Co-authored-by: davmoz <26165830+davmoz@users.noreply.github.com>
Co-authored-by: davmoz <26165830+davmoz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix library exception in current MicroPython on ESP32 Fix buffer protocol TypeError in I2C write operations for current MicroPython Jan 14, 2026
Copilot AI requested a review from davmoz January 14, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

library does throws exception with current micropyhton on esp32

2 participants