Skip to content

Commit

Permalink
Handle GPIO relays with inverse logic (active low)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej1024 committed Nov 3, 2017
2 parents 19c326b + a69e3e8 commit 8e7a759
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ Version history
0.11: 30/12/2016
- Added multiple cards support for command line ("-s" option to specify cards serial number)
- Created C library version of crelay for inclusion in third party applications

0.12: 03/11/2017
- Handle GPIO relays with inverse logic (active low)
1 change: 1 addition & 0 deletions conf/crelay.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ relay5_label = Device 5 # label for relay 5
relay6_label = Device 6 # label for relay 6
relay7_label = Device 7 # label for relay 7
relay8_label = Device 8 # label for relay 8
pulse_duration = 1 # duration of a 'pulse' command in seconds

# GPIO driver parameters
################################################
Expand Down
17 changes: 14 additions & 3 deletions src/crelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ config_t config;
static char rlabels[MAX_NUM_RELAYS][32] = {"My appliance 1", "My appliance 2", "My appliance 3", "My appliance 4",
"My appliance 5", "My appliance 6", "My appliance 7", "My appliance 8"};


/**********************************************************
* Function: config_cb()
*
Expand Down Expand Up @@ -134,6 +133,10 @@ static int config_cb(void* user, const char* section, const char* name, const ch
{
pconfig->relay8_label = strdup(value);
}
else if (MATCH("HTTP server", "pulse_duration"))
{
pconfig->pulse_duration = atoi(value);
}
else if (MATCH("GPIO drv", "num_relays"))
{
pconfig->gpio_num_relays = atoi(value);
Expand Down Expand Up @@ -483,13 +486,13 @@ int process_http_request(int sock)
if (rstate[relay-1] == ON)
{
crelay_set_relay(com_port, relay, OFF, serial);
sleep(1);
sleep(config.pulse_duration);
crelay_set_relay(com_port, relay, ON, serial);
}
else
{
crelay_set_relay(com_port, relay, ON, serial);
sleep(1);
sleep(config.pulse_duration);
crelay_set_relay(com_port, relay, OFF, serial);
}
}
Expand Down Expand Up @@ -668,6 +671,7 @@ int main(int argc, char *argv[])
if (config.relay6_label != NULL) syslog(LOG_DAEMON | LOG_NOTICE, "relay6_label: %s\n", config.relay6_label);
if (config.relay7_label != NULL) syslog(LOG_DAEMON | LOG_NOTICE, "relay7_label: %s\n", config.relay7_label);
if (config.relay8_label != NULL) syslog(LOG_DAEMON | LOG_NOTICE, "relay8_label: %s\n", config.relay8_label);
if (config.pulse_duration != 0) syslog(LOG_DAEMON | LOG_NOTICE, "pulse_duration: %u\n", config.pulse_duration);
if (config.gpio_num_relays != 0) syslog(LOG_DAEMON | LOG_NOTICE, "gpio_num_relays: %u\n", config.gpio_num_relays);
if (config.gpio_active_value >= 0) syslog(LOG_DAEMON | LOG_NOTICE, "gpio_active_value: %u\n", config.gpio_active_value);
if (config.relay1_gpio_pin != 0) syslog(LOG_DAEMON | LOG_NOTICE, "relay1_gpio_pin: %u\n", config.relay1_gpio_pin);
Expand Down Expand Up @@ -705,11 +709,18 @@ int main(int argc, char *argv[])
{
port = config.server_port;
}

}
else
{
syslog(LOG_DAEMON | LOG_NOTICE, "Can't load %s, using default parameters\n", CONFIG_FILE);
}

/* Ensure pulse duration is valid **/
if (config.pulse_duration == 0)
{
config.pulse_duration = 1;
}

/* Parse command line for relay labels (overrides config file)*/
for (i=0; i<argc-2 && i<MAX_NUM_RELAYS; i++)
Expand Down
1 change: 1 addition & 0 deletions src/data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct
const char* relay6_label;
const char* relay7_label;
const char* relay8_label;
uint8_t pulse_duration;

/* [GPIO drv] */
uint8_t gpio_num_relays;
Expand Down

0 comments on commit 8e7a759

Please sign in to comment.