6060_SPI_MANF_ID = const (0x04 )
6161_SPI_PROD_ID = const (0x302 )
6262
63+
6364class FRAM :
6465 """
6566 Driver base for the FRAM Breakout.
@@ -121,7 +122,6 @@ def __len__(self):
121122 """
122123 return self ._max_size
123124
124-
125125 def __getitem__ (self , address ):
126126 """ Read the value at the given index, or values in a slice.
127127
@@ -135,20 +135,28 @@ def __getitem__(self, address):
135135 """
136136 if isinstance (address , int ):
137137 if not 0 <= address < self ._max_size :
138- raise ValueError ("Address '{0}' out of range. It must be 0 <= address < {1}."
139- .format (address , self ._max_size ))
138+ raise ValueError (
139+ "Address '{0}' out of range. It must be 0 <= address < {1}." .format (
140+ address , self ._max_size
141+ )
142+ )
140143 buffer = bytearray (1 )
141144 read_buffer = self ._read_address (address , buffer )
142145 elif isinstance (address , slice ):
143146 if address .step is not None :
144147 raise ValueError ("Slice stepping is not currently available." )
145148
146- regs = list (range (address .start if address .start is not None else 0 ,
147- address .stop + 1 if address .stop is not None else self ._max_size ))
149+ regs = list (
150+ range (
151+ address .start if address .start is not None else 0 ,
152+ address .stop + 1 if address .stop is not None else self ._max_size ,
153+ )
154+ )
148155 if regs [0 ] < 0 or (regs [0 ] + len (regs )) > self ._max_size :
149- raise ValueError ("Address slice out of range. It must be 0 <= [starting address"
150- ":stopping address] < {0}."
151- .format (self ._max_size ))
156+ raise ValueError (
157+ "Address slice out of range. It must be 0 <= [starting address"
158+ ":stopping address] < {0}." .format (self ._max_size )
159+ )
152160
153161 buffer = bytearray (len (regs ))
154162 read_buffer = self ._read_address (regs [0 ], buffer )
@@ -171,11 +179,15 @@ def __setitem__(self, address, value):
171179
172180 if isinstance (address , int ):
173181 if not isinstance (value , (int , bytearray , list , tuple )):
174- raise ValueError ("Data must be a single integer, or a bytearray,"
175- " list, or tuple." )
182+ raise ValueError (
183+ "Data must be a single integer, or a bytearray," " list, or tuple."
184+ )
176185 if not 0 <= address < self ._max_size :
177- raise ValueError ("Address '{0}' out of range. It must be 0 <= address < {1}."
178- .format (address , self ._max_size ))
186+ raise ValueError (
187+ "Address '{0}' out of range. It must be 0 <= address < {1}." .format (
188+ address , self ._max_size
189+ )
190+ )
179191
180192 self ._write (address , value , self ._wraparound )
181193
@@ -190,6 +202,7 @@ def _write(self, start_address, data, wraparound):
190202 # Implemened by subclass
191203 raise NotImplementedError
192204
205+
193206class FRAM_I2C (FRAM ):
194207 """ I2C class for FRAM.
195208
@@ -200,17 +213,19 @@ class FRAM_I2C(FRAM):
200213 :param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
201214 Must be a ``digitalio.DigitalInOut`` object.
202215 """
203- #pylint: disable=too-many-arguments
204- def __init__ (self , i2c_bus , address = 0x50 , write_protect = False ,
205- wp_pin = None ):
206- from adafruit_bus_device .i2c_device import I2CDevice as i2cdev
216+
217+ # pylint: disable=too-many-arguments
218+ def __init__ (self , i2c_bus , address = 0x50 , write_protect = False , wp_pin = None ):
219+ from adafruit_bus_device .i2c_device import ( # pylint: disable=import-outside-toplevel
220+ I2CDevice as i2cdev ,
221+ )
222+
207223 dev_id_addr = 0xF8 >> 1
208224 read_buf = bytearray (3 )
209225 with i2cdev (i2c_bus , dev_id_addr ) as dev_id :
210- dev_id .write_then_readinto (bytearray ([(address << 1 )]),
211- read_buf )
212- manf_id = (((read_buf [0 ] << 4 ) + (read_buf [1 ] >> 4 )))
213- prod_id = (((read_buf [1 ] & 0x0F ) << 8 ) + read_buf [2 ])
226+ dev_id .write_then_readinto (bytearray ([(address << 1 )]), read_buf )
227+ manf_id = (read_buf [0 ] << 4 ) + (read_buf [1 ] >> 4 )
228+ prod_id = ((read_buf [1 ] & 0x0F ) << 8 ) + read_buf [2 ]
214229 if (manf_id != _I2C_MANF_ID ) and (prod_id != _I2C_PROD_ID ):
215230 raise OSError ("FRAM I2C device not found." )
216231
@@ -241,9 +256,11 @@ def _write(self, start_address, data, wraparound=False):
241256 if wraparound :
242257 pass
243258 else :
244- raise ValueError ("Starting address + data length extends beyond"
245- " FRAM maximum address. Use ``write_wraparound`` to"
246- " override this warning." )
259+ raise ValueError (
260+ "Starting address + data length extends beyond"
261+ " FRAM maximum address. Use ``write_wraparound`` to"
262+ " override this warning."
263+ )
247264 with self ._i2c as i2c :
248265 for i in range (0 , data_length ):
249266 if not (start_address + i ) > self ._max_size - 1 :
@@ -264,6 +281,7 @@ def write_protected(self, value):
264281 if not self ._wp_pin is None :
265282 self ._wp_pin .value = value
266283
284+
267285# the following pylint disables are related to the '_SPI_OPCODE' consts, the super
268286# class setter '@FRAM.write_protected.setter', and pylint not being able to see
269287# 'spi.write()' in SPIDevice. Travis run for reference:
@@ -282,18 +300,22 @@ class FRAM_SPI(FRAM):
282300 :param int baudrate: SPI baudrate to use. Default is ``1000000``.
283301 """
284302
285- _SPI_OPCODE_WREN = const (0x6 ) # Set write enable latch
286- _SPI_OPCODE_WRDI = const (0x4 ) # Reset write enable latch
287- _SPI_OPCODE_RDSR = const (0x5 ) # Read status register
288- _SPI_OPCODE_WRSR = const (0x1 ) # Write status register
289- _SPI_OPCODE_READ = const (0x3 ) # Read memory code
290- _SPI_OPCODE_WRITE = const (0x2 ) # Write memory code
291- _SPI_OPCODE_RDID = const (0x9F ) # Read device ID
292-
293- #pylint: disable=too-many-arguments,too-many-locals
294- def __init__ (self , spi_bus , spi_cs , write_protect = False ,
295- wp_pin = None , baudrate = 100000 ):
296- from adafruit_bus_device .spi_device import SPIDevice as spidev
303+ _SPI_OPCODE_WREN = const (0x6 ) # Set write enable latch
304+ _SPI_OPCODE_WRDI = const (0x4 ) # Reset write enable latch
305+ _SPI_OPCODE_RDSR = const (0x5 ) # Read status register
306+ _SPI_OPCODE_WRSR = const (0x1 ) # Write status register
307+ _SPI_OPCODE_READ = const (0x3 ) # Read memory code
308+ _SPI_OPCODE_WRITE = const (0x2 ) # Write memory code
309+ _SPI_OPCODE_RDID = const (0x9F ) # Read device ID
310+
311+ # pylint: disable=too-many-arguments,too-many-locals
312+ def __init__ (
313+ self , spi_bus , spi_cs , write_protect = False , wp_pin = None , baudrate = 100000
314+ ):
315+ from adafruit_bus_device .spi_device import ( # pylint: disable=import-outside-toplevel
316+ SPIDevice as spidev ,
317+ )
318+
297319 _spi = spidev (spi_bus , spi_cs , baudrate = baudrate )
298320
299321 read_buffer = bytearray (4 )
@@ -328,9 +350,11 @@ def _write(self, start_address, data, wraparound=False):
328350 if wraparound :
329351 pass
330352 else :
331- raise ValueError ("Starting address + data length extends beyond"
332- " FRAM maximum address. Use 'wraparound=True' to"
333- " override this warning." )
353+ raise ValueError (
354+ "Starting address + data length extends beyond"
355+ " FRAM maximum address. Use 'wraparound=True' to"
356+ " override this warning."
357+ )
334358 with self ._spi as spi :
335359 spi .write (bytearray ([_SPI_OPCODE_WREN ]))
336360 with self ._spi as spi :
@@ -354,9 +378,9 @@ def write_protected(self, value):
354378 write_buffer = bytearray (2 )
355379 write_buffer [0 ] = _SPI_OPCODE_WRSR
356380 if value :
357- write_buffer [1 ] = 0x8C # set WPEN, BP0, and BP1
381+ write_buffer [1 ] = 0x8C # set WPEN, BP0, and BP1
358382 else :
359- write_buffer [1 ] = 0x00 # clear WPEN, BP0, and BP1
383+ write_buffer [1 ] = 0x00 # clear WPEN, BP0, and BP1
360384 with self ._spi as spi :
361385 spi .write (write_buffer )
362386 if self ._wp_pin is not None :
0 commit comments