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-2020 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 
34 #ifdef ARDUINO
35 
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 
40 #include "freertos/FreeRTOS.h"
41 
42 #include "fabglconf.h"
43 #include "fabutils.h"
44 #include "comdrivers/tsi2c.h"
45 
46 
47 
48 namespace fabgl {
49 
50 
54 struct DateTime {
55  int8_t seconds;
56  int8_t minutes;
57  int8_t hours;
58  int8_t dayOfWeek;
59  int8_t dayOfMonth;
60  int8_t month;
61  int16_t year;
63  DateTime() { }
64  DateTime(int seconds_, int minutes_, int hours_, int dayOfMonth_, int month_, int year_);
65 
73  time_t timestamp(int timezone);
74 
75 private:
76  void calcDayOfWeek();
77 };
78 
79 
98 class DS3231 {
99 
100 public:
101 
102  DS3231();
103 
109  void begin(I2C * i2c);
110 
116  bool available() { return m_available; }
117 
125  bool dateTimeValid() { return m_dateTimeValid; }
126 
132  DateTime const & datetime();
133 
141  bool setDateTime(DateTime const & value);
142 
148  double temperature();
149 
155  void clockEnabled(bool value);
156 
157 private:
158 
159  uint8_t readReg(int reg);
160 
161  bool writeReg(int reg, int value);
162 
163  I2C * m_i2c;
164  bool m_available;
165  bool m_dateTimeValid;
166 
167  DateTime m_datetime;
168 
169 
170 };
171 
172 
173 
174 } // fabgl namespace
175 
176 
177 #endif // #ifdef ARDUINO
int8_t dayOfMonth
Definition: DS3231.h:59
int8_t dayOfWeek
Definition: DS3231.h:58
int16_t year
Definition: DS3231.h:61
int8_t seconds
Definition: DS3231.h:55
int8_t month
Definition: DS3231.h:60
void clockEnabled(bool value)
Enables or disables DS3231 oscillator.
Definition: DS3231.cpp:199
Represents date and time.
Definition: DS3231.h:54
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:76
bool available()
Determines if DS3231 is reachable.
Definition: DS3231.h:116
DateTime const & datetime()
Queries DS3231 for current date and time.
Definition: DS3231.cpp:124
This file contains some utility classes and functions.
Definition: canvas.cpp:31
This file contains fabgl::I2C definition.
This file contains FabGL library configuration settings, like number of supported colors...
int8_t hours
Definition: DS3231.h:57
DS3231 Real Time Clock driver.
Definition: DS3231.h:98
void begin(I2C *i2c)
Initializes DS3231 driver.
Definition: DS3231.cpp:97
int8_t minutes
Definition: DS3231.h:56
time_t timestamp(int timezone)
Calculates Unix timestamp.
Definition: DS3231.cpp:71
double temperature()
Forces DS3231 to read current temperature.
Definition: DS3231.cpp:176
bool setDateTime(DateTime const &value)
Sets current date and time.
Definition: DS3231.cpp:154
bool dateTimeValid()
Determines the validity of datetime.
Definition: DS3231.h:125