3 This module provides a 313MHz/434MHz radio interface compatible 4 with the Virtual Wire library used on Arduinos. 6 It has been tested between a Pi, TI Launchpad, and Arduino Pro Mini. 20 _HEADER=[0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x38, 0x2c]
25 0x0d, 0x0e, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c,
26 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x32, 0x34]
30 for nibble
in range(16):
31 if symbol == _SYMBOL[nibble]:
37 data = data ^ (crc & 0xFF);
39 data = (data ^ (data << 4)) & 0xFF;
42 (((data << 8) & 0xFFFF) | (crc >> 8)) ^
43 ((data >> 4) & 0x00FF) ^ ((data << 3) & 0xFFFF)
50 Instantiate a transmitter with the Pi, the transmit gpio, 51 and the bits per second (bps). The bps defaults to 2000. 52 The bps is constrained to be within MIN_BPS to MAX_BPS. 63 self.
mics = int(1000000 / bps)
69 pi.set_mode(txgpio, pigpio.OUTPUT)
82 self.
_nibble(_SYMBOL[byte&0x0F])
87 Transmit a message. If the message is more than 88 MAX_MESSAGE_BYTES in size it is discarded. If a message 89 is currently being transmitted it is aborted and replaced 90 with the new message. True is returned if message 91 transmission has successfully started. False indicates 94 if len(data) > MAX_MESSAGE_BYTES:
104 crc = self.
_byte(0xFFFF, len(data)+_CTL)
108 if type(i) == type(
""):
113 crc = self.
_byte(crc, v)
117 self.
_byte(0, crc&0xFF)
118 self.
_byte(0, crc>>8)
120 self.pi.wave_add_generic(self.
wf)
122 self.
wave_id = self.pi.wave_create()
125 self.pi.wave_send_once(self.
wave_id)
133 Returns True if a new message may be transmitted. 135 return not self.pi.wave_tx_busy()
139 Cancels the wireless transmitter, aborting any message 143 self.pi.wave_tx_stop()
144 self.pi.wave_delete(self.
wave_id)
145 self.pi.wave_add_new()
153 Instantiate a receiver with the Pi, the receive gpio, and 154 the bits per second (bps). The bps defaults to 2000. 155 The bps is constrained to be within MIN_BPS to MAX_BPS. 170 slack_mics = int(slack * self.
mics)
187 pi.set_mode(rxgpio, pigpio.INPUT)
189 self.
cb = pi.callback(rxgpio, pigpio.EITHER_EDGE, self.
_cb)
200 for i
in range(bits):
220 if byte > (MAX_MESSAGE_BYTES+_CTL):
231 self.pi.set_watchdog(self.
rxgpio, 0)
236 self.messages.append(
242 if self.
token == 0xB38:
248 def _cb(self, gpio, level, tick):
252 if level == pigpio.TIMEOUT:
254 self.pi.set_watchdog(self.
rxgpio, 0)
285 bitlen = (100 * edge) / self.
mics 303 Returns the next unread message, or None if none is avaiable. 306 return self.messages.pop(0)
312 Returns True if there is a message available to be read. 318 Cancels the wireless receiver. 320 if self.
cb is not None:
322 self.pi.set_watchdog(self.
rxgpio, 0)
325 if __name__ ==
"__main__":
347 while (time.time()-start) < 300:
351 while not tx.ready():
356 tx.put([48, 49, 65, ((msg>>6)&0x3F)+32, (msg&0x3F)+32])
358 while not tx.ready():
363 tx.put(
"Hello World #{}!".format(msg))
366 print(
"".join(chr (c)
for c
in rx.get()))
def _byte(self, crc, byte)
def __init__(self, pi, rxgpio, bps=2000)
def _insert(self, bits, level)
def _nibble(self, nibble)
def __init__(self, pi, txgpio, bps=2000)
def _cb(self, gpio, level, tick)
def _crc_ccitt_update(crc, data)