circuitpython/tests/extmod/machine_pulse.py
Paul Sokolovsky d5e9ab6e61 extmod/machine_pulse: Make time_pulse_us() not throw exceptions.
machine.time_pulse_us() is intended to provide very fine timing, including
while working with signal bursts, where each transition is tracked in row.
Throwing and handling an exception may take too much time and "signal loss".
So instead, in case of a timeout, just return negative value. Cases of
timeout while waiting for initial signal stabilization, and during actual
timing, are recognized.

The documentation is updated accordingly, and rewritten somewhat to clarify
the function behavior.
2017-02-05 14:20:17 +03:00

48 lines
836 B
Python

try:
import umachine as machine
except ImportError:
import machine
try:
machine.PinBase
machine.time_pulse_us
except AttributeError:
print("SKIP")
import sys
sys.exit()
class ConstPin(machine.PinBase):
def __init__(self, value):
self.v = value
def value(self, v=None):
if v is None:
return self.v
else:
self.v = v
class TogglePin(machine.PinBase):
def __init__(self):
self.v = 0
def value(self, v=None):
if v is None:
self.v = 1 - self.v
print("value:", self.v)
return self.v
p = TogglePin()
t = machine.time_pulse_us(p, 1)
print(type(t))
t = machine.time_pulse_us(p, 0)
print(type(t))
p = ConstPin(0)
print(machine.time_pulse_us(p, 1, 10))
print(machine.time_pulse_us(p, 0, 10))