FabGL
ESP32 Display Controller and Graphics Library
DS3231.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
35 #include <stdint.h>
36 #include <stddef.h>
37 
38 #include "freertos/FreeRTOS.h"
39 
40 #include "fabglconf.h"
41 #include "fabutils.h"
42 #include "comdrivers/tsi2c.h"
43 
44 
45 
46 namespace fabgl {
47 
48 
52 struct DateTime {
53  int8_t seconds;
54  int8_t minutes;
55  int8_t hours;
56  int8_t dayOfWeek;
57  int8_t dayOfMonth;
58  int8_t month;
59  int16_t year;
61  DateTime() { }
62  DateTime(int seconds_, int minutes_, int hours_, int dayOfMonth_, int month_, int year_);
63 
71  time_t timestamp(int timezone);
72 
73 private:
74  void calcDayOfWeek();
75 };
76 
77 
96 class DS3231 {
97 
98 public:
99 
100  DS3231();
101 
107  void begin(I2C * i2c);
108 
114  bool available() { return m_available; }
115 
123  bool dateTimeValid() { return m_dateTimeValid; }
124 
130  DateTime const & datetime();
131 
139  bool setDateTime(DateTime const & value);
140 
146  double temperature();
147 
153  void clockEnabled(bool value);
154 
155 private:
156 
157  uint8_t readReg(int reg);
158 
159  bool writeReg(int reg, int value);
160 
161  I2C * m_i2c;
162  bool m_available;
163  bool m_dateTimeValid;
164 
165  DateTime m_datetime;
166 
167 
168 };
169 
170 
171 
172 
173 
174 
175 
176 }
void begin(I2C *i2c)
Initializes DS3231 driver.
Definition: DS3231.cpp:93
time_t timestamp(int timezone)
Calculates Unix timestamp.
Definition: DS3231.cpp:67
DateTime const & datetime()
Queries DS3231 for current date and time.
Definition: DS3231.cpp:120
int8_t seconds
Definition: DS3231.h:53
Represents date and time.
Definition: DS3231.h:52
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:73
int8_t month
Definition: DS3231.h:58
bool dateTimeValid()
Determines the validity of datetime.
Definition: DS3231.h:123
int8_t minutes
Definition: DS3231.h:54
void clockEnabled(bool value)
Enables or disables DS3231 oscillator.
Definition: DS3231.cpp:195
This file contains some utility classes and functions.
int8_t hours
Definition: DS3231.h:55
Definition: canvas.cpp:31
int8_t dayOfWeek
Definition: DS3231.h:56
This file contains fabgl::I2C definition.
This file contains FabGL library configuration settings, like number of supported colors...
DS3231 Real Time Clock driver.
Definition: DS3231.h:96
int16_t year
Definition: DS3231.h:59
int8_t dayOfMonth
Definition: DS3231.h:57
bool available()
Determines if DS3231 is reachable.
Definition: DS3231.h:114
bool setDateTime(DateTime const &value)
Sets current date and time.
Definition: DS3231.cpp:150
double temperature()
Forces DS3231 to read current temperature.
Definition: DS3231.cpp:172