Better CLI

This commit is contained in:
Andrea Mistrali 2023-05-02 15:13:05 +02:00
parent e61c3bb78f
commit 9ee0cdac3b
No known key found for this signature in database
GPG Key ID: 6FB0A77F6D5DA9B2
3 changed files with 38 additions and 8 deletions

View File

@ -2,8 +2,7 @@
Pulses is a python module to drive LEDs on RPi using PWM (Pulse Width Modulation)
### INSTALL
### BUILD
1. Clone the repo

15
pulses-cli.py Normal file
View File

@ -0,0 +1,15 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2023 andre <andrea@mistrali.pw>
#
# Distributed under terms of the MIT license.
"""
"""
from pulses import cli
cli.main()

View File

@ -7,9 +7,9 @@ Usage:
Options:
-g --gpio=<gpio> GPIO to use [default: 12]
-i --initial=<method> Initial method
-i --initial=<method> Initial method [default: linear]
-l --loop=<method> Loop method [default: sin]
-f --final=<method> Final method
-f --final=<method> Final method [default: linear]
-d --delay=<method> Delay method [default: constant]
-m --min=<min> Minimum value [default: 2]
@ -34,11 +34,16 @@ logging.basicConfig(format="%(name)s %(msg)s", stream=sys.stdout)
def signal_handler(sig, frame):
print('bailing out...')
print('stopping pulse...', end="", flush=True)
led.stop()
led.join()
print('done.\nBailing out')
sys.exit(0)
def main():
global led
args = docopt(__doc__, version=f'pulses {VERSION}')
signal.signal(signal.SIGINT, signal_handler)
@ -48,9 +53,21 @@ def main():
log.setLevel('DEBUG')
log.info('setting verbose mode')
led = ledPulse(int(args.get('--gpio')))
params = {}
for method in ['initial', 'loop', 'final', 'delay']:
params[f"{method}Method"] = args.get(f"--{method}")
for method in ['initial', 'loop', 'final']:
methodName = args.get(f"--{method}")
if methodName not in led.valueMethods:
print(f"error: no value method '{methodName}' defined")
sys.exit(1)
params[f"{method}Method"] = methodName
methodName = args.get("--delay")
if methodName not in led.delayMethods:
print(f"error: no delay method '{methodName}' defined")
sys.exit(1)
params['delayMethod'] = methodName
params['min'] = int(args.get('--min'))
params['max'] = int(args.get('--max'))
@ -62,7 +79,6 @@ def main():
print("\n> loop parameters")
for param, value in params.items():
print(f" {param}: {value}")
led = ledPulse(int(args.get('--gpio')))
led.set(**params)
print('\nstarting loop, press Ctrl-C to stop...')
led.start()