move pico libs to pico_lib.h,

add INV option to PW_SWITCH and PW_STATE
This commit is contained in:
Arthur Lu 2024-02-18 11:55:43 -08:00
parent 9da9e35393
commit 7aadd6d4e0
5 changed files with 15 additions and 11 deletions

1
cgi.h
View File

@ -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 []) {

View File

@ -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);

3
main.c
View File

@ -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"

8
pico_lib.h Normal file
View File

@ -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

2
ssi.h
View File

@ -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"};