2020-02-01 21:20:53 +01:00
|
|
|
import display
|
2020-02-03 20:22:54 +01:00
|
|
|
import time
|
2020-02-01 21:20:53 +01:00
|
|
|
|
2020-01-31 20:36:55 +01:00
|
|
|
class Tracer(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
print(f'{self.__class__.__name__}.__init__{args} {kwargs}')
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
if name.upper() == name:
|
|
|
|
return name
|
|
|
|
return lambda *args, **kwargs: print(f'{self.__class__.__name__}.{name}{args} {kwargs}')
|
|
|
|
|
|
|
|
class ADC(Tracer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Pin(object):
|
|
|
|
IN = 'IN'
|
|
|
|
OUT = 'OUT'
|
|
|
|
|
2020-02-01 21:20:53 +01:00
|
|
|
def __init__(self, id, direction, value=1, quiet=False):
|
2020-01-31 20:36:55 +01:00
|
|
|
self._id = id
|
|
|
|
self._value = 0
|
2020-02-01 21:20:53 +01:00
|
|
|
self._quiet = quiet
|
2020-01-31 20:36:55 +01:00
|
|
|
|
|
|
|
def init(self, d, value):
|
|
|
|
self.value(value)
|
|
|
|
|
|
|
|
def on(self):
|
|
|
|
self.value(1)
|
|
|
|
|
|
|
|
def off(self):
|
|
|
|
self.value(0)
|
|
|
|
|
|
|
|
def value(self, v=None):
|
|
|
|
if v is None:
|
2020-02-01 21:20:53 +01:00
|
|
|
if not self._quiet:
|
|
|
|
print(f'{self._id}: read {self._value}')
|
2020-01-31 20:36:55 +01:00
|
|
|
return self._value
|
|
|
|
if v:
|
2020-02-01 21:20:53 +01:00
|
|
|
if not self._quiet:
|
|
|
|
print(self._id + ": set on")
|
2020-01-31 20:36:55 +01:00
|
|
|
self._value = False
|
|
|
|
else:
|
2020-02-01 21:20:53 +01:00
|
|
|
if not self._quiet:
|
|
|
|
print(self._id + ": set off")
|
2020-01-31 20:36:55 +01:00
|
|
|
self._value = True
|
|
|
|
|
|
|
|
def __call__(self, v=None):
|
|
|
|
self.value(v)
|
|
|
|
|
|
|
|
class PWM(Tracer):
|
|
|
|
FREQ_16MHZ = 'FREQ_16MHZ'
|
|
|
|
|
|
|
|
class SPI(object):
|
|
|
|
def __init__(self, id):
|
|
|
|
self._id = id
|
2020-02-01 21:20:53 +01:00
|
|
|
if id == 0:
|
2020-03-07 12:50:26 +01:00
|
|
|
self.sim = display.spi_st7789_sim
|
2020-02-01 21:20:53 +01:00
|
|
|
else:
|
|
|
|
self.sim = None
|
2020-01-31 20:36:55 +01:00
|
|
|
|
|
|
|
def init(self, baudrate=1000000, polarity=0, phase=0, bits=8, sck=None, mosi=None, miso=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def write(self, buf):
|
2020-02-01 21:20:53 +01:00
|
|
|
if self.sim:
|
|
|
|
self.sim.write(buf)
|
|
|
|
else:
|
|
|
|
print("Sending data: " + str(buf))
|
2020-02-03 20:22:54 +01:00
|
|
|
|
2020-03-07 12:50:26 +01:00
|
|
|
class I2C():
|
|
|
|
def __init__(self, id):
|
|
|
|
self.id = id
|
|
|
|
if id == 0:
|
|
|
|
self.sim = display.i2c_cst816s_sim
|
|
|
|
else:
|
|
|
|
self.sim = None
|
|
|
|
|
|
|
|
def readfrom_mem_into(self, addr, reg, dbuf):
|
|
|
|
if self.sim:
|
|
|
|
self.sim.readfrom_mem_into(addr, reg, dbuf)
|
|
|
|
else:
|
|
|
|
raise OSError
|
|
|
|
|
2020-03-08 22:22:54 +01:00
|
|
|
class Timer():
|
|
|
|
def __init__(self, id, period=1000000):
|
|
|
|
self.then = None
|
|
|
|
self.period = period
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.then = time.time()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.then = None
|
|
|
|
|
|
|
|
def time(self):
|
|
|
|
now = time.time()
|
|
|
|
elapsed_sec = now - self.then
|
|
|
|
elapsed_us = int(elapsed_sec * 1000000)
|
|
|
|
|
|
|
|
return elapsed_us % self.period
|
|
|
|
|
|
|
|
def period(self):
|
|
|
|
self.time()
|
|
|
|
|
2020-02-03 20:22:54 +01:00
|
|
|
def lightsleep(ms=10):
|
2020-03-07 12:50:26 +01:00
|
|
|
display.tick()
|
2020-02-03 20:22:54 +01:00
|
|
|
time.sleep(ms / 1000)
|
|
|
|
|
|
|
|
def deepsleep(ms=10):
|
|
|
|
lightsleep(ms)
|