简体   繁体   中英

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. 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

The required variables are from the DateTime class. They are protected variables, so all methods in the DateTime class can access them.

As seen in RTClib.h line 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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