Ejemplo n.º 1
0
class Rbutton:
    toggle = 1  # toggle is ignored in NEC mode

    def __init__(self, irb, pin, addr, data, proto):
        self.irb = irb
        self.sw = Switch(pin)
        self.addr = addr
        self.data = data
        self.proto = proto

        self.sw.close_func(self.cfunc)
        self.sw.open_func(self.ofunc)
        self.tim = Delay_ms(self.repeat)

    def cfunc(self):  # Button push: send data
        tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
        self.irb.transmit(self.addr, self.data, tog, True)  # Test validation
        # Auto repeat. The Sony protocol specifies 45ms but this is tight.
        # In 20 bit mode a data burst can be upto 39ms long.
        self.tim.trigger(108)

    def ofunc(self):  # Button release: cancel repeat timer
        self.tim.stop()
        Rbutton.toggle ^= 1  # Toggle control

    async def repeat(self):
        await asyncio.sleep(0)  # Let timer stop before retriggering
        if not self.sw():  # Button is still pressed: retrigger
            self.tim.trigger(108)
            if self.proto == 0:
                self.irb.repeat()  # NEC special case: send REPEAT code
            else:
                tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
                self.irb.transmit(self.addr, self.data, tog,
                                  True)  # Test validation
Ejemplo n.º 2
0
    def __init__(self, irb, pin, addr, data, proto):
        self.irb = irb
        self.sw = Switch(pin)
        self.addr = addr
        self.data = data
        self.proto = proto

        self.sw.close_func(self.cfunc)
        self.sw.open_func(self.ofunc)
        self.tim = Delay_ms(self.repeat)
Ejemplo n.º 3
0
 def __init__(self, dio):
     self.value = False
     self.last_pressed = time.ticks_ms()
     self.interval_ms = 0
     self.min_interval_ms = TAP_TEMPO_DEFAULT_MIN_INTERVAL_MS
     self.max_interval_ms = TAP_TEMPO_DEFAULT_MAX_INTERVAL_MS
     # Use an underlying asyncio Switch to automatically debounce the input
     pin = Pin(dio, Pin.IN, Pin.PULL_UP)
     self.switch = Switch(pin)
     self.switch.close_func(self.set_value, (True,))
     self.switch.open_func(self.set_value, (False,))
Ejemplo n.º 4
0
    def __init__(self, id, rpin, swpin, name="Light Switch", type="Shelly"):
        super().__init__(id=id, name=name, type=type)
        self.relay = Pin(rpin, Pin.OUT, value=0)
        self.switch = Switch(Pin(swpin, Pin.IN))

        self.p_power = HomieProperty(
            id=id,
            name="Power",
            settable=True,
            datatype=BOOLEAN,
            default=FALSE,
        )
        self.add_property(self.p_power, self.on_power_msg)

        self.switch.open_func(self.toggle, ())
        self.switch.close_func(self.toggle, ())
Ejemplo n.º 5
0
class MomentaryButton:
    def __init__(self, dio):
        self.value = False
        self.last_pressed = time.ticks_ms()
        self.interval_ms = 0
        self.min_interval_ms = TAP_TEMPO_DEFAULT_MIN_INTERVAL_MS
        self.max_interval_ms = TAP_TEMPO_DEFAULT_MAX_INTERVAL_MS
        # Use an underlying asyncio Switch to automatically debounce the input
        pin = Pin(dio, Pin.IN, Pin.PULL_UP)
        self.switch = Switch(pin)
        self.switch.close_func(self.set_value, (True,))
        self.switch.open_func(self.set_value, (False,))

    def set_value(self, value):
        if value and not self.is_pressed:
            # Rising edge
            now = time.ticks_ms()

            # Update the running average interval between button presses
            delta = time.ticks_diff(now, self.last_pressed)
            if delta >= self.min_interval_ms and delta < self.max_interval_ms:
                if self.interval_ms > 0:
                    self.interval_ms = (self.interval_ms + delta) / 2.0
                else:
                    self.interval_ms = delta
            elif delta >= self.max_interval_ms:
                self.interval_ms = 0

            self.last_pressed = now

        self.value = True if value else False

    def set_interval_limits(self, min_interval_ms, max_interval_ms):
        self.min_interval_ms = min_interval_ms
        self.max_interval_ms = max_interval_ms

    @property
    def is_pressed(self,):
        return self.value == True

    @property
    def tap_tempo_ms(self,):
        if self.interval_ms < self.min_interval_ms or self.interval_ms > self.max_interval_ms:
            return self.max_interval_ms
        return self.interval_ms
Ejemplo n.º 6
0
class ShellyRelay(HomieNode):
    def __init__(self, id, rpin, swpin, name="Light Switch", type="Shelly"):
        super().__init__(id=id, name=name, type=type)
        self.relay = Pin(rpin, Pin.OUT, value=0)
        self.switch = Switch(Pin(swpin, Pin.IN))

        self.p_power = HomieProperty(
            id=id,
            name="Power",
            settable=True,
            datatype=BOOLEAN,
            default=FALSE,
        )
        self.add_property(self.p_power, self.on_power_msg)

        self.switch.open_func(self.toggle, ())
        self.switch.close_func(self.toggle, ())

    def off(self):
        self.relay(0)
        self.p_power.value = FALSE

    def on(self):
        self.relay(1)
        self.p_power.value = TRUE

    def on_power_msg(self, topic, payload, retained):
        if payload == FALSE:
            self.off()
        elif payload == TRUE:
            self.on()

    def toggle(self):
        if self.power_property.data == TRUE:
            self.off()
        else:
            self.on()
Ejemplo n.º 7
0
def test_swcb():
    s = '''
close toggles red
open toggles green
'''
    print('Test of switch executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red,))
    sw.open_func(toggle, (green,))
    run()
Ejemplo n.º 8
0
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    run()
Ejemplo n.º 9
0
def test_swcb():
    s = '''
close toggles red
open toggles green
'''
    print('Test of switch executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red, ))
    sw.open_func(toggle, (green, ))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
Ejemplo n.º 10
0
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
Ejemplo n.º 11
0
    def __init__(self, txt_rf_ms, verbose):
        self.txt_rf_ms = txt_rf_ms
        self.verbose = verbose
        self.tmax = 30  # Initial temperature range
        self.tmin = 15
        self.mode = _NORM
        # Enable initial update
        self.rf_disp = True
        self.rf_txt = True

        # Instantiate color mapper
        self.mapper = Mapper(self.tmin, self.tmax)

        # Instantiate switches
        self.timer = Delay_ms(duration=2000)  # Long press delay
        # Release arg rarg enables calling switch to be identified.
        for item in (('X4', self.chmax, 5, self.ar, 0), ('Y1', self.chmax, -5,
                                                         self.ar, 1),
                     ('X5', self.chmin, 5, eliza, 2), ('X6', self.chmin, -5,
                                                       eliza, 3)):
            sw, func, arg, long_func, rarg = item
            cs = Switch(Pin(sw, Pin.IN, Pin.PULL_UP))
            cs.close_func(self.press, (func, arg))
            cs.open_func(self.release, (long_func, rarg))

        # Instantiate display
        pdc = Pin('X1', Pin.OUT_PP, value=0)
        pcs = Pin('X2', Pin.OUT_PP, value=1)
        prst = Pin('X3', Pin.OUT_PP, value=1)
        # In practice baudrate made no difference to update rate which is
        # dominated by interpolation time
        spi = SPI(2, baudrate=13_500_000)
        verbose and print('SPI:', spi)
        ssd = SSD(spi, pcs, pdc, prst)  # Create a display instance
        ssd.fill(0)
        ssd.show()

        self.avg = 0.0
        # Instantiate PIR temperature sensor
        i2c = I2C(2)
        pir = AMG88XX(i2c)
        pir.ma_mode(True)  # Moving average mode

        # Run the camera
        asyncio.create_task(self.run(pir, ssd))
Ejemplo n.º 12
0
              max_val=9, 
              reverse=False, 
              range_mode=RotaryIRQ.RANGE_WRAP)
'''
import uasyncio as asyncio
from primitives.switch import Switch
from primitives.pushbutton import Pushbutton
#import uasyncio
import gc
import array  #数组,给温度用
import math
import logging
logging.basicConfig(level=logging.INFO)

cancel_pin = Pin(33, Pin.IN, Pin.PULL_UP)
cancel_sw = Switch(cancel_pin)
start_pin = Pin(32, Pin.IN, Pin.PULL_UP)
start_sw = Switch(start_pin)
timer_pin = Pin(21, Pin.IN, Pin.PULL_UP)
timer_sw = Switch(timer_pin)
temp_pin = Pin(22, Pin.IN, Pin.PULL_UP)
temp_sw = Switch(temp_pin)
menu_pin = Pin(4, Pin.IN, Pin.PULL_UP)
menu_sw = Switch(menu_pin)
#旋转编码器按钮
rotary_btn_pin = Pin(34, Pin.IN, Pin.PULL_UP)
rotary_sw = Switch(rotary_btn_pin)
#旋转编码器按钮按动次数计数器
rotary_press_count = 0
#旋转编码器
rotary = RotaryIRQ(pin_num_clk=36,