简体   繁体   English

了解Arduino RTClib中缺少变量的C ++函数

[英]Understanding c++ function with missing variables in Arduino's RTClib

I'm trying to understand a function call that doesn't supply the required parameters but seems to work. 我试图了解一个不提供所需参数但似乎可以正常工作的函数调用。 The code is in an Arduino library called RTClib. 该代码位于名为RTClib的Arduino库中。 Why/how does this work???? 为什么/如何运作???

The function making the call: 进行调用的函数:

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
}

The function being called: 该函数被调用:

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;
}

The full library: https://github.com/adafruit/RTClib 完整的库: https : //github.com/adafruit/RTClib

The required variables are from the DateTime class. 必需的变量来自DateTime类。 They are protected variables, so all methods in the DateTime class can access them. 它们是受保护的变量,因此DateTime类中的所有方法都可以访问它们。

As seen in RTClib.h line 27 : RTClib.h第27行所示

protected:
    uint8_t yOff, m, d, hh, mm, ss;

Those variables are set by the various functions in RTClib.cpp , such as the constructors that first initialize them: 这些变量由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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM