0

I'm trying to write a library encapsulating things that I do with a DS3231 real time clock. I have managed to compile a sketch using the library, but it apparently blows up when attempting to call clock methods such as getTime().

Header:

// JBP_DS3231.h
#ifndef __JBP_DS3231_h__
#define __JBP_DS3231_h__

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

class JBP_DS3231
{
  public:
    JBP_DS3231();
    String TimeStamp();
  private:
    DS3231* _realTimeClock;
};

#endif

cpp:

#include <JBP_DS3231.h>
#include <SD.h>
#include "DS3231.h"
#include "Arduino.h"

JBP_DS3231::JBP_DS3231() {
  String thisTime = "";

  _realTimeClock = &DS3231(SDA, SCL); 

  Serial.println("About to try to get time from RTC."); // Last line we hear from.

  thisTime = _realTimeClock->getTimeStr();
  Serial.print("Time in constructor is: ");
  Serial.println(thisTime);
}

String JBP_DS3231::TimeStamp() {
  String timeStamp = "";

  Serial.println("In TimeStamp().");
  timeStamp = _realTimeClock->getDateStr(FORMAT_LONG, FORMAT_MIDDLEENDIAN, '/');
  Serial.print("Value of timestamp is: ");
  Serial.println(timeStamp);
  timeStamp.concat(" ");
  timeStamp.concat(_realTimeClock->getTimeStr());
  Serial.println(timeStamp);

  return timeStamp;
} 

Script:

const String WHO_AM_I = "JBP_DS3231_library_test.ino";
const    int VERSION = 0;

#include <JBP_DS3231.h>

void setup() {
  Serial.begin(9600);
  AnnounceAwakening();

  JBP_DS3231 johnsRTC;
  Serial.println(johnsRTC.TimeStamp());
}

void loop() {
  return;
}

void AnnounceAwakening() {
  Serial.print("Sketch ");
  Serial.print(WHO_AM_I);
  Serial.print(" version ");
  Serial.print(VERSION);
  Serial.println(".");
  Serial.println("");
} 
  • Define "blows up"...? – Majenko Sep 27 '18 at 22:30
  • 1
    If all you want to do is to enhance what the existing DS3231 library can do I suggest you just extend the class (create your own class which inherits the existing class) and add your own functions to it. – Majenko Sep 27 '18 at 22:31
  • 1
    Also, you will have to provide a link to the DS3231 library you are using. I found at least 3, and none of them appear to be the one you are using (judging by the constructors). – Majenko Sep 27 '18 at 22:34
  • "blows up" means that no subsequent statements are executed after the first call to _realTimeClock->getDateStr(). – johnbryanpeters Sep 27 '18 at 22:41
0

You appear to be providing a pointer to a constructor, not to an object. Either create a new object with new, or instantiate the object separately and provide the pointer to the object:

_realTimeClock = new DS3231(SDA, SCL); 

Better still, extend the existing class and implement your own methods:

clsss JBP_DS3231 : public DS3231 {
    public:
        JBP_DS3231(uint8_t d, uint8_t c) : DS3231(d, c) {
            String thisTime = "";
            Serial.println("About to try to get time from RTC."); // Last line we hear from.

            thisTime = getTimeStr();
            Serial.print("Time in constructor is: ");
            Serial.println(thisTime);
        }

        String TimeStamp() {
            String timeStamp = "";

            Serial.println("In TimeStamp().");
            timeStamp = getDateStr(FORMAT_LONG, FORMAT_MIDDLEENDIAN, '/');
            Serial.print("Value of timestamp is: ");
            Serial.println(timeStamp);
            timeStamp.concat(" ");
            timeStamp.concat(getTimeStr());
            Serial.println(timeStamp);

            return timeStamp;
        } 
};

void setup() {
    Serial.begin(115200);
    JBP_DS3231 myRTC(SDA, SCL);
    Serial.println(johnsRTC.TimeStamp());
}

void loop() {
}

(Note: untested)

  • For what I'm doing, extending the source class seems to be logical. I will attempt and report back, thank you. – johnbryanpeters Sep 27 '18 at 22:45
  • Majenko, the example you gave is not in the form of a library. I attempted a few variations on; this gives me error: expected primary-expression before ',' token. JBP_DS3231.h class JBP_DS3231 : public DS3231 { public: JBP_DS3231(uint8_t, uint8_t) : DS3231(uint8_t, uint8_t); JBP_DS3231.cpp JBP_DS3231::JBP_DS3231(uint8_t _SDA, uint8_t _SCL) : DS3231(_SDA, _SCL) { – johnbryanpeters Sep 28 '18 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.