upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
ds1307.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Adapted from Seeed Studio library:
6  * https://github.com/Seeed-Studio/RTC_DS1307
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <mraa/i2c.h>
31 
32 #define DS1307_I2C_BUS 0
33 #define DS1307_I2C_ADDR 0x68
34 
35 // Days of the week
36 #define DS1307_DAY_MON 1
37 #define DS1307_DAY_TUE 2
38 #define DS1307_DAY_WED 3
39 #define DS1307_DAY_THU 4
40 #define DS1307_DAY_FRI 5
41 #define DS1307_DAY_SAT 6
42 #define DS1307_DAY_SUN 7
43 
44 namespace upm {
70  class DS1307 {
71  public:
77  DS1307(int bus);
78 
82  ~DS1307();
83 
89  bool loadTime();
90 
97  bool setTime();
98 
104  mraa_result_t enableClock();
105 
112  mraa_result_t disableClock();
113 
122  mraa_result_t writeBytes(uint8_t reg, uint8_t *buffer, unsigned int len);
123 
132  uint8_t readBytes(uint8_t reg, uint8_t *buffer, unsigned int len);
133 
140  unsigned int bcdToDec(uint8_t val);
141 
148  uint8_t decToBcd(unsigned int val);
149 
150  // These variables store the time data loaded with loadTime(), and
151  // will be the source of data when setTime() is called. It is a
152  // good idea call loadTime() to setup the current values before
153  // calling setTime() to ensure RTC data is consistent
154 
158  unsigned int seconds;
162  unsigned int minutes;
166  unsigned int hours;
170  unsigned int dayOfWeek;
174  unsigned int dayOfMonth;
178  unsigned int month;
182  unsigned int year;
186  bool amPmMode;
190  bool pm;
191 
192  private:
193  mraa_i2c_context m_i2c;
194  };
195 }
196 
197 
uint8_t decToBcd(unsigned int val)
Definition: ds1307.cxx:197
unsigned int minutes
Definition: ds1307.h:162
bool pm
Definition: ds1307.h:190
uint8_t readBytes(uint8_t reg, uint8_t *buffer, unsigned int len)
Definition: ds1307.cxx:73
unsigned int dayOfWeek
Definition: ds1307.h:170
mraa_result_t writeBytes(uint8_t reg, uint8_t *buffer, unsigned int len)
Definition: ds1307.cxx:53
~DS1307()
Definition: ds1307.cxx:48
unsigned int dayOfMonth
Definition: ds1307.h:174
mraa_result_t disableClock()
Definition: ds1307.cxx:182
Definition: a110x.h:29
unsigned int seconds
Definition: ds1307.h:158
C++ API for the DS1307 Real Time CLock.
Definition: ds1307.h:70
unsigned int bcdToDec(uint8_t val)
Definition: ds1307.cxx:203
bool setTime()
Definition: ds1307.cxx:130
unsigned int year
Definition: ds1307.h:182
DS1307(int bus)
Definition: ds1307.cxx:37
bool loadTime()
Definition: ds1307.cxx:84
bool amPmMode
Definition: ds1307.h:186
mraa_result_t enableClock()
Definition: ds1307.cxx:169
unsigned int month
Definition: ds1307.h:178
unsigned int hours
Definition: ds1307.h:166