@@ -222,55 +222,41 @@ def _read_reg_into(self, reg, buf):
222222 finally :
223223 self .cs (1 )
224224
225- def _read_page (self , address ):
226- msb = (address >> 8 ) & 0x0F
227- lsb = address & 0xFF
225+ def _select_page (self , address , value = None ):
226+ """
227+ Selects the embedded function page and reads/writes the value at the given address.
228+ If value is None, it reads the value at the address. Otherwise, it writes the value to the address.
229+ """
230+ msb = (address >> 8 ) & 0x0F # MSB is the page number
231+ lsb = address & 0xFF # LSB is the register address within the page
228232
229233 self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
230234
231- # Clear both read and write bits first, then set read bit (bit 5).
232- self ._write_reg (_PAGE_RW , (self ._read_reg (_PAGE_RW ) & 0x9F ) | 0x20 )
235+ rw_bit = 0x20 if value is None else 0x40
236+ # Clear both read and write bits first, then set read (bit 5) or write (bit 6).
237+ self ._write_reg (_PAGE_RW , (self ._read_reg (_PAGE_RW ) & 0x9F ) | rw_bit )
233238
234239 # select page
235240 self ._write_reg (_PAGE_SEL , (msb << 4 ) | 0x01 )
236241
237242 # set page addr
238243 self ._write_reg (_PAGE_ADDRESS , lsb )
239244
240- # read value
241- val = self ._read_reg (_PAGE_VALUE )
245+ val = None
246+ if value is None :
247+ # read value
248+ val = self ._read_reg (_PAGE_VALUE )
249+ else :
250+ # write value
251+ self ._write_reg (_PAGE_VALUE , value )
242252
243- # unset page read and page_sel
244- self ._clear_bits (_PAGE_RW , 0x20 )
253+ # unset page write/read and page_sel
245254 self ._write_reg (_PAGE_SEL , 0x01 )
255+ self ._clear_bits (_PAGE_RW , rw_bit )
246256
247257 self .set_mem_bank (_FUNC_CFG_BANK_USER )
248258 return val
249259
250- def _write_page (self , address , val ):
251- msb = (address >> 8 ) & 0x0F
252- lsb = address & 0xFF
253-
254- self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
255-
256- # Clear both read and write bits first, then set write bit (bit 6).
257- self ._write_reg (_PAGE_RW , (self ._read_reg (_PAGE_RW ) & 0x9F ) | 0x40 )
258-
259- # select page
260- self ._write_reg (_PAGE_SEL , (msb << 4 ) | 0x01 )
261-
262- # set page addr
263- self ._write_reg (_PAGE_ADDRESS , lsb )
264-
265- # write value
266- self ._write_reg (_PAGE_VALUE , val )
267-
268- # unset page write and page_sel
269- self ._write_reg (_PAGE_SEL , 0x01 )
270- self ._clear_bits (_PAGE_RW , 0x40 )
271-
272- self .set_mem_bank (_FUNC_CFG_BANK_USER )
273-
274260 def reset (self ):
275261 self ._write_reg (_CTRL3_C , self ._read_reg (_CTRL3_C ) | 0x1 )
276262 for i in range (10 ):
@@ -408,12 +394,12 @@ def pedometer_steps(self):
408394 @property
409395 def pedometer_debounce_steps (self ):
410396 """Get the pedometer debounce steps."""
411- return self ._read_page (_PEDO_DEB_STEPS_CONF )
397+ return self ._select_page (_PEDO_DEB_STEPS_CONF )
412398
413399 @pedometer_debounce_steps .setter
414400 def pedometer_debounce_steps (self , steps ):
415401 """Set the pedometer debounce steps. Default is 10."""
416- self ._write_page (_PEDO_DEB_STEPS_CONF , steps )
402+ self ._select_page (_PEDO_DEB_STEPS_CONF , steps )
417403
418404 def gyro (self ):
419405 """Returns gyroscope vector in degrees/sec."""
0 commit comments