From 3eea6d3e316ab2c309ace5d7c1ad84f9942c5beb Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sun, 3 May 2020 16:43:48 +0200 Subject: [PATCH] curtain: add parameters for open and close --- src/curtain.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/curtain.c b/src/curtain.c index 46f2e61..990b4f8 100644 --- a/src/curtain.c +++ b/src/curtain.c @@ -6,6 +6,7 @@ #include #include #include +#include #define PAGE_SIZE (4*1024) #define BLOCK_SIZE (4*1024) @@ -49,13 +50,14 @@ void setup_io(); #define CLOSE_IN 20 -int main() +int main(int argc, char *argv[]) { int i; // Set up gpi pointer for direct register access setup_io(); + // Initialize the IO pins. INP_GPIO(OPEN_OUT); // must use INP_GPIO before we can use OUT_GPIO OUT_GPIO(OPEN_OUT); INP_GPIO(OPEN_IN); @@ -64,9 +66,40 @@ int main() OUT_GPIO(CLOSE_OUT); INP_GPIO(CLOSE_IN); - GPIO_SET = 1 << OPEN_OUT; // Close the switch - sleep(1); - GPIO_CLR = 1 << OPEN_OUT; // Open the switch + // Scan the arguments. + + const char *usage = "Usage: curtain [-c close_seconds] [-o open_seconds]\n"; + + int option; + int pulse_time; + + + while ((option = getopt(argc, argv, "c:o:")) != -1) + { + switch (option) + { + case 'c': + pulse_time = atoi(optarg); + GPIO_SET = 1 << CLOSE_OUT; // Close the switch + sleep(pulse_time); + GPIO_CLR = 1 << CLOSE_OUT; // Open the switch + break; + + case 'o': + pulse_time = atoi(optarg); + GPIO_SET = 1 << OPEN_OUT; // Close the switch + sleep(pulse_time); + GPIO_CLR = 1 << OPEN_OUT; // Open the switch + break; + + case '?': + case ':': + fprintf(stderr, "%s", usage); + exit(1); + + } + } + return 0; } -- 2.20.1