esp32墨水屏学习3

以下是main.cpp文件:

#include <Arduino.h>
#include <ArduinoJson.h>

#include <WiFiManager.h>

#include "OneButton.h"

#include "led.h"
#include "_sntp.h"
#include "weather.h"
#include "screen_ink.h"
#include "_preference.h"

#include "version.h"
#include <esp_attr.h>

#define PIN_BUTTON GPIO_NUM_14 // 注意:由于此按键负责唤醒,因此需要选择支持RTC唤醒的PIN脚。
OneButton button;
RTC_DATA_ATTR volatile int _display_mode =0;

void IRAM_ATTR checkTicks() {
    button.tick();
}

WiFiManager wm;
WiFiManagerParameter para_qweather_host("qweather_host", "和风天气Host", "", 64); //     和风天气key
WiFiManagerParameter para_qweather_key("qweather_key", "和风天气API Key", "", 32); //     和风天气key
// const char* test_html = "<br/><label for='test'>天气模式</label><br/><input type='radio' name='test' value='0' checked> 每日天气test </input><input type='radio' name='test' value='1'> 实时天气test</input>";
// WiFiManagerParameter para_test(test_html);
WiFiManagerParameter para_qweather_type("qweather_type", "天气类型(0:每日天气,1:实时天气)", "0", 2, "pattern='\\[0-1]{1}'"); //     城市code
WiFiManagerParameter para_qweather_location("qweather_loc", "位置ID", "", 64); //     城市code
WiFiManagerParameter para_cd_day_label("cd_day_label", "倒数日(4字以内)", "", 10); //     倒数日
WiFiManagerParameter para_cd_day_date("cd_day_date", "日期(yyyyMMdd)", "", 8, "pattern='\\d{8}'"); //     城市code
WiFiManagerParameter para_tag_days("tag_days", "日期Tag(yyyyMMddx,详见README)", "", 30); //     日期Tag
WiFiManagerParameter para_si_week_1st("si_week_1st", "每周起始(0:周日,1:周一)", "0", 2, "pattern='\\[0-1]{1}'"); //     每周第一天

void print_wakeup_reason() {
    esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
    switch (wakeup_reason) {
    case ESP_SLEEP_WAKEUP_EXT0:
        Serial.println(F("Wakeup caused by external signal using RTC_IO"));
        _display_mode++;
        _display_mode %= 3;
        Serial.printf("display mode: %d\r\n", _display_mode);
        break;
    case ESP_SLEEP_WAKEUP_EXT1:
        Serial.println(F("Wakeup caused by external signal using RTC_CNTL"));
        break;
    case ESP_SLEEP_WAKEUP_TIMER:
        Serial.println(F("Wakeup caused by timer"));
        break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD:
        Serial.println(F("Wakeup caused by touchpad"));
        break;
    case ESP_SLEEP_WAKEUP_ULP:
        Serial.println(F("Wakeup caused by ULP program"));
        break;
    default:
        Serial.printf( "Wakeup was not caused by deep sleep.\n");
    }
}

void buttonClick(void* oneButton);
void buttonDoubleClick(void* oneButton);
void buttonLongPressStop(void* oneButton);
void go_sleep();

unsigned long _idle_millis;
unsigned long TIME_TO_SLEEP = 180 * 1000;

bool _wifi_flag = false;
unsigned long _wifi_failed_millis;
void setup() {
    delay(10);
    Serial.begin(115200);
    Serial.println(".");
    print_wakeup_reason();
    Serial.println("\r\n\r\n\r\n");
    delay(10);
    button.setup(PIN_BUTTON,INPUT_PULLUP,true);
    button.setClickMs(300);
    button.setPressMs(3000); // 设置长按的时长
    button.attachClick(buttonClick, &button);
    button.attachDoubleClick(buttonDoubleClick, &button);
    // button.attachMultiClick()
    button.attachLongPressStop(buttonLongPressStop, &button);
    attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), checkTicks, CHANGE);

    Serial.printf("***********************\r\n");
    Serial.printf("      J-Calendar\r\n");
    Serial.printf("    version: %s\r\n", J_VERSION);
    Serial.printf("***********************\r\n\r\n");
    Serial.printf("Copyright © 2022-2025 JADE Software Co., Ltd. All Rights Reserved.\r\n\r\n");

    led_init();
    led_fast();
    Serial.println(F("Wm begin..."));
    wm.setHostname(F("J-Calendar"));
    wm.setEnableConfigPortal(false);
    wm.setConnectTimeout(10);
    if (wm.autoConnect()) {
        Serial.println(F("Connect OK."));
        led_on();
        _wifi_flag = true;
    } else {
        Serial.println(F("Connect failed."));
        _wifi_flag = false;
        _wifi_failed_millis = millis();
        led_slow();
        _sntp_exec(2);
        weather_exec(2);
        WiFi.mode(WIFI_OFF); // 提前关闭WIFI,省电
        Serial.println(F("Wifi closed."));
    }
}

/**
 * 处理各个任务
 * 1. sntp同步
 *      前置条件:Wifi已连接
 * 2. 刷新日历
 *      前置条件:sntp同步完成(无论成功或失败)
 * 3. 刷新天气信息
 *      前置条件:wifi已连接
 * 4. 系统配置
 *      前置条件:无
 * 5. 休眠
 *      前置条件:所有任务都完成或失败,
 */
void loop() {
    button.tick(); // 单击,刷新页面;双击,打开配置;长按,重启
    wm.process();
    // 前置任务:wifi已连接
    // sntp同步
    if(_display_mode == 0 ){
        if (_sntp_status() == -1) {
            _sntp_exec();
        }
        // 如果是定时器唤醒,并且接近午夜(23:50之后),则直接休眠
        if (_sntp_status() == SYNC_STATUS_TOO_LATE) {
            go_sleep();
        }
        // 前置任务:wifi已连接
        // 获取Weather信息
        if (weather_status() == -1) {
            weather_exec();
        }
    
        // 刷新日历
        // 前置任务:sntp、weather
        // 执行条件:屏幕状态为待处理
        if (_sntp_status() > 0 && weather_status() > 0 && si_screen_status() == -1) {
            // 数据获取完毕后,关闭Wifi,省电
            if (!wm.getConfigPortalActive()) {
                WiFi.mode(WIFI_OFF);
            }
            Serial.println(F("Wifi closed after data fetch."));
            Serial.printf("display mode: %d\r\n", _display_mode);
            int mode = _display_mode;
            si_screen(mode);
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值