add: pulse mode
add: editorconfig
This commit is contained in:
parent
679175f1a9
commit
0f342b4aa8
13 changed files with 151 additions and 70 deletions
12
.editorconfig
Normal file
12
.editorconfig
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# EditorConfig is awesome:
|
||||||
|
https://EditorConfig.org
|
||||||
|
|
||||||
|
# top-most EditorConfig file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
# Unix-style newlines with a newline ending every file
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(controller
|
project(controller
|
||||||
VERSION 0.2.0
|
VERSION 0.2.1
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(controller src/main.c)
|
add_executable(controller src/main.c)
|
||||||
|
|
|
@ -54,8 +54,10 @@ inverted = 1
|
||||||
driver = gpio
|
driver = gpio
|
||||||
pin = 16
|
pin = 16
|
||||||
inverted = 1
|
inverted = 1
|
||||||
|
pulse-duration = 3
|
||||||
|
|
||||||
[relay-9]
|
[relay-9]
|
||||||
driver = gpio
|
driver = gpio
|
||||||
pin = 15
|
pin = 15
|
||||||
inverted = 1
|
inverted = 1
|
||||||
|
pulse-duration = 3
|
||||||
|
|
|
@ -11,6 +11,7 @@ typedef struct
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
int inverted;
|
int inverted;
|
||||||
relay_driver_t driver;
|
relay_driver_t driver;
|
||||||
|
uint8_t pulse_duration;
|
||||||
} config_relay_t;
|
} config_relay_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|
|
@ -24,6 +24,7 @@ typedef enum
|
||||||
COMMAND_MAPPING_SCHEDULE_ID = 4,
|
COMMAND_MAPPING_SCHEDULE_ID = 4,
|
||||||
COMMAND_MAPPING_PERIODS_COUNT = 5,
|
COMMAND_MAPPING_PERIODS_COUNT = 5,
|
||||||
COMMAND_MAPPING_PERIODS_BLOB = 6,
|
COMMAND_MAPPING_PERIODS_BLOB = 6,
|
||||||
|
COMMAND_MAPPING_PULSE_DURATION = 7,
|
||||||
} control_mapping_t;
|
} control_mapping_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
|
@ -36,6 +37,7 @@ typedef enum
|
||||||
COMMAND_CODE_GET_SCHEDULE = 103,
|
COMMAND_CODE_GET_SCHEDULE = 103,
|
||||||
COMMAND_CODE_SET_RELAY_NAME = 104,
|
COMMAND_CODE_SET_RELAY_NAME = 104,
|
||||||
COMMAND_CODE_GET_RELAY_NAME = 105,
|
COMMAND_CODE_GET_RELAY_NAME = 105,
|
||||||
|
COMMAND_CODE_PULSE = 200,
|
||||||
} command_code_t;
|
} command_code_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
|
|
|
@ -12,6 +12,8 @@ typedef struct
|
||||||
{
|
{
|
||||||
uint8_t number;
|
uint8_t number;
|
||||||
int is_on;
|
int is_on;
|
||||||
|
int is_on_schedule;
|
||||||
|
int pulse_timer;
|
||||||
int sent_to_broker;
|
int sent_to_broker;
|
||||||
char name[MAX_NAME_LENGTH + 1];
|
char name[MAX_NAME_LENGTH + 1];
|
||||||
schedule_t *schedules[7];
|
schedule_t *schedules[7];
|
||||||
|
@ -53,7 +55,7 @@ int
|
||||||
relay_save(relay_t *relay, MDB_env *mdb_env);
|
relay_save(relay_t *relay, MDB_env *mdb_env);
|
||||||
|
|
||||||
int
|
int
|
||||||
relay_is_active(relay_t *relay, struct tm *time_struct);
|
relay_is_on_schedule(relay_t *relay, struct tm *time_struct);
|
||||||
|
|
||||||
void
|
void
|
||||||
relay_free(relay_t *relay);
|
relay_free(relay_t *relay);
|
||||||
|
|
|
@ -4,12 +4,13 @@
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
|
|
||||||
#ifdef WIRING_PI_DEBUG
|
#ifdef WIRING_PI_DEBUG
|
||||||
#define wiringPiSetup() LOG_DEBUG("wiringPi wiringPiSetup()\n")
|
#define LOG_WIRING_PI LOG_TRACE
|
||||||
#define wiringPiSetupSys() LOG_DEBUG("wiringPi wiringPiSetupSys()\n")
|
#define wiringPiSetup() LOG_WIRING_PI("wiringPi wiringPiSetup()\n")
|
||||||
#define pinMode(x,y) LOG_DEBUG("wiringPi pinMode(%d, %d)\n", x, y)
|
#define wiringPiSetupSys() LOG_WIRING_PI("wiringPi wiringPiSetupSys()\n")
|
||||||
#define digitalWrite(x,y) LOG_DEBUG("wiringPi digitalWrite(%d, %d)\n", x, y)
|
#define pinMode(x,y) LOG_WIRING_PI("wiringPi pinMode(%d, %d)\n", x, y)
|
||||||
|
#define digitalWrite(x,y) LOG_WIRING_PI("wiringPi digitalWrite(%d, %d)\n", x, y)
|
||||||
|
|
||||||
#define piFaceSetup(x) LOG_DEBUG("wiringPi piFaceSetup(%d)\n", x)
|
#define piFaceSetup(x) LOG_WIRING_PI("wiringPi piFaceSetup(%d)\n", x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
||||||
|
|
|
@ -18,6 +18,22 @@
|
||||||
#include <mpack.h>
|
#include <mpack.h>
|
||||||
#include <models/schedule.h>
|
#include <models/schedule.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
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));
|
||||||
|
if(duration == 0)
|
||||||
|
{
|
||||||
|
duration = global_config.relay_configs[relay_num].pulse_duration;
|
||||||
|
}
|
||||||
|
target_relay->pulse_timer = duration;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handler_command_set_name(mpack_node_t map, controller_t *controller)
|
handler_command_set_name(mpack_node_t map, controller_t *controller)
|
||||||
{
|
{
|
||||||
|
@ -72,8 +88,10 @@ void
|
||||||
handler_command(struct mg_connection *c, int ev, void *ev_data)
|
handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
{
|
{
|
||||||
(void)ev_data;
|
(void)ev_data;
|
||||||
if(ev == MG_EV_RECV)
|
if(ev != MG_EV_RECV)
|
||||||
{
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
|
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
|
||||||
LOG_DEBUG("payload_length %d\n", payload_length);
|
LOG_DEBUG("payload_length %d\n", payload_length);
|
||||||
|
|
||||||
|
@ -114,6 +132,9 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_GET_RELAY_NAME:
|
case COMMAND_CODE_GET_RELAY_NAME:
|
||||||
break;
|
break;
|
||||||
|
case COMMAND_CODE_PULSE:
|
||||||
|
handler_command_pulse(root, global_controller);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_ERROR("received invalid command\n");
|
LOG_ERROR("received invalid command\n");
|
||||||
}
|
}
|
||||||
|
@ -122,6 +143,5 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
{
|
{
|
||||||
LOG_WARN("error when destroying mpack tree\n");
|
LOG_WARN("error when destroying mpack tree\n");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
controller_save(global_controller, global_mdb_env);
|
controller_save(global_controller, global_mdb_env);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,43 +21,65 @@ handler_loop(struct mg_connection *c_mqtt)
|
||||||
char controller_uid[UUID_STR_LEN];
|
char controller_uid[UUID_STR_LEN];
|
||||||
uuid_unparse(global_controller->id, controller_uid);
|
uuid_unparse(global_controller->id, controller_uid);
|
||||||
|
|
||||||
time_t timestamp = time(NULL);
|
struct tm time_last, time_now;
|
||||||
struct tm *time_struct = localtime(×tamp);
|
time_t timestamp;
|
||||||
LOG_DEBUG("===== IDLE LOOP START =====\n");
|
|
||||||
|
timestamp = time(NULL) - 1;
|
||||||
|
localtime_r(×tamp, &time_last);
|
||||||
|
timestamp = time(NULL);
|
||||||
|
localtime_r(×tamp, &time_now);
|
||||||
|
LOG_TRACE("===== IDLE LOOP START =====\n");
|
||||||
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
||||||
{
|
{
|
||||||
relay_t *relay = global_controller->relays[i];
|
relay_t *relay = global_controller->relays[i];
|
||||||
int is_active = 0;
|
int is_on = 0;
|
||||||
if(relay_is_active(relay, time_struct))
|
|
||||||
|
int is_on_schedule = relay_is_on_schedule(relay, &time_now);
|
||||||
|
|
||||||
|
if(relay->is_on_schedule != is_on_schedule && relay->is_on_schedule != -1)
|
||||||
{
|
{
|
||||||
LOG_DEBUG("relay %d is active\n", i);
|
relay->pulse_timer = global_config.relay_configs[i].pulse_duration;
|
||||||
is_active = 1;
|
}
|
||||||
|
if(is_on_schedule && !global_config.relay_configs[i].pulse_duration)
|
||||||
|
{
|
||||||
|
is_on = 1;
|
||||||
|
}
|
||||||
|
if(relay->pulse_timer > 0)
|
||||||
|
{
|
||||||
|
is_on = 1;
|
||||||
|
--relay->pulse_timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(relay->is_on != is_active)
|
if(is_on)
|
||||||
|
{
|
||||||
|
LOG_DEBUG("relay %d is active\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(relay->is_on != is_on)
|
||||||
{
|
{
|
||||||
relay->sent_to_broker = 0;
|
relay->sent_to_broker = 0;
|
||||||
}
|
}
|
||||||
if(!relay->sent_to_broker && c_mqtt)
|
if(!relay->sent_to_broker && c_mqtt)
|
||||||
{
|
{
|
||||||
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
|
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
|
||||||
sprintf(payload_buf, "%u", is_active);
|
sprintf(payload_buf, "%u", is_on);
|
||||||
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
|
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
|
||||||
relay->sent_to_broker = 1;
|
relay->sent_to_broker = 1;
|
||||||
}
|
}
|
||||||
relay->is_on = is_active;
|
relay->is_on = is_on;
|
||||||
|
relay->is_on_schedule = is_on_schedule;
|
||||||
|
|
||||||
if(global_config.relay_configs[i].inverted)
|
if(global_config.relay_configs[i].inverted)
|
||||||
{
|
{
|
||||||
is_active = !is_active;
|
is_on = !is_on;
|
||||||
}
|
}
|
||||||
switch(global_config.relay_configs[i].driver)
|
switch(global_config.relay_configs[i].driver)
|
||||||
{
|
{
|
||||||
case RELAY_DRIVER_GPIO:
|
case RELAY_DRIVER_GPIO:
|
||||||
driver_gpio_set(global_config.relay_configs[i].pin, is_active);
|
driver_gpio_set(global_config.relay_configs[i].pin, is_on);
|
||||||
break;
|
break;
|
||||||
case RELAY_DRIVER_PIFACE:
|
case RELAY_DRIVER_PIFACE:
|
||||||
driver_piface_set(global_config.relay_configs[i].pin, is_active);
|
driver_piface_set(global_config.relay_configs[i].pin, is_on);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_WARN("relay %d is not using a driver\n", i);
|
LOG_WARN("relay %d is not using a driver\n", i);
|
||||||
|
|
|
@ -82,6 +82,9 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
||||||
for(uint8_t i = 0; i < config->relay_count; ++i)
|
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||||
{
|
{
|
||||||
config->relay_configs[i].driver = RELAY_DRIVER_NONE;
|
config->relay_configs[i].driver = RELAY_DRIVER_NONE;
|
||||||
|
config->relay_configs[i].inverted = 0;
|
||||||
|
config->relay_configs[i].pin = 0;
|
||||||
|
config->relay_configs[i].pulse_duration = 0;
|
||||||
}
|
}
|
||||||
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -99,6 +102,11 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
||||||
config->relay_configs[i].inverted = atoi(disp->value);
|
config->relay_configs[i].inverted = atoi(disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if(CONFINI_IS_KEY(relay_section_name, "pulse-duration"))
|
||||||
|
{
|
||||||
|
config->relay_configs[i].pulse_duration = atoi(disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if(CONFINI_IS_KEY(relay_section_name, "driver"))
|
if(CONFINI_IS_KEY(relay_section_name, "driver"))
|
||||||
{
|
{
|
||||||
if(strcasecmp(disp->value, "gpio") == 0)
|
if(strcasecmp(disp->value, "gpio") == 0)
|
||||||
|
|
|
@ -140,15 +140,20 @@ main(int argc, const char** argv)
|
||||||
|
|
||||||
/******************** START MAIN LOOP ********************/
|
/******************** START MAIN LOOP ********************/
|
||||||
|
|
||||||
|
time_t timer = time(NULL);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
mg_mgr_poll(&mgr, 1000);
|
mg_mgr_poll(&mgr, 200);
|
||||||
|
if(time(NULL) - timer >= 1)
|
||||||
|
{
|
||||||
if(!global_connection_mqtt)
|
if(!global_connection_mqtt)
|
||||||
{
|
{
|
||||||
LOG_DEBUG("mqtt connection is not open\n");
|
|
||||||
connection_mqtt_connect(&mgr);
|
connection_mqtt_connect(&mgr);
|
||||||
}
|
}
|
||||||
handler_loop(global_connection_mqtt);
|
handler_loop(global_connection_mqtt);
|
||||||
|
timer = time(NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terminate(0);
|
terminate(0);
|
||||||
|
|
|
@ -15,6 +15,8 @@ relay_create(uint8_t number)
|
||||||
new_relay->name[0] = '\0';
|
new_relay->name[0] = '\0';
|
||||||
|
|
||||||
new_relay->is_on = -1;
|
new_relay->is_on = -1;
|
||||||
|
new_relay->is_on_schedule = -1;
|
||||||
|
new_relay->pulse_timer = 0;
|
||||||
new_relay->sent_to_broker = 0;
|
new_relay->sent_to_broker = 0;
|
||||||
|
|
||||||
uuid_t off_id;
|
uuid_t off_id;
|
||||||
|
@ -37,7 +39,7 @@ relay_set_name(relay_t *relay, const char *name)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
relay_is_active(relay_t *relay, struct tm *time_struct)
|
relay_is_on_schedule(relay_t *relay, struct tm *time_struct)
|
||||||
{
|
{
|
||||||
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
if(schedule->length == 0)
|
if(schedule->length == 0)
|
||||||
|
|
|
@ -65,7 +65,11 @@ relay_load(MDB_env *mdb_env, uint8_t num)
|
||||||
|
|
||||||
new_relay = malloc(sizeof(relay_t));
|
new_relay = malloc(sizeof(relay_t));
|
||||||
new_relay->number = num;
|
new_relay->number = num;
|
||||||
|
|
||||||
new_relay->is_on = -1;
|
new_relay->is_on = -1;
|
||||||
|
new_relay->is_on_schedule = -1;
|
||||||
|
new_relay->pulse_timer = 0;
|
||||||
|
new_relay->sent_to_broker = 0;
|
||||||
|
|
||||||
MDB_val value;
|
MDB_val value;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue