Funcionan los temporizadores

Buenas noches
This commit is contained in:
Nacho 2025-05-30 02:48:08 +02:00
parent 70a7144d52
commit ef52dba409
7 changed files with 67 additions and 31 deletions

View File

@ -150,6 +150,7 @@ extern bool k4_prev;
extern bool inactive;
extern bool screenOff;
extern bool skipSleep;
extern uint64_t lastPressedButtonTime;
extern uint64_t lastUpdateTime;

View File

@ -19,27 +19,30 @@ void energy_setUpLightSleep() {
Serial.println("Failed to set EXT1 Wake-Up as wake-up source.");
}
//esp_sleep_enable_timer_wakeup(SLEEP_TIME_US);
esp_sleep_enable_timer_wakeup(SLEEP_TIME_US);
}
void energy_startLightSleep() {
if (skipSleep) {
skipSleep = false;
return;
}
esp_light_sleep_start();
// 6) Figure out which woke you
auto cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_EXT1) {
uint64_t mask = esp_sleep_get_ext1_wakeup_status();
int pin = mask ? __builtin_ctzll(mask) : -1;
Serial.printf("Woke by button on GPIO %d\n", pin);
if (cause == ESP_SLEEP_WAKEUP_EXT0) {
Serial.println("Woke by button on GPIO");
} else if (cause == ESP_SLEEP_WAKEUP_TIMER) {
Serial.println("Woke by timer");
} else {
Serial.printf("Other wakeup: %d\n", cause);
}
runVpetTasks = true;
byte pinValue = digitalRead(26);
Serial.println(pinValue);
// 7) Re-init your display (SPI.begin(), tft.init(), redraw…)
}

View File

@ -65,12 +65,14 @@ uint8_t eggNumber = 0;
TaskHandle_t secondLoop = NULL;
TaskHandle_t readSteps = NULL;
bool skipSleep = false;
void loop2();
void secondCoreTask(void*);
void loop_readSteps(void*);
void setup() {
//Serial.begin(115200);
Serial.begin(115200);
delay(100); // Give MPU6050 and ESP32 time to power up
Wire.begin(MPU_SDA_PIN, MPU_SCL_PIN); // I2C init before MPU6050
@ -102,11 +104,6 @@ void setup() {
void loop() {
if (screenOff) {
printf("[TEST] Going to sleep\n");
energy_startLightSleep();
}
switch (screenKey) {
case TITLE_SCREEN:
menu_drawTitle(bg);
@ -197,9 +194,15 @@ void loop() {
void loop2() {
buttons_checkInactivity();
vpet_runVpetTasks();
getLocalTime(&timeInfo, 50);
dayUnixTime = mktime(&timeInfo) % SECONDS_IN_DAY;
if (screenOff && !skipSleep) {
energy_startLightSleep();
} else if (screenOff && skipSleep) {
skipSleep = false;
}
}
void secondCoreTask(void*) {

View File

@ -54,5 +54,7 @@ void menu_reloadEggs(uint8_t selectedEgg) {
lines_getSingleLine(fileName);
lines_getLineCareMistakes(fileName);
vpetLastEvaluationTime = esp_timer_get_time();
charaData.hatching = true;
}

View File

@ -3,12 +3,12 @@
int interruptKey = -1;
void menu_timerFinishedScreen(TFT_eSprite &bg, TFT_eSprite &sprite, struct SpriteData* spriteData) {
lastPressedButtonTime = esp_timer_get_time();
digitalWrite(BL_PIN, HIGH);
screenOff = false;
inactive = false;
lastPressedButtonTime = esp_timer_get_time();
digitalWrite(BL_PIN, HIGH);
beepCounter = 0;
screenKey = interruptKey;

View File

@ -5,6 +5,8 @@
hw_timer_t *actionTimerDelta = NULL;
bool runVpetTasks = false;
uint64_t vpetLastEvaluationTime = esp_timer_get_time();
void vpet_initTimer() {
printf("[TIMER] Timer Init.\n");
actionTimerDelta = timerBegin(0, 80, true);
@ -21,7 +23,7 @@ void vpet_computeCallLight() {
);
}
bool vpet_evalSleep() {
bool vpet_evalSleep(uint8_t diff_sec) {
// Se devuelve true si quieres pausar los otros contadores
// False ejecutara los contadores correspondientes
if (
@ -58,7 +60,10 @@ bool vpet_evalSleep() {
// durante el dia.
// Resultado, el personaje deberia de dormir una siesta
) {
charaData.sleepCareMistakeCounter++;
charaData.sleepCareMistakeCounter += diff_sec;
if (charaData.hungerCareMistakeTimer <= 0) {
skipSleep = true;
}
return true;
@ -103,7 +108,7 @@ bool vpet_evalSleep() {
// pero no se le ha mandado a dormir, empieza a contar para pasar
// un care mistake
) {
charaData.sleepCareMistakeCounter++;
charaData.sleepCareMistakeCounter += diff_sec;
return true;
@ -124,7 +129,7 @@ bool vpet_evalSleep() {
} else if (
!charaData.gotLifeYearAdded &&
dayUnixTime < 1 // This stinks
dayUnixTime < 60 // This stinks
// Esto se ejecuta cuando es media noche.
// Resultado: se incrementa la edad por 1
) {
@ -148,11 +153,18 @@ void vpet_evalTimers() {
vpet_evalStrengthTimer();
}
void vpet_evalHungerTimer() {
void vpet_reduceTimers(uint8_t diff_sec) {
if (charaData.hungerCareMistakeTimer > 0) {
charaData.hungerCareMistakeTimer--;
charaData.hungerCareMistakeTimer -= diff_sec;
}
} else if (
if (charaData.strengthCareMistakeTimer > 0) {
charaData.strengthCareMistakeTimer -= diff_sec;
}
}
void vpet_evalHungerTimer() {
if (
charaData.hungerCareMistakeTimer <= 0 &&
charaData.hunger > 0
) {
@ -187,10 +199,7 @@ void vpet_evalHungerTimer() {
}
void vpet_evalStrengthTimer() {
if (charaData.strengthCareMistakeTimer > 0) {
charaData.strengthCareMistakeTimer--;
} else if (
if (
charaData.strengthCareMistakeTimer <= 0 &&
charaData.strength > 0
) {
@ -223,8 +232,16 @@ void IRAM_ATTR onActionTimerDelta() {
void vpet_runVpetTasks() {
if (runVpetTasks && charaData.hatched) {
uint64_t currentEvaluationTime = esp_timer_get_time();
uint64_t deltaUs = currentEvaluationTime - vpetLastEvaluationTime;
printf("[DELTA] deltaUpdate=%lu\n", deltaUs);
uint8_t diffSec = (deltaUs + 1000000 - 1000) / 1000000; // round up
printf("[DEBUG] diffSec=%i\n", diffSec);
vpet_computeCallLight();
if (!vpet_evalSleep()) {
if (!vpet_evalSleep(diffSec)) {
vpet_reduceTimers(diffSec);
vpet_evalTimers();
}
@ -236,10 +253,15 @@ void vpet_runVpetTasks() {
printf("[MAIN]: Care mistake count is %d\n", charaData.careMistakes);
printf("[MAIN]: Is sleep care mistake tripped? %d\n", charaData.sleepCareMistakeObtained);
vpetLastEvaluationTime = currentEvaluationTime;
runVpetTasks = false;
} else if (runVpetTasks && !charaData.hatched && charaData.hatching) {
charaData.hatchTimer++;
uint64_t currentEvaluationTime = esp_timer_get_time();
uint64_t deltaUs = currentEvaluationTime - vpetLastEvaluationTime;
uint8_t diffSec = (deltaUs + 1000000 - 1000) / 1000000; // round up
charaData.hatchTimer += diffSec;
printf("[DEBUG] hatchTimer=%i out of hatchTimer=%i\n", charaData.hatchTimer, currentLine[currentCharacter]->hatchTime);
if (charaData.hatchTimer > currentLine[currentCharacter]->hatchTime) {
interruptKey = EGG_HATCH_SCREEN;
@ -247,5 +269,7 @@ void vpet_runVpetTasks() {
}
runVpetTasks = false;
vpetLastEvaluationTime = currentEvaluationTime;
}
}

View File

@ -3,12 +3,15 @@
#include <Arduino.h>
extern uint64_t vpetLastEvaluationTime;
void IRAM_ATTR onActionTimerDelta();
void vpet_initTimer();
void vpet_computeCallLight();
bool vpet_evalSleep();
bool vpet_evalSleep(uint8_t diff_sec);
void vpet_evalTimers();
void vpet_runVpetTasks();
void vpet_reduceTimers(uint8_t diff_sec);
void vpet_evalHungerTimer();
void vpet_evalStrengthTimer();