def test_can_wait_with_a_timeout():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1, INPUT, identifier=999)
        
        selector.wait(timeout=0)
        assert selector.ready == None
def test_can_use_a_different_value_to_identify_the_event_source():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1, INPUT, identifier=999)
        
        ev1.signal()
        
        selector.wait()
        assert selector.ready == 999
def test_event_mask_defaults_to_input_and_error():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1)
        ev1.signal()
        
        selector.wait(timeout=0)
        assert selector.ready == ev1
        assert selector.has_input == True
def test_can_wait_for_timer():
    selector = Selector()
    timer = Timer(blocking=False,offset=0.0125)
    with selector, timer:
        selector.add(timer, INPUT)
        
        timer.start()
        
        selector.wait()
        
        assert selector.ready == timer
def test_selector_is_a_convenient_api_to_epoll():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1, INPUT)
        
        ev1.signal()
        
        selector.wait()
        assert selector.ready == ev1
        assert selector.has_input == True
        assert selector.has_output == False
        assert selector.has_error == False
        assert selector.has_hangup == False
        assert selector.has_priority_input == False
예제 #6
0
def test_can_remove_source_from_selector():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1, INPUT)

        ev1.signal()

        selector.wait(timeout=0)
        assert selector.ready == ev1

        selector.remove(ev1)

        selector.wait(timeout=0)
        assert selector.ready == None
예제 #7
0
def test_can_wait_with_a_timeout():
    with Semaphore(blocking=False) as ev1, \
         Selector() as selector:

        selector.add(ev1, INPUT, identifier=999)

        selector.wait(timeout=0)
        assert selector.ready == None
def test_can_remove_source_from_selector():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    with selector, ev1:
        selector.add(ev1, INPUT)
        
        ev1.signal()
        
        selector.wait(timeout=0)
        assert selector.ready == ev1
        
        selector.remove(ev1)
        
        selector.wait(timeout=0)
        assert selector.ready == None
예제 #9
0
def test_event_mask_defaults_to_input_and_error():
    with Selector() as selector:
        ev1 = Semaphore(blocking=False)

        selector.add(ev1)
        ev1.signal()

        selector.wait(timeout=0)
        assert selector.ready == ev1
        assert selector.has_input == True
예제 #10
0
def test_can_use_a_different_value_to_identify_the_event_source():
    with Semaphore(blocking=False) as ev1, \
         Selector() as selector:

        selector.add(ev1, INPUT, identifier=999)

        ev1.signal()

        selector.wait()
        assert selector.ready == 999
예제 #11
0
def test_can_wait_for_timer():
    with Timer(blocking=False,offset=0.0125) as timer, \
         Selector() as selector:

        selector.add(timer, INPUT)

        timer.start()

        selector.wait()

        assert selector.ready == timer
예제 #12
0
def test_selector_is_a_convenient_api_to_epoll():
    with Semaphore(blocking=False) as ev1, \
         Selector() as selector:

        selector.add(ev1, INPUT)

        ev1.signal()

        selector.wait()
        assert selector.ready == ev1
        assert selector.has_input == True
        assert selector.has_output == False
        assert selector.has_error == False
        assert selector.has_hangup == False
        assert selector.has_priority_input == False
예제 #13
0
def test_selecting_from_multiple_event_sources():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    ev2 = Semaphore(blocking=False)
    with selector, ev1, ev2:
        selector.add(ev1, INPUT)
        selector.add(ev2, INPUT)

        ev1.signal()
        ev2.signal()

        selector.wait()
        first = selector.ready
        first.wait()

        selector.wait()
        second = selector.ready
        second.wait()

        assert first in (ev1, ev2)
        assert second in (ev1, ev2)
        assert first is not second
예제 #14
0
def test():
    with i2c.I2CMaster() as bus, \
         pi_header_1.pin(ALERT_PIN, direction=In, interrupt=Both, pull=PullUp) as alrt_pin, \
         Selector() as selector:
        selector.add(alrt_pin)
        tmp102 = TMP102(bus)
        tmp102.setExtendedMode(1)
        tmp102.setConversionRate(0)
        tmp102.setBoundTemp(False, 17.0)
        tmp102.setBoundTemp(True, 19.0)
        print(tmp102.getBoundTemp(False))
        print(tmp102.getBoundTemp(True))
        #tmp102.setAlertMode(1)
        tmp102.setFault(1)
        while True:
            selector.wait(30)
            if selector.ready is alrt_pin and selector.has_input:
                print("ALERT: alert_pin:", alrt_pin.value)
            temp = tmp102.readTemperature()
            alrt = tmp102.alert()
            print("Temperature: {0:6}C, alert: {1}, alert_pin: {2}".format(temp, alrt, alrt_pin.value))
예제 #15
0
def test_selecting_from_multiple_event_sources():
    selector = Selector()
    ev1 = Semaphore(blocking=False)
    ev2 = Semaphore(blocking=False)
    with selector, ev1, ev2:
        selector.add(ev1, INPUT)
        selector.add(ev2, INPUT)
        
        ev1.signal()
        ev2.signal()
        
        selector.wait()
        first = selector.ready
        first.wait()
        
        selector.wait()
        second = selector.ready
        second.wait()
        
        assert first in (ev1, ev2)
        assert second in (ev1, ev2)
        assert first is not second
예제 #16
0
    def run(self):
        logging.info("Temperature logging starting...")

        with i2c.I2CMaster() as bus,\
             Selector() as selector,\
             self.semaphore,\
             self.log_timer,\
             Timer(offset=0.1, interval=LCD_INTERVAL) as lcd_timer,\
             Adafruit_LCDPlate.get_interrupt_pin() as int_pin:

            # tolerate missing LCD
            lcd = None
            try:
                lcd = Adafruit_LCDPlate.Adafruit_LCDPlate(bus)
                fmt = FMT_ALL
                logging.info("LCD is attached")
            except IOError:
                logging.info("LCD is not attached")

            # resolution (9-12 bit) gets stored as (0-3) in bits 5,6 of config reg
            config = (BITS - 9) << 5
            bus.transaction(
                i2c.writing_bytes(TMP100A_ADDRESS, CONFIG_REGISTER, config),
                i2c.writing_bytes(TMP100B_ADDRESS, CONFIG_REGISTER, config))

            for addr in (TMP100A_ADDRESS, TMP100B_ADDRESS):
                read_results = bus.transaction(
                    i2c.writing_bytes(addr, CONFIG_REGISTER),
                    i2c.reading(addr, 1))
                # should be 0x80 at startup/reset
                logging.info("conf register for 0x{0:02x}: 0x{1:02x}".format(addr, read_results[0][0]))

            right_shift = 16 - BITS
            div = 1 << (BITS - 8)

            selector.add(self.semaphore)
            selector.add(self.log_timer)
            self.log_timer.start()

            if lcd:
                selector.add(lcd_timer)
                selector.add(int_pin)
                lcd_timer.start()

            while not self._terminated:
                try:
                    if lcd:
                        lcd.set_led(Adafruit_LCDPlate.GREEN_PIN, False)

                    selector.wait()
                    if self._terminated:
                        break
                    if not selector.ready is int_pin:
                        selector.ready.wait() # consume event

                    for i, addr in enumerate((TMP100A_ADDRESS, TMP100B_ADDRESS)):
                        read_results = bus.transaction(
                            i2c.writing_bytes(addr, TEMP_REGISTER),
                            i2c.reading(addr, 2))
                        t = read_results[0]
                        self.temperatures[i] = (t[0] if t[0] <= 127 else t[0] - 256) + (t[1] >> right_shift) / div

                    self.timestamp = time()

                    if lcd:
                        lcd.set_led(Adafruit_LCDPlate.GREEN_PIN, True)
                        lcd.set_led(Adafruit_LCDPlate.RED_PIN, False)

                    if selector.ready is self.log_timer:
                        self.log_handler.emit(TempLogRecord(self.timestamp, self.temperatures))
                    elif selector.ready is lcd_timer:
                        lcd.clear()
                        lcd.message(fmt.format(
                            self.temperatures[0], c2f(self.temperatures[0]),
                            self.temperatures[1], c2f(self.temperatures[1])))
                    elif selector.ready is int_pin:
                        # this is hacky...
                        intcap = lcd.chip.registers.read_register(INTCAPA) # required?
                        button_state = lcd.get_button_state()
                        if button_state == 0:
                            pass # button up event: ignore
                        else:
                            if lcd.button_state_pressed(button_state, Adafruit_LCDPlate.SELECT):
                                fmt = FMT_ALL
                            elif lcd.button_state_pressed(button_state, Adafruit_LCDPlate.UP):
                                fmt = FMT_INSIDE
                            elif lcd.button_state_pressed(button_state, Adafruit_LCDPlate.DOWN):
                                fmt = FMT_OUTSIDE
                            elif lcd.button_state_pressed(button_state, Adafruit_LCDPlate.LEFT):
                                fmt = FMT_C
                            elif lcd.button_state_pressed(button_state, Adafruit_LCDPlate.RIGHT):
                                fmt = FMT_F
                            else:
                                fmt = "Unknown\nButton!"
                            lcd_timer.stop()
                            lcd_timer.start()
                except KeyboardInterrupt:
                    logging.info("Keyboard interrupt received")
                    self._terminated = True
                except SystemExit:
                    logging.info("SystemExit received")
                    self._terminated = True
                except:
                    logging.exception("Error reading sensors:")
                    if lcd:
                        lcd.set_led(Adafruit_LCDPlate.RED_PIN, True)
            if lcd:
                lcd.clear()
                lcd.set_backlight(False)
                lcd.set_led(Adafruit_LCDPlate.GREEN_PIN, False)
                lcd.set_led(Adafruit_LCDPlate.RED_PIN, False)

        logging.info("Temperature logging stopped.")
        self.log_handler.close()
예제 #17
0
        lcd.message("It's\nworking!")
        sleep(5)
        lcd.set_backlight(False)

        for p in [GREEN_PIN, RED_PIN]:
            lcd.set_led(p, True)
            sleep(1)
            lcd.set_led(p, False)
            sleep(1)

        lcd.set_backlight(True)

        lcd.clear()
        lcd.message("Push some\nbuttons...")

        with Selector() as selector, get_interrupt_pin() as int_pin:
            selector.add(int_pin)
            for i in range(10):
                selector.wait(10)
                if selector.ready is int_pin:
                    print("Button state: 0x{0:02x}, 0b{0:08b}".format(
                        lcd.get_button_state()))
        """
        while True:
            print("GPIOA: 0x{0:02x}, 0b{0:08b}".format(lcd.chip.registers.read_register(GPIOA)))
            print("GPIOB: 0x{0:02x}, 0b{0:08b}".format(lcd.chip.registers.read_register(GPIOB)))
            sleep(1)
        """

        slow_type_from_left(lcd, "Mary had a\nlittle lamb!")
        sleep(5)