0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5Stack Tab5 の初期ファームウェアをカスタマイズする(その3)

0
Posted at

↑ 初回記事の続きです。

1. はじめに

rtc_setting.png

↑ 上図のように、RTCの設定は 手動 で 年月日時分秒 を指定します。
ですが、初回のカスタマイズで、WiFiを ステーションモード に変更して自宅のWiFiルータに接続するようにしたので、NTPで時刻同期することができますね。今回は、NTPにて時刻を取得してRTCを同期させるカスタマイズです。

↓ 次のように『Sync NTP』ボタンを追加して、ボタンが押されたら NTP同期を実行するようにします。

sync_ntp.png

2. 環境準備

「ESP-IDF v5.4.2」の環境 と 「M5Stack User Demo」のビルド環境の構築は、冒頭の初回カスタマイズ記事を参照してください。

また、今回の「NTP同期のカスタマイズ」は、「初回カスタマイズのWifiのステーションモードへの変更」が前提となりますので、初回カスタマイズの適用後に、今回のカスタマイズを行ってください。

3.カスタマイズ

今回は、次の2点を変更します。

  1. NTP同期処理の追加
  2. RTC設定画面に『Sync NTP』ボタンを追加(ボタンアクションでNTP同期処理を呼び出す)

(以降の変更箇所は、コピペしてタイプミスを防止することをお勧めします)

3.1 NTP同期処理の追加

次の3ファイルを変更します。

  • app/hal/hal.h
  • platforms/tab5/main/hal/hal_esp32.h
  • platforms/tab5/main/hal/hal_esp32.cpp

(1) hal.h

hal.hの162行目あたりに、次の1行を追加します。

hal.h
    virtual void setRtcTime(tm time)
    {
    }
+   virtual void setNTPTime(){ }
    virtual void clearRtcIrq()
    {
    }

(2) hal_esp32.h

hal_esp32.hの42行目あたりに、次の1行を追加します。

hal_esp32.h
    void clearRtcIrq() override;
    void setRtcTime(tm time) override;
+   void setNTPTime() override; // set NTP time to RTC

(3) hal_esp32.cpp

  1. hal_esp32.cppの12行目あたりに、次のinclude文を1行を追加します
hal_esp32.cpp
  #include <esp_timer.h>
+ #include <esp_sntp.h> 


2. hal_esp32.cppのファイルの最後(387行目あたり)に、次のsetNTPTime()を丸々追加します。

NTPサーバにjp.pool.ntp.orgを指定していますが、他のNTPサーバでも可。

hal_esp32.cpp
void HalEsp32::setNTPTime()
{
    static bool obtained = false;
    if (obtained) return;
    obtained = true;

    setenv("TZ", "JST-9", 1);  // Japan Standard Time
    tzset();

    mclog::tagInfo(_tag, "Initializing SNTP");
    sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
    esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
    esp_sntp_setservername(0, "jp.pool.ntp.org");
    esp_sntp_init();

    time_t now = 0;
    tm timeinfo = { 0 };

    // wait for time to be set
    int retry = 0;
    const int retry_count = 10;
    while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
        mclog::tagInfo(_tag, "Waiting for system time to be set... ({}/{})", retry, retry_count);
        delay(2000);
    }
    time(&now);
    localtime_r(&now, &timeinfo);

    mclog::tagInfo(_tag, "set rtc time to {}/{}/{} {:02d}:{:02d}:{:02d}", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1,
                   timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
    rx8130.setTime(&timeinfo);
    delay(50);

    update_system_time();
}

3.2 RTC設定画面に『Sync NTP』ボタンを追加

次の1ファイルを変更します。

  • app/apps/app_launcher/view/panel_rtc.cpp

(1) panel_rtc.cpp

  1. panel_rtc.cppの150行目あたりに、次の1行を追加します
hal_wifi.cpp
private:
    std::unique_ptr<Calendar> _calendar;
    std::unique_ptr<Roller> _roller_h;
    std::unique_ptr<Roller> _roller_m;
    std::unique_ptr<Roller> _roller_s;
    std::unique_ptr<Label> _label_colon_a;
    std::unique_ptr<Label> _label_colon_b;
    std::unique_ptr<Button> _btn_apply;
+   std::unique_ptr<Button> _btn_ntp;
    std::unique_ptr<Label> _label_msg;


2. panel_rtc.cppの69行目あたりを、次のように変更します

panel_rtc.cpp
    _btn_apply = std::make_unique<Button>(_window->get());
-   _btn_apply->align(LV_ALIGN_CENTER, 280, 184);
-   _btn_apply->setSize(155, 43);
+   _btn_apply->align(LV_ALIGN_CENTER, 120, 184);
+   _btn_apply->setSize(140, 43);
    _btn_apply->label().setText("Apply");


3. panel_rtc.cppの96行目あたりに、次の処理を追加します

panel_rtc.cpp
        GetHAL()->setRtcTime(target_time);
    });

+   _btn_ntp = std::make_unique<Button>(_window->get());
+   _btn_ntp->align(LV_ALIGN_CENTER, 288, 184);
+   _btn_ntp->setSize(140, 43);
+   _btn_ntp->label().setText("Sync NTP");
+   _btn_ntp->label().setTextFont(&lv_font_montserrat_24);
+   _btn_ntp->setShadowWidth(0);
+   _btn_ntp->setRadius(18);
+   _btn_ntp->setBgColor(lv_color_hex(0xF26F42));
+   _btn_ntp->onClick().connect([&]() {
+       GetHAL()->setNTPTime();
+       close(); // close this popup window
+   });


4. panel_rtc.cppの109行目あたりから、次のように変更します

panel_rtc.cpp
    _roller_h = std::make_unique<Roller>(_window->get());
    _roller_h->setOptions(generate_options(0, 23), LV_ROLLER_MODE_INFINITE);
    _roller_h->setSelected(local_time->tm_hour);
-   apply_roller_style(_roller_h.get(), -290, 185);
+   apply_roller_style(_roller_h.get(), -308, 185);

    _roller_m = std::make_unique<Roller>(_window->get());
    _roller_m->setOptions(generate_options(0, 59), LV_ROLLER_MODE_INFINITE);
    _roller_m->setSelected(local_time->tm_min);
-   apply_roller_style(_roller_m.get(), -110, 185);
+   apply_roller_style(_roller_m.get(), -172, 185);

    _roller_s = std::make_unique<Roller>(_window->get());
    _roller_s->setOptions(generate_options(0, 59), LV_ROLLER_MODE_INFINITE);
    _roller_s->setSelected(local_time->tm_sec);
-   apply_roller_style(_roller_s.get(), 70, 185);
+   apply_roller_style(_roller_s.get(), -36, 185);

    _label_colon_a = std::make_unique<Label>(_window->get());
    _label_colon_a->setTextFont(&lv_font_montserrat_24);
-   _label_colon_a->align(LV_ALIGN_CENTER, -200, 183);
+   _label_colon_a->align(LV_ALIGN_CENTER, -240, 183);
    _label_colon_a->setText(":");

    _label_colon_b = std::make_unique<Label>(_window->get());
    _label_colon_b->setTextFont(&lv_font_montserrat_24);
-   _label_colon_b->align(LV_ALIGN_CENTER, -20, 183);
+   _label_colon_b->align(LV_ALIGN_CENTER, -104, 183);
    _label_colon_b->setText(":");
}

以上で、ソースコードの変更は終わりです。
各ファイルを保存してビルドします。

4. ビルド & 書き込み

ビルド および カスタマイズしたファームウェアの書き込みについては、前回記事を参照してください。

5. 結果確認

『Sync NTP』ボタンを押すと、NTPにて時刻を取得し RTCを同期させて、設定ウインドウを自動で閉じます。その後の時刻表示が現在時刻(JST)に一致していることが確認できます。

ホワイトバランスが悪くて見にくいですが、「2025/ 4/17 9:12」→「2026/ 1/30 19:12」に時刻が変わっています。

↓ クローズアップ

実行ログ(抜粋)「2025/ 4/17 19:10」→「2026/ 1/30 22:10」
[2025-04-17 19:10:08.598] [info] [hal] Initializing SNTP
[2025-04-17 19:10:08.599] [info] [hal] Waiting for system time to be set... (1/10)
[2025-04-17 19:10:10.599] [info] [hal] Waiting for system time to be set... (2/10)
[2026-01-30 22:10:46.072] [info] [hal] set rtc time to 2026/1/30 22:10:46
[2026-01-30 22:10:46.123] [info] [hal] update system time
[2026-01-30 22:10:46.123] [info] [hal] sync to rtc time: 2026-01-30 22:10:46

7. 終わりに

無事に実装できました。
ESP-IDF環境で NTP同期させる場合の参考になると思います。


以上 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?