Class using class

Hi. I have been wondering how this bit of code works. The h file has two separate classes, and one class is using the other. But how does the getTime() function work? Especially "Time DS3231::getTime()" and "t = rtc.getTime()" . I used some of the code from this library: DS3231.h and this website: tech is so cool- Ds32321.
The code compiles.

.ino file:


#include "Arduino.h"
#include <Wire.h>
#include "DS3231.h"
DS3231 rtc;
Time t;

void setup()
{
  Serial.begin(115200);
  Serial.println("Serial Communication Established");
  rtc.begin();
}


void loop()
{
  t = rtc.getTime();
  Serial.print("time: ");
  Serial.print(t.hour);
  Serial.print(":");
  Serial.print(t.min);
  Serial.print(":");
  Serial.println(t.sec);
  delay(1000);
}

.h file:

#ifndef DS3231_h
#define DS3231_h

#define DS3231_READ  0xD1
#define DS3231_WRITE 0xD0
#define DS3231_ADDR  0x68

//DS3231 Registers
#define DS3231_SECONDS  0x00
#define DS3231_MINUTES  0x01
#define DS3231_HOURS  0x02
#define DS3231_DAY 0x03
#define DS3231_DATE 0x04
#define DS3231_CEN_MONTH 0x05
#define DS3231_DEC_YEAR 0x06
#define DS3231_ALARM1_SECONDS 0x07
#define DS3231_ALARM1_MINUTES 0x08
#define DS3231_ALARM1_HOURS 0x09
#define DS3231_ALARM1_DAY_DATE 0x0a
#define DS3231_ALARM2_MINUTES 0x0b
#define DS3231_ALARM2_HOURS 0x0c
#define DS3231_ALARM2_DAY_DATE 0x0d
#define DS3231_CONTROL 0x0e
#define DS3231_CTL_STATUS 0x0f
#define DS3231_AGING_OFFSET 0x10
#define DS3231_TEMP_MSB 0x11
#define DS3231_TEMP_LSB 0x12

class Time
{
  public:
    Time();
	  uint8_t	hour;
	  uint8_t	min;
	  uint8_t	sec;
};



class DS3231
{
  public:
    void begin();   
    Time getTime();
    void setTime(uint8_t hour, uint8_t min, uint8_t sec);

  private:
    uint8_t _readRegister(uint8_t reg);
    void _writeRegister(uint8_t reg, uint8_t data);
    uint8_t _readTimeRegister(uint8_t reg); // accounts for BCD
    void _writeTimeRegister(uint8_t reg, uint8_t data); // accounts for BCD
    uint8_t _toBcd(uint8_t num); // decimal -> BCD conversion   
    uint8_t _fromBcd(uint8_t bcd); // BCD -> decimal conversion      
};

#endif

.cpp file:

#include "Arduino.h"
#include <Wire.h>
#include "DS3231.h"


// **public**

Time::Time()
{
	this->hour = 0;
	this->min  = 0;
	this->sec  = 0;
}


// starts i2c
void DS3231::begin()
{
  Wire.begin();
}


// updates variables in class "Time"
Time DS3231::getTime()
{
  Time t;  
  t.hour = _readTimeRegister(DS3231_HOURS);
  t.min = _readTimeRegister(DS3231_MINUTES);
  t.sec = _readTimeRegister(DS3231_SECONDS);
}


// sets hours, minutes, and seconds
void DS3231::setTime(uint8_t hour, uint8_t min, uint8_t sec)
{
  _writeTimeRegister(DS3231_HOURS, hour);
  _writeTimeRegister(DS3231_MINUTES, min);
  _writeTimeRegister(DS3231_SECONDS, sec);  
}


// **private**

uint8_t DS3231::_readRegister(uint8_t reg)
{	
	Wire.write(reg);
	Wire.endTransmission();					
	Wire.requestFrom(DS3231_ADDR,1);
  return Wire.read();
}


void DS3231::_writeRegister(uint8_t reg, uint8_t data)
{
	Wire.beginTransmission(DS3231_ADDR);
	Wire.write(reg);
	Wire.write(data);
	Wire.endTransmission();
}


uint8_t DS3231::_toBcd(uint8_t num)
{
	uint8_t bcd = ((num / 10) << 4) + (num % 10);
	return bcd;
}


uint8_t DS3231::_fromBcd(uint8_t bcd)
{
	uint8_t num = (10*((bcd & 0xf0) >> 4)) + (bcd & 0x0f);
	return num;
}


uint8_t DS3231::_readTimeRegister(uint8_t reg)
{
  Wire.beginTransmission(DS3231_ADDR);  
  Wire.write(reg);			
  Wire.endTransmission();   		
  Wire.requestFrom(DS3231_ADDR,1);  	
  return _fromBcd(Wire.read());      	
}


void DS3231::_writeTimeRegister(uint8_t reg, uint8_t data)
{
  Wire.beginTransmission(DS3231_ADDR);  
  Wire.write(reg);            		
  Wire.write(_toBcd(data));           	
  Wire.endTransmission();         	
}

I have my doubts about that. At least, I can't see it compiling without generating warnings. For example, in this function you promise to return a variable of type Time. But, then you break your promise:

The comment doesn't make any sense either.

Time DS3231::getTime() is a member function of the DS3231 class. It returns an instance of the Time class.

't' is one of your variables. It is an instance of the Time class.

'rtc' is one of your variables. it is an instance of the DS3231 class.

t = rtc.getTime(); is your sketch calling the member function 'getTime' of the instance 'rtc' of the class 'DS3231'. That member function returns an instance of the 'Time' class and replaces your instance, stored in 't', with the returned instance.

Of course rtc is a variable from traditional C's point of view. But, is it not an object from C++'s perspective?

Thanks a lot! That answeres my questions. :+1:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.