Skip to content

Instantly share code, notes, and snippets.

@Starfox64
Created September 20, 2017 18:33
Show Gist options
  • Save Starfox64/1f3a382ce208de1f0f06a5f0d45d109c to your computer and use it in GitHub Desktop.
Save Starfox64/1f3a382ce208de1f0f06a5f0d45d109c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import RPi.GPIO as GPIO
import os.path
import os
import click
### SETTINGS ###
PIN_NUMBER = 4
LOCKFILE = '/var/alfred.lock'
ON = GPIO.LOW
OFF = GPIO.HIGH
### SETTINGS ###
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_NUMBER, GPIO.OUT)
def lock():
open(LOCKFILE, 'a').close()
def unlock():
try:
os.remove(LOCKFILE)
except Exception as e:
pass
def locked():
return os.path.exists(LOCKFILE)
# Commands
@click.group()
def cli():
pass
@cli.command()
def on(shouldLock = True):
GPIO.output(PIN_NUMBER, ON)
click.echo('On!')
if shouldLock:
lock()
@cli.command()
def off(shouldLock = True):
GPIO.output(PIN_NUMBER, OFF)
click.echo('Off!')
if shouldLock:
lock()
@cli.command()
def con():
if not locked():
on(False)
@cli.command()
def coff():
if not locked():
off(False)
@cli.command()
def reset():
unlock()
@cli.command()
def status():
if GPIO.input(PIN_NUMBER) == ON:
click.echo('Output is on.')
else:
click.echo('Output is off.')
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment