add: choose initial relay mode

This commit is contained in:
Tobias Reisinger 2020-08-20 21:51:42 +02:00
parent 5e05d603c1
commit d7c274ca86
11 changed files with 57 additions and 27 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project(controller
VERSION 0.2.8
VERSION 0.2.9
LANGUAGES C)
add_executable(controller src/main.c)

View file

@ -8,6 +8,8 @@ mqtt-port = 1885
mqtt-host = localhost
relay-count = 10
relay-init = 1
database = controller_db.lmdb
log-level = debug
log-file = stdout
@ -16,11 +18,13 @@ log-file = stdout
driver = piface
pin = 0
inverted = 0
init = 0
[relay-1]
driver = piface
pin = 1
inverted = 0
init = 0
[relay-2]
driver = gpio

View file

@ -11,6 +11,7 @@ typedef struct
{
uint8_t pin;
int inverted;
int init;
relay_driver_t driver;
uint8_t pulse_duration;
} config_relay_t;
@ -32,6 +33,7 @@ typedef struct
uint16_t mqtt_port;
char mqtt_host[256];
uint8_t relay_count;
int relays_init;
config_relay_t *relay_configs;
} config_t;

View file

@ -1,5 +1,5 @@
#ifndef CORE_LOGGER_H
#define CORE_LOGGER_H
#ifndef EMGAUWA_LOGGER_H
#define EMGAUWA_LOGGER_H
#include <stdio.h>
#include <time.h>
@ -19,4 +19,4 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
#define LOGGER_CRIT(...) logger_log(LOG_CRIT , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define LOGGER_EMERG(...) logger_log(LOG_EMERG , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#endif //CORE_LOGGER_H
#endif //EMGAUWA_LOGGER_H

View file

@ -128,6 +128,11 @@ config_load(IniDispatch *disp, void *config_void)
strcpy(config->mqtt_host, disp->value);
return 0;
}
if(CONFINI_IS_KEY("controller", "relay-init"))
{
strcpy(config->relay_init, disp->value);
return 0;
}
if(CONFINI_IS_KEY("controller", "relay-count"))
{
config->relay_count = atoi(disp->value);
@ -136,6 +141,7 @@ config_load(IniDispatch *disp, void *config_void)
{
config->relay_configs[i].driver = RELAY_DRIVER_NONE;
config->relay_configs[i].inverted = 0;
config->relay_configs[i].init = -1;
config->relay_configs[i].pin = 0;
config->relay_configs[i].pulse_duration = 0;
}
@ -154,6 +160,11 @@ config_load(IniDispatch *disp, void *config_void)
config->relay_configs[i].inverted = atoi(disp->value);
return 0;
}
if(CONFINI_IS_KEY(relay_section_name, "init"))
{
config->relay_configs[i].init = atoi(disp->value);
return 0;
}
if(CONFINI_IS_KEY(relay_section_name, "pulse-duration"))
{
config->relay_configs[i].pulse_duration = atoi(disp->value);

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include <lmdb.h>
#include <logger.h>
#include <config.h>
#include <constants.h>
@ -13,19 +14,19 @@ database_setup(MDB_env **mdb_env, config_t *config)
if(mdb_env_create(mdb_env) != 0)
{
perror("Can't create mdb handle");
LOGGER_CRIT("Can't create mdb handle");
exit(1);
}
if((err = mdb_env_set_maxdbs(*mdb_env, MDB_MAXDBS)) != 0)
{
fprintf(stderr, "mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
LOGGER_CRIT("mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
exit(1);
}
if(mdb_env_open(*mdb_env, config->database, MDB_NOSUBDIR, 0700) != 0)
{
perror("Can't open mdb file");
LOGGER_CRIT("Can't open mdb file");
exit(1);
}
}

View file

@ -7,8 +7,5 @@
void
driver_gpio_set(int pin, int value)
{
// disable "unused parameter" warning (happens when using wiring_debug)
(void)pin;
(void)value;
digitalWrite(pin, value);
}

View file

@ -7,8 +7,5 @@
void
driver_piface_set(int pin, int value)
{
// disable "unused parameter" warning (happens when using wiring_debug)
(void)pin;
(void)value;
digitalWrite(PIFACE_GPIO_BASE + pin, value);
}

View file

@ -21,17 +21,18 @@
static void
handler_command_pulse(mpack_node_t map, controller_t *controller)
{
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
int relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
relay_t *target_relay = controller->relays[relay_num];
(void)target_relay;
uint8_t duration = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_PULSE_DURATION));
int duration = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_PULSE_DURATION));
if(duration == 0)
{
duration = global_config.relay_configs[relay_num].pulse_duration;
}
target_relay->pulse_timer = duration;
LOGGER_DEBUG("pulsing relay %d for %ds\n", relay_num, duration);
}
static void
@ -40,12 +41,14 @@ handler_command_set_name(mpack_node_t map, controller_t *controller)
char name_buffer[MAX_NAME_LENGTH + 1];
mpack_node_copy_cstr(mpack_node_map_uint(map, COMMAND_MAPPING_NAME), name_buffer, MAX_NAME_LENGTH + 1);
controller_set_name(controller, name_buffer);
LOGGER_DEBUG("setting new name %s for controller\n", name_buffer);
}
static void
handler_command_set_schedule(mpack_node_t map, controller_t *controller)
{
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
LOGGER_DEBUG("setting schedules for relay %d\n", relay_num);
relay_t *target_relay = controller->relays[relay_num];
@ -82,6 +85,7 @@ handler_command_set_relay_name(mpack_node_t map, controller_t *controller)
relay_set_name(controller->relays[relay_num], relay_name);
}
relay_debug(controller->relays[relay_num]);
LOGGER_DEBUG("setting new name %s for relay %d\n", relay_name, relay_num);
}
void
@ -93,7 +97,6 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
return;
}
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
LOGGER_DEBUG("payload_length %d\n", payload_length);
if(c->recv_mbuf.len < payload_length + sizeof(payload_length))
{
@ -109,7 +112,7 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
LOGGER_INFO("received command %d\n", command_code);
LOGGER_DEBUG("received command %d\n", command_code);
switch(command_code)
{

View file

@ -42,16 +42,13 @@ handler_loop(struct mg_connection *c_mqtt)
if(is_on_schedule && !global_config.relay_configs[i].pulse_duration)
{
is_on = 1;
LOGGER_DEBUG("relay %d is active\n", i);
}
if(relay->pulse_timer > 0)
{
is_on = 1;
--relay->pulse_timer;
}
if(is_on)
{
LOGGER_DEBUG("relay %d is active\n", i);
LOGGER_DEBUG("relay %d is pulsing for %d more seconds\n", i, relay->pulse_timer);
}
if(relay->is_on != is_on)
@ -64,6 +61,7 @@ handler_loop(struct mg_connection *c_mqtt)
sprintf(payload_buf, "%u", is_on);
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
relay->sent_to_broker = 1;
LOGGER_DEBUG("sent relay %d status (%d) to mqtt broker\n", i, is_on);
}
relay->is_on = is_on;
relay->is_on_schedule = is_on_schedule;

View file

@ -77,6 +77,9 @@ main(int argc, const char** argv)
global_config.discovery_port = 4421;
global_config.mqtt_port = 1885;
global_config.relay_count = 0;
global_config.relay_init = -1;
global_config.log_level = LOG_INFO;
global_config.log_file = stdout;
@ -138,15 +141,29 @@ main(int argc, const char** argv)
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
{
int relay_default = global_config.relay_configs[i].init
if(relay_default == -1)
{
relay_default = global_config.relays_init;
}
if(relay_default == -1)
{
relay_default = global_config.relay_configs[i].inverted
}
if(global_config.relay_configs[i].driver == RELAY_DRIVER_GPIO)
{
pinMode(global_config.relay_configs[i].pin, OUTPUT);
digitalWrite(global_config.relay_configs[i].pin, global_config.relay_configs[i].inverted);
driver_gpio_set(global_config.relay_configs[i].pin, relay_default);
}
if((global_config.relay_configs[i].driver == RELAY_DRIVER_PIFACE) && !piface_setup)
if(global_config.relay_configs[i].driver == RELAY_DRIVER_PIFACE)
{
piFaceSetup(PIFACE_GPIO_BASE);
piface_setup = 1;
if(!piface_setup)
{
piFaceSetup(PIFACE_GPIO_BASE);
piface_setup = 1;
}
driver_piface_set(global_config.relay_configs[i].pin, relay_default);
}
}
@ -162,7 +179,7 @@ main(int argc, const char** argv)
/******************** START MAIN LOOP ********************/
time_t timer = time(NULL);
time_t timer = 0;
for (;;)
{