microsoft / microsoft/pxt-microbit

pins.i2cReadNumber() should not return 0 when the device is not connected/answered

Open
#2,411 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

apis help wanted serial
Dominant language
TypeScript
Stars
804
Forks
721
Avg merge
2d 9h
Merged PRs (30d)
2

Description

Is your feature request related to a problem? Please describe.
pins.i2cReadNumber() will return 0 (without any error) even when the target device (specified with address parameter) is not connected or it have some trouble and cannot answer correctly or any other errors.

We cannot detect the disconnection nor internal error of the devices with current API set.

Describe the solution you'd like
It should indicate the problem about the device with

  • throw exception
  • return null

when we cannot read the value from the target i2c device.

Describe alternatives you've considered
If we can detect I2C devices (#1780), we'll detect it and then read/write to the devices. But event when the device is connected and worked correctly for the first time, i2c devices sometimes will stop working with some internal errors (not so rare case). To indicate it, i2cReadNumber() should throw exception or at least should return null or some valued which clearly indicated the error.

Additional context
note: pins have only read/write i2c APIs: i2cReadNumber() and i2cWriteNumber()

https://makecode.microbit.org/reference/pins
https://makecode.microbit.org/reference/pins/i2c-read-number
https://makecode.microbit.org/reference/pins/i2c-write-number

implementations of them:
https://github.com/microsoft/pxt-microbit/blob/master/libs/core/pins.ts#L21

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pins implementation in libs/core/pins.ts around the linked i2cReadNumber() code, then read the pins and i2c-read-number reference pages. Determine how an unanswered or failing I2C read should be represented, choosing between an exception and null. Done means callers can distinguish a valid zero from a device or read error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, embedded-iot
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.