[英]Understanding c++ function with missing variables in Arduino's RTClib
我試圖了解一個不提供所需參數但似乎可以正常工作的函數調用。 該代碼位於名為RTClib的Arduino庫中。 為什么/如何運作???
進行調用的函數:
uint8_t DateTime::dayOfWeek() const {
uint16_t day = date2days(yOff, m, d);
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
該函數被調用:
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += pgm_read_byte(daysInMonth + i - 1);
if (m > 2 && y % 4 == 0)
++days;
return days + 365 * y + (y + 3) / 4 - 1;
}
完整的庫: https : //github.com/adafruit/RTClib
必需的變量來自DateTime類。 它們是受保護的變量,因此DateTime類中的所有方法都可以訪問它們。
protected:
uint8_t yOff, m, d, hh, mm, ss;
這些變量由RTClib.cpp中的各種函數設置 ,例如首先對其進行初始化的構造函數:
DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
if (year >= 2000)
year -= 2000;
yOff = year;
m = month;
d = day;
hh = hour;
mm = min;
ss = sec;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.