From 425a13f2da01bda3bbf69a2617ecf2f15244cc98 Mon Sep 17 00:00:00 2001 From: GOB Date: Thu, 26 Feb 2026 20:40:43 +0900 Subject: [PATCH] Cosmetic change --- .../PlotToSerial/main/PlotToSerial.cpp | 5 +- src/Unit_RTC.cpp | 96 +++++++++++-------- src/Unit_RTC.h | 20 ++-- src/unit/unit_PCF8563.cpp | 4 +- test/embedded/test_pcf8563/pcf8563_test.cpp | 11 +-- test/pcf8563_types_test.cpp | 33 +++---- 6 files changed, 91 insertions(+), 78 deletions(-) diff --git a/examples/UnitUnified/UnitRTC/PlotToSerial/main/PlotToSerial.cpp b/examples/UnitUnified/UnitRTC/PlotToSerial/main/PlotToSerial.cpp index 9f8490f..25e3f91 100644 --- a/examples/UnitUnified/UnitRTC/PlotToSerial/main/PlotToSerial.cpp +++ b/examples/UnitUnified/UnitRTC/PlotToSerial/main/PlotToSerial.cpp @@ -21,7 +21,7 @@ namespace { // ============================================================== // NOTICE: Set your WiFi credentials and timezone before building // ============================================================== -const char* WIFI_SSID = ""; // Your WiFi SSID +const char* WIFI_SSID = ""; // Your WiFi SSID const char* WIFI_PASS = ""; // Your WiFi password // POSIX timezone string (default: JST-9 = UTC+9, no DST) const char* YOUR_TIMEZONE = "JST-9"; @@ -145,8 +145,7 @@ bool sync_ntp_to_rtc() WiFi.disconnect(true); WiFi.mode(WIFI_OFF); - struct tm t { - }; + struct tm t{}; if (!getLocalTime(&t, 10000)) { M5_LOGE("Failed to get NTP time"); display_printf("\nNTP FAILED\n"); diff --git a/src/Unit_RTC.cpp b/src/Unit_RTC.cpp index 7091a6c..497a770 100644 --- a/src/Unit_RTC.cpp +++ b/src/Unit_RTC.cpp @@ -1,15 +1,18 @@ #include "Unit_RTC.h" -Unit_RTC::Unit_RTC() { +Unit_RTC::Unit_RTC() +{ _addr = DEVICE_ADDR; } -Unit_RTC::Unit_RTC(uint8_t addr) { +Unit_RTC::Unit_RTC(uint8_t addr) +{ _addr = addr; } /*! @brief Initialize the RTC. */ -void Unit_RTC::begin() { +void Unit_RTC::begin() +{ _wire = &Wire; _wire->begin(); writeReg(0x00, 0x00); @@ -18,7 +21,8 @@ void Unit_RTC::begin() { } /*! @brief Initialize the RTC. */ -void Unit_RTC::begin(TwoWire *wire) { +void Unit_RTC::begin(TwoWire *wire) +{ _wire = wire; _wire->begin(); writeReg(0x00, 0x00); @@ -27,8 +31,8 @@ void Unit_RTC::begin(TwoWire *wire) { } /*! @brief Initialize the RTC. */ -void Unit_RTC::begin(TwoWire *wire, uint8_t scl, uint8_t sda, - uint32_t i2c_freq) { +void Unit_RTC::begin(TwoWire *wire, uint8_t scl, uint8_t sda, uint32_t i2c_freq) +{ _wire = wire; _wire->begin(DEVICE_ADDR, sda, scl, i2c_freq); writeReg(0x00, 0x00); @@ -37,7 +41,8 @@ void Unit_RTC::begin(TwoWire *wire, uint8_t scl, uint8_t sda, } /*! @brief Write data to register. */ -void Unit_RTC::writeReg(uint8_t reg, uint8_t data) { +void Unit_RTC::writeReg(uint8_t reg, uint8_t data) +{ _wire->beginTransmission(DEVICE_ADDR); _wire->write(reg); _wire->write(data); @@ -45,7 +50,8 @@ void Unit_RTC::writeReg(uint8_t reg, uint8_t data) { } /*! @brief Read data from register. */ -uint8_t Unit_RTC::ReadReg(uint8_t reg) { +uint8_t Unit_RTC::ReadReg(uint8_t reg) +{ _wire->beginTransmission(DEVICE_ADDR); _wire->write(reg); _wire->endTransmission(false); @@ -53,7 +59,8 @@ uint8_t Unit_RTC::ReadReg(uint8_t reg) { return _wire->read(); } -void Unit_RTC::Str2Time(void) { +void Unit_RTC::Str2Time(void) +{ Second = (asc[0] - 0x30) * 10 + asc[1] - 0x30; Minute = (asc[2] - 0x30) * 10 + asc[3] - 0x30; Hour = (asc[4] - 0x30) * 10 + asc[5] - 0x30; @@ -66,16 +73,17 @@ void Unit_RTC::Str2Time(void) { */ } -void Unit_RTC::DataMask() { - _trdata[0] = _trdata[0] & 0x7f; //秒 - _trdata[1] = _trdata[1] & 0x7f; //分 - _trdata[2] = _trdata[2] & 0x3f; //时 +void Unit_RTC::DataMask() +{ + _trdata[0] = _trdata[0] & 0x7f; // 秒 + _trdata[1] = _trdata[1] & 0x7f; // 分 + _trdata[2] = _trdata[2] & 0x3f; // 时 - _trdata[3] = _trdata[3] & 0x3f; //日 - _trdata[4] = _trdata[4] & 0x07; //星期 - _trdata[5] = _trdata[5] & 0x1f; //月 + _trdata[3] = _trdata[3] & 0x3f; // 日 + _trdata[4] = _trdata[4] & 0x07; // 星期 + _trdata[5] = _trdata[5] & 0x1f; // 月 - _trdata[6] = _trdata[6] & 0xff; //年 + _trdata[6] = _trdata[6] & 0xff; // 年 } /******************************************************************** 函 数 名: void Bcd2asc(void) @@ -85,22 +93,24 @@ void Unit_RTC::DataMask() { 入口参数: 返 回 值:无 ***********************************************************************/ -void Unit_RTC::Bcd2asc(void) { +void Unit_RTC::Bcd2asc(void) +{ uint8_t i, j; for (j = 0, i = 0; i < 7; i++) { - asc[j++] = - (_trdata[i] & 0xf0) >> 4 | 0x30; /*格式为: 秒 分 时 日 月 星期 年 */ + asc[j++] = (_trdata[i] & 0xf0) >> 4 | 0x30; /*格式为: 秒 分 时 日 月 星期 年 */ asc[j++] = (_trdata[i] & 0x0f) | 0x30; } } -uint8_t Unit_RTC::Bcd2ToByte(uint8_t Value) { +uint8_t Unit_RTC::Bcd2ToByte(uint8_t Value) +{ uint8_t tmp = 0; tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10; return (tmp + (Value & (uint8_t)0x0F)); } -uint8_t Unit_RTC::ByteToBcd2(uint8_t Value) { +uint8_t Unit_RTC::ByteToBcd2(uint8_t Value) +{ uint8_t bcdhigh = 0; while (Value >= 10) { @@ -111,7 +121,8 @@ uint8_t Unit_RTC::ByteToBcd2(uint8_t Value) { return ((uint8_t)(bcdhigh << 4) | Value); } -void Unit_RTC::getTime(rtc_time_type *RTC_TimeStruct) { +void Unit_RTC::getTime(rtc_time_type *RTC_TimeStruct) +{ // if() uint8_t buf[3] = {0}; @@ -126,14 +137,15 @@ void Unit_RTC::getTime(rtc_time_type *RTC_TimeStruct) { buf[2] = _wire->read(); } - RTC_TimeStruct->Seconds = Bcd2ToByte(buf[0] & 0x7f); //秒 - RTC_TimeStruct->Minutes = Bcd2ToByte(buf[1] & 0x7f); //分 - RTC_TimeStruct->Hours = Bcd2ToByte(buf[2] & 0x3f); //时 + RTC_TimeStruct->Seconds = Bcd2ToByte(buf[0] & 0x7f); // 秒 + RTC_TimeStruct->Minutes = Bcd2ToByte(buf[1] & 0x7f); // 分 + RTC_TimeStruct->Hours = Bcd2ToByte(buf[2] & 0x3f); // 时 } -int Unit_RTC::setTime(rtc_time_type *RTC_TimeStruct) { - if (RTC_TimeStruct == NULL || RTC_TimeStruct->Hours > 24 || - RTC_TimeStruct->Minutes > 60 || RTC_TimeStruct->Seconds > 60) +int Unit_RTC::setTime(rtc_time_type *RTC_TimeStruct) +{ + if (RTC_TimeStruct == NULL || RTC_TimeStruct->Hours > 24 || RTC_TimeStruct->Minutes > 60 || + RTC_TimeStruct->Seconds > 60) return 0; _wire->beginTransmission(DEVICE_ADDR); @@ -145,7 +157,8 @@ int Unit_RTC::setTime(rtc_time_type *RTC_TimeStruct) { return 1; } -void Unit_RTC::getDate(rtc_date_type *RTC_DateStruct) { +void Unit_RTC::getDate(rtc_date_type *RTC_DateStruct) +{ uint8_t buf[4] = {0}; _wire->beginTransmission(DEVICE_ADDR); @@ -171,9 +184,10 @@ void Unit_RTC::getDate(rtc_date_type *RTC_DateStruct) { } } -int Unit_RTC::setDate(rtc_date_type *RTC_DateStruct) { - if (RTC_DateStruct == NULL || RTC_DateStruct->WeekDay > 7 || - RTC_DateStruct->Date > 31 || RTC_DateStruct->Month > 12) +int Unit_RTC::setDate(rtc_date_type *RTC_DateStruct) +{ + if (RTC_DateStruct == NULL || RTC_DateStruct->WeekDay > 7 || RTC_DateStruct->Date > 31 || + RTC_DateStruct->Month > 12) return 0; _wire->beginTransmission(DEVICE_ADDR); _wire->write(0x05); @@ -193,7 +207,8 @@ int Unit_RTC::setDate(rtc_date_type *RTC_DateStruct) { return 1; } -int Unit_RTC::setAlarmIRQ(int afterSeconds) { +int Unit_RTC::setAlarmIRQ(int afterSeconds) +{ uint8_t reg_value = 0; reg_value = ReadReg(0x01); @@ -224,7 +239,8 @@ int Unit_RTC::setAlarmIRQ(int afterSeconds) { return afterSeconds * div; } -int Unit_RTC::setAlarmIRQ(const rtc_time_type &RTC_TimeStruct) { +int Unit_RTC::setAlarmIRQ(const rtc_time_type &RTC_TimeStruct) +{ uint8_t irq_enable = false; uint8_t out_buf[4] = {0x80, 0x80, 0x80, 0x80}; @@ -257,8 +273,8 @@ int Unit_RTC::setAlarmIRQ(const rtc_time_type &RTC_TimeStruct) { return irq_enable ? 1 : 0; } -int Unit_RTC::setAlarmIRQ(const rtc_date_type &RTC_DateStruct, - const rtc_time_type &RTC_TimeStruct) { +int Unit_RTC::setAlarmIRQ(const rtc_date_type &RTC_DateStruct, const rtc_time_type &RTC_TimeStruct) +{ uint8_t irq_enable = false; uint8_t out_buf[4] = {0x80, 0x80, 0x80, 0x80}; @@ -298,11 +314,13 @@ int Unit_RTC::setAlarmIRQ(const rtc_date_type &RTC_DateStruct, return irq_enable ? 1 : 0; } -void Unit_RTC::clearIRQ() { +void Unit_RTC::clearIRQ() +{ uint8_t data = ReadReg(0x01); writeReg(0x01, data & 0xf3); } -void Unit_RTC::disableIRQ() { +void Unit_RTC::disableIRQ() +{ clearIRQ(); uint8_t data = ReadReg(0x01); writeReg(0x01, data & 0xfC); diff --git a/src/Unit_RTC.h b/src/Unit_RTC.h index afa6f79..bdfc83d 100644 --- a/src/Unit_RTC.h +++ b/src/Unit_RTC.h @@ -23,7 +23,8 @@ struct rtc_time_type { uint8_t Minutes; uint8_t Seconds; rtc_time_type(uint8_t hours = 0, uint8_t minutes = 0, uint8_t seconds = 0) - : Hours{hours}, Minutes{minutes}, Seconds{seconds} { + : Hours{hours}, Minutes{minutes}, Seconds{seconds} + { } }; @@ -32,14 +33,14 @@ struct rtc_date_type { uint8_t Month; uint8_t Date; uint8_t WeekDay; - rtc_date_type(uint16_t year = 2000, uint8_t month = 0, uint8_t date = 0, - uint8_t weekDay = 0) - : Year{year}, Month{month}, Date{date}, WeekDay{weekDay} { + rtc_date_type(uint16_t year = 2000, uint8_t month = 0, uint8_t date = 0, uint8_t weekDay = 0) + : Year{year}, Month{month}, Date{date}, WeekDay{weekDay} + { } }; class Unit_RTC { - private: +private: void Bcd2asc(void); void DataMask(); void Str2Time(void); @@ -48,7 +49,7 @@ class Unit_RTC { uint8_t Bcd2ToByte(uint8_t Value); uint8_t ByteToBcd2(uint8_t Value); - private: +private: /*定义数组用来存储读取的时间数据 */ uint8_t _trdata[7]; /*定义数组用来存储转换的 asc 码时间数据*/ @@ -56,7 +57,7 @@ class Unit_RTC { uint8_t _addr; TwoWire *_wire; - public: +public: Unit_RTC(); Unit_RTC(uint8_t addr); void begin(); @@ -72,13 +73,12 @@ class Unit_RTC { int setAlarmIRQ(int afterSeconds); int setAlarmIRQ(const rtc_time_type &RTC_TimeStruct); - int setAlarmIRQ(const rtc_date_type &RTC_DateStruct, - const rtc_time_type &RTC_TimeStruct); + int setAlarmIRQ(const rtc_date_type &RTC_DateStruct, const rtc_time_type &RTC_TimeStruct); void clearIRQ(); void disableIRQ(); - public: +public: uint8_t Second; uint8_t Minute; uint8_t Hour; diff --git a/src/unit/unit_PCF8563.cpp b/src/unit/unit_PCF8563.cpp index 07c99ae..3c8b16a 100644 --- a/src/unit/unit_PCF8563.cpp +++ b/src/unit/unit_PCF8563.cpp @@ -433,7 +433,7 @@ bool UnitPCF8563::readTimerControl(bool& enabled, pcf8563::TimerClock& clock) if (!readRegister8(TIMER_CONTROL_REG, val, 0)) { return false; } - enabled = (val & 0x80) != 0; // TE bit + enabled = (val & 0x80) != 0; // TE bit clock = static_cast(val & 0x03); // TD bits return true; } @@ -604,7 +604,7 @@ void UnitPCF8563::setSystemTimeFromRtc(struct timezone* tz) } struct tm t = dt.to_tm(); - struct timeval tv {}; + struct timeval tv{}; tv.tv_sec = mktime(&t); settimeofday(&tv, tz); } diff --git a/test/embedded/test_pcf8563/pcf8563_test.cpp b/test/embedded/test_pcf8563/pcf8563_test.cpp index 27f8ed6..0d509af 100644 --- a/test/embedded/test_pcf8563/pcf8563_test.cpp +++ b/test/embedded/test_pcf8563/pcf8563_test.cpp @@ -26,8 +26,7 @@ protected: { // ESP32-C6 boards (NessoN1, NanoC6) have 1 hardware I2C used by M5Unified; use HAL auto board = M5.getBoard(); - if (board == m5::board_t::board_ArduinoNessoN1 || board == m5::board_t::board_M5NanoC6 - || is_using_hal()) { + if (board == m5::board_t::board_ArduinoNessoN1 || board == m5::board_t::board_M5NanoC6 || is_using_hal()) { auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); if (board == m5::board_t::board_ArduinoNessoN1) { @@ -144,7 +143,7 @@ TEST_P(TestPCF8563, DateTime) // --- DateTime round-trip (struct tm) --- { SCOPED_TRACE("DateTime struct tm"); - struct tm wt {}; + struct tm wt{}; wt.tm_year = 126; // 2026 wt.tm_mon = 1; // February wt.tm_mday = 26; @@ -154,7 +153,7 @@ TEST_P(TestPCF8563, DateTime) wt.tm_sec = 33; EXPECT_TRUE(unit->writeDateTime(wt)); - struct tm rt {}; + struct tm rt{}; EXPECT_TRUE(unit->readDateTime(rt)); EXPECT_EQ(rt.tm_year, 126); EXPECT_EQ(rt.tm_mon, 1); @@ -436,7 +435,7 @@ TEST_P(TestPCF8563, CompatAPI) SCOPED_TRACE("setAlarmIRQ date+time"); m5::rtc_date_t ad = {2000, 1, 15, 3}; m5::rtc_time_t at = {12, 30, -1}; - int result = unit->setAlarmIRQ(ad, at); + int result = unit->setAlarmIRQ(ad, at); EXPECT_EQ(result, 1); // Verify via readAlarm @@ -695,7 +694,7 @@ TEST_P(TestPCF8563, CompatAlarmTimer) // setAlarmIRQ (same values) m5::rtc_date_t cd = {2000, 1, 25, -1}; m5::rtc_time_t ct = {14, 30, -1}; - int result = unit->setAlarmIRQ(cd, ct); + int result = unit->setAlarmIRQ(cd, ct); EXPECT_EQ(result, 1); rtc_time_t rt2{}; diff --git a/test/pcf8563_types_test.cpp b/test/pcf8563_types_test.cpp index 8758086..e62e826 100644 --- a/test/pcf8563_types_test.cpp +++ b/test/pcf8563_types_test.cpp @@ -15,8 +15,7 @@ using namespace m5::unit::pcf8563; // rtc_time_t // ============================================================ -class RtcTimeTest : public ::testing::Test { -}; +class RtcTimeTest : public ::testing::Test {}; TEST_F(RtcTimeTest, DefaultConstructor) { @@ -36,7 +35,7 @@ TEST_F(RtcTimeTest, ParameterizedConstructor) TEST_F(RtcTimeTest, ConstructFromTm) { - struct tm src {}; + struct tm src{}; src.tm_hour = 23; src.tm_min = 59; src.tm_sec = 58; @@ -49,7 +48,7 @@ TEST_F(RtcTimeTest, ConstructFromTm) TEST_F(RtcTimeTest, FromTm) { - struct tm src {}; + struct tm src{}; src.tm_hour = 8; src.tm_min = 15; src.tm_sec = 0; @@ -72,7 +71,7 @@ TEST_F(RtcTimeTest, ToTm) TEST_F(RtcTimeTest, RoundTripTm) { rtc_time_t original(10, 25, 50); - struct tm mid = original.to_tm(); + struct tm mid = original.to_tm(); rtc_time_t back = rtc_time_t::from_tm(mid); EXPECT_EQ(original, back); } @@ -145,8 +144,7 @@ TEST_F(RtcTimeTest, ComparisonEdgeCases) // rtc_date_t // ============================================================ -class RtcDateTest : public ::testing::Test { -}; +class RtcDateTest : public ::testing::Test {}; TEST_F(RtcDateTest, DefaultConstructor) { @@ -168,11 +166,11 @@ TEST_F(RtcDateTest, ParameterizedConstructor) TEST_F(RtcDateTest, ConstructFromTm) { - struct tm src {}; + struct tm src{}; src.tm_year = 126; // 2026 src.tm_mon = 1; // February src.tm_mday = 26; - src.tm_wday = 4; // Thursday + src.tm_wday = 4; // Thursday rtc_date_t d(src); EXPECT_EQ(d.year, 2026); @@ -183,9 +181,9 @@ TEST_F(RtcDateTest, ConstructFromTm) TEST_F(RtcDateTest, FromTm) { - struct tm src {}; - src.tm_year = 99; // 1999 - src.tm_mon = 11; // December + struct tm src{}; + src.tm_year = 99; // 1999 + src.tm_mon = 11; // December src.tm_mday = 31; src.tm_wday = 5; @@ -209,7 +207,7 @@ TEST_F(RtcDateTest, ToTm) TEST_F(RtcDateTest, RoundTripTm) { rtc_date_t original(2026, 6, 15, 1); - struct tm mid = original.to_tm(); + struct tm mid = original.to_tm(); rtc_date_t back = rtc_date_t::from_tm(mid); EXPECT_EQ(original, back); EXPECT_EQ(back.weekDay, 1); @@ -280,8 +278,7 @@ TEST_F(RtcDateTest, GreaterEqual) // rtc_datetime_t // ============================================================ -class RtcDateTimeTest : public ::testing::Test { -}; +class RtcDateTimeTest : public ::testing::Test {}; TEST_F(RtcDateTimeTest, DefaultConstructor) { @@ -314,7 +311,7 @@ TEST_F(RtcDateTimeTest, ComponentConstructor) TEST_F(RtcDateTimeTest, ConstructFromTm) { - struct tm src {}; + struct tm src{}; src.tm_year = 126; // 2026 src.tm_mon = 1; // February src.tm_mday = 26; @@ -352,7 +349,7 @@ TEST_F(RtcDateTimeTest, ToTm) TEST_F(RtcDateTimeTest, FromTm) { - struct tm src {}; + struct tm src{}; src.tm_year = 126; src.tm_mon = 1; src.tm_mday = 26; @@ -368,7 +365,7 @@ TEST_F(RtcDateTimeTest, FromTm) TEST_F(RtcDateTimeTest, AssignFromTm) { - struct tm src {}; + struct tm src{}; src.tm_year = 126; src.tm_mon = 1; src.tm_mday = 26;