first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
MIT License
Copyright (c) 2015 JeeLabs http://news.jeelabs.org/code/
2017 NeiroN neiron.nxn@gmail.com
2019 45gfg9 Mc45@qq.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,59 @@
// Simple date conversions and calculations
// #include <Wire.h>
#include <RTClib.h>
void showDate(const char* txt, const DateTime& dt) {
// buffer for DateTime.tostr
static char buf[20];
Serial.print(txt);
Serial.print(' ');
Serial.print(dt.tostr(buf));
Serial.print(" = ");
Serial.print(dt.unixtime());
Serial.print("s / ");
Serial.print(dt.unixtime() / 86400L);
Serial.print("d since 1970");
Serial.println();
}
void setup() {
Serial.begin(9600);
DateTime dt0(0, 1, 1, 0, 0, 0);
showDate("dt0", dt0);
DateTime dt1(1, 1, 1, 0, 0, 0);
showDate("dt1", dt1);
DateTime dt2(2009, 1, 1, 0, 0, 0);
showDate("dt2", dt2);
DateTime dt3(2009, 1, 2, 0, 0, 0);
showDate("dt3", dt3);
DateTime dt4(2009, 1, 27, 0, 0, 0);
showDate("dt4", dt4);
DateTime dt5(2009, 2, 27, 0, 0, 0);
showDate("dt5", dt5);
DateTime dt6(2009, 12, 27, 0, 0, 0);
showDate("dt6", dt6);
DateTime dt7(dt6.unixtime() + 3600); // one hour later
showDate("dt7", dt7);
DateTime dt8(dt6.unixtime() + 86400L); // one day later
showDate("dt8", dt8);
DateTime dt9(dt6 + TimeDelta(7 * 86400L)); // one week later
showDate("dt9", dt9);
}
void loop() {
// do nothing
}

View File

@@ -0,0 +1,54 @@
// Date and time functions using a DS1302 RTC
#include <RTClib.h>
// Init rtc object
// DS1302 rtc;
// DS1302 rtc(ce_pin, sck_pin, io_pin);
//
// ce_pin (RST): default 4
// sck_pin (CLK): default 5
// io_pin (DAT): default 6
DS1302 rtc;
//DS1302 rtc(8, 6, 7);
// buffer for DateTime.tostr
char buf[20];
void setup() {
Serial.begin(9600);
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.tostr(buf));
Serial.print(" since midnight 1970/01/01 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future(now + (7 * 86400L + 30));
Serial.print(" now + 7d + 30s: ");
Serial.println(future.tostr(buf));
// calculate a date which is 30 days before
DateTime past(now - TimeDelta(30 * 86400L));
Serial.print(" now - 30d: ");
Serial.println(past.tostr(buf));
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,51 @@
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <RTClib.h>
DS1307 rtc;
// buffer for DateTime.tostr
char buf[20];
void setup() {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.tostr(buf));
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future(now + (7 * 86400L + 30));
Serial.print(" now + 7d + 30s: ");
Serial.println(future.tostr(buf));
// calculate a date which is 30 days before
DateTime past(now - TimeDelta(30 * 86400L));
Serial.print(" now - 30d: ");
Serial.println(past.tostr(buf));
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,72 @@
// Example for using internal RAM on DS series chips
#include <Wire.h>
#include <RTClib.h>
// For DS1302 pin configuration, please check ds1302 example
DS1302 rtc;
//DS1307 rtc;
//DS3231 rtc;
#define BUFSIZE DS1302_RAMSIZE
//#define BUFSIZE DS1307_RAMSIZE
// buffer
char buf[BUFSIZE];
void printArray(const byte* ptr, int len) {
Serial.print("[");
for (int i = 0; i < len - 1; i++) {
Serial.print(*(ptr + i), DEC);
Serial.print(", ");
}
Serial.print(*(ptr + len - 1), DEC);
Serial.println("]");
}
void setup() {
Serial.begin(9600);
Wire.begin(); // for DS1307
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
byte b = 63;
Serial.print("Set first byte of RAM to: ");
rtc.writeram(0, b);
Serial.println(b);
Serial.print("Get first byte of RAM: ");
b = rtc.readram(0);
Serial.println(b);
Serial.println();
// test data
for (int i = 0; i < BUFSIZE; i++) buf[i] = i;
rtc.putram(buf, BUFSIZE);
}
void loop() {
static int counter = 0;
DateTime now = rtc.now();
Serial.println("Previous data in RAM:");
rtc.getram(buf, BUFSIZE);
printArray(buf, BUFSIZE);
Serial.println("Setting new data to RAM:");
snprintf(buf, BUFSIZE, "%s count %d", now.tostr(buf), counter);
Serial.println(buf);
rtc.putram(buf, BUFSIZE);
counter++;
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,51 @@
// Example for DS3231 module connected via I2C interface
#include <RTClib.h>
DS3231 rtc;
// buffer for DateTime.tostr
char buf[20];
void setup() {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.tostr(buf));
Serial.print(" since midnight 1970/1/1 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future(now + (7 * 86400L + 30));
Serial.print(" now + 7d + 30s: ");
Serial.println(future.tostr(buf));
// calculate a date which is 30 days before
DateTime past(now - 30 * 86400L);
Serial.print(" now - 30d: ");
Serial.println(past.tostr(buf));
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,53 @@
#include <Wire.h>
#include <RTClib.h>
PCF8563 rtc;
DateTime alarm_l;
const byte rtc_int = 2;
void setup() {
Serial.begin(9600);
Serial.println("start");
pinMode(rtc_int, INPUT);
attachInterrupt(digitalPinToInterrupt(rtc_int), interrupt, FALLING);
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(__DATE__, __TIME__));
}
else {
Serial.println("RTC OK");
DateTime now = DateTime(2020, 4, 1, 12, 00, 0);
rtc.adjust(now);
setAlarm(1);
}
}
void loop() {
DateTime now = rtc.now();
char buf[100];
strncpy(buf, "DD.MM.YYYY hh:mm:ss", 100);
Serial.println(now.format(buf));
delay(1000);
}
void interrupt() {
Serial.println("WakeUp atmega");
}
void setAlarm(int value) {
char buf[100];
strncpy(buf, "DD.MM.YYYY hh:mm:ss", 100);
DateTime alarm = rtc.now();
alarm.setminute(alarm.minute() + value);
Serial.print("Setting alarm: ");
strncpy(buf, "DD hh:mm MM", 100);
Serial.println(alarm.format(buf));
rtc.set_alarm(alarm, {1, 0, 0, 0});
rtc.on_alarm();
}

View File

@@ -0,0 +1,30 @@
// DateTime format utility example
#include <Wire.h>
#include <RTClib.h>
DS1307 rtc;
//DS1302 rtc; // see ds1302 example for pin configuration
//DS3231 rtc;
//PCF8563 rtc;
//PCF8583 rtc;
//RTC_Millis rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
DateTime now = rtc.now();
char buf[100];
strncpy(buf, "YYYY.MM.DD hh:mm:ss", 100);
Serial.println(now.format(buf));
delay(1000);
}

View File

@@ -0,0 +1,31 @@
// Date and time functions using just software, based on millis() & timer
#include <RTClib.h>
RTC_Millis rtc;
// buffer for DateTime.tostr
char buf[20];
void setup() {
Serial.begin(9600);
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(__DATE__, __TIME__));
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.tostr(buf));
Serial.print(" seconds since 1970: ");
Serial.println(now.unixtime());
// calculate a date which is 7 days and 30 seconds into the future
DateTime future(now + (7 * 86400L + 30));
Serial.println(future.tostr(buf));
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,48 @@
#######################################
# Syntax Coloring Map For RTC
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
DateTime KEYWORD1
TimeDelta KEYWORD1
DS1302 KEYWORD1
DS1307 KEYWORD1
DS3231 KEYWORD1
PCF8563 KEYWORD1
PCF8583 KEYWORD1
RTC_Millis KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
year KEYWORD2
month KEYWORD2
day KEYWORD2
hour KEYWORD2
minute KEYWORD2
second KEYWORD2
dayOfWeek KEYWORD2
unixtime KEYWORD2
begin KEYWORD2
stop KEYWORD2
adjust KEYWORD2
isrunning KEYWORD2
now KEYWORD2
format KEYWORD2
getTemp KEYWORD2
tostr KEYWORD2
readram KEYWORD2
writeram KEYWORD2
getram KEYWORD2
putram KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
DS1302_RAMSIZE LITERAL1
DS1307_RAMSIZE LITERAL1

View File

@@ -0,0 +1,12 @@
{
"name": "RTCLib by NeiroN",
"keywords": "RTC, DS1302, DS1307, DS3231, PCF8583, PCF8563, RTC-Millis, DateTime",
"description": "A library that makes interfacing many Real Time Clock modules easy. With same DateTime class which supports all standard conversions.",
"repository": {
"type": "git",
"url": "https://github.com/NeiroNx/RTCLib.git"
},
"version": "1.6.3",
"frameworks": "arduino",
"platforms": "*"
}

View File

@@ -0,0 +1,9 @@
name=RTCLib by NeiroN
version=1.6.3
author=JeeLabs (http://news.jeelabs.org/code/), NeiroN (neiron.nxn@gmail.com)
maintainer=NeiroN (neiron.nxn@gmail.com)
sentence=A library that makes interfacing DS1302, DS1307, DS3231, PCF8583, PCF8563, RTC_Millis Real Time Clock modules easy.
paragraph=Including temperature, alarms and memory storage if present. Includes DateTime class implementation and its conversion.
category=Device Control
url=https://github.com/NeiroNx/RTCLib
architectures=*

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,247 @@
// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
#pragma once
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <Wire.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#define WIRE Wire
#elif defined ESP8266
#include <pgmspace.h>
#define WIRE Wire
#elif defined ARDUINO_RASPBERRY_PI_PICO
#include <pgmspace.h>
#define WIRE Wire
#define BUFFER_LENGTH WIRE_BUFFER_SIZE
#else
#define PROGMEM
#define pgm_read_byte(addr) (*(const unsigned char*)(addr))
#define WIRE Wire1
#endif
#define DS1302_RAMSIZE 31 // bytes
#define DS1307_RAMSIZE 56 // bytes
#define SECONDS_PER_DAY 86400L
#define SECONDS_FROM_1970_TO_2000 946684800L
struct alarm_flags {
char minute;
char hour;
char day;
char wday;
};
// TimeDelta which can represent changes in time with seconds accuracy.
// TODO: handle negative delta
class TimeDelta {
public:
TimeDelta(uint32_t seconds = 0, bool neg = false);
TimeDelta(uint16_t days, uint8_t hours, uint8_t minutes, uint8_t seconds);
TimeDelta(const TimeDelta& copy);
uint16_t days() const { return _sec / 86400L; }
uint8_t hours() const { return _sec / 3600 % 24; }
uint8_t minutes() const { return _sec / 60 % 60; }
uint8_t seconds() const { return _sec % 60; }
int32_t totalseconds() const { return _sec; }
bool operator==(const TimeDelta& td) const;
bool operator!=(const TimeDelta& td) const;
bool operator>(const TimeDelta& td) const;
bool operator<(const TimeDelta& td) const;
bool operator>=(const TimeDelta& td) const;
bool operator<=(const TimeDelta& td) const;
TimeDelta operator+(uint32_t t) const;
TimeDelta operator+(const TimeDelta& td) const;
TimeDelta operator-(uint32_t t) const;
TimeDelta operator-(const TimeDelta& td) const;
TimeDelta& operator+=(uint32_t t);
TimeDelta& operator+=(const TimeDelta& td);
TimeDelta& operator-=(uint32_t t);
TimeDelta& operator-=(const TimeDelta& td);
protected:
uint32_t _sec;
};
// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
class DateTime {
public:
char* format(char* ret);
char* tostr(char* charr);
DateTime(uint32_t t = 0);
DateTime(uint16_t year, uint8_t month, uint8_t day,
uint8_t hour = 0, uint8_t min = 0, uint8_t sec = 0);
DateTime(const char* date, const char* time);
DateTime(const __FlashStringHelper* date, const __FlashStringHelper* time);
DateTime(const char* sdate); // Do we really need this?
uint16_t year() const { return 2000 + yOff; }
uint8_t month() const { return m; }
uint8_t day() const { return d; }
uint8_t hour() const { return hh; }
uint8_t minute() const { return mm; }
uint8_t second() const { return ss; }
uint8_t dayOfWeek() const;
void SetTime(const char* time);
void SetDate(const char* date);
void setyear(uint16_t year) { yOff = year - (year >= 2000 ? 2000 : 0); }
void setmonth(uint8_t month) { m = month; }
void setday(uint8_t day) { d = day; }
void sethour(uint8_t hour) { hh = hour % 24; }
void setminute(uint8_t minute) { mm = minute % 60; }
void setsecond(uint8_t second) { ss = second % 60; }
// 32-bit UNIX timestamp
// An uint32_t should be able to store up to 2106,
// which is beyond most chip's upper bound 2099
void setunixtime(uint32_t t);
uint32_t unixtime() const;
bool operator==(const DateTime& date) const;
bool operator==(const char* sdate) const;
bool operator!=(const DateTime& date) const;
bool operator!=(const char* sdate) const;
bool operator<(const DateTime& date) const;
bool operator>(const DateTime& date) const;
bool operator<=(const DateTime& date) const;
bool operator>=(const DateTime& date) const;
DateTime operator+(uint32_t t) const;
DateTime operator+(const TimeDelta& delta) const;
DateTime operator-(uint32_t t) const;
DateTime operator-(const TimeDelta& delta) const;
TimeDelta operator-(const DateTime& date) const;
DateTime& operator+=(uint32_t t);
DateTime& operator+=(const TimeDelta& delta);
DateTime& operator-=(uint32_t t);
DateTime& operator-=(const TimeDelta& delta);
protected:
uint8_t yOff, m, d, hh, mm, ss;
};
// RTC based on the DS1302 chip connected via pins
class DS1302 {
// RAII class for data transferring
class TransferHelper {
public:
TransferHelper(uint8_t ce_pin, uint8_t sck_pin);
~TransferHelper();
protected:
uint8_t ce, sck;
const static uint8_t ce_to_sck_setup = 4;
const static uint8_t ce_inactive_time = 4;
};
uint8_t read();
void write(uint8_t val);
public:
DS1302(uint8_t ce_pin = 4, uint8_t sck_pin = 5, uint8_t io_pin = 6);
uint8_t read(uint8_t addr);
void write(uint8_t addr, uint8_t val);
void begin();
uint8_t isrunning();
DateTime now();
void adjust(const DateTime& dt);
uint8_t readram(uint8_t addr);
void writeram(uint8_t addr, uint8_t val);
uint8_t* getram(uint8_t* arr, uint8_t len);
void putram(const uint8_t* arr, uint8_t len);
protected:
uint8_t ce, sck, io;
};
// RTC based on the DS1307 chip connected via I2C and the Wire library
class DS1307 {
public:
uint8_t begin();
void adjust(const DateTime& dt);
uint8_t isrunning();
DateTime now();
uint8_t read(uint8_t addr);
void write(uint8_t addr, uint8_t val);
uint8_t readram(uint8_t addr);
void writeram(uint8_t addr, uint8_t val);
uint8_t* getram(uint8_t* arr, uint8_t len);
void putram(const uint8_t* arr, uint8_t len);
};
class DS3231 {
public:
uint8_t begin();
void adjust(const DateTime& dt);
uint8_t isrunning();
double getTemp();
void set_alarm(const DateTime& dt); // TODO: implement DS3231 alarm
DateTime now();
uint8_t read(uint8_t addr);
void write(uint8_t addr, uint8_t val);
};
class PCF8583 {
int address;
public:
PCF8583();
PCF8583(int device_address);
uint8_t begin();
DateTime now();
uint8_t isrunning();
void adjust(const DateTime& dt);
void off_alarm();
DateTime get_alarm();
void set_alarm(const DateTime& dt);
};
class PCF8563 {
int address;
int status1;
int status2;
public:
PCF8563();
PCF8563(int device_address);
uint8_t begin();
DateTime now();
uint8_t isrunning();
uint8_t isvalid();
void adjust(const DateTime& dt);
void off_alarm();
void on_alarm();
DateTime get_alarm();
void set_alarm(const DateTime& dt, alarm_flags flags);
};
// RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (~49.7d)
// TODO: handle millis() overflow (if possible)
class RTC_Millis {
bool running = false;
public:
void begin();
void begin(const DateTime& dt);
void stop();
void adjust(const DateTime& dt);
DateTime now();
bool isrunning();
protected:
uint32_t offset;
};