From 914d73a86a9b1c5912ec8b8789bc284e5bc49fba Mon Sep 17 00:00:00 2001 From: ltcptgeneral Date: Sun, 18 Feb 2024 11:55:43 -0800 Subject: [PATCH] move pico libs to pico_lib.h, add INV option to PW_SWITCH and PW_STATE --- cgi.h | 1 - handlers.h | 12 ++++++------ main.c | 3 +-- pico_lib.h | 8 ++++++++ ssi.h | 2 -- 5 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 pico_lib.h diff --git a/cgi.h b/cgi.h index 51cea1e..2665358 100644 --- a/cgi.h +++ b/cgi.h @@ -2,7 +2,6 @@ #define CGI_H #include "lwip/apps/httpd.h" -#include "pico/cyw43_arch.h" #include "handlers.h" const char * cgi_power_handler (int iIndex, int iNumParams, char * pcParam [], char * pcValue []) { diff --git a/handlers.h b/handlers.h index 4b7b481..add813e 100644 --- a/handlers.h +++ b/handlers.h @@ -1,12 +1,12 @@ #ifndef HANDLERS_H #define HANDLERS_H -#include "pico/stdlib.h" - #define PW_SWITCH_PIN 0 #define PW_SWITCH_DELAY_MS 100 +#define PW_SWITCH_INV 0 #define PW_STATE_PIN 1 #define PW_STATE_UPDATE_REPEAT_DELAY_MS 100 +#define PW_STATE_INV 1 bool current_state = false; struct repeating_timer * state_update_timer = NULL; @@ -14,25 +14,25 @@ struct repeating_timer * state_update_timer = NULL; // handler fn to set the power switch pin to an active state int64_t pw_sw_on_async (alarm_id_t id, void * user_data) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); - gpio_put(PW_SWITCH_PIN, 1); + gpio_put(PW_SWITCH_PIN, 1 ^ PW_SWITCH_INV); return 0; // do not reschedule alarm } // handler fn to set the power switch pin to an inactive state int64_t pw_sw_off_async (alarm_id_t id, void * user_data) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - gpio_put(PW_SWITCH_PIN, 0); + gpio_put(PW_SWITCH_PIN, 0 ^ PW_SWITCH_INV); return 0; // do not reschedule alarm } // hander fn to read from the power state bool update_current_state_async (repeating_timer_t * rt) { - current_state = gpio_get(PW_STATE_PIN); + current_state = gpio_get(PW_STATE_PIN) ^ PW_STATE_INV; return true; // continue repeating alarm } // handler fn called to attempt to set the power state to the requested state -bool bmc_power_handler (bool requested_state) { +void bmc_power_handler (bool requested_state) { if (requested_state != current_state) { add_alarm_in_ms(0, pw_sw_on_async, NULL, true); add_alarm_in_ms(PW_SWITCH_DELAY_MS, pw_sw_off_async, NULL, true); diff --git a/main.c b/main.c index 488bd49..494b511 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ #include "lwip/apps/httpd.h" -#include "pico/stdlib.h" -#include "pico/cyw43_arch.h" +#include "pico_lib.h" #include "lwipopts.h" #include "ssi.h" #include "cgi.h" diff --git a/pico_lib.h b/pico_lib.h new file mode 100644 index 0000000..75bcb4e --- /dev/null +++ b/pico_lib.h @@ -0,0 +1,8 @@ +#ifndef PICO_LIB_H +#define PICO_LIB_H + +#include "pico/cyw43_arch.h" +#include "pico/stdlib.h" +#include "hardware/adc.h" + +#endif \ No newline at end of file diff --git a/ssi.h b/ssi.h index 0af385d..5f34730 100644 --- a/ssi.h +++ b/ssi.h @@ -2,8 +2,6 @@ #define SSI_H #include "lwip/apps/httpd.h" -#include "pico/cyw43_arch.h" -#include "hardware/adc.h" #include "handlers.h" const char * ssi_tags[] = {"volt", "temp", "power"};