def get_device_info(self): """ Return information about the device. :return: A list containing board type, major and minor firmware versions, 16 byte unique identifier, \ microcontroller part and bootcode version numbers. """ sfp_code, args = decode_sfp(self.low_level_io(1, encode_sfp(255, []))) if sfp_code != 255: errmsg("IoTPy error: get_device_info wrong code.") # raise IoTPy_APIError("") device_data = args if device_data[0] >> 24 != 0x55: # 0x55 = 'U' print("IoTPy error: getDeviceInfo unknown device/firmware type") # return device_info = { 'fw_maj': (device_data[0] & 0x00ff0000) >> 16, # Firmware major version number 'fw_min': device_data[0] & 0x0000ffff, # Firmware minor version number 'uid': UUID(bytes=device_data[1]), # 128bit unique ID 'cpu_id': device_data[2], # UPER LPC CPU part number 'boot': device_data[3], # UPER LPC CPU bootload code version } return device_info
def read_pulse(self, level=HIGH, timeout=100000): if self.direction != self.INPUT: self.setup(self.INPUT, self.resistor) return decode_sfp( self.board.low_level_io( 1, encode_sfp(9, [self.logical_pin, level, timeout])))[1][0]
def transaction(self, address, write_data, read_length): """ Perform I2C data transaction. I2C data transaction consists of (optional) write transaction, followed by (optional) read transaction. :param address: I2C device address. :type address: int :param write_data: A byte sequence to be transmitted. If write_data is empty string, no write transaction \ will be executed. :type write_data: str :param read_length: A number of bytes to be received. If read_length is 0, no read transaction will be executed. :type read_length: int :return: Received data and I2C transaction status/error code. :rtype: (str, int) :raise: IoTPy_APIError, IoTPy_ThingError """ try: result = decode_sfp(self.board.low_level_io(1, encode_sfp(41, [address, write_data, read_length]))) except IoTPy_APIError: errmsg("UPER API: I2C bus not connected.") raise IoTPy_IOError("I2C bus not connected.") return result[1][1], result[1][2] # return read buffer and error
def read_raw(self): """ Read ADC value :return: Raw ADC value. :rtype: int """ return decode_sfp(self.board.low_level_io(1, encode_sfp(10, [self.adc_pin])))[1][1]
def read_raw(self): """ Read ADC value :return: Raw ADC value. :rtype: int """ return decode_sfp( self.board.low_level_io(1, encode_sfp(10, [self.adc_pin])))[1][1]
def test_decode_sfp(self): decode_result = sfp.decode_sfp( b'\xd4\x00i\xff\xc4P09876543210987654321098765432109876543210987654321098765432109876543210987654321J0987654321\x00\x01\xc0@\xc1\x17p\xc2\x01\x11p' ) sfp_parameters = [ b'09876543210987654321098765432109876543210987654321098765432109876543210987654321', b'0987654321', 0, 1, 64, 6000, 70000 ] sfp_command = 255 self.assertEqual(decode_result, (sfp_command, sfp_parameters))
def read(self): """ Read a digital signal value. If GPIO pis in not configure as input, set it to GPIO.PULL_UP pin mode. :return: Digital signal value: 0 (LOW) or 1 (HIGH). :rtype: int """ if self.direction != self.INPUT: self.setup(self.INPUT, self.resistor) return decode_sfp( self.board.low_level_io(1, encode_sfp(5, [self.logical_pin])))[1][1]
def transaction(self, write_data, read_from_slave=True): """ Perform SPI data transaction. :param write_data: Data to be shifted on MOSI line. :type write_data: str :param read_from_slave: Flag indicating whether the data received on MISO line should be ignored or not. Optional, default True. :type read_from_slave: bool :return: Data received on MISO line, if read_from_slave is True. :rtype: str """ res = self.board.low_level_io(read_from_slave, encode_sfp(21 + self.port * 10, [write_data, int(read_from_slave)])) if res: return decode_sfp(res)[1][0]
def detect_sfp_serial(uid=None): ports_list = [] my_platform = platform.system() if uid: uid = UUID(uid) if my_platform == "Windows": for i in range(256): try: serial_tmp = serial.Serial("COM" + str(i)) ports_list.append(serial_tmp.portstr) serial_tmp.close() except serial.SerialException: pass elif my_platform == "Darwin": ports_list = glob.glob("/dev/tty.usbmodem*") elif my_platform == "Linux": ports_list = glob.glob("/dev/ttyACM*") for my_port in ports_list: try: port_to_try = serial.Serial( port=my_port, baudrate=230400, # virtual com port on USB is always max speed parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1, ) komanda_siuntimui = encode_sfp(255, []) port_to_try.write(komanda_siuntimui) response = port_to_try.read(1) # read one, blocking n = port_to_try.inWaiting() # look if there is more if n: response = response + port_to_try.read(n) sfp = decode_sfp(response) if sfp[0] == 255: # device info sfp packet dev_uid = UUID(bytes=sfp[1][1]) if not uid or uid == dev_uid: return port_to_try port_to_try.close() except: pass raise IoTPy_APIError("No SFP device was found on serial ports.")
def detect_sfp_serial(uid=None): ports_list = [] my_platform = platform.system() if uid: uid = UUID(uid) if my_platform == "Windows": for i in range(256): try: serial_tmp = serial.Serial('COM' + str(i)) ports_list.append(serial_tmp.portstr) serial_tmp.close() except serial.SerialException: pass elif my_platform == "Darwin": ports_list = glob.glob("/dev/tty.usbmodem*") elif my_platform == "Linux": ports_list = glob.glob("/dev/ttyACM*") for my_port in ports_list: try: port_to_try = serial.Serial( port=my_port, baudrate=230400, # virtual com port on USB is always max speed parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1) komanda_siuntimui = encode_sfp(255, []) port_to_try.write(komanda_siuntimui) response = port_to_try.read(1) # read one, blocking n = port_to_try.inWaiting() # look if there is more if n: response = response + port_to_try.read(n) sfp = decode_sfp(response) if sfp[0] == 255: # device info sfp packet dev_uid = UUID(bytes=sfp[1][1]) if not uid or uid == dev_uid: return port_to_try port_to_try.close() except: pass raise IoTPy_APIError("No SFP device was found on serial ports.")
def read(self): """ Read a digital port value. If GPIO port in not configure as input, set it to GPIO.PULL_UP pin mode. :return: Digital port value. :rtype: int """ if self.direction != GPIO.INPUT: self.setup(GPIO.INPUT, self.resistor) values = decode_sfp( self.board.low_level_io(1, encode_sfp(5, [self._logical_pins])))[1][1] value = 0 for i, bit in enumerate(values): value |= (ord(bit) & 0x1) << i return value
def test_simple_sfp_decode(self): self.assertEqual(sfp.decode_sfp(b'\xd4\x00\x01\xff'),(255, []))
def test_simple_sfp_decode(self): self.assertEqual(sfp.decode_sfp(b'\xd4\x00\x01\xff'), (255, []))
def test_decode_sfp(self): decode_result = sfp.decode_sfp(b'\xd4\x00i\xff\xc4P09876543210987654321098765432109876543210987654321098765432109876543210987654321J0987654321\x00\x01\xc0@\xc1\x17p\xc2\x01\x11p') sfp_parameters = [b'09876543210987654321098765432109876543210987654321098765432109876543210987654321', b'0987654321', 0, 1, 64, 6000, 70000] sfp_command = 255 self.assertEqual(decode_result, (sfp_command, sfp_parameters))
def execute_sfp(self, binary_sfp_command): decoded_sfp_command = decode_sfp(binary_sfp_command) results = self.sfp_comands[decoded_sfp_command[0]]( decoded_sfp_command[1]) if results: return encode_sfp(decoded_sfp_command[0], results)
def execute_sfp(self, binary_sfp_command): decoded_sfp_command = decode_sfp(binary_sfp_command) results = self.sfp_comands[decoded_sfp_command[0]](decoded_sfp_command[1]) if results: return encode_sfp(decoded_sfp_command[0], results)