def test_sound(self, monkeypatch, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        @asyncio.coroutine
        def mockreturn(readline):
            return b'55'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'sound': 'sound'}

        r = Request()

        status_text = yield from my_server.sound(r, True)
        assert status_text == '55'
    def test_board_led(self, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        # test for LED Off
        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'off_on': 'Off'}

        r = Request()
        yield from my_server.board_led(r)
        assert my_server.command == 'L0\r\n'

        # test for LED On
        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'off_on': 'On'}

        r = Request()
        yield from my_server.board_led(r)
        assert my_server.command == 'L1\r\n'
    def test_temp_units(self, mocker):
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'temp_units': 'Celcius'}

        r = Request()

        status_text = yield from my_server.temp_units(r)
        assert my_server.command == 'U0\r\n'

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'temp_units': 'Fahrenheit'}

        r = Request()

        yield from my_server.temp_units(r)
        assert my_server.command == 'U1\r\n'
    def test_board_orientation(self, mocker):
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'orientation': 'Joystick_On_Left'}

        r = Request()

        yield from my_server.board_orientation(r)
        assert my_server.command == 'O1\r\n'

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'orientation': 'Joystick_On_Right'}

        r = Request()

        yield from my_server.board_orientation(r)
        assert my_server.command == 'O2\r\n'
    def test_tinker_out(self, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'tk_chan': 'A', 'value': '123'}

        r = Request()
        yield from my_server.tinker_out(r)
        assert my_server.command == 'J123\r\n'

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'tk_chan': 'B', 'value': '456'}

        r = Request()
        yield from my_server.tinker_out(r)
        assert my_server.command == 'K456\r\n'
    def test_tone2(self, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'frequency': '1000'}

        r = Request()
        yield from my_server.tone2(r)
        assert my_server.command == 'T1000\r\n'
    def test_play_tone(self, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        # test for LED Off
        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'notes': 'G_Sharp--A_Flat'}

        r = Request()
        yield from my_server.play_tone(r)
        assert my_server.command == 'T831\r\n'
    def test_leds(self, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        # test for LED Off
        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'leds': 'red', 'intensity': '100'}

        r = Request()
        yield from my_server.leds(r)
        assert my_server.command == 'red100\r\n'
    def test_accelerometer(self, monkeypatch, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        @asyncio.coroutine
        def mockreturn(readline):
            return b'2'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'axis': 'X__Side-Twist'}

        r = Request()

        status_text = yield from my_server.accelerometer(r, True)
        assert status_text == '2'

        @asyncio.coroutine
        def mockreturn(readline):
            return b'4'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'axis': 'Y__Front-Twist'}

        r = Request()

        status_text = yield from my_server.accelerometer(r, True)
        assert status_text == '4'

        @asyncio.coroutine
        def mockreturn(readline):
            return b'6'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'axis': 'Z__Raise-Lower'}

        r = Request()

        status_text = yield from my_server.accelerometer(r, True)
        assert status_text == '6'
    def test_joystick(self, monkeypatch, mocker):
        # allow the instantiation of EsploraSerial to proceed without a real serial connection
        mocker.patch('serial.Serial')

        # instantiate necessary objects
        my_serial = EsploraSerial("/dev/ttyACM0")
        my_server = Esp4sHttpServer(my_serial)

        @asyncio.coroutine
        def mockreturn(readline):
            return b'2'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'control': 'Button'}

        r = Request()

        status_text = yield from my_server.joystick(r, True)
        assert status_text == '2'

        @asyncio.coroutine
        def mockreturn(readline):
            return b'4'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'control': 'Left-Right_(X)'}

        r = Request()

        status_text = yield from my_server.joystick(r, True)
        assert status_text == '4'

        @asyncio.coroutine
        def mockreturn(readline):
            return b'6'

        monkeypatch.setattr(EsploraSerial, 'readline', mockreturn)

        class Request:
            def __init__(self):
                self._match_info = None

            @property
            def match_info(self):
                return {'control': 'Up-Down_(Y)'}

        r = Request()

        status_text = yield from my_server.joystick(r, True)
        assert status_text == '6'
Beispiel #11
0
# logging.basicConfig(level=logging.DEBUG)

# set the comport based upon command line parameter
if len(sys.argv) == 2:
    com_port = str(sys.argv[1])
else:
    com_port = '/dev/ttyACM0'

print(
    'esp4s-aio version 1.0    Copyright(C) 2015 Alan Yorinks   All Rights Reserved'
)
print('{} {}'.format('Using COM port ', com_port))

arduino = esplora_serial.EsploraSerial(com_port)
http_server = Esp4sHttpServer(arduino)

loop = asyncio.get_event_loop()
asyncio.Task(http_server.init(loop))


# signal handler function called when Control-C occurs
def signal_handler(signal, frame):
    print("Control-C detected. See you soon.")
    loop.stop()
    loop.close()


# listen for SIGINT
signal.signal(signal.SIGINT, signal_handler)