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-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3.
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 
38 #ifdef ARDUINO
39 
40 
41 #include <stdint.h>
42 #include <stddef.h>
43 
44 #include "freertos/FreeRTOS.h"
45 
46 #include "fabglconf.h"
47 #include "fabutils.h"
48 #include "comdrivers/tsi2c.h"
49 
50 
51 
52 namespace fabgl {
53 
54 
58 struct DateTime {
59  int8_t seconds;
60  int8_t minutes;
61  int8_t hours;
62  int8_t dayOfWeek;
63  int8_t dayOfMonth;
64  int8_t month;
65  int16_t year;
67  DateTime() { }
68  DateTime(int seconds_, int minutes_, int hours_, int dayOfMonth_, int month_, int year_);
69 
77  time_t timestamp(int timezone);
78 
79 private:
80  void calcDayOfWeek();
81 };
82 
83 
102 class DS3231 {
103 
104 public:
105 
106  DS3231();
107 
113  void begin(I2C * i2c);
114 
120  bool available() { return m_available; }
121 
129  bool dateTimeValid() { return m_dateTimeValid; }
130 
136  DateTime const & datetime();
137 
145  bool setDateTime(DateTime const & value);
146 
152  double temperature();
153 
159  void clockEnabled(bool value);
160 
161 private:
162 
163  uint8_t readReg(int reg);
164 
165  bool writeReg(int reg, int value);
166 
167  I2C * m_i2c;
168  bool m_available;
169  bool m_dateTimeValid;
170 
171  DateTime m_datetime;
172 
173 
174 };
175 
176 
177 
178 } // fabgl namespace
179 
180 
181 #endif // #ifdef ARDUINO
int8_t dayOfMonth
Definition: DS3231.h:63
int8_t dayOfWeek
Definition: DS3231.h:62
int16_t year
Definition: DS3231.h:65
int8_t seconds
Definition: DS3231.h:59
int8_t month
Definition: DS3231.h:64
void clockEnabled(bool value)
Enables or disables DS3231 oscillator.
Definition: DS3231.cpp:203
Represents date and time.
Definition: DS3231.h:58
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:80
bool available()
Determines if DS3231 is reachable.
Definition: DS3231.h:120
DateTime const & datetime()
Queries DS3231 for current date and time.
Definition: DS3231.cpp:128
This file contains some utility classes and functions.
Definition: canvas.cpp:36
This file contains fabgl::I2C definition.
This file contains FabGL library configuration settings, like number of supported colors...
int8_t hours
Definition: DS3231.h:61
DS3231 Real Time Clock driver.
Definition: DS3231.h:102
void begin(I2C *i2c)
Initializes DS3231 driver.
Definition: DS3231.cpp:101
int8_t minutes
Definition: DS3231.h:60
time_t timestamp(int timezone)
Calculates Unix timestamp.
Definition: DS3231.cpp:75
double temperature()
Forces DS3231 to read current temperature.
Definition: DS3231.cpp:180
bool setDateTime(DateTime const &value)
Sets current date and time.
Definition: DS3231.cpp:158
bool dateTimeValid()
Determines the validity of datetime.
Definition: DS3231.h:129