From 930ae5498a785ec1d47772920e936fe21a64a562 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 16 Feb 2024 14:45:10 -0800 Subject: [PATCH] implement bmc gpio, clkeanup some code --- cgi.h | 4 ---- handlers.h | 36 +++++++++++++++++++++++++++--------- main.c | 7 +++++++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/cgi.h b/cgi.h index 395f2e9..51cea1e 100644 --- a/cgi.h +++ b/cgi.h @@ -5,10 +5,6 @@ #include "pico/cyw43_arch.h" #include "handlers.h" -char parseRequestedState (char * requested_state) { - -} - const char * cgi_power_handler (int iIndex, int iNumParams, char * pcParam [], char * pcValue []) { // Check if an request for power has been made (/power?requested_state=x) if (strcmp(pcParam[0] , "requested_state") == 0){ diff --git a/handlers.h b/handlers.h index c82933f..4b7b481 100644 --- a/handlers.h +++ b/handlers.h @@ -3,36 +3,54 @@ #include "pico/stdlib.h" -#define PW_SW_DELAY_MS 100 -#define STATE_UPDATE_REPEAT_DELAY_MS 100 +#define PW_SWITCH_PIN 0 +#define PW_SWITCH_DELAY_MS 100 +#define PW_STATE_PIN 1 +#define PW_STATE_UPDATE_REPEAT_DELAY_MS 100 bool current_state = false; 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, 1); - return 0; // do not reschedule alarm -} - -int64_t pw_sw_off_async (alarm_id_t id, void * user_data) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); + gpio_put(PW_SWITCH_PIN, 1); 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); + 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); 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) { if (requested_state != current_state) { add_alarm_in_ms(0, pw_sw_on_async, NULL, true); - add_alarm_in_ms(PW_SW_DELAY_MS, pw_sw_off_async, NULL, true); + add_alarm_in_ms(PW_SWITCH_DELAY_MS, pw_sw_off_async, NULL, true); } } void bmc_handler_init () { + // init power switch pin as output + gpio_init(PW_SWITCH_PIN); + gpio_set_dir(PW_SWITCH_DELAY_MS, GPIO_OUT); + + // init power state pin as input + gpio_init(PW_STATE_PIN); + gpio_set_dir(PW_STATE_PIN, GPIO_IN); + + // set repeating timer for power state update state_update_timer = malloc(sizeof(struct repeating_timer)); - add_repeating_timer_ms(STATE_UPDATE_REPEAT_DELAY_MS, update_current_state_async, NULL, state_update_timer); + add_repeating_timer_ms(PW_STATE_UPDATE_REPEAT_DELAY_MS, update_current_state_async, NULL, state_update_timer); } void bmc_handler_deinit () { diff --git a/main.c b/main.c index fd29148..488bd49 100644 --- a/main.c +++ b/main.c @@ -23,17 +23,24 @@ int main() { } printf("Wi-Fi connected\n"); + // init httpd httpd_init(); printf("HTTP Server initialized at %s on port %d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), HTTPD_SERVER_PORT); + // enable ssi ssi_init(); printf("SSI Handler initialized\n"); + // enable cgi cgi_init(); printf("CGI Handler initialised\n"); + // init bmc handler bmc_handler_init(); printf("BMC handler initialized\n"); + // set LED to on to indicate it has connected and initialized + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); + while(1); } \ No newline at end of file