cat-tracker/receive.py
2025-04-10 15:09:29 -07:00

41 lines
1014 B
Python

def burst_read(addr, length):
"""Read multiple bytes"""
READ_SINGLE = ACCESS["READ_SINGLE"]["dec"]
READ_BURST = ACCESS["READ_BURST"]["dec"]
return spi.xfer2([addr | READ_SINGLE | READ_BURST] + [0x00] * length)
def read_fifo():
# Burst read RX FIFO
READ_BURST = ACCESS["READ_BURST"]["dec"]
RXFIFO = MEMORY["RXFIFI"]["dec"]
fifo = spi.xfer2([RXFIFO | READ_BURST] + [0x00]*64) # Max 64 bytes
return fifo[1:]
def receive_packet():
# Flush RX FIFO
SFRX = STROBES["SFRX"]["dec"]
strobe(SFRX) # SFRX
SRX = STROBES["SRX"]["dec"]
# Go into RX mode
strobe(SRX) # SRX
# Wait for data (use GDO0 in real app)
time.sleep(0.5)
# Check RXBYTES
RXBYTES = STATUS["RXBYTES"]["dec"]
rx_bytes = read_register(RXBYTES) & 0x7F
if rx_bytes == 0:
print("No data received.")
return None
# Read data
RXFIFO = MEMORY["RXFIFO"]["dec"]
data = read_fifo() # 0x3F = RX FIFO
print(f"Received: {data}")
return data