예제 #1
0
def test_btn(suppress=False, lf=True, df=True):
    s = '''
press pulses red
release pulses green
double click pulses yellow
long press pulses blue
'''
    print('Test of pushbutton scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin, suppress)
    pb.press_func(pulse, (red, 1000))
    pb.release_func(pulse, (green, 1000))
    if df:
        print('Doubleclick enabled')
        pb.double_func(pulse, (yellow, 1000))
    if lf:
        print('Long press enabled')
        pb.long_func(pulse, (blue, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
예제 #2
0
def test_btncb():
    print('Test of pushbutton executing callbacks.')
    print(helptext)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (red, ))
    pb.release_func(toggle, (green, ))
    pb.double_func(toggle, (yellow, ))
    pb.long_func(toggle, (blue, ))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
예제 #3
0
def test_btn():
    print('Test of pushbutton scheduling coroutines.')
    print(helptext)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin)
    pb.press_func(pulse, (red, 1000))
    pb.release_func(pulse, (green, 1000))
    pb.double_func(pulse, (yellow, 1000))
    pb.long_func(pulse, (blue, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
예제 #4
0
async def test_btn(pin, suppress=True, lf=True, df=True):
    ''' handler for aswitch.Pushbutton instances '''
    pb = Pushbutton(pin, suppress)
    print('{} set up'.format(pin))
    pb.press_func(button_operation, (
        str(pin),
        "pushed",
    ))
    pb.release_func(button_operation, (
        str(pin),
        "released",
    ))
    pb.double_func(button_operation, (
        str(pin),
        "double-clicked",
    ))
    pb.long_func(button_operation, (
        str(pin),
        "long pressed",
    ))
예제 #5
0
class ScooterDisplay(object):
    def __init__(self, screens):
        self.screens = screens
        if not self.screens:
            self.screens = [DemoScreen()]
        self._screen = 0

        # OLED
        spi = SPI(2,
                  baudrate=14500000,
                  sck=Pin(PIN_DISPLAY_CLK),
                  mosi=Pin(PIN_DISPLAY_MOSI))
        self.display = SafeDisplay(spi,
                                   dc=Pin(PIN_DISPLAY_DC),
                                   cs=Pin(PIN_DISPLAY_CS),
                                   rst=Pin(PIN_DISPLAY_RST))

        loop = asyncio.get_event_loop()
        loop.create_task(self.update_display())

        # Encoder rotation
        self.encoder = RotaryEncoder(PIN_KNOB_CLK,
                                     PIN_KNOB_DT,
                                     cw=self.encoder_cw,
                                     ccw=self.encoder_ccw)

        # Encoder button
        self.button = Pushbutton(Pin(PIN_KNOB_SWITCH, Pin.IN))
        self.button.release_func(self.encoder_push)
        self.button.double_func(self.encoder_dblpush)
        self.button.long_func(self.encoder_longpush)

    def encoder_cw(self, steps):
        print("DISPLAY: Encoder CW, {} steps".format(steps))
        self._screen += 1
        if self._screen > len(self.screens) - 1:
            self._screen = 0

    def encoder_ccw(self, steps):
        print("DISPLAY: Encoder CCW, {} steps".format(steps))
        self._screen -= 1
        if self._screen < 0:
            self._screen = len(self.screens) - 1

    def encoder_push(self):
        print("DISPLAY: Encoder PUSH")
        self.screens[self._screen].encoder_click()

    def encoder_dblpush(self):
        print("DISPLAY: Encoder DBLPUSH")
        self.screens[self._screen].encoder_dblclick()

    def encoder_longpush(self):
        print("DISPLAY: Encoder LONGPUSH")
        self.screens[self._screen].encoder_longpress()

    async def update_display(self):
        current_screen = self._screen
        screen_changed = True
        needs_pixel_shift = False

        while True:
            if screen_changed or needs_pixel_shift:
                self.display.clear()
            if needs_pixel_shift:
                self.screens[self._screen].pixel_shift()

            self.screens[self._screen].update(self.display,
                                              needs_full_redraw=screen_changed
                                              or needs_pixel_shift)
            current_screen = self._screen
            await asyncio.sleep_ms(1000)

            screen_changed = current_screen != self._screen
            needs_pixel_shift = self.screens[self._screen].needs_pixel_shift()
예제 #6
0
파일: switch.py 프로젝트: sassod/uPyEasy
class switch_plugin:
    inputtype = "normal"  # Default switch type
    datastore = None  # Place where plugin data is stored for reboots

    def __init__(self):
        # generic section
        self._log = core._log
        self._log.debug("Plugin: switch contruction")
        self._utils = core._utils
        self._plugins = core._plugins
        self._hal = core._hal
        self._lock = Event()
        self.dxpin = dxpin
        # plugin specific section
        self.valuenames = {}
        self.valuenames["valueN1"] = "switch"
        self.valuenames["valueF1"] = ""
        self.valuenames["valueD1"] = 0
        # release lock, ready for next measurement
        self._lock.clear()

    def init(self, plugin, device, queue, scriptqueue):
        self._log.debug("Plugin: switch init")
        # generic section
        self._utils.plugin_initdata(self, plugin, device, queue, scriptqueue)
        self.content = plugin.get('content', content)
        plugin['dtype'] = dtype
        plugin['stype'] = stype
        plugin['template'] = template
        datastore = self._plugins.readstore(self.devicename)
        # plugin specific section
        self.switch_status = bootstate
        if self.inputtype == 'normal':
            self._log.debug("Plugin: switch init normal, pin: " + self.dxpin)
            # Setup switch
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Switch(self.swpin)
            # Register coros to launch on contact close and open
            self.switch.close_func(self.asyncswitchopen)
            self.switch.open_func(self.asyncswitchclosed)
        elif self.inputtype == 'low':
            self._log.debug("Plugin: switch init low, pin: " + self.dxpin)
            # Setup button active low
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Pushbutton(self.swpin)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)
        else:
            self._log.debug("Plugin: switch init high, pin: " + self.dxpin)
            # Setup button active high
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_DOWN)
            self.switch = Pushbutton(self.swpin)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)
        return True

    def loadform(self, plugindata):
        self._log.debug("Plugin: switch loadform")
        # generic section
        self._utils.plugin_loadform(self, plugindata)
        # plugin specific section
        plugindata['inputtype'] = self.inputtype
        plugindata['dxpin0'] = self.dxpin

    def saveform(self, plugindata):
        self._log.debug("Plugin: switch saveform")
        # generic section
        self._utils.plugin_saveform(self, plugindata)
        # plugin specific section
        self.inputtype = plugindata['inputtype']
        self.dxpin = plugindata['dxpin0']

        # store values
        data = {}
        data["inputtype"] = self.inputtype
        data["dxpin"] = self.dxpin
        data["valueN1"] = self.valuenames["valueN1"]
        data["valueF1"] = self.valuenames["valueF1"]
        data["valueD1"] = self.valuenames["valueD1"]
        self._plugins.writestore(self.devicename, data)

        if self.inputtype == 'normal':
            # Setup switch
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Switch(self.swpin)
            # Register coros to launch on contact close and open
            self.switch.close_func(self.asyncswitchopen)
            self.switch.open_func(self.asyncswitchclosed)
        elif self.inputtype == 'low':
            # Setup button active low
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Pushbutton(self.swpin)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)
        else:
            # Setup button active high
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_DOWN)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)

    def read(self, values):
        self._log.debug("Plugin: switch read")
        # generic section
        values['valueN1'] = self.valuenames["valueN1"]
        values['valueD1'] = self.switch_status

    def write(self, values):
        self._log.debug("Plugin: switch write")

    async def asyncprocess(self):
        self._log.debug("Plugin: switch process")
        # plugin specific section
        # If a switch occured
        if self.switch_status:
            # send data to protocol and script/rule queues
            self.valuenames["valueD1"] = self.switch_status
            self._utils.plugin_senddata(self)
            # erase status
            self.switch_status = ""
        # release lock, ready for next measurement
        self._lock.clear()

    #
    #CUSTOM SENSOR CODE...
    #

    async def asyncswitchopen(self):
        self.switch_status = 'open'
        # release lock, ready for next measurement
        self._lock.clear()

    async def asyncswitchclosed(self):
        self.switch_status = 'closed'
        # release lock, ready for next measurement
        self._lock.clear()

    async def asyncbuttonpress(self):
        self.switch_status = 'press'
        # release lock, ready for next measurement
        self._lock.clear()

    async def asyncbuttonrelease(self):
        self.switch_status = 'release'
        # release lock, ready for next measurement
        self._lock.clear()

    async def asyncbuttondouble(self):
        self.switch_status = 'double'
        # release lock, ready for next measurement
        self._lock.clear()

    async def asyncbuttonlong(self):
        self.switch_status = 'long'
        # release lock, ready for next measurement
        self._lock.clear()