def test_single_shot_timer(): event = Event() timer = Timer((0, 1000000), event, 0x1) print("starting single shot timer") timer.start() event.read(0x1) print("timeout")
def test_nothing_to_change(): event1 = Event( title='event1', start_date=date(2020, 3, 1), end_date=date(2020, 3, 2), website='https://www.google.com', description='test conference' ) event2 = Event( title='event2', start_date=date(2020, 3, 2), end_date=date(2020, 3, 4), website='https://www.google.com', description='another test conference' ) events = set(( event1, )) plan = plan_changes( existing_events={ event1: 1, event2: 2 }, desired_events=set((event1, event2)) ) assert plan.to_add == set() assert plan.to_remove == set()
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_periodic_timer(): event = Event() timer = Timer(0.2, event, 0x1, flags=Timer.PERIODIC) print("starting periodic timer") timer.start() for i in range(3): event.read(0x1) print("timeout", i) timer.stop()
def __init__(self): self.state = STATE_STOPPED self.dac = Dac([Board.PIN_DAC0, Board.PIN_DAC1], 10000) self.event = Event() self.playlist = os.listdir() self.index = 0 self.fsong = None self.timer = None self.identity = None
def test_bad_arguments(): event = Event() queue = Queue() # Bad device. with assert_raises(ValueError, "bad exti device -1"): Exti(-1, Exti.BOTH, event, 1) # Bad trigger. with assert_raises(ValueError, "bad trigger -1"): Exti(board.EXTI_D3, -1, event, 1) # Bad channel. with assert_raises(TypeError, "expected <class 'Event'> or <class 'Queue'>"): Exti(board.EXTI_D3, Exti.BOTH, None, 1) # Bad event data. with assert_raises(TypeError, "can't convert NoneType to int"): Exti(board.EXTI_D3, Exti.BOTH, event, None, 1) # Bad queue data. with assert_raises(TypeError, "object with buffer protocol required"): Exti(board.EXTI_D3, Exti.BOTH, queue, None, 1) # Bad callback. with assert_raises(TypeError, "bad callback"): Exti(board.EXTI_D3, Exti.BOTH, event, 1, 1)
def test_syncer_obeys_the_plan(): changed_event = Event( title='changed', start_date=date(2020, 3, 2), end_date=date(2020, 3, 4), website='https://www.google.com', description='updated description' ) removed_event_id = 123 plan = Plan( to_add=set((changed_event,)), to_remove=set((removed_event_id,)) ) calendar_api = Mock() csv_parser = Mock() planner = Mock() calendar_api.add = Mock() calendar_api.remove = Mock() planner.plan = Mock(return_value=plan) syncer = Syncer(calendar_api, csv_parser, planner) syncer.sync() calendar_api.add.assert_called_once_with(changed_event) calendar_api.remove.assert_called_once_with(removed_event_id)
def test_edit_event(): original_event = Event( title='original', start_date=date(2020, 3, 2), end_date=date(2020, 3, 4), website='https://www.google.com', description='another test conference' ) changed_event = Event( title='changed', start_date=date(2020, 3, 2), end_date=date(2020, 3, 4), website='https://www.google.com', description='updated description' ) unchanged_event = Event( title='unchanged', start_date=date(2020, 3, 1), end_date=date(2020, 3, 2), website='https://www.google.com', description='test conference' ) existing_events = { unchanged_event: 1, original_event: 2, } desired_events = set(( unchanged_event, changed_event )) plan = plan_changes( existing_events=existing_events, desired_events=desired_events ) assert plan.to_add == set((changed_event,)) assert plan.to_remove == set((2,))
def test_falling_edge(): pin = Pin(board.PIN_D4, Pin.OUTPUT) pin.write(1) event = Event() exti = Exti(board.EXTI_D3, Exti.FALLING, event, 0x1) exti.start() # Make sure no interrupt has already occured. assert event.size() == 0 # Trigger the interrupt and wait for the event. pin.write(0) if not 'Linux' in os.uname().machine: print("Waiting for the interrupt to occur... ") assert event.read(0x1) == 0x1 exti.stop()
def test_falling_edge(): pin = Pin(board.PIN_D4, Pin.OUTPUT) pin.write(1) event = Event() exti = Exti(board.EXTI_D3, Exti.FALLING, event, 0x1) exti.start() # Make sure no interrupt has already occured. assert event.size() == 0 # Trigger the interrupt and wait for the event. pin.write(0) if not 'Linux' in os.uname().machine: print("Waiting for the interrupt to occur... ", end="") assert event.read(0x1) == 0x1 print("ok.") exti.stop()
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_read_write(): event = Event() event.write(0x6) assert event.size() == 1 assert event.read(0x2) == 0x2 assert event.read(0x4) == 0x4
def test_bad_arguments(): event = Event() with assert_raises(TypeError, "can't convert NoneType to int"): event.read(None) with assert_raises(TypeError, "can't convert NoneType to int"): event.write(None)
def test_register_unregister(): poll = select.poll() queue = Queue() event = Event() can = Can(board.CAN_0) poll.register(queue) poll.register(event) poll.register(can) poll.unregister(queue) poll.unregister(event) poll.unregister(can) with assert_raises(OSError): poll.unregister(queue)
# # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # This file is part of the Pumbaa project. # from kernel import Timer from sync import Event EVENT = Event() TIMER = Timer(1, EVENT, 0x1, flags=Timer.PERIODIC) TIMER.start() for i in range(10): EVENT.read(0x1) print("timeout", i) TIMER.stop()
class MusicPlayer(object): def __init__(self): self.state = STATE_STOPPED self.dac = Dac([Board.PIN_DAC0, Board.PIN_DAC1], 10000) self.event = Event() self.playlist = os.listdir() self.index = 0 self.fsong = None self.timer = None self.identity = None def main(self): # Start the periodic fill timer. self.timer = Timer(0.1, self.event, EVENT_TIMEOUT, flags=Timer.PERIODIC) self.timer.start() print('Pumbaa Music Player!') print('Playlist:') for number, song in enumerate(self.playlist, 1): print("{}: {}".format(number, song)) # Start the main loop of the music player. while True: mask = self.event.read(EVENT_ALL) if mask & EVENT_STOP: self.handle_event_stop() if mask & EVENT_PAUSE: self.handle_event_pause() if mask & EVENT_NEXT: self.handle_event_next() if mask & EVENT_PREV: self.handle_event_prev() if mask & EVENT_PLAY: self.handle_event_play() # Play if the state in playing, eyy! if self.state == STATE_PLAYING: self.play_chunk() def start(self): if self.identity is None: self.identity = _thread.start_new_thread(self.main, ()) def play(self): self.event.write(EVENT_PLAY) def pause(self): self.event.write(EVENT_PAUSE) def stop(self): self.event.write(EVENT_STOP) def next(self): self.event.write(EVENT_NEXT) def prev(self): self.event.write(EVENT_PREV) def handle_event_play(self): path = self.playlist[self.index] if self.state == STATE_STOPPED: self.fsong = open(path, "rb") self.state = STATE_PLAYING print("Playing |", path) def handle_event_pause(self): if self.state == STATE_PLAYING: print("Paused |", self.playlist[self.index]) self.state = STATE_PAUSED def handle_event_stop(self): if self.state in [STATE_PLAYING, STATE_PAUSED]: self.fsong.close() self.state = STATE_STOPPED def handle_event_next(self): if self.state in [STATE_PLAYING, STATE_PAUSED]: self.fsong.close() self.index += 1 self.index %= len(self.playlist) self.fsong = open(self.playlist[self.index], "rb") def handle_event_prev(self): if self.state in [STATE_PLAYING, STATE_PAUSED]: self.fsong.close() self.index -= 1 self.index %= len(self.playlist) self.fsong = open(self.playlist[self.index], "rb") def play_chunk(self): samples = self.fsong.read(1024) if len(samples) > 0: self.dac.async_convert(samples) else: self.handle_event_next() print("Playing |", self.playlist[self.index])
def test_help(): event = Event() timer = Timer(1, event, 0x1) help(Timer) help(timer)
# furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # This file is part of the Pumbaa project. # from kernel import Timer from sync import Event EVENT = Event() TIMER = Timer(1, EVENT, 0x1, flags=Timer.PERIODIC) TIMER.start() for i in range(10): EVENT.read(0x1) print("timeout", i) TIMER.stop()
def test_print(): print(Exti) event = Event() exti = Exti(board.EXTI_D0, Exti.RISING, event, 1) print(exti)
# # This file is part of the Pumbaa project. # import select import socket from sync import Event, Queue from drivers import Exti import board 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:
# SOFTWARE. # # This file is part of the Pumbaa project. # import select import socket from sync import Event, Queue from drivers import Exti import board 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:
def test_help(): event = Event() help(Event) help(event)
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # This file is part of the Pumbaa project. # import os import _thread from sync import Event import harness import gc EVENT = Event() def thread_main(mask): EVENT.write(mask) def test_print(): help(_thread) def test_start(): mask = 0x1 _thread.start_new_thread(thread_main, (mask, )) assert EVENT.read(mask) == mask