Simple RTC class based on DS1307. Emphasis on simple. Allows you to run at 100k or 400k Hz (for newer DS1307 capable devices). MapTime() allows you to set the time() service to the same as the RTC. Uses struct tm throughout so you can use traditional time functions for manipulation.

Dependents:   AdaFruit_RGBLCD

RTclock.h

Committer:
vtraveller
Date:
2014-08-09
Revision:
5:d71d6e5a7eee
Parent:
0:98b84d9c8c96
Child:
6:984fd3ba519a

File content as of revision 5:d71d6e5a7eee:

#ifndef __RTCLOCK_H__
#define __RTCLOCK_H__

#include <time.h>

typedef I2C RTclock_parent;

class RTclock
    : public RTclock_parent
{
public:
    // Frequency values for the square wave output
    enum ESquareWaveRates
    {
        eSWR_1Hz = 0,
        eSWR_4kHz = 1,
        eSWR_8kHz = 2,
        eSWR_32Hz = 3
    };

public:
                            RTclock
                            (
                                PinName             in_nSDA,
                                PinName             in_nSCL,
                                bool                in_bHiSpeed = false
                            );
    virtual                 ~RTclock();

            bool            IsTwelveHour();                 // true if set to a 12hr clock    
    virtual bool            MapTime();                      // Maps RTC chip to C time.h time system
    virtual bool            GetTime(tm & out_sTM);          // Get a TM structure directly
    static  const char *    GetWeekday(int in_nWeekDay);
    virtual bool            SetSquareWaveOutput
                            (
                                bool                in_bEnable,
                                ESquareWaveRates    in_nRateSelect
                            );
    virtual bool            SetTime                         // Set time time using a TM structure (always starts)
                            (
                                const tm &          in_sTM,
                                bool                in_bTwelveHour
                            );

protected:
    static int              BcdToDecimal(int in_nBCD);
    static int              DecimalToBcd(int in_nDecimal);
    virtual bool            read
                            (
                                int                 in_nAddress,
                                char *              out_pBuffer,
                                int                 in_nLength
                            );
    virtual bool            SetRunning(bool in_nEnable);
    virtual bool            write
                            (
                                int                 in_nAddress,
                                const char *        in_pBuffer,
                                int                 in_nLength
                            );

private:
    static  const char *    m_aWeekDays[];              // Days of the week
            bool            m_bTwelveHour;
};

#endif // __RTCLOCK_H__