22registers in a dict."""
33
44import logging
5- from typing import Union , Tuple
5+ from typing import Union , Tuple , List
66import pytest
77from circuitpython_nrf24l01 .rf24 import (
88 RF24 ,
@@ -54,8 +54,8 @@ class RadioState:
5454 }
5555
5656 # SPI commands
57- rx_fifo = [] # command 0x61
58- tx_fifo = [] # commands 0xA0, 0xB0, 0xA8 + pipe number
57+ rx_fifo : List [ bytearray ] = [] # command 0x61
58+ tx_fifo : List [ bytearray ] = [] # commands 0xA0, 0xB0, 0xA8 + pipe number
5959 logger = logging .getLogger ("nRF24L01" )
6060
6161 def __init__ (self ):
@@ -80,6 +80,7 @@ def __init__(self):
8080 def read_payload (master_out_buf ):
8181 """Read payload from state machine's RX FIFO.
8282 For simplicity, this doesn't pop payloads from the FIFO."""
83+ status_byte = RadioState .registers [7 ][0 ] # copy current STATUS byte
8384 if RadioState .rx_fifo :
8485 RadioState .logger .debug (
8586 "Requesting %d bytes from the RX FIFO" , len (master_out_buf )
@@ -107,21 +108,22 @@ def read_payload(master_out_buf):
107108 if len (RadioState .rx_fifo ) >= 3 :
108109 RadioState .registers [0x17 ][0 ] |= 2
109110 return (
110- RadioState . registers [ 7 ]
111+ bytearray ([ status_byte ])
111112 + ret_val [:size ]
112113 + bytearray (b"." * (len (master_out_buf ) - size ))
113114 )
114- return RadioState . registers [ 7 ] + (b"." * len (master_out_buf ))
115+ return bytearray ([ status_byte ]) + (b"." * len (master_out_buf ))
115116
116117 @staticmethod
117118 def write_payload (payload ):
118119 """Write payload to state machine's TX FIFO."""
120+ status_byte = RadioState .registers [7 ][0 ] # copy current STATUS byte
119121 RadioState .tx_fifo .append (payload )
120122 RadioState .registers [0x17 ][0 ] &= 0xCF
121123 if len (RadioState .tx_fifo ) >= 3 :
122124 RadioState .registers [0x17 ][0 ] |= 0x20
123125 RadioState .registers [7 ][0 ] |= 1
124- return RadioState . registers [ 7 ] + (b"\0 " * len (payload ))
126+ return bytearray ([ status_byte ]) + (b"\0 " * len (payload ))
125127
126128 @staticmethod
127129 def get_pl_width (* _ ):
@@ -138,20 +140,22 @@ def non_op(*_):
138140 @staticmethod
139141 def flush_tx (* _ ):
140142 """Flush the state machine's TX FIFO."""
143+ status_byte = RadioState .registers [7 ][0 ] # copy current STATUS byte
141144 RadioState .tx_fifo .clear ()
142145 RadioState .registers [7 ][0 ] &= 0xFE
143146 RadioState .registers [0x17 ][0 ] &= 0xCF
144147 RadioState .registers [0x17 ][0 ] |= 0x10
145- return RadioState . registers [ 7 ]
148+ return bytearray ([ status_byte ])
146149
147150 @staticmethod
148151 def flush_rx (* _ ):
149152 """Flush the state machine's RX FIFO."""
153+ status_byte = RadioState .registers [7 ][0 ] # copy current STATUS byte
150154 RadioState .rx_fifo .clear ()
151155 RadioState .registers [7 ][0 ] |= 0x0E
152156 RadioState .registers [0x17 ][0 ] &= 0xF0
153157 RadioState .registers [0x17 ][0 ] |= 1
154- return RadioState . registers [ 7 ]
158+ return bytearray ([ status_byte ])
155159
156160 def __getitem__ (self , __offset : int ) -> bytearray :
157161 register = __offset & 0x1F
@@ -212,15 +216,17 @@ def xfer2(self, out_buf: Union[bytes, bytearray], baud_rate: int) -> bytearray:
212216 """A mock function for a full duplex SPI transaction."""
213217 register = out_buf [0 ]
214218 assert baud_rate
219+ # copy STATUS register before outcome alters it
220+ status_byte = self .state .registers [7 ]
215221 try : # assume `register` is a write operation
216222 self .state [register ] = bytearray (out_buf [1 :])
217- return self . state . registers [ 7 ]
223+ return status_byte
218224 except RegisterReadOnly : # `register` is a SPI read operation
219225 return self .state [register ]
220226 except RegisterError : # `register` is a SPI command
221227 if register in self .state .commands :
222228 return self .state .commands [register ](out_buf [1 :])
223- return self . state . registers [ 7 ]
229+ return status_byte
224230
225231
226232class ShimDigitalIO :
0 commit comments