microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
45 stars 25 forks source link

expose pulsein to digital pins #58

Closed xmeow closed 3 years ago

xmeow commented 3 years ago

This should resolve #26, make hcsr04 works with newbit. Tested with the following code snippet:

def sonar(p):
  p.write_digital(0)
  sleep_us(2)
  p.write_digital(1)
  sleep_us(10)
  p.write_digital(0)
  p.read_digital()
  duration = p.pulsein(2000)
  if duration<0:
    return -1
  return (duration/2)/29.1
dpgeorge commented 3 years ago

Thanks for the contribution!

machine.time_pulse_us() should now be fixed by b7a76e449cf1b437972c21efaf468fbe5891c6a2

The change in this PR adds a new method to pins, which changes (expands) the existing API so is probably not the best way to do it (because then it becomes hard to write code that works on V1 and V2).