Exemplo n.º 1
0
    def digital_write(self, pin_number, digital_value):
        """
        Digital write to a pin with data

        :param pin_number: int
        :param digital_value: int
        :return: boolean
        """

        if not ParameterValidator.validate_pin_number(pin_number):
            return False

        if not ParameterValidator.validate_digital_range(digital_value):
            return False

        print_verbose('Controller.digital_write.writing.to')

        try:
            command = ProtocolStringFormatter.format_digital_write(
                pin_number, digital_value)
            if not ProtocolFormatValidator.validate_write_action(command):
                return False

            print_verbose('Controller.digital_write.writing.to.with',
                          digital_value,
                          pin_number,
                          indent=1)
            self.conn.write(command.encode())
            return True
        except Exception as e:
            print('\t[-]', e)
            return False
Exemplo n.º 2
0
    def test_positive_parameter_validator_pin_mode(self):
        self.assertEqual(ParameterValidator.validate_pin_modes('o'), True)
        self.assertEqual(ParameterValidator.validate_pin_modes('O'), True)
        self.assertEqual(ParameterValidator.validate_pin_modes('P'), True)

        for i in VALID_PIN_MODES.keys():
            self.assertEqual(ParameterValidator.validate_pin_modes(i), True)

        for i in VALID_PIN_MODES.keys():
            self.assertEqual(ParameterValidator.validate_pin_modes(i.lower()),
                             True)
Exemplo n.º 3
0
    def analog_read(self, pin_number):
        """
        Analog read on a pin. Arduino can only return from 0 to 1023

        :param pin_number: int
        :return: boolean
        """
        if not ParameterValidator.validate_pin_number(pin_number):
            return False

        print_verbose('Controller.analog_read.reading.from')

        try:
            command = ProtocolStringFormatter.format_analog_read(pin_number)
            if not ProtocolFormatValidator.validate_read_action(command):
                return False

            self.conn.write(command.encode())
            print_verbose('Controller.analog_read.reading.from.pin',
                          pin_number)
            line_received = self.conn.readline().decode().strip()
            header, value = line_received.split(':')
        except Exception as e:
            print('\t[-]', e)
            return -1

        if header == ('A' + str(pin_number)):
            return int(value)
        return -1
Exemplo n.º 4
0
    def digital_read(self, pin_number):
        """
        Digital read on a pin

        :param pin_number: int
        :return: int
        """
        if not ParameterValidator.validate_pin_number(pin_number):
            return False

        print_verbose('Controller.digital_read.reading.from')

        try:
            command = ProtocolStringFormatter.format_digital_read(pin_number)
            if not ProtocolFormatValidator.validate_read_action(command):
                return False

            self.conn.write(command.encode())
            print_verbose('Controller.digital_read.reading.from.pin',
                          pin_number,
                          indent=1)
            line_received = self.conn.readline().decode().strip()
            header, value = line_received.split(':')
        except Exception as e:
            print('\t[-]', e)
            return -1

        if header and header == ('D' + str(pin_number)):
            return int(value)
        return -1
Exemplo n.º 5
0
 def test_negative_parameter_validator_pin_mode(self):
     self.assertEqual(ParameterValidator.validate_pin_modes('h'), False)
     self.assertEqual(ParameterValidator.validate_pin_modes('L'), False)
     self.assertEqual(ParameterValidator.validate_pin_modes([]), False)
     self.assertEqual(ParameterValidator.validate_pin_modes({}), False)
     self.assertEqual(ParameterValidator.validate_pin_modes(()), False)
     self.assertEqual(ParameterValidator.validate_pin_modes(True), False)
     self.assertEqual(ParameterValidator.validate_pin_modes(False), False)
Exemplo n.º 6
0
    def __init__(self, conn: Connector, pin_number: int, mode: str = OUTPUT):
        PinInterface.__init__(self)

        if not ParameterValidator.validate_pin_number(pin_number):
            raise ValueError("pin_number must be a valid digital pin")

        if not Controller().set_pin_mode(pin_number, mode=mode):
            raise Exception("Failed to set pin " + str(pin_number) + " to " +
                            mode)

        self.conn = conn
        self.pin_number = pin_number
        self.mode = mode
Exemplo n.º 7
0
    def test_negative_parameter_validator_digital_range(self):
        for i in range(-50, 0):
            self.assertEqual(ParameterValidator.validate_digital_range(i),
                             False)
            self.assertEqual(ParameterValidator.validate_digital_range(str(i)),
                             False)

        for i in range(2, 50):
            self.assertEqual(ParameterValidator.validate_digital_range(i),
                             False)
            self.assertEqual(ParameterValidator.validate_digital_range(str(i)),
                             False)

        self.assertEqual(ParameterValidator.validate_digital_range([]), False)
        self.assertEqual(ParameterValidator.validate_digital_range({}), False)
        self.assertEqual(ParameterValidator.validate_digital_range(()), False)
        self.assertEqual(ParameterValidator.validate_digital_range(False),
                         False)
        self.assertEqual(ParameterValidator.validate_digital_range(True),
                         False)
Exemplo n.º 8
0
    def set_pin_mode(self, pin_number, mode='OUTPUT'):
        """
        Sets a pin with a mode

        :param pin_number:  int
        :param mode: string specified in self.pin_modes
        :return: boolean
        """
        if not ParameterValidator.validate_pin_number(pin_number):
            return False

        if not ParameterValidator.validate_pin_modes(mode):
            return False

        print_verbose('Controller.set_pin_mode.pin.mode')

        try:
            command = ProtocolStringFormatter.format_single_pin_mode(
                pin_number, mode)
            if not ProtocolFormatValidator.validate_pin_mode(command):
                return False

            print_verbose('Controller.set_pin_mode.pin.mode.as',
                          pin_number,
                          mode,
                          indent=1)
            self.conn.write(command.encode())
            print_verbose('Controller.set_pin_mode.saving.as',
                          pin_number,
                          mode,
                          indent=1)
            self.currentActivePins.append({'pin': pin_number, 'mode': mode})
            return True
        except Exception as e:
            print('\t[-]', e)
            return False
Exemplo n.º 9
0
    def test_negative_parameter_validator_is_digit(self):
        printable_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
        for i in printable_characters:
            self.assertEqual(ParameterValidator.is_digit(i), False)

        self.assertEqual(ParameterValidator.is_digit([]), False)
        self.assertEqual(ParameterValidator.is_digit({}), False)
        self.assertEqual(ParameterValidator.is_digit(()), False)
        self.assertEqual(ParameterValidator.is_digit(True), False)
        self.assertEqual(ParameterValidator.is_digit(False), False)
Exemplo n.º 10
0
 def test_positive_parameter_validator_digital_range(self):
     self.assertEqual(ParameterValidator.validate_digital_range(0), True)
     self.assertEqual(ParameterValidator.validate_digital_range(1), True)
     self.assertEqual(ParameterValidator.validate_digital_range('HIGH'),
                      True)
     self.assertEqual(ParameterValidator.validate_digital_range('high'),
                      True)
     self.assertEqual(ParameterValidator.validate_digital_range('LOW'),
                      True)
     self.assertEqual(ParameterValidator.validate_digital_range('low'),
                      True)
Exemplo n.º 11
0
    def test_negative_parameter_validator_analog_range(self):
        for i in range(-50, 0):
            self.assertEqual(ParameterValidator.validate_analog_range(i),
                             False)

        for i in range(1024, 1100):
            self.assertEqual(ParameterValidator.validate_analog_range(i),
                             False)

        self.assertEqual(ParameterValidator.validate_analog_range([]), False)
        self.assertEqual(ParameterValidator.validate_analog_range({}), False)
        self.assertEqual(ParameterValidator.validate_analog_range(()), False)
        self.assertEqual(ParameterValidator.validate_analog_range(True), False)
        self.assertEqual(ParameterValidator.validate_analog_range(False),
                         False)
Exemplo n.º 12
0
 def test_positive_parameter_validator_is_digit(self):
     for i in range(0, 10):
         self.assertEqual(ParameterValidator.is_digit(i), True)
         self.assertEqual(ParameterValidator.is_digit(str(i)), True)
Exemplo n.º 13
0
    def test_negative_parameter_validator_pin_number(self):
        for i in range(-50, 1):
            self.assertEqual(ParameterValidator.validate_pin_number(i), False)
            self.assertEqual(ParameterValidator.validate_pin_number(str(i)),
                             False)

        for i in range(14, 50):
            self.assertEqual(ParameterValidator.validate_pin_number(i), False)
            self.assertEqual(ParameterValidator.validate_pin_number(str(i)),
                             False)

        self.assertEqual(ParameterValidator.validate_pin_number(''), False)
        self.assertEqual(ParameterValidator.validate_pin_number('a'), False)
        self.assertEqual(ParameterValidator.validate_pin_number('asDFs'),
                         False)
        self.assertEqual(ParameterValidator.validate_pin_number([]), False)
        self.assertEqual(ParameterValidator.validate_pin_number({}), False)
        self.assertEqual(ParameterValidator.validate_pin_number(()), False)
        self.assertEqual(ParameterValidator.validate_pin_number(True), False)
        self.assertEqual(ParameterValidator.validate_pin_number(False), False)
Exemplo n.º 14
0
 def test_positive_parameter_validator_pin_number(self):
     for i in range(1, 14):
         self.assertEqual(ParameterValidator.validate_pin_number(i), True)
         self.assertEqual(ParameterValidator.validate_pin_number(str(i)),
                          True)
Exemplo n.º 15
0
 def test_positive_parameter_validator_analog_range(self):
     for i in range(0, 1024):
         self.assertEqual(ParameterValidator.validate_analog_range(i), True)
         self.assertEqual(ParameterValidator.validate_analog_range(str(i)),
                          True)