44
55from . import ioe_regs , sioe_regs
66
7- __version__ = ' 0.0.5'
7+ __version__ = " 0.0.5"
88
99
1010# These values encode our desired pin function: IO, ADC, PWM
@@ -186,7 +186,7 @@ def __init__(
186186 if not skip_chip_id_check :
187187 chip_id = self .get_chip_id ()
188188 if chip_id != self ._chip_id :
189- raise RuntimeError ("Chip ID invalid: {:04x} expected: {:04x}." . format ( chip_id , self . _chip_id ) )
189+ raise RuntimeError (f "Chip ID invalid: { chip_id :04x} expected: { self . _chip_id :04x} ." )
190190
191191 # Reset the chip if requested, to put it into a known state
192192 if perform_reset :
@@ -253,7 +253,7 @@ def i2c_write16(self, reg_l, reg_h, value):
253253 def get_pin (self , pin ):
254254 """Get a pin definition from its index."""
255255 if pin < 1 or pin > len (self ._pins ):
256- raise ValueError ("Pin should be in range 1-{}." . format ( len (self ._pins )) )
256+ raise ValueError (f "Pin should be in range 1-{ len (self ._pins )} ." )
257257
258258 return self ._pins [pin - 1 ]
259259
@@ -262,7 +262,7 @@ def setup_switch_counter(self, pin, mode=IN_PU):
262262 io_pin = self .get_pin (pin )
263263
264264 if io_pin .port not in (0 , 1 ):
265- raise ValueError ("Pin {} does not support switch counting." . format ( pin ) )
265+ raise ValueError (f "Pin { pin } does not support switch counting." )
266266
267267 if mode not in [IN , IN_PU ]:
268268 raise ValueError ("Pin mode should be one of IN or IN_PU" )
@@ -277,7 +277,7 @@ def read_switch_counter(self, pin):
277277 io_pin = self .get_pin (pin )
278278
279279 if io_pin .port not in (0 , 1 ):
280- raise ValueError ("Pin {} does not support switch counting." . format ( pin ) )
280+ raise ValueError (f "Pin { pin } does not support switch counting." )
281281
282282 sw_reg = [self .REG_SWITCH_P00 , self .REG_SWITCH_P10 ][io_pin .port ] + io_pin .pin
283283
@@ -292,7 +292,7 @@ def clear_switch_counter(self, pin):
292292 io_pin = self .get_pin (pin )
293293
294294 if io_pin .port not in (0 , 1 ):
295- raise ValueError ("Pin {} does not support switch counting." . format ( pin ) )
295+ raise ValueError (f "Pin { pin } does not support switch counting." )
296296
297297 sw_reg = [self .REG_SWITCH_P00 , self .REG_SWITCH_P10 ][io_pin .port ] + io_pin .pin
298298
@@ -307,15 +307,15 @@ def setup_rotary_encoder(self, channel, pin_a, pin_b, pin_c=None, count_microste
307307 enc_channel_a = self .get_pin (pin_a ).enc_channel
308308 enc_channel_b = self .get_pin (pin_b ).enc_channel
309309 if enc_channel_a is None :
310- raise ValueError ("Pin {} does not support an encoder." . format ( pin_a ) )
310+ raise ValueError (f "Pin { pin_a } does not support an encoder." )
311311 if enc_channel_b is None :
312- raise ValueError ("Pin {} does not support an encoder." . format ( pin_b ) )
312+ raise ValueError (f "Pin { pin_b } does not support an encoder." )
313313
314314 self .set_mode (pin_a , PIN_MODE_PU , schmitt_trigger = True )
315315 self .set_mode (pin_b , PIN_MODE_PU , schmitt_trigger = True )
316316 if pin_c is not None :
317317 if pin_c < 1 or pin_c > len (self ._pins ):
318- raise ValueError ("Pin C should be in range 1-{}, or None." . format ( len ( self . _pins )) )
318+ raise ValueError (f "Pin C should be in range 1-{ len ( self . _pins ) } , or None." )
319319 self .set_mode (pin_c , PIN_MODE_OD )
320320 self .output (pin_c , 0 )
321321
@@ -480,12 +480,12 @@ def reset(self):
480480
481481 def get_pwm_module (self , pin ):
482482 if pin < 1 or pin > len (self ._pins ):
483- raise ValueError ("Pin should be in range 1-{}." . format ( len (self ._pins )) )
483+ raise ValueError (f "Pin should be in range 1-{ len (self ._pins )} ." )
484484
485485 io_pin = self ._pins [pin - 1 ]
486486 if PIN_MODE_PWM not in io_pin .type :
487487 io_mode = (PIN_MODE_PWM >> 2 ) & 0b11
488- raise ValueError ("Pin {} does not support {}!" . format ( pin , MODE_NAMES [io_mode ]) )
488+ raise ValueError (f "Pin { pin } does not support { MODE_NAMES [io_mode ]} !" )
489489
490490 if isinstance (io_pin , DUAL_PWM_PIN ) and io_pin .is_using_alt ():
491491 if io_pin .is_using_alt ():
@@ -538,7 +538,7 @@ def set_pwm_control(self, divider, pwm_module=0):
538538 128 : 0b111 ,
539539 }[divider ]
540540 except KeyError :
541- raise ValueError ("A clock divider of {}" . format ( divider ) )
541+ raise ValueError (f "A clock divider of { divider } " )
542542
543543 # TODO: This currently sets GP, PWMTYP and FBINEN to 0
544544 # It might be desirable to make these available to the user
@@ -608,17 +608,12 @@ def set_mode(self, pin, mode, schmitt_trigger=False, invert=False):
608608 initial_state = mode >> 4
609609
610610 if io_mode != PIN_MODE_IO and mode not in io_pin .type :
611- raise ValueError ("Pin {} does not support {}!" . format ( pin , MODE_NAMES [io_mode ]) )
611+ raise ValueError ("Pin {pin } does not support {MODE_NAMES[io_mode]}!" )
612612
613613 io_pin .mode = mode
614614 if self ._debug :
615615 print (
616- "Setting pin {pin} to mode {mode} {name}, state: {state}" .format (
617- pin = pin ,
618- mode = MODE_NAMES [io_mode ],
619- name = GPIO_NAMES [gpio_mode ],
620- state = STATE_NAMES [initial_state ],
621- )
616+ f"Setting pin { pin } to mode { MODE_NAMES [io_mode ]} { GPIO_NAMES [gpio_mode ]} , state: { STATE_NAMES [initial_state ]} "
622617 )
623618
624619 if mode == PIN_MODE_PWM :
@@ -681,7 +676,7 @@ def input(self, pin, adc_timeout=1):
681676
682677 if io_pin .mode == PIN_MODE_ADC :
683678 if self ._debug :
684- print ("Reading ADC from pin {}" . format ( pin ) )
679+ print (f "Reading ADC from pin { pin } " )
685680
686681 if io_pin .adc_channel > 8 :
687682 self .i2c_write8 (self .REG_AINDIDS1 , 1 << (io_pin .adc_channel - 8 ))
@@ -708,7 +703,7 @@ def input(self, pin, adc_timeout=1):
708703 return (reading / 4095.0 ) * self ._vref
709704 else :
710705 if self ._debug :
711- print ("Reading IO from pin {}" . format ( pin ) )
706+ print (f "Reading IO from pin { pin } " )
712707 pv = self .get_bit (self .get_pin_regs (io_pin ).p , io_pin .pin )
713708
714709 return HIGH if pv else LOW
@@ -723,7 +718,7 @@ def output(self, pin, value, load=True, wait_for_load=True):
723718
724719 if io_pin .mode == PIN_MODE_PWM :
725720 if self ._debug :
726- print ("Outputting PWM to pin: {pin}" . format ( pin = pin ) )
721+ print (f "Outputting PWM to pin: { pin } " )
727722
728723 if isinstance (io_pin , DUAL_PWM_PIN ) and io_pin .is_using_alt ():
729724 alt_regs = self .get_alt_pwm_regs (io_pin )
@@ -738,11 +733,11 @@ def output(self, pin, value, load=True, wait_for_load=True):
738733 else :
739734 if value == LOW :
740735 if self ._debug :
741- print ("Outputting LOW to pin: {pin} (or HIGH if inverted)" . format ( pin = pin ) )
736+ print (f "Outputting LOW to pin: { pin } (or HIGH if inverted)" )
742737 self .change_bit (self .get_pin_regs (io_pin ).p , io_pin .pin , io_pin .is_inverted ())
743738 elif value == HIGH :
744739 if self ._debug :
745- print ("Outputting HIGH to pin: {pin} (or LOW if inverted)" . format ( pin = pin ) )
740+ print (f "Outputting HIGH to pin: { pin } (or LOW if inverted)" )
746741 self .change_bit (self .get_pin_regs (io_pin ).p , io_pin .pin , not io_pin .is_inverted ())
747742
748743 def get_pwm_regs (self , pin ):
@@ -774,7 +769,7 @@ def get_pin_regs(self, pin):
774769
775770 def switch_pwm_to_alt (self , pin ):
776771 if pin < 1 or pin > len (self ._pins ):
777- raise ValueError ("Pin should be in range 1-{}." . format ( len (self ._pins )) )
772+ raise ValueError (f "Pin should be in range 1-{ len (self ._pins )} ." )
778773
779774 io_pin = self ._pins [pin - 1 ]
780775
@@ -993,7 +988,7 @@ def set_watchdog_control(self, divider):
993988 256 : 0b111 , # 1.638s
994989 }[divider ]
995990 except KeyError :
996- raise ValueError ("A clock divider of {}" . format ( divider ) )
991+ raise ValueError (f "A clock divider of { divider } " )
997992
998993 wdt = self .i2c_read8 (self .REG_WDCON )
999994 wdt = wdt & 0b11111000 # Clear the WDPS bits
0 commit comments