Python ustruct.pack_into() Examples
The following are 30
code examples of ustruct.pack_into().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ustruct
, or try the search function
.
Example #1
Source File: mqtt_as.py From micropython-mqtt with MIT License | 6 votes |
def subscribe(self, topic, qos): pkt = bytearray(b"\x82\0\0\0") pid = next(self.newpid) self.rcv_pids.add(pid) struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, pid) async with self.lock: await self._as_write(pkt) await self._send_str(topic) await self._as_write(qos.to_bytes(1, "little")) if not await self._await_pid(pid): raise OSError(-1) # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .setup() method. Other (internal) MQTT # messages processed internally. # Immediate return if no data available. Called from ._handle_msg().
Example #2
Source File: mqtt_as.py From microhomie with MIT License | 6 votes |
def unsubscribe(self, topic): pkt = bytearray(b"\xa2\0\0\0") pid = next(self.newpid) self.rcv_pids.add(pid) struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic), pid) async with self.lock: await self._as_write(pkt) await self._send_str(topic) if not await self._await_pid(pid): raise OSError(-1) # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .setup() method. Other (internal) MQTT # messages processed internally. # Immediate return if no data available. Called from ._handle_msg().
Example #3
Source File: mqtt_as.py From microhomie with MIT License | 6 votes |
def _publish(self, topic, msg, retain, qos, dup, pid): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain | dup << 3 sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 if sz >= 2097152: raise MQTTException('Strings too long.') i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz await self._as_write(pkt, i + 1) await self._send_str(topic) if qos > 0: struct.pack_into("!H", pkt, 0, pid) await self._as_write(pkt, 2) await self._as_write(msg) # Can raise OSError if WiFi fails. Subclass traps
Example #4
Source File: simple.py From xbee-micropython with MIT License | 6 votes |
def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1, "little")) while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) return # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .set_callback() method. Other (internal) MQTT # messages processed internally.
Example #5
Source File: umqtt.py From pyaiot with BSD 3-Clause "New" or "Revised" License | 6 votes |
def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1)) while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) return # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .set_callback() method. Other (internal) MQTT # messages processed internally.
Example #6
Source File: mqtt.py From upython-aq-monitor with MIT License | 6 votes |
def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1)) while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) return # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .set_callback() method. Other (internal) MQTT # messages processed internally.
Example #7
Source File: simple.py From UIFlow-Code with GNU General Public License v3.0 | 6 votes |
def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" self.sock.setblocking(True) pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1, "little")) # print("simple --> wait socket msg") while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) # print("simple --> wait socket finish") return
Example #8
Source File: mqtt_as.py From micropython-mqtt with MIT License | 6 votes |
def _publish(self, topic, msg, retain, qos, dup, pid): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain | dup << 3 sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 if sz >= 2097152: raise MQTTException('Strings too long.') i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz await self._as_write(pkt, i + 1) await self._send_str(topic) if qos > 0: struct.pack_into("!H", pkt, 0, pid) await self._as_write(pkt, 2) await self._as_write(msg) # Can raise OSError if WiFi fails. Subclass traps
Example #9
Source File: ip5306.py From micropython-m5stack with MIT License | 5 votes |
def _register_char(self, register, value=None, buf=bytearray(1)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return buf[0] ustruct.pack_into("<b", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)
Example #10
Source File: mqtt_as.py From microhomie with MIT License | 5 votes |
def subscribe(self, topic, qos): pkt = bytearray(b"\x82\0\0\0") pid = next(self.newpid) self.rcv_pids.add(pid) struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, pid) async with self.lock: await self._as_write(pkt) await self._send_str(topic) await self._as_write(qos.to_bytes(1, "little")) if not await self._await_pid(pid): raise OSError(-1) # Can raise OSError if WiFi fails. Subclass traps
Example #11
Source File: neopixel.py From Adafruit_CircuitPython_seesaw with MIT License | 5 votes |
def __setitem__(self, key, color): """Set one pixel to a new value""" cmd = bytearray(2 + self._bpp) struct.pack_into(">H", cmd, 0, key * self._bpp) if isinstance(color, int): w = color >> 24 r = (color >> 16) & 0xFF g = (color >> 8) & 0xFF b = color & 0xFF else: if self._bpp == 3: r, g, b = color else: r, g, b, w = color # If all components are the same and we have a white pixel then use it # instead of the individual components. if self._bpp == 4 and r == g == b and w == 0: w = r r = 0 g = 0 b = 0 if self.brightness < 0.99: r = int(r * self.brightness) g = int(g * self.brightness) b = int(b * self.brightness) if self._bpp == 4: w = int(w * self.brightness) # Store colors in correct slots cmd[2 + self._pixel_order[0]] = r cmd[2 + self._pixel_order[1]] = g cmd[2 + self._pixel_order[2]] = b if self._bpp == 4: cmd[2 + self._pixel_order[3]] = w self._seesaw.write(_NEOPIXEL_BASE, _NEOPIXEL_BUF, cmd) if self.auto_write: self.show()
Example #12
Source File: simple.py From xbee-micropython with MIT License | 5 votes |
def wait_msg(self): res = self.sock.read(1) self.sock.setblocking(True) if res is None: return None if res == b"": raise OSError(-1) if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 return None op = res[0] if op & 0xf0 != 0x30: return op sz = self._recv_len() topic_len = self.sock.read(2) topic_len = (topic_len[0] << 8) | topic_len[1] topic = self.sock.read(topic_len) sz -= topic_len + 2 if op & 6: pid = self.sock.read(2) pid = pid[0] << 8 | pid[1] sz -= 2 msg = self.sock.read(sz) self.cb(topic, msg) if op & 6 == 2: pkt = bytearray(b"\x40\x02\0\0") struct.pack_into("!H", pkt, 2, pid) self.sock.write(pkt) elif op & 6 == 4: assert 0 # Checks whether a pending message from server is available. # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg.
Example #13
Source File: mpu6500.py From micropython-mpu9250 with MIT License | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return ustruct.unpack(">h", buf)[0] ustruct.pack_into(">h", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)
Example #14
Source File: simple.py From xbee-micropython with MIT License | 5 votes |
def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 assert sz < 2097152 i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt, i + 1) self._send_str(topic) if qos > 0: self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) self.sock.write(pkt, 2) self.sock.write(msg) if qos == 1: while 1: op = self.wait_msg() if op == 0x40: sz = self.sock.read(1) assert sz == b"\x02" rcv_pid = self.sock.read(2) rcv_pid = rcv_pid[0] << 8 | rcv_pid[1] if pid == rcv_pid: return elif qos == 2: assert 0
Example #15
Source File: i2c_struct.py From Adafruit_CircuitPython_Register with MIT License | 5 votes |
def __set__(self, obj, value): buf = bytearray(1 + struct.calcsize(self.format)) buf[0] = self.address struct.pack_into(self.format, buf, 1, value) with obj.i2c_device as i2c: i2c.write(buf)
Example #16
Source File: i2c_struct.py From Adafruit_CircuitPython_Register with MIT License | 5 votes |
def __set__(self, obj, value): struct.pack_into(self.format, self.buffer, 1, *value) with obj.i2c_device as i2c: i2c.write(self.buffer)
Example #17
Source File: umqtt.py From pyaiot with BSD 3-Clause "New" or "Revised" License | 5 votes |
def wait_msg(self): res = self.sock.read(1) self.sock.setblocking(True) if res is None: return None if res == b"": raise OSError(-1) if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 return None op = res[0] if op & 0xf0 != 0x30: return op sz = self._recv_len() topic_len = self.sock.read(2) topic_len = (topic_len[0] << 8) | topic_len[1] topic = self.sock.read(topic_len) sz -= topic_len + 2 if op & 6: pid = self.sock.read(2) pid = pid[0] << 8 | pid[1] sz -= 2 msg = self.sock.read(sz) self.cb(topic, msg) if op & 6 == 2: pkt = bytearray(b"\x40\x02\0\0") struct.pack_into("!H", pkt, 2, pid) self.sock.write(pkt) elif op & 6 == 4: assert 0 # Checks whether a pending message from server is available. # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg.
Example #18
Source File: mpu6500.py From micropython-mpu9250 with MIT License | 5 votes |
def _register_char(self, register, value=None, buf=bytearray(1)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return buf[0] ustruct.pack_into("<b", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)
Example #19
Source File: umqtt.py From pyaiot with BSD 3-Clause "New" or "Revised" License | 5 votes |
def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 assert sz < 2097152 i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt, i + 1) self._send_str(topic) if qos > 0: self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) self.sock.write(pkt, 2) self.sock.write(msg) if qos == 1: while 1: op = self.wait_msg() if op == 0x40: sz = self.sock.read(1) assert sz == b"\x02" rcv_pid = self.sock.read(2) rcv_pid = rcv_pid[0] << 8 | rcv_pid[1] if pid == rcv_pid: return elif qos == 2: assert 0
Example #20
Source File: simple.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 assert sz < 2097152 i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt, i + 1) self._send_str(topic) if qos > 0: self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) self.sock.write(pkt, 2) self.sock.write(msg) if qos == 1: while 1: op = self.wait_msg() if op == 0x40: sz = self.sock.read(1) assert sz == b"\x02" rcv_pid = self.sock.read(2) rcv_pid = rcv_pid[0] << 8 | rcv_pid[1] if pid == rcv_pid: return elif qos == 2: assert 0
Example #21
Source File: mqtt.py From upython-aq-monitor with MIT License | 5 votes |
def wait_msg(self): res = self.sock.read(1) self.sock.setblocking(True) if res is None: return None if res == b"": raise OSError(-1) if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 return None op = res[0] if op & 0xf0 != 0x30: return op sz = self._recv_len() topic_len = self.sock.read(2) topic_len = (topic_len[0] << 8) | topic_len[1] topic = self.sock.read(topic_len) sz -= topic_len + 2 if op & 6: pid = self.sock.read(2) pid = pid[0] << 8 | pid[1] sz -= 2 msg = self.sock.read(sz) self.cb(topic, msg) if op & 6 == 2: pkt = bytearray(b"\x40\x02\0\0") struct.pack_into("!H", pkt, 2, pid) self.sock.write(pkt) elif op & 6 == 4: assert 0 # Checks whether a pending message from server is available. # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg.
Example #22
Source File: ak8963.py From micropython-mpu9250 with MIT License | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return ustruct.unpack("<h", buf)[0] ustruct.pack_into("<h", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)
Example #23
Source File: mqtt.py From upython-aq-monitor with MIT License | 5 votes |
def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain sz = 2 + len(topic) + len(msg) if qos > 0: sz += 2 assert sz < 2097152 i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt, i + 1) self._send_str(topic) if qos > 0: self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) self.sock.write(pkt, 2) self.sock.write(msg) if qos == 1: while 1: op = self.wait_msg() if op == 0x40: sz = self.sock.read(1) assert sz == b"\x02" rcv_pid = self.sock.read(2) rcv_pid = rcv_pid[0] << 8 | rcv_pid[1] if pid == rcv_pid: return elif qos == 2: assert 0
Example #24
Source File: _color.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self.i2c.readfrom_mem_into(self.addr, register, buf) return buf[1]*256 + buf[0] ustruct.pack_into(">h", buf, 0, value) return self.i2c.writeto_mem(self.addr, register, buf)
Example #25
Source File: _color.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_char(self, register, value=None, buf=bytearray(1)): if value is None: self.i2c.readfrom_mem_into(self.addr, register, buf) return buf[0] ustruct.pack_into("<b", buf, 0, value) return self.i2c.writeto_mem(self.addr, register, buf)
Example #26
Source File: _tof.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self.i2c.readfrom_mem_into(_ADDR, register, buf) return ustruct.unpack(">h", buf)[0] ustruct.pack_into(">h", buf, 0, value) return self.i2c.writeto_mem(_ADDR, register, buf)
Example #27
Source File: _tof.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_char(self, register, value=None, buf=bytearray(1)): if value is None: self.i2c.readfrom_mem_into(_ADDR, register, buf) return buf[0] ustruct.pack_into("<b", buf, 0, value) return self.i2c.writeto_mem(_ADDR, register, buf)
Example #28
Source File: _tracker.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self._i2c.readfrom_mem_into(self._addr, register, buf) return ustruct.unpack(">h", buf)[0] ustruct.pack_into(">h", buf, 0, value) return self._i2c.writeto_mem(self._addr, register, buf)
Example #29
Source File: mpu6050.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_char(self, register, value=None, buf=bytearray(1)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return buf[0] ustruct.pack_into("<b", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)
Example #30
Source File: mpu6050.py From UIFlow-Code with GNU General Public License v3.0 | 5 votes |
def _register_short(self, register, value=None, buf=bytearray(2)): if value is None: self.i2c.readfrom_mem_into(self.address, register, buf) return ustruct.unpack(">h", buf)[0] ustruct.pack_into(">h", buf, 0, value) return self.i2c.writeto_mem(self.address, register, buf)