/* $Id: sprinkle.c,v 1.2 2005/01/25 23:25:10 grog Exp $ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __FreeBSD__ /* PPI interface */ #include #include #endif int relayfd; /* relay port */ /* * Open relay line. * Return 1 on failure. */ int openrelayline (char *relayline) { if (relayfd) close (relayfd); /* Now open the files. */ relayfd = open (relayline, O_WRONLY); if (relayfd < 0) { fprintf (stderr, "Can't open relay controller %s: %s (%d)\n", relayline, strerror (errno), errno ); return 1; } return 0; } /* Talk to the relay board */ void setrelay (int bits) { #ifdef __FreeBSD__ ioctl (relayfd, PPISDATA, &bits); /* just output the bits */ #else /* This kludge to work around potential endianness problems */ char cbits [8]; cbits [0] = bits; /* only last byte */ if (write (relayfd, cbits, 1) < 1) fprintf (stderr, "Can't write to relay board: %s (%d)\n", strerror (errno), errno ); #endif } /* $0 relayline valve valve ... */ int main (int argc, char *argv []) { int i; int duration; int verbose = 0; /* set to display progress */ int logging = 0; /* set to log progress */ char *relayline = NULL; int relay; i = 1; if (argc < 2) { syslog (LOG_ERR, "No parameters specified\n"); return 1; } while (argv [i] [0] == '-') { switch (argv [i] [1]) { case 'v': verbose = 1; break; case 'l': logging = 1; break; default: syslog (LOG_ERR, "Invalid parameter: %c\n", argv [i] [1]); return 1; } i++; if (i == argc) { syslog (LOG_ERR, "All parameters are options\n"); return 1; } } relayline = argv [i++]; if (i == argc) { syslog (LOG_ERR, "No sprinklers specified\n"); return 1; } if ((argc - i) > 7) { /* * Invalid number of valves. We'll do what we can anyway. * * We have 8 relays, but the eighth (bit 0x80) is for the pump, so * we can only support 7 valves. */ syslog (LOG_WARNING, "Too many valves: %d (must be between 1 and 7)\n", argc - i ); argc = i + 7; } if (openrelayline (relayline)) /* can't open relay */ { syslog (LOG_ERR, "Can't open relay line %s: %s (%d)\n", relayline, strerror (errno), errno ); exit (1); } for (relay = i; relay < argc; relay++) /* each valve */ { duration = atoi (argv [relay]) * 60; /* duration in seconds */ if (duration) { if (logging) syslog (LOG_INFO, "Starting circuit %d for %d minutes\n", relay - i + 1, duration / 60); setrelay ((1 << (relay - i)) | 0x80); sleep (duration); if (logging) syslog (LOG_INFO, "Finishing circuit %d for %d minutes\n", relay - i + 1, duration / 60); } } setrelay (0); /* all off now, including pump */ return 0; }