def test_bad_arguments(): queue = Queue() with assert_raises(TypeError, "can't convert NoneType to int"): queue.read(None) with assert_raises(TypeError, "object with buffer protocol required"): queue.write(None)
def test_poll(): poll = select.poll() queue = Queue() event = Event() can = Can(board.CAN_0) uart = Uart(1) # Register both event channels. poll.register(queue) poll.register(event) poll.register(can) poll.register(uart) # Timeout waiting for event. assert poll.poll(0.01) == [] # Event write, poll and read. event.write(0x1) assert poll.poll() == [(event, select.POLLIN)] assert event.read(0x1) == 0x1 # Queue write, poll and read. queue.write(b'foo') assert poll.poll() == [(queue, select.POLLIN)] assert queue.read(3) == b'foo'
def test_poll(): poll = select.poll() queue = Queue() event = Event() # Register both event channels. poll.register(queue) poll.register(event) # Timeout waiting for event. assert poll.poll(0.01) == [] # Event write, poll and read. event.write(0x1) assert poll.poll() == [(event, select.POLLIN)] assert event.read(0x1) == 0x1 # Queue write, poll and read. queue.write(b"foo") assert poll.poll() == [(queue, select.POLLIN)] assert queue.read(3) == b"foo"
def test_rising_edge_two_pins(): pin_a = Pin(board.PIN_D4, Pin.OUTPUT) pin_b = Pin(board.PIN_D6, Pin.OUTPUT) pin_a.write(0) pin_b.write(0) queue = Queue() exti_a = Exti(board.EXTI_D3, Exti.RISING, queue, 'a') exti_b = Exti(board.EXTI_D5, Exti.RISING, queue, 'b') exti_a.start() exti_b.start() # Make sure no interrupt has already occured. assert queue.size() == 0 # Trigger interrupts to create the character sequence 'aaba'. pin_a.write(1) pin_a.write(0) pin_a.write(1) pin_a.write(0) pin_b.write(1) pin_b.write(0) pin_a.write(1) pin_a.write(0) if not 'Linux' in os.uname().machine: print("Waiting for the character sequence... ") assert queue.read(4) == b'aaba' exti_a.stop() exti_b.stop()
def test_read_write(): queue = Queue() queue.write(b'foo') assert queue.size() == 3 assert queue.read(3) == b'foo'
BUTTON_PIN = board.PIN_GPIO0 UDP_ADDRESS = '192.168.1.103' UDP_PORT = 30303 button = Event() exti = Exti(BUTTON_PIN, Exti.FALLING, button, 0x1) queue = Queue() udp = socket.socket(type=socket.SOCK_DGRAM) udp.bind((UDP_ADDRESS, UDP_PORT)) poll = select.poll() poll.register(button) poll.register(queue) poll.register(udp) queue.write('foo') while True: [(channel, eventmask)] = poll.poll() if channel is button: button.read(0x1) print("button") elif channel is queue: print("queue:", queue.read(3)) elif channel is udp: print("udp:", udp.recv(1024))