def actuate(terminal):
    connection = SerialConnection(port=terminal)
    arduinoDue = ArduinoInterface(connection, boards.Due)

    arduinoDue.set_precision(12)
    arduinoDue.add_input(54)
    arduinoDue.add_output(40)

    last_time = time.time()
    start_time = last_time
    read_c = 0

    diff = last_time - start_time
    measures = []
    while diff < 10:
        print "Reading... {0}".format(diff)
        results = arduinoDue.actuate([(40,1)])
        measures.append(results)
        last_time = time.time()
        diff = last_time - start_time

    # Process samples
    samples = []
    for element in measures:
        for i in xrange(len(element)):
            samples.append(int(element[i][1]))

    # print "{0}".format(samples)
    print "Samples len: {0}".format(len(samples))

    plt.clf()
    plt.plot(samples)
    plt.show(block=True)
def actuate(terminal):
    connection = SerialConnection(port=terminal)
    arduinoDue = ArduinoInterface(connection, boards.Due)

    arduinoDue.reset()
    arduinoDue.set_report_mode(REPORT_MODES.BULK, read_count=5, read_delay=100)
    arduinoDue.add_output(40)
    arduinoDue.add_input(64)
    arduinoDue.add_input(63)
    #    arduinoDue.add_input(2)

    output = arduinoDue.actuate([(40, 1)])
    for i in output.keys():
        print i
        print output[i]
def actuate(terminal):
    global BREAK_COUNT
    connection = SerialConnection(port=terminal)
    arduinoDue = ArduinoInterface(connection, boards.Due)

    arduinoDue.reset()  #Just in case
    arduinoDue.set_report_mode(protocol.REPORT_MODES.AVERAGE,
                               read_count=100,
                               read_delay=0)
    arduinoDue.set_precision(12)

    arduinoDue.add_output(40)
    arduinoDue.add_output(66)
    #    arduinoDue.add_input(65)
    #    arduinoDue.add_input(64)
    #    arduinoDue.add_input(63)
    #    arduinoDue.add_input(62)
    #    arduinoDue.add_input(61)
    #    arduinoDue.add_input(60)
    #    arduinoDue.add_input(59)
    #    arduinoDue.add_input(58)
    #    arduinoDue.add_input(57)
    #    arduinoDue.add_input(56)
    #    arduinoDue.add_input(55)
    arduinoDue.add_input(54)
    arduinoDue.add_input(55)
    arduinoDue.add_input(56)

    last_time = time.time()
    start_time = last_time
    read_c = 0

    for i in xrange(0, BREAK_COUNT):
        output = arduinoDue.actuate([(40, 1), (66, 255)])
        #print output
        read_c = read_c + 1
        new_time = time.time()

        if (new_time - start_time) > 1:
            print str(read_c - 1)  # The current read was out of the period
            read_c = 1
            start_time = new_time

        last_time = new_time
class TestArduinoInterface(unittest.TestCase):
    def setUp(self):
        self._connection = MockConnection(ACK)
        self._interface = ArduinoInterface(self._connection, boards.Due)

    def test_set_precision(self):
        self._interface.set_precision(12)
        data = self._connection.pop_data()
        self.assertEqual("\x01\x00\x00\x00\x01\x0C", data)
        with self.assertRaises(ValueError):
            self._interface.set_precision(33)

    def test_report_mode(self):
        self._interface.set_report_mode(REPORT_MODES.AVERAGE)
        data = self._connection.pop_data()
        self.assertEqual("\x05\x00\x00\x00\x03\x00\x00\x00", data)
        self._interface.set_report_mode(REPORT_MODES.AVERAGE,
                                        read_count=10,
                                        read_delay=5)
        data = self._connection.pop_data()
        self.assertEqual("\x05\x00\x00\x00\x03\x00\x09\x05", data)
        with self.assertRaises(ValueError):
            self._interface.set_report_mode(12334)  # Invalid report mode

    def test_add_input(self):
        self._interface.add_input(60)
        data = self._connection.pop_data()
        self.assertEqual("\x02\x00\x00\x00\x01\x3C", data)
        data = self._connection.pop_data()
        self.assertEqual("\x04\x00\x00\x00\x02\x3C\x00", data)
        self._interface.add_input(60)
        with self.assertRaises(Exception):  # Checks that no data has been sent
            data = self._connection.pop_data()
        with self.assertRaises(
                ValueError):  # Checks that no data has been sent
            self._interface.add_output(128)

    def test_add_output(self):
        self._interface.add_output(60)
        data = self._connection.pop_data()
        self.assertEqual("\x03\x00\x00\x00\x01\x3C", data)
        data = self._connection.pop_data()
        self.assertEqual("\x04\x00\x00\x00\x02\x3C\x01", data)
        self._interface.add_output(60)
        with self.assertRaises(Exception):  # Checks that no data has been sent
            data = self._connection.pop_data()
        with self.assertRaises(
                ValueError):  # Checks that no data has been sent
            self._interface.add_output(128)

    def test_error_adding_output_that_is_input(self):
        self._interface.add_input(60)
        with self.assertRaises(ProtocolSetupException):
            self._interface.add_output(60)

    def test_actuate_error_response(self):
        self._interface.add_output(60)
        self._connection.pop_data()
        with self.assertRaises(ProtocolIOException):
            self._interface.actuate([(60, 128)])

    def test_actuate_with_one_read(self):
        self._connection = MockConnection(REPORT)
        self._interface = ArduinoInterface(self._connection, boards.Due)
        self._interface.add_output(60)
        self._interface.add_input(16)
        self._interface.add_input(61)
        response = self._interface.actuate([(60, 128)])
        self.assertEqual(1, len(response["D16"]))
        self.assertTrue(response["D16"][0])
        self.assertEqual(1, len(response["A7"]))
        self.assertEqual(0x0520, response["A7"][0])

    def test_actuate_with_many_readings(self):
        self._connection = MockConnection(REPORT_B)
        self._interface = ArduinoInterface(self._connection, boards.Due)
        self._interface.set_report_mode(REPORT_MODES.BULK,
                                        read_count=11,
                                        read_delay=5)
        self._interface.add_output(60)
        self._interface.add_input(61)
        self._interface.add_input(16)
        response = self._interface.actuate([(60, 128)])
        self.assertEqual(2, len(response))
        self.assertTrue("D16" in response.keys())
        self.assertEqual(11, len(response["D16"]))
        self.assertTrue(response["D16"][0])
        self.assertTrue(response["D16"][6])
        self.assertTrue(response["D16"][7])
        self.assertFalse(response["D16"][8])
        self.assertTrue(response["D16"][9])
        self.assertFalse(response["D16"][10])
        for i in range(0, 11):
            self.assertEqual(0x0100, response["A7"][i])

    def test_actuate_with_many_pins_readings(self):
        self._connection = MockConnection(REPORT_C)
        self._interface = ArduinoInterface(self._connection, boards.Due)
        self._interface.set_report_mode(REPORT_MODES.BULK,
                                        read_count=5,
                                        read_delay=5)
        self._interface.add_output(60)  # Pin A6
        self._interface.add_input(61)  # Pin A7
        self._interface.add_input(62)  # Pin A8
        response = self._interface.actuate([(60, 128)])
        for i in range(0, 5):
            self.assertEqual(0x0100, response["A7"][i])
        for i in range(0, 5):
            self.assertEqual(0x0100, response["A8"][i])

    def test_average(self):
        self._connection = MockConnection(REPORT_B)
        self._interface = ArduinoInterface(self._connection, boards.Due)
        self._interface.set_report_mode(REPORT_MODES.AVERAGE,
                                        read_count=11,
                                        read_delay=5)
        self._interface.add_output(boards.Due["ANALOG_PINS"][6])
        self._interface.add_input(boards.Due["ANALOG_PINS"][7])  # Pin A7
        self._interface.add_input(boards.Due["DIGITAL_PINS"][16])
        response = self._interface.actuate([(60, 128)])
        self.assertEqual(2, len(response))
        self.assertEqual(1, len(response["D16"]))
        self.assertTrue(response["D16"][0])
        self.assertEqual(0x0100, response["A7"][0])