class TestInternalsOfPyArduino(unittest.TestCase): # pylint: disable=R0904 def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR) self.arduino.connect() def test_send_cmd(self): def valid_transformer1(arg1): return arg1 def valid_transformer2(arg1): # pylint: disable=W0613 return "RESPONSE_FROM_TRANSFORMER" def invalid_transformer1(): pass def invalid_transformer2(arg1, arg2): # pylint: disable=W0613 pass def invalid_transformer3(arg1): # pylint: disable=W0613 raise (Exception("Exception while transforming")) # send_cmd(self, cmd, expected_response=None, timeout=None, response_transformer=None): self.arduino.send_cmd("_ms", response_transformer=int) self.arduino.send_cmd("_ms", response_transformer=valid_transformer1) self.arduino.send_cmd("_ms", response_transformer=valid_transformer2, expected_response="RESPONSE_FROM_TRANSFORMER") self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer1) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer2) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer3) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", expected_response="X") self.assertRaises(InvalidCommand, self.arduino.send_cmd, "_INEXISTING_CMD") def tearDown(self): # pylint: disable=C0103 self.arduino.close()
def main(): """ Expose object using PyRO """ if "ENABLE_LOGGING" in os.environ: logging.basicConfig(level=logging.DEBUG) Pyro4.config.HMAC_KEY = hmac.new('this-is-py-arduino').digest() Pyro4.config.SOCK_REUSE = True arduino = PyArduino() try: from py_arduino_web.dj.models import DjStorage storage = DjStorage() except: logging.error("-" * 80) logging.exception("Couldn't import DjStorage") logging.error("-" * 80) storage = Storage() Pyro4.Daemon.serveSimple( { arduino: "py_arduino.arduino", storage: "py_arduino_web.storage", }, host="localhost", port=61234, ns=False)
def main(): proxy = get_pyro3_proxy() print "Connecting to Arduino using", arduino_dev arduino = PyArduino() arduino.connect(arduino_dev, 9600) arduino.ping() print " + ping(): OK" last_a0 = arduino.analogRead(0) last_a1 = arduino.analogRead(1) print "Entering loop()..." while True: a0 = arduino.analogRead(0) a1 = arduino.analogRead(1) if abs(a0 - last_a0) >= threshold: last_a0 = a0 print "a0 ->", a0 proxy.set_layer_opacity(a0) if abs(a1 - last_a1) >= threshold: last_a1 = a1 print "a1 ->", a1 proxy.set_paintbrush_opacity(a1)
class TestInternalsOfPyArduino(unittest.TestCase): # pylint: disable=R0904 def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR) self.arduino.connect() def test_send_cmd(self): def valid_transformer1(arg1): return arg1 def valid_transformer2(arg1): # pylint: disable=W0613 return "RESPONSE_FROM_TRANSFORMER" def invalid_transformer1(): pass def invalid_transformer2(arg1, arg2): # pylint: disable=W0613 pass def invalid_transformer3(arg1): # pylint: disable=W0613 raise(Exception("Exception while transforming")) # send_cmd(self, cmd, expected_response=None, timeout=None, response_transformer=None): self.arduino.send_cmd("_ms", response_transformer=int) self.arduino.send_cmd("_ms", response_transformer=valid_transformer1) self.arduino.send_cmd("_ms", response_transformer=valid_transformer2, expected_response="RESPONSE_FROM_TRANSFORMER") self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer1) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer2) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", response_transformer=invalid_transformer3) self.assertRaises(InvalidResponse, self.arduino.send_cmd, "_ms", expected_response="X") self.assertRaises(InvalidCommand, self.arduino.send_cmd, "_INEXISTING_CMD") def tearDown(self): # pylint: disable=C0103 self.arduino.close()
def main(): # pylint: disable=R0915 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Test autoconnect() #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ logging.basicConfig(level=logging.DEBUG) print "Test autoconnect()" arduino = PyArduino() arduino.autoconnect() arduino.ping() arduino.close()
def start(self): (options, args) = self.parser.parse_args() self.validate(options, args) # setup logging if options.debug: logging.basicConfig(level=logging.DEBUG) elif options.info or options.arduino_debug: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.ERROR) serial_device = self.get_device(options, args) logging.info("Creating PyArduino instance - serial: %s - speed: %s", serial_device, 9600) if options.initial_wait == 0: arduino = PyArduino(serial_device, 9600, wait_after_open=0, call_validate_connection=not(options.dont_call_validate_connection)).connect() elif options.initial_wait is None: # TODO: move this logging to PyArduino logging.info("Waiting some seconds to let the Arduino reset...") arduino = PyArduino(serial_device, 9600, call_validate_connection=not(options.dont_call_validate_connection)).connect() else: initial_wait = int(options.initial_wait) if initial_wait > 0: # TODO: move this logging to PyArduino logging.info("Waiting %d seconds to let the Arduino reset...", initial_wait) arduino = PyArduino(serial_device, 9600, wait_after_open=initial_wait, call_validate_connection=not(options.dont_call_validate_connection)).connect() # enable debugging if required if options.arduino_debug: arduino.enableDebug() if self.auto_close: try: return self.run(options, args, arduino) except KeyboardInterrupt: print "" return None except Exception: raise finally: if self.auto_close: # While `run()` is executed, `self.auto_close` can become False arduino.close() else: return self.run(options, args, arduino)
def main(): print "Connecting to Arduino using", arduino_dev arduino = PyArduino() arduino.connect(arduino_dev, 9600) arduino.ping() print " + ping(): OK" print "midi.init()" pygame.midi.init() print "get_output_device_id()" device_id = get_output_device_id('VirMIDI 2-0') # # In console, run: # $ sudo modprobe snd-virmidi # assert device_id is not None print " + device:", device_id print "midi.Output()" midi_out = pygame.midi.Output(device_id, 0) print "midi_out.set_instrument()" midi_out.set_instrument(CHURCH_ORGAN) last_a0 = arduino.analogRead(0) last_a1 = arduino.analogRead(1) print "Entering loop()..." while True: a0 = arduino.analogRead(0) a1 = arduino.analogRead(1) if abs(a0 - last_a0) >= threshold: midi_out.note_on(NOTE_0, map_1024_to_midi(a0)) last_a0 = a0 print "a0 ->", a0 midi_out.note_off(NOTE_0) if abs(a1 - last_a1) >= threshold: midi_out.note_on(NOTE_1, map_1024_to_midi(a1)) last_a1 = a1 print "a1 ->", a1 midi_out.note_off(NOTE_1)
class TestPyArduinoWithInitialContentInSerialBuffer(unittest.TestCase): # pylint: disable=R0904 """ Testcase for commands. """ def setUp(self): # pylint: disable=C0103 self._INITIAL_OUT_BUFFER_CONTENTS = emulator.INITIAL_OUT_BUFFER_CONTENTS emulator.INITIAL_OUT_BUFFER_CONTENTS = "** SOME TEXT **\n" * 5 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR, call_validate_connection=False) self.arduino.connect() def test_ping(self): self.arduino.validateConnection() response = self.arduino.ping() self.assertEquals(response, 'PING_OK') def tearDown(self): # pylint: disable=C0103 emulator.INITIAL_OUT_BUFFER_CONTENTS = self._INITIAL_OUT_BUFFER_CONTENTS self.arduino.close()
class TestPyArduinoWithInitialContentInSerialBuffer(unittest.TestCase): # pylint: disable=R0904 """ Testcase for commands. """ def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR, call_validate_connection=True) self.arduino.connect() # Inject after connect() self.arduino.serial_port.inject_to_in_buffer("** SOME TEXT **\n" * 5) def test_ping_after_validateConnection(self): self.arduino.validateConnection() response = self.arduino.ping() self.assertEquals(response, 'PING_OK') def test_ping_without_validateConnection(self): self.assertRaises(InvalidResponse, self.arduino.ping) def tearDown(self): # pylint: disable=C0103 self.arduino.close()
def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR) self.arduino.connect()
class TestProxiedMethodsOfPyArduino(unittest.TestCase): # pylint: disable=R0904 """ Testcase for commands. """ def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR) self.arduino.connect() def test_ping(self): response = self.arduino.ping() self.assertEquals(response, 'PING_OK') def test_multiping(self): for i in range(0, 10): # pylint: disable=W0612 @UnusedVariable start = time.time() response = self.arduino.ping() end = time.time() self.assertEquals(response, 'PING_OK') logging.info("PING took %.2f ms", ((end - start) * 1000)) def test_analog_read(self): response = self.arduino.analogRead(5) self.assertTrue(type(response) is int) if response < 0 or response > 1023: self.fail("analogRead() returned invalid value: %d" % response) # test with invalid arguments for an_arg in (None, 'something', Exception(), 1.1): self.assertRaises(InvalidArgument, self.arduino.analogRead, an_arg) def test_digital_read(self): response = self.arduino.digitalRead(99) self.assertTrue(response in [HIGH, LOW]) # test with invalid arguments for an_arg in (None, 'something', Exception(), 1.1): self.assertRaises(InvalidArgument, self.arduino.digitalRead, an_arg) def test_digital_write(self): self.arduino.digitalWrite(99, HIGH) self.arduino.digitalWrite(99, LOW) # test with invalid arguments for an_arg in (None, 'something', Exception(), 1.1): self.assertRaises(InvalidArgument, self.arduino.digitalWrite, 99, an_arg) self.assertRaises(InvalidArgument, self.arduino.digitalWrite, an_arg, HIGH) self.assertRaises(InvalidArgument, self.arduino.digitalWrite, an_arg, LOW) def test_analog_write(self): for value in range(0, 256): self.arduino.analogWrite(99, value) # test with invalid arguments self.assertRaises(InvalidArgument, self.arduino.analogWrite, 99, -1) self.assertRaises(InvalidArgument, self.arduino.analogWrite, 99, 256) self.assertRaises(InvalidArgument, self.arduino.analogWrite, 99, 1000) for an_arg in (None, 'something', Exception(), 1.1): self.assertRaises(InvalidArgument, self.arduino.analogWrite, 99, an_arg) self.assertRaises(InvalidArgument, self.arduino.analogWrite, an_arg, 0) self.assertRaises(InvalidArgument, self.arduino.analogWrite, an_arg, 1) self.assertRaises(InvalidArgument, self.arduino.analogWrite, an_arg, 100) self.assertRaises(InvalidArgument, self.arduino.analogWrite, an_arg, 255) def test_pin_mode(self): self.arduino.pinMode(99, OUTPUT) self.arduino.pinMode(99, INPUT) # test with invalid arguments for an_arg in (None, 'something', Exception(), 1.1): self.assertRaises(InvalidArgument, self.arduino.pinMode, 99, an_arg) self.assertRaises(InvalidArgument, self.arduino.pinMode, an_arg, OUTPUT) self.assertRaises(InvalidArgument, self.arduino.pinMode, an_arg, INPUT) def test_delay(self): self.arduino.delay(0) self.arduino.delay(99) self.arduino.delayMicroseconds(0) self.arduino.delayMicroseconds(99) # test with invalid arguments for an_arg in (None, 'something', Exception(), -1): self.assertRaises(InvalidArgument, self.arduino.delay, an_arg) self.assertRaises(InvalidArgument, self.arduino.delayMicroseconds, an_arg) def test_millis_micros(self): self.assertTrue(self.arduino.millis() >= 0) self.assertTrue(self.arduino.micros() >= 0) def test_disableDebug(self): self.arduino.disableDebug() def test_enableDebug(self): self.arduino.enableDebug() def test_getArduinoTypeStruct(self): ard_type_st = self.arduino.getArduinoTypeStruct() self.assertTrue(ard_type_st['analog_pins']) self.assertTrue(ard_type_st['digital_pins']) self.assertTrue(ard_type_st['pwm_pins_bitmap']) self.assertTrue(ard_type_st['eeprom_size']) self.assertTrue(ard_type_st['flash_size']) self.assertTrue(ard_type_st['ram_size']) self.assertTrue(ard_type_st['pwm_pin_list']) self.assertTrue(ard_type_st['eeprom_size_bytes']) self.assertTrue(ard_type_st['flash_size_bytes']) self.assertTrue(ard_type_st['ram_size_bytes']) enhanced = self.arduino.enhanceArduinoTypeStruct(ard_type_st) self.assertTrue(enhanced['digital_pins_items']) self.assertTrue(enhanced['analog_pins_items']) self.assertTrue(enhanced['digital_pins_struct']) for item in enhanced['digital_pins_struct']: self.assertTrue('pin' in item) self.assertTrue('digital' in item) self.assertTrue('pwm' in item) # self.assertTrue('label' in item) self.assertTrue(enhanced['analog_pins_struct']) def test_getAvrCpuType(self): self.arduino.getAvrCpuType() def tearDown(self): # pylint: disable=C0103 self.arduino.close()
def setUp(self): # pylint: disable=C0103 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR, call_validate_connection=True) self.arduino.connect() # Inject after connect() self.arduino.serial_port.inject_to_in_buffer("** SOME TEXT **\n" * 5)
def setUp(self): # pylint: disable=C0103 self._INITIAL_OUT_BUFFER_CONTENTS = emulator.INITIAL_OUT_BUFFER_CONTENTS emulator.INITIAL_OUT_BUFFER_CONTENTS = "** SOME TEXT **\n" * 5 self.arduino = PyArduino(tty=DEVICE_FOR_EMULATOR, call_validate_connection=False) self.arduino.connect()
def start(self): (options, args) = self.parser.parse_args() self.validate(options, args) # setup logging if options.debug: logging.basicConfig(level=logging.DEBUG) elif options.info or options.arduino_debug: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.ERROR) serial_device = self.get_device(options, args) logging.info("Creating PyArduino instance - serial: %s - speed: %s", serial_device, 9600) if options.initial_wait == 0: arduino = PyArduino( serial_device, 9600, wait_after_open=0, call_validate_connection=not ( options.dont_call_validate_connection)).connect() elif options.initial_wait is None: # TODO: move this logging to PyArduino logging.info("Waiting some seconds to let the Arduino reset...") arduino = PyArduino( serial_device, 9600, call_validate_connection=not ( options.dont_call_validate_connection)).connect() else: initial_wait = int(options.initial_wait) if initial_wait > 0: # TODO: move this logging to PyArduino logging.info("Waiting %d seconds to let the Arduino reset...", initial_wait) arduino = PyArduino( serial_device, 9600, wait_after_open=initial_wait, call_validate_connection=not ( options.dont_call_validate_connection)).connect() # enable debugging if required if options.arduino_debug: arduino.enableDebug() if self.auto_close: try: return self.run(options, args, arduino) except KeyboardInterrupt: print "" return None except Exception: raise finally: if self.auto_close: # While `run()` is executed, `self.auto_close` can become False arduino.close() else: return self.run(options, args, arduino)
def test_autoconnect(self): logging.basicConfig(level=logging.DEBUG) arduino = PyArduino() arduino.autoconnect() arduino.ping() arduino.close()