本文整理汇总了Python中ustruct.unpack方法的典型用法代码示例。如果您正苦于以下问题:Python ustruct.unpack方法的具体用法?Python ustruct.unpack怎么用?Python ustruct.unpack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ustruct
的用法示例。
在下文中一共展示了ustruct.unpack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: measure
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def measure(self):
buf = self.buf
address = self.address
# wake sensor
try:
self.i2c.writeto(address, b'')
except OSError:
pass
# read 4 registers starting at offset 0x00
self.i2c.writeto(address, b'\x03\x00\x04')
# wait at least 1.5ms
time.sleep_ms(2)
# read data
self.i2c.readfrom_mem_into(address, 0, buf)
# debug print
print(ustruct.unpack('BBBBBBBB', buf))
crc = ustruct.unpack('<H', bytearray(buf[-2:]))[0]
if (crc != self.crc16(buf[:-2])):
raise Exception("checksum error")
示例2: acceleration
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def acceleration(self):
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
divider = 1
accel_range = self.range
if accel_range == RANGE_16_G:
divider = 1365
elif accel_range == RANGE_8_G:
divider = 4096
elif accel_range == RANGE_4_G:
divider = 8190
elif accel_range == RANGE_2_G:
divider = 16380
x, y, z = struct.unpack('<hhh', self._read_register(_REG_OUT_X_L | 0x80, 6))
# convert from Gs to m / s ^ 2 and adjust for the range
x = (x / divider) * STANDARD_GRAVITY
y = (y / divider) * STANDARD_GRAVITY
z = (z / divider) * STANDARD_GRAVITY
return AccelerationTuple(x, y, z)
示例3: finger_fast_search
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def finger_fast_search(self):
"""Asks the sensor to search for a matching fingerprint template to the
last model generated. Stores the location and confidence in self.finger_id
and self.confidence. Returns the packet error code or OK success"""
# high speed search of slot #1 starting at page 0x0000 and page #0x00A3
# self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
# or page #0x03E9 to accommodate modules with up to 1000 capacity
# self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x03, 0xE9])
# or base the page on module's capacity
self.read_sysparam()
capacity = self.library_size
self._send_packet(
[_HISPEEDSEARCH, 0x01, 0x00, 0x00, capacity >> 8, capacity & 0xFF]
)
r = self._get_packet(16)
self.finger_id, self.confidence = struct.unpack(">HH", bytes(r[1:5]))
# print(r)
return r[0]
##################################################
示例4: from_buffer
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def from_buffer(data):
"""
SMP code is the first octet of the PDU
0 1 2 3 4 5 6 7
-----------------
| code |
-----------------
References can be found here:
* https://www.bluetooth.org/en-us/specification/adopted-specifications
** Core specification 4.1
** [vol 3] Part H (Section 3.3) - Command Format
"""
code = ustruct.unpack(SMP.struct_format, data[:SMP.struct_size])[0]
data = data[SMP.struct_size:]
return SMP(code, data)
示例5: from_buffer
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def from_buffer(data):
"""
Parse HCI event data
References can be found here:
* https://www.bluetooth.org/en-us/specification/adopted-specifications
- Core specification 4.1
** [vol 2] Part E (Section 5) - HCI Data Formats
** [vol 2] Part E (Section 5.4) - Exchange of HCI-specific information
** [vol 2] Part E (Section 7.7) - Events
** [vol 2] Part E (Section 7.7.65) - Le Meta Event
All integer values are stored in "little-endian" order.
"""
evtcode, _ = ustruct.unpack(
HCI_EVENT.struct_format,
data[:HCI_EVENT._struct_size]
)
data = data[HCI_EVENT._struct_size:]
return HCI_EVENT(evtcode, data=data)
示例6: from_buffer
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def from_buffer(data):
"""
Parse the signaling channel data.
The signaling channel is a L2CAP packet with channel id 0x0001 (L2CAP CID_SCH)
or 0x0005 (L2CAP_CID_LE_SCH)
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
-----------------------------------------------------------------
| code | cid | length |
-----------------------------------------------------------------
References can be found here:
* https://www.bluetooth.org/en-us/specification/adopted-specifications
** Core specification 4.1
** [vol 3] Part A (Section 4) - Signaling Packet Formats
"""
code, cid, _ = ustruct.unpack(
L2CAP_SCH.struct_format,
data[:L2CAP_SCH.struct_size]
)
data = data[L2CAP_SCH.struct_size:]
return L2CAP_SCH(code, cid, data)
示例7: getTemp
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def getTemp(bus):
for _ in range(3):
try:
bus.write_byte(0x5C, 0x00)
except:
pass
bus.write_i2c_block_data(0x5C, 0x03, [0x02, 2])
time.sleep(0.001)
result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6))
if result[0] != 0x3 or result[1] != 2:
raise AM2320ReadError("Command does not match returned data")
temp = struct.unpack(">H", result[2:-2])[0]
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
crc2 = _crc16(result[0:-2])
if crc1 != crc2:
raise AM2320ReadError("CRC Mismatch")
if temp >= 32768:
temp = 32768 - temp
return (temp / 10.0)
示例8: getHumi
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def getHumi(bus):
for _ in range(3):
try:
bus.write_byte(0x5C, 0x00)
except:
pass
bus.write_i2c_block_data(0x5C, 0x03, [0x00, 2])
time.sleep(0.001)
result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6))
if result[0] != 0x3 or result[1] != 2:
raise AM2320ReadError("Command does not match returned data")
humi = struct.unpack(">H", result[2:-2])[0]
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
crc2 = _crc16(result[0:-2])
if crc1 != crc2:
raise AM2320ReadError("CRC Mismatch")
return (humi / 10.0)
示例9: _read_calibration
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def _read_calibration(self):
"""Read & save the calibration coefficients"""
coeff = self._read(_BME680_BME680_COEFF_ADDR1, 25)
coeff += self._read(_BME680_BME680_COEFF_ADDR2, 16)
coeff = list(struct.unpack("<hbBHhbBhhbbHhhBBBHbbbBbHhbb", bytes(coeff[1:39])))
# print("\n\n",coeff)
coeff = [float(i) for i in coeff]
self._temp_calibration = [coeff[x] for x in [23, 0, 1]]
self._pressure_calibration = [
coeff[x] for x in [3, 4, 5, 7, 8, 10, 9, 12, 13, 14]
]
self._humidity_calibration = [coeff[x] for x in [17, 16, 18, 19, 20, 21, 22]]
self._gas_calibration = [coeff[x] for x in [25, 24, 26]]
# flip around H1 & H2
self._humidity_calibration[1] *= 16
self._humidity_calibration[1] += self._humidity_calibration[0] % 16
self._humidity_calibration[0] /= 16
self._heat_range = (self._read_byte(0x02) & 0x30) / 16
self._heat_val = self._read_byte(0x00)
self._sw_err = (self._read_byte(0x04) & 0xF0) / 16
示例10: moisture_read
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def moisture_read(self):
"""Read the value of the moisture sensor"""
buf = bytearray(2)
self.read(_TOUCH_BASE, _TOUCH_CHANNEL_OFFSET, buf, 0.005)
ret = struct.unpack(">H", buf)[0]
time.sleep(0.001)
# retry if reading was bad
count = 0
while ret > 4095:
self.read(_TOUCH_BASE, _TOUCH_CHANNEL_OFFSET, buf, 0.005)
ret = struct.unpack(">H", buf)[0]
time.sleep(0.001)
count += 1
if count > 3:
raise RuntimeError("Could not get a valid moisture reading.")
return ret
示例11: parse_response
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def parse_response(self, rsp_data, msg_buffer):
msg_args = []
try:
msg_type, msg_id, h_data = struct.unpack('!BHH', rsp_data[:self.MSG_HEAD_LEN])
except Exception as p_err:
raise BlynkError('Message parse error: {}'.format(p_err))
if msg_id == 0:
raise BlynkError('invalid msg_id == 0')
elif h_data >= msg_buffer:
raise BlynkError('Command too long. Length = {}'.format(h_data))
elif msg_type in (self.MSG_RSP, self.MSG_PING):
pass
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_INTERNAL, self.MSG_REDIRECT):
msg_body = rsp_data[self.MSG_HEAD_LEN: self.MSG_HEAD_LEN + h_data]
msg_args = [itm.decode('utf-8') for itm in msg_body.split(b'\0')]
else:
raise BlynkError("Unknown message type: '{}'".format(msg_type))
return msg_type, msg_id, h_data, msg_args
示例12: _read_coefficients
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def _read_coefficients(self):
"""Read & save the calibration coefficients"""
coeff = self._read_register(_BME280_REGISTER_DIG_T1, 24)
coeff = list(struct.unpack("<HhhHhhhhhhhh", bytes(coeff)))
coeff = [float(i) for i in coeff]
self._temp_calib = coeff[:3]
self._pressure_calib = coeff[3:]
self._humidity_calib = [0] * 6
self._humidity_calib[0] = self._read_byte(_BME280_REGISTER_DIG_H1)
coeff = self._read_register(_BME280_REGISTER_DIG_H2, 7)
coeff = list(struct.unpack("<hBbBbb", bytes(coeff)))
self._humidity_calib[1] = float(coeff[0])
self._humidity_calib[2] = float(coeff[1])
self._humidity_calib[3] = float((coeff[2] << 4) | (coeff[3] & 0xF))
self._humidity_calib[4] = float((coeff[4] << 4) | (coeff[3] >> 4))
self._humidity_calib[5] = float(coeff[5])
示例13: acceleration
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def acceleration(self):
"""The x, y, z acceleration values returned in a 3-tuple in m / s ^ 2."""
x, y, z = unpack("<hhh", self._read_register(_REG_DATAX0, 6))
x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
y = y * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
z = z * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
return (x, y, z)
示例14: _read_register_unpacked
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def _read_register_unpacked(self, register):
return unpack("<b", self._read_register(register, 1))[0]
示例15: init
# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import unpack [as 别名]
def init(self):
# Open the font file and grab the character width and height values.
# Note that only fonts up to 8 pixels tall are currently supported.
self._font = open(self._font_name, 'rb')
self._font_width, self._font_height = ustruct.unpack('BB', self._font.read(2))