curtain: add parameters for open and close
authorArjen Baart <arjen@andromeda.nl>
Sun, 3 May 2020 14:43:48 +0000 (16:43 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sun, 3 May 2020 14:43:48 +0000 (16:43 +0200)
src/curtain.c

index 46f2e61..990b4f8 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/mman.h>
 #include <unistd.h>
 #include <errno.h>
+#include <getopt.h>
 
 #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;
 }