From 922420a6fddb4e949b2f77002e321814d89d8e77 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Sat, 27 Jan 2018 17:18:20 +0100 Subject: [PATCH] Added relay handler --- .vscode/c_cpp_properties.json | 6 ++++-- src/main.c | 2 ++ src/relayHandler.c | 29 +++++++++++++++++++++++++++++ src/relayHandler.h | 13 +++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/relayHandler.c create mode 100644 src/relayHandler.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e9bfc3f..901e34f 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -9,11 +9,13 @@ "${workspaceRoot}/../modules/mongoose-os/fw/include", "${workspaceRoot}/../modules/mongoose-os/common", "${workspaceRoot}/../modules/mongoose-os/src", - "${workspaceRoot}/../modules/mjs", + "${workspaceRoot}/../modules/libs/mjs", "${workspaceRoot}/../modules/libs/http-server/include", "${workspaceRoot}/../modules/libs/http-i2c/include", "${workspaceRoot}/../modules/libs/mqtt/include", - "${workspaceRoot}/../modules/libs/cron/include" + "${workspaceRoot}/../modules/libs/cron/include", + "${workspaceRoot}/../modules/libs/crontab/include", + "${workspaceRoot}/../modules/libs/wifi/include" ], "defines": [], "intelliSenseMode": "clang-x64", diff --git a/src/main.c b/src/main.c index d0fab74..88a8a8d 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,7 @@ #include "buttonHandler.h" #include "ledHandler.h" +#include "relayHandler.h" #define RELAY_PIN 12 @@ -52,6 +53,7 @@ static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user static void buttonPressOne(int pressCount) { LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount)); + set_relay(RELAY_TOGGLE); } static void buttonPressTwo(int pressCount) { diff --git a/src/relayHandler.c b/src/relayHandler.c new file mode 100644 index 0000000..2936301 --- /dev/null +++ b/src/relayHandler.c @@ -0,0 +1,29 @@ +#include "mgos.h" +#include "mgos_gpio.h" +#include "relayHandler.h" + +#define ON_BOARD_RELAY_PIN 8 + + +void set_relay(enum RelayAction rm) { + switch (rm) { + case RELAY_ON: + mgos_gpio_write(ON_BOARD_RELAY_PIN, false); + break; + case RELAY_OFF: + mgos_gpio_write(ON_BOARD_RELAY_PIN, true); + break; + case RELAY_TOGGLE: + mgos_gpio_toggle(ON_BOARD_RELAY_PIN); + break; + default: + LOG(LL_ERROR, ("ERROR: Unknown relay mode %d -- ignored", rm)); + break; + } + LOG(LL_DEBUG, ("set_relay mode=%d", rm)); +} + +void init_relay_handler() { + mgos_gpio_set_mode(ON_BOARD_RELAY_PIN, MGOS_GPIO_MODE_OUTPUT); + mgos_gpio_write(ON_BOARD_RELAY_PIN, true); +} \ No newline at end of file diff --git a/src/relayHandler.h b/src/relayHandler.h new file mode 100644 index 0000000..4d7a52a --- /dev/null +++ b/src/relayHandler.h @@ -0,0 +1,13 @@ +// relayHandler.h +#ifndef _relayHandler_h_included_ +#define _relayHandler_h_included_ + +enum RelayAction { + RELAY_ON = 0, + RELAY_OFF, + RELAY_TOGGLE +}; +extern void set_relay(enum RelayAction newStatus); +extern void init_relay_handler(); + +#endif