Skip to content

Commit 30b4c74

Browse files
authored
Merge pull request #281 from makermelissa/main
Allow 'disabled' to be passed in via command line in spi-reassign script
2 parents 4663279 + f5cfbca commit 30b4c74

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

raspi-spi-reassign.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,24 @@
3131
def valid_pins(ce0_pin, ce1_pin):
3232
if ce0_pin is None and ce1_pin is not None:
3333
return False
34-
if ce0_pin == ce1_pin:
34+
if ce0_pin is not None and ce0_pin == ce1_pin:
3535
return False
36-
if int(ce0_pin) not in allowed_gpios:
36+
if ce0_pin is not None and int(ce0_pin) not in allowed_gpios:
3737
return False
38-
if int(ce1_pin) not in allowed_gpios:
38+
if ce1_pin is not None and int(ce1_pin) not in allowed_gpios:
3939
return False
4040
return True
4141

42+
def convert_option(pin):
43+
if pin == "disabled":
44+
return None
45+
return int(pin)
46+
47+
def valid_options(ce0_option, ce1_option):
48+
if ce0_option is None or ce1_option is None:
49+
return False
50+
return valid_pins(convert_option(ce0_option), convert_option(ce1_option))
51+
4252
def disable_spi():
4353
print("Disabling SPI")
4454
shell.run_command("sudo raspi-config nonint do_spi 1")
@@ -78,16 +88,18 @@ def write_new_custom(ce0_pin, ce1_pin):
7888
shell.write_text_file(f"{boot_dir}/config.txt", overlay_command + "\n")
7989

8090
@click.command()
81-
@click.option('--ce0', nargs=1, default=None, help="Specify a GPIO for CE0")
82-
@click.option('--ce1', nargs=1, default=None, help="Specify a GPIO for CE1")
91+
@click.option('--ce0', nargs=1, default=None, help="Specify a GPIO for CE0 or 'disabled' to disable", type=str)
92+
@click.option('--ce1', nargs=1, default=None, help="Specify a GPIO for CE1 or 'disabled' to disable", type=str)
8393
@click.option('--reboot', nargs=1, default=None, type=click.Choice(['yes', 'no']), help="Specify whether to reboot after the script is finished")
8494
def main(ce0, ce1, reboot):
8595
ask_reboot = True
8696
auto_reboot = False
8797
if reboot is not None:
8898
ask_reboot = False
8999
auto_reboot = reboot.lower() == 'yes'
90-
if valid_pins(ce0, ce1):
100+
if valid_options(ce0, ce1):
101+
ce0 = convert_option(ce0)
102+
ce1 = convert_option(ce1)
91103
remove_custom()
92104
write_new_custom(ce0, ce1)
93105
if auto_reboot:

0 commit comments

Comments
 (0)