This commit is contained in:
2021-02-17 22:54:53 -05:00
parent bc316375e0
commit 68f57a0807
20 changed files with 3554 additions and 23 deletions

View File

@@ -36,7 +36,6 @@ void AttinyDebounce::update(unsigned long millisNow) {
break;
case TRIGGERED:
// TODO: Debounce this part too
if (!assertedNow) {
state = IDLE;
}

View File

@@ -29,13 +29,14 @@ constexpr uint8_t BUTTON_PIN = 1; // Button press
constexpr uint8_t RTC_PIN = 4; // RTC alarm (currently, from the ESP8266 RTC)
constexpr uint8_t AUX_PIN = 5; // Another wakeup source. May be used for other sensors in the future.
constexpr uint8_t DEBUG_LED = RTC_PIN;
// constexpr uint8_t DEBUG_LED = RTC_PIN;
constexpr unsigned long CMD_TIMEOUT_MILLIS = 1000; // After this period of with no commands, the device will sleep
constexpr unsigned long WAKE_PULSE_MILLIS = 100; // Length of pulse sent to wake the main MCU
constexpr unsigned long LONG_PRESS_MILLIS = 5000;
constexpr unsigned long I2C_WATCHDOG_PERIOD_MILLIS = 2000; // If we don't get a request from the Main MCU in this time, just go to sleep.
constexpr unsigned long DEBOUNCE_MILLIS = 100;
constexpr unsigned long I2C_WATCHDOG_PERIOD_MILLIS = 1000; // If we don't get a request from the Main MCU in this time, just go to sleep.
constexpr unsigned long DEBOUNCE_MILLIS = 1;
constexpr unsigned long NO_WAKEUP_SRC_TIMEOUT_MILLIS = DEBOUNCE_MILLIS + 5;
constexpr uint8_t I2C_DEVICE_ADDR = 0x4F; // Chosen arbitrarily
@@ -78,6 +79,7 @@ static unsigned long startWakeMillis, i2cWatchDogTime;
static I2cReq i2c_cmd_byte;
static volatile WakeSource wakeSource;
static volatile unsigned long endButtonMillis;
static unsigned long wakeupMillis = 0;
static void i2cReceiveHook(int numBytes) {
while (numBytes > 0) {
@@ -96,8 +98,6 @@ static void i2cRequestHook() {
}
}
static void enablePinChangeInterrupt() {
GIMSK |= _BV(PCIE);
PCMSK |= _BV(BUTTON_PIN);
@@ -123,15 +123,15 @@ static void resetState() {
void setup() {
// Init pin states
pinMode(MAIN_MCU_NRESET_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(RTC_PIN, INPUT);
pinMode(AUX_PIN, INPUT);
pinMode(DEBUG_LED, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RTC_PIN, INPUT_PULLUP);
pinMode(AUX_PIN, INPUT_PULLUP);
// pinMode(DEBUG_LED, OUTPUT);
// On first run, reset the main MCU to get in sync.
digitalWrite(MAIN_MCU_NRESET_PIN, HIGH);
delay(WAKE_PULSE_MILLIS);
digitalWrite(MAIN_MCU_NRESET_PIN, LOW);
delay(WAKE_PULSE_MILLIS);
digitalWrite(MAIN_MCU_NRESET_PIN, HIGH);
appState = AppState::SLEEP;
@@ -151,21 +151,19 @@ static void buttonCallback(int index) {
}
ISR(PCINT0_vect) {
unsigned long millisNow = millis();
ButtonDebounce.update(millisNow);
ButtonDebounce.update(millis());
}
void loop() {
unsigned long millisNow = millis();
ButtonDebounce.update(millisNow);
ButtonDebounce.update(millis());
switch (appState) {
case AppState::SLEEP:
resetState();
//digitalWrite(DEBUG_LED, HIGH);
// digitalWrite(DEBUG_LED, LOW);
sleepUntilPinChange();
//digitalWrite(DEBUG_LED, LOW);
wakeupMillis = millis();
// digitalWrite(DEBUG_LED, HIGH);
appState = AppState::WATCHING_INPUT;
break;
case AppState::WATCHING_INPUT:
@@ -173,21 +171,24 @@ void loop() {
// Keep waiting. For example, waiting to see if long or short button press.
// TODO: Not yet implemented
} else if (wakeSource == WakeSource::NONE) {
// appState = AppState::SLEEP;
if (millis() > wakeupMillis + NO_WAKEUP_SRC_TIMEOUT_MILLIS) {
appState = AppState::SLEEP;
break;
}
} else {
// We know the wakeup source. Time to inform the main MCU.
appState = AppState::START_WAKE_MAIN_MCU;
}
break;
case AppState::START_WAKE_MAIN_MCU:
digitalWrite(MAIN_MCU_NRESET_PIN, HIGH);
digitalWrite(MAIN_MCU_NRESET_PIN, LOW);
startWakeMillis = millis();
appState = AppState::WAKING_MAIN_MCU;
break;
case AppState::WAKING_MAIN_MCU:
if (millis() > startWakeMillis + WAKE_PULSE_MILLIS) {
appState = AppState::WAITING_FOR_I2C;
digitalWrite(MAIN_MCU_NRESET_PIN, LOW);
digitalWrite(MAIN_MCU_NRESET_PIN, HIGH);
}
break;
case AppState::WAITING_FOR_I2C: