def __init__(self, name, sda=None, scl=None, width=128, height=64, addr=0x3c): self.present = False self._y = 0 self._x = 0 self.last_text = "" self.char_width = width // 8 self.char_height = height // 8 if type(sda) is I2C: i2c = sda else: i2c = I2C(sda=sda, scl=scl) # test if lcd is responding try: self.dp = ssd1306.SSD1306_I2C(width, height, i2c, addr=addr) self.clear(show=False) self.println(" iot.ulno.net\n", show=True) except OSError: print("lcd not found") else: self.present = True Device.__init__(self, name, i2c, setters={"set": self.evaluate}, report_change=False) self.getters[""] = self.value
def __init__(self, name, pin, ignore_case=True, on_change=None, report_change=False, turn_time_ms=700, freq=50, min_us=600, max_us=2400, angle=180): self.min_us = min_us self.max_us = max_us self.us = 0 self.freq = freq self.angle = angle self.angle_list = None self.turn_time_ms = turn_time_ms self.turn_start = None Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0), setters={"set": self.turn}, ignore_case=ignore_case, on_change=on_change, report_change=report_change) self._init()
def __init__(self, name, pin, ignore_case=True, on_change=None, report_change=False): self.is_on = False self.last_rgb = _c.white self.current_rgb = _c.black self.ani = None b = "brightness" r = "rgb" s = "set" st = "/status" Device.__init__(self, name, pin, setters={ b + "/" + s: self._command_brightness, r + "/" + s: self._command_rgb, s: self._command_status, "animation": self.animation }, getters={ "": self.get_status, b + st: self.get_brightness, r + st: self.get }, ignore_case=ignore_case, on_change=on_change, report_change=report_change) self.set(*_c.black) # off
def __init__(self, name, trigger_pin, echo_pin, echo_timeout_us=30000, precision=10, on_change=None, report_change=True, filter=None): # trigger_pin: Output pin to send pulses # echo_pin: Readonly pin to measure the distance. # The pin should be protected with 1k resistor # echo_timeout_us: Timeout in microseconds to listen to echo pin. # By default is based in sensor limit range (4m) self.current_value = 0 self._last_measured = 0 if type(trigger_pin) is not Pin: trigger_pin = Pin(trigger_pin) self.trigger_pin = trigger_pin if type(echo_pin) is not Pin: echo_pin = Pin(trigger_pin) self.echo_pin = echo_pin self.precision = precision trigger_pin.init(Pin.OUT) trigger_pin.off() echo_pin.init(Pin.IN) echo_pin.init(Pin.OPEN_DRAIN) self.echo_timeout_us = echo_timeout_us self.distance = None Device.__init__(self, name, (trigger_pin, echo_pin), on_change=on_change, report_change=report_change, filter=filter)
def __init__(self, name, sda=None, scl=None, addr=8, ignore_case=False, report_change=True, filter=None): not_found = "i2c device not found" self.addr = addr self.ownaddr = addr self.count = None self.current_value = "" self.suspend_start = None self.suspend_time = 0 self.msgq = None if type(sda) is I2C: i2c = sda else: i2c = I2C(sda=sda, scl=scl) try: l = i2c.scan() # see, if you can find the dive with respective addr except OSError: print(not_found) else: if addr in l: self.present = True Device.__init__(self, name, i2c, setters={"set": self.evaluate}, ignore_case=ignore_case, report_change=report_change, filter=filter) else: print(not_found)
def __init__(self, name, pin, *args, report_high="on", report_low="off", pullup=True, threshold=0, on_change=None, report_change=True, filter=None): if len(args) > 0: report_high = args[0] if len(args) > 1: report_low = args[1] Device.__init__(self, name, pin, value_map={ True: report_high, False: report_low }, on_change=on_change, report_change=report_change, filter=filter) if pullup: pin.init(Pin.IN, Pin.PULL_UP) else: pin.init(Pin.IN) try: Pin.init(Pin.OPEN_DRAIN) except: pass self.threshold = threshold + 1 self.debouncer = self.port() * self.threshold
def __init__(self, name, pin, rising=False, falling=False, pullup=True, on_change=None, report_change=True): if pullup: pin.init(Pin.IN, Pin.PULL_UP) else: pin.init(Pin.IN, Pin.OPEN_DRAIN) if rising and falling: trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING elif not rising and falling: trigger = Pin.IRQ_FALLING else: # also if both all false trigger = Pin.IRQ_RISING pin.irq(trigger=trigger, handler=self.callback) self.counter = 0 self.report_counter = 0 self.triggered = False Device.__init__(self, name, pin, on_change=on_change, report_change=report_change) self.getters[""] = self.value
def __init__(self, name, pin, *args, high_command='on', low_command='off', ignore_case=True, on_change=None, report_change=False): if len(args) > 0: high_command = args[0] if len(args) > 1: low_command = args[1] if ignore_case: high_command = high_command.lower() low_command = low_command.lower() self.high_command = high_command self.low_command = low_command Device.__init__(self, name, pin, setters={"set": self.evaluate}, ignore_case=ignore_case, on_change=on_change, report_change=report_change, value_map={ 1: self.high_command, 0: self.low_command }) pin.init(Pin.OUT) self.state = pin()
def __init__(self, name, pin, on_change=None, filter=None): import onewire, ds18x20 gc.collect() self.ds = ds18x20.DS18X20(onewire.OneWire(pin)) self.roms = self.ds.scan() self.lasttime = time.ticks_ms() self.ds.convert_temp() self.temp_list = None Device.__init__(self, name, pin, on_change=on_change, filter=filter)
def __init__(self, name, precision=1, threshold=None, on_change=None, report_change=True): self.precision = precision self.threshold = None if threshold is not None: self.threshold = max(1, min(threshold, 1023)) self.current_value = -10000 Device.__init__(self, name, ADC(0), on_change=on_change, report_change=report_change) self.getters[""] = self.value
def __init__(self, name, pin, dht_dev, delay, on_change=None, report_change=True): self.delay = delay import dht self.dht = dht_dev self.lasttime = time.ticks_ms() self.dht.measure() Device.__init__(self, name, pin, on_change=on_change, getters={"temperature": self.temperature, "humidity": self.humidity}, report_change=report_change)
def __init__(self, name, precision=1, threshold=None, on_change=None, report_change=True, filter=None): self.precision = precision self.threshold = None if threshold is not None: self.threshold = max(1, min(threshold, 1023)) self.last_value = None Device.__init__(self, name, ADC(0), on_change=on_change, report_change=report_change, filter=filter)
def __init__(self, name, sda=None, scl=None, width=16, height=2, addr=0x27): self.present = False self.last_text = "" self.char_width = width self.char_height = height self.display_buffer = bytearray(b" " * width * height) self.back_buffer = bytearray(b" " * width * height) self.x = 0 self.y = 0 if type(sda) is I2C: i2c = sda else: i2c = I2C(sda=sda, scl=scl) # test if lcd is responding try: self.dp = I2cLcd(i2c, addr, height, width) self.dp.clear() self.clear() self.hide_cursor() self.println("iot.ulno.net") except OSError: print("lcd not found") else: self.present = True Device.__init__(self, name, i2c, setters={"set": self.evaluate}, report_change=False) self.getters[""] = self.value
def __init__(self, name, pin, freq=50, duty=0, ignore_case=True, on_change=None, report_change=False): self._duty = 0 self._freq = freq Device.__init__(self, name, machine.PWM(pin, freq=freq, duty=duty), setters={ "freq/set": self.set_freq, "duty/set": self.set_duty }, getters={ "freq": self.get_freq, "duty": self.get_duty }, ignore_case=ignore_case, on_change=on_change, report_change=report_change)
def __init__(self, name, rst, cs=None, sck=None, mosi=None, miso=None, key=b'\xff\xff\xff\xff\xff\xff', datasize=128, on_change=None, report_change=True, filter=None): rst.init(Pin.OUT) self.rst = rst if self.rst is not None: self.rst.value(0) self.datasize = datasize self.key = key board = uname()[0] if sck is None: # select defaults (hw based spi) if board == 'WiPy' or board == 'LoPy' or board == 'FiPy': spi = SPI(0) spi.init(SPI.MASTER, baudrate=1000000, pins=(14, 13, 12)) elif board == 'esp8266': spi = SPI(1, baudrate=2500000, polarity=0, phase=0) spi.init() # hw uses the following pins # sck = Pin(14,Pin.OUT) # d5 (ck) # mosi = Pin(13,Pin.OUT) # d7 (mo) # miso = Pin(12,Pin.IN) # d6 (mi) else: raise RuntimeError("Unsupported platform") # TODO: does it make sense to set cs here to default? if cs is None: self.cs = Pin(15, Pin.OUT) # d8 (cs) else: self.cs = cs cs.init(Pin.OUT) self.spi = spi else: self.cs = cs if cs is not None: cs.init(Pin.OUT) if type(sck) is SPI: self.spi = sck else: if board == 'WiPy' or board == 'LoPy' or board == 'FiPy': spi = SPI(0) spi.init(SPI.MASTER, baudrate=1000000, pins=(sck, mosi, miso)) elif board == 'esp8266': sck.init(Pin.OUT) mosi.init(Pin.OUT) miso.init(Pin.IN) spi = SPI(baudrate=100000, polarity=0, phase=0, sck=sck, mosi=mosi, miso=miso) spi.init() else: raise RuntimeError("Unsupported platform") self.spi = spi if self.rst is not None: self.rst.value(1) if self.cs is not None: self.cs.value(0) # get some memory self.reg_buf = bytearray(4) self.block_write_buf = bytearray(18) self.auth_buf = bytearray(12) self.wreg_buf = bytearray(2) self.rreg_buf = bytearray(1) self.recv_buf = bytearray(16) self.recv_mv = memoryview(self.recv_buf) self.maxdata = datasize self.card_data_buf = bytearray(16) self.phys_init() self.last_access = time.ticks_ms() self.access_interval = 500 # only allow reads every 500ms Device.__init__(self, name, spi, on_change=on_change, report_change=report_change, filter=filter)