project
Ghost Hand
Public Unreviewedby John Lauer
A small battery gadget that keeps an automatic sink faucet running while you use the sink: it learns the faucet's timeout, re-arms with the shortest blip, and stops after 10 minutes or when picked up.
main
John Lauer
Studio: project overview, how it works, lights, charging, battery life, firmware source, dashboard screenshots; licenseā¦
c2eaf61
3d ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
/*
* power.h : clocks, time base, sleep modes, battery / USB sensing, EEPROM, watchdog
*/
#ifndef POWER_H
#define POWER_H
#include <stdint.h>
#include <stdbool.h>
/* ---- clock ---- */
void clock_init(void); /* OSC20M / 4 = 5 MHz */
void clock_fast(void); /* OSC20M / 2 = 10 MHz (LED writes only) */
void clock_normal(void); /* back to 5 MHz */
/* ---- time base: RTC on OSCULP32K / 32 = 1024 Hz, PIT 64 Hz tick ---- */
#define TICKS_PER_S 1024u
void time_init(void);
void time_suspend(void); /* before power-down */
void time_resume(void);
uint32_t time_ticks(void); /* 1/1024 s, safe in and out of ISRs */
uint32_t time_ms(void); /* ticks converted to ms */
bool tick_take(void); /* true once per PIT tick (64 Hz) */
bool tick_pending(void); /* a tick is waiting (do not sleep) */
/* ---- sleep ---- */
typedef enum { SLEEP_IDLE_MODE, SLEEP_STANDBY_MODE, SLEEP_POWERDOWN_MODE } sleep_mode_t;
void sleep_now(sleep_mode_t m);
/* ---- pins and wake sources ---- */
void pins_init(void); /* every pin in its safe / low-power state */
void wake_arm(void); /* PA2 rising + PB2 rising wake power-down */
bool wake_take_accel(void); /* ACC_INT1 edge seen since the last call */
bool wake_take_vbus(void); /* VBUS_SENSE edge seen since the last call */
/* ---- battery and charger ---- */
bool vbus_present(void);
bool charge_complete(void); /* CHG_STAT high while VBUS present */
uint16_t vbat_mv(void); /* always-on divider, long sampling, averaged */
uint8_t vbat_percent(uint16_t mv); /* LiPo curve, 4.20 V = 100, 3.45 V = 0 */
/* ---- ADC0 (shared by the battery and the receiver noise floor) ---- */
uint16_t adc_read_mv(uint8_t muxpos, bool ref_2v5, uint8_t nsamp_log2, uint8_t sampctrl);
/* ---- watchdog ---- */
void wdt_on(void); /* 1 s */
void wdt_off(void);
void wdt_kick(void);
/* ---- EEPROM ---- */
void eeprom_read_bytes(uint8_t addr, void *dst, uint8_t n);
void eeprom_write_bytes(uint8_t addr, const void *src, uint8_t n); /* one page (32 B) max */
void delay_ms_busy(uint16_t ms);
#endif