Update librairies + optimizations

loop time and memory optimization
This commit is contained in:
sinseman
2015-02-11 18:26:16 +01:00
parent 982dcb7ff9
commit 5737aff17a
20 changed files with 1400 additions and 418 deletions

View File

@@ -0,0 +1,102 @@
// Simple date conversions and calculations
#include <Wire.h>
#include "RTClib.h"
void showDate(const char* txt, const DateTime& dt) {
Serial.print(txt);
Serial.print(' ');
Serial.print(dt.year(), DEC);
Serial.print('/');
Serial.print(dt.month(), DEC);
Serial.print('/');
Serial.print(dt.day(), DEC);
Serial.print(' ');
Serial.print(dt.hour(), DEC);
Serial.print(':');
Serial.print(dt.minute(), DEC);
Serial.print(':');
Serial.print(dt.second(), DEC);
Serial.print(" = ");
Serial.print(dt.unixtime());
Serial.print("s / ");
Serial.print(dt.unixtime() / 86400L);
Serial.print("d since 1970");
Serial.println();
}
void showTimeSpan(const char* txt, const TimeSpan& ts) {
Serial.print(txt);
Serial.print(" ");
Serial.print(ts.days(), DEC);
Serial.print(" days ");
Serial.print(ts.hours(), DEC);
Serial.print(" hours ");
Serial.print(ts.minutes(), DEC);
Serial.print(" minutes ");
Serial.print(ts.seconds(), DEC);
Serial.print(" seconds (");
Serial.print(ts.totalseconds(), DEC);
Serial.print(" total seconds)");
Serial.println();
}
void setup () {
Serial.begin(57600);
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 dt75 = dt6 + TimeSpan(0, 1, 0, 0); // One hour later with TimeSpan addition.
showDate("dt7.5", dt75);
DateTime dt8 (dt6.unixtime() + 86400L); // One day later.
showDate("dt8", dt8);
DateTime dt85 = dt6 + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
showDate("dt8.5", dt85);
DateTime dt9 (dt6.unixtime() + 7 * 86400L); // One week later.
showDate("dt9", dt9);
DateTime dt95 = dt6 + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
showDate("dt9.5", dt95);
DateTime dt10 = dt6 + TimeSpan(0, 0, 42, 42); // Fourty two minutes and fourty two seconds later.
showDate("dt10", dt10);
DateTime dt11 = dt6 - TimeSpan(7, 0, 0, 0); // One week ago.
showDate("dt11", dt11);
TimeSpan ts1 = dt6 - dt5;
showTimeSpan("dt6-dt5", ts1);
TimeSpan ts2 = dt10 - dt6;
showTimeSpan("dt10-dt6", ts2);
}
void loop () {
}

View File

@@ -0,0 +1,68 @@
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
#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(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
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.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

View File

@@ -0,0 +1,56 @@
// SQW/OUT pin mode using a DS1307 RTC connected via I2C.
//
// According to the data sheet (http://datasheets.maxim-ic.com/en/ds/DS1307.pdf), the
// DS1307's SQW/OUT pin can be set to low, high, 1Hz, 4.096kHz, 8.192kHz, or 32.768kHz.
//
// This sketch reads the state of the pin, then iterates through the possible values at
// 5 second intervals.
//
// NOTE:
// You must connect a pull up resistor (~10kohm) from the SQW pin up to VCC. Without
// this pull up the wave output will not work!
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
int mode_index = 0;
Ds1307SqwPinMode modes[] = {OFF, ON, SquareWave1HZ, SquareWave4kHz, SquareWave8kHz, SquareWave32kHz};
void print_mode() {
Ds1307SqwPinMode mode = rtc.readSqwPinMode();
Serial.print("Sqw Pin Mode: ");
switch(mode) {
case OFF: Serial.println("OFF"); break;
case ON: Serial.println("ON"); break;
case SquareWave1HZ: Serial.println("1Hz"); break;
case SquareWave4kHz: Serial.println("4.096kHz"); break;
case SquareWave8kHz: Serial.println("8.192kHz"); break;
case SquareWave32kHz: Serial.println("32.768kHz"); break;
default: Serial.println("UNKNOWN"); break;
}
}
void setup () {
Serial.begin(57600);
Wire.begin();
rtc.begin();
print_mode();
}
void loop () {
rtc.writeSqwPinMode(modes[mode_index++]);
print_mode();
if (mode_index > 5) {
mode_index = 0;
}
delay(5000);
}

View File

@@ -0,0 +1,59 @@
// Example of using the non-volatile RAM storage on the DS1307.
// You can write up to 56 bytes from address 0 to 55.
// Data will be persisted as long as the DS1307 has battery power.
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void printnvram(uint8_t address) {
Serial.print("Address 0x");
Serial.print(address, HEX);
Serial.print(" = 0x");
Serial.println(rtc.readnvram(address), HEX);
}
void setup () {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
// Print old RAM contents on startup.
Serial.println("Current NVRAM values:");
for (int i = 0; i < 6; ++i) {
printnvram(i);
}
// Write some bytes to non-volatile RAM storage.
// NOTE: You can only read and write from addresses 0 to 55 (i.e. 56 byte values).
Serial.println("Writing NVRAM values.");
// Example writing one byte at a time:
rtc.writenvram(0, 0xFE);
rtc.writenvram(1, 0xED);
// Example writing multiple bytes:
uint8_t writeData[4] = { 0xBE, 0xEF, 0x01, 0x02 };
rtc.writenvram(2, writeData, 4);
// Read bytes from non-volatile RAM storage.
Serial.println("Reading NVRAM values:");
// Example reading one byte at a time.
Serial.println(rtc.readnvram(0), HEX);
Serial.println(rtc.readnvram(1), HEX);
// Example reading multiple bytes:
uint8_t readData[4] = {0};
rtc.readnvram(readData, 4, 2);
Serial.println(readData[0], HEX);
Serial.println(readData[1], HEX);
Serial.println(readData[2], HEX);
Serial.println(readData[3], HEX);
}
void loop () {
// Do nothing in the loop.
}

View File

@@ -0,0 +1,55 @@
// Date and time functions using just software, based on millis() & timer
#include <Wire.h>
#include "RTClib.h"
RTC_Millis rtc;
void setup () {
Serial.begin(57600);
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
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.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}