mirror of
https://github.com/digistump/DigistumpArduino.git
synced 2025-09-17 17:32:25 -07:00
Initial import of support files for all Digistump boards - Digispark, Pro, DigiX - including libraries, examples, tools, and other support files for the Arduino IDE
This commit is contained in:
11
hardware/digistump/avr/libraries/TinyRTClib/README.md
Normal file
11
hardware/digistump/avr/libraries/TinyRTClib/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
TinyRTClib
|
||||
==========
|
||||
|
||||
DS1307's Arduino Adafruit library modified to run on Digispark's attiny85.
|
||||
|
||||
I've searched everywhere for a DS1307 library that could work on my Digispark but found none.
|
||||
|
||||
This is the Adafruit version (https://github.com/adafruit/RTClib) with their examples modified to work on Digispark's attiny85.
|
||||
|
||||
**I'm unable to push the files into the right folders by now. That's why I've them renamed to the folders name and slashs...
|
||||
**Will try to add to the right place later.
|
243
hardware/digistump/avr/libraries/TinyRTClib/TinyRTClib.cpp
Normal file
243
hardware/digistump/avr/libraries/TinyRTClib/TinyRTClib.cpp
Normal file
@@ -0,0 +1,243 @@
|
||||
// Code by JeeLabs http://news.jeelabs.org/code/
|
||||
// Released to the public domain! Enjoy!
|
||||
|
||||
// --Refactored by nGoline http://arduino.ngoline.com
|
||||
// --to fit Digispark and the attiny85
|
||||
|
||||
#include <TinyWireM.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include "TinyRTClib.h"
|
||||
|
||||
#define DS1307_ADDRESS 0x68
|
||||
#define SECONDS_PER_DAY 86400L
|
||||
|
||||
#define SECONDS_FROM_1970_TO_2000 946684800
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
|
||||
int i = 0; //The new wire library needs to take an int when you are sending for the zero register
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// utility code, some of this could be exposed in the DateTime API if needed
|
||||
|
||||
const uint8_t daysInMonth [] PROGMEM = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //has to be const or compiler compaints
|
||||
|
||||
// number of days since 2000/01/01, valid for 2001..2099
|
||||
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
|
||||
if (y >= 2000)
|
||||
y -= 2000;
|
||||
uint16_t days = d;
|
||||
for (uint8_t i = 1; i < m; ++i)
|
||||
days += pgm_read_byte(daysInMonth + i - 1);
|
||||
if (m > 2 && y % 4 == 0)
|
||||
++days;
|
||||
return days + 365 * y + (y + 3) / 4 - 1;
|
||||
}
|
||||
|
||||
static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {
|
||||
return ((days * 24L + h) * 60 + m) * 60 + s;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// DateTime implementation - ignores time zones and DST changes
|
||||
// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second
|
||||
|
||||
DateTime::DateTime (uint32_t t) {
|
||||
t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970
|
||||
|
||||
ss = t % 60;
|
||||
t /= 60;
|
||||
mm = t % 60;
|
||||
t /= 60;
|
||||
hh = t % 24;
|
||||
uint16_t days = t / 24;
|
||||
uint8_t leap;
|
||||
for (yOff = 0; ; ++yOff) {
|
||||
leap = yOff % 4 == 0;
|
||||
if (days < 365 + leap)
|
||||
break;
|
||||
days -= 365 + leap;
|
||||
}
|
||||
for (m = 1; ; ++m) {
|
||||
uint8_t daysPerMonth = pgm_read_byte(daysInMonth + m - 1);
|
||||
if (leap && m == 2)
|
||||
++daysPerMonth;
|
||||
if (days < daysPerMonth)
|
||||
break;
|
||||
days -= daysPerMonth;
|
||||
}
|
||||
d = days + 1;
|
||||
}
|
||||
|
||||
DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
|
||||
if (year >= 2000)
|
||||
year -= 2000;
|
||||
yOff = year;
|
||||
m = month;
|
||||
d = day;
|
||||
hh = hour;
|
||||
mm = min;
|
||||
ss = sec;
|
||||
}
|
||||
|
||||
static uint8_t conv2d(const char* p) {
|
||||
uint8_t v = 0;
|
||||
if ('0' <= *p && *p <= '9')
|
||||
v = *p - '0';
|
||||
return 10 * v + *++p - '0';
|
||||
}
|
||||
|
||||
// A convenient constructor for using "the compiler's time":
|
||||
// DateTime now (__DATE__, __TIME__);
|
||||
// NOTE: using PSTR would further reduce the RAM footprint
|
||||
DateTime::DateTime (const char* date, const char* time) {
|
||||
// sample input: date = "Dec 26 2009", time = "12:34:56"
|
||||
yOff = conv2d(date + 9);
|
||||
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
|
||||
switch (date[0]) {
|
||||
case 'J': m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7; break;
|
||||
case 'F': m = 2; break;
|
||||
case 'A': m = date[2] == 'r' ? 4 : 8; break;
|
||||
case 'M': m = date[2] == 'r' ? 3 : 5; break;
|
||||
case 'S': m = 9; break;
|
||||
case 'O': m = 10; break;
|
||||
case 'N': m = 11; break;
|
||||
case 'D': m = 12; break;
|
||||
}
|
||||
d = conv2d(date + 4);
|
||||
hh = conv2d(time);
|
||||
mm = conv2d(time + 3);
|
||||
ss = conv2d(time + 6);
|
||||
}
|
||||
|
||||
uint8_t DateTime::dayOfWeek() const {
|
||||
uint16_t day = date2days(yOff, m, d);
|
||||
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
|
||||
}
|
||||
|
||||
uint32_t DateTime::unixtime(void) const {
|
||||
uint32_t t;
|
||||
uint16_t days = date2days(yOff, m, d);
|
||||
t = time2long(days, hh, mm, ss);
|
||||
t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// RTC_DS1307 implementation
|
||||
|
||||
static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
|
||||
static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
|
||||
|
||||
uint8_t RTC_DS1307::begin(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
|
||||
uint8_t RTC_DS1307::isrunning(void) {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
|
||||
TinyWireM.requestFrom(DS1307_ADDRESS, 1);
|
||||
uint8_t ss = TinyWireM.receive();
|
||||
return !(ss>>7);
|
||||
}
|
||||
|
||||
void RTC_DS1307::adjust(const DateTime& dt) {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.send(bin2bcd(dt.second()));
|
||||
TinyWireM.send(bin2bcd(dt.minute()));
|
||||
TinyWireM.send(bin2bcd(dt.hour()));
|
||||
TinyWireM.send(bin2bcd(0));
|
||||
TinyWireM.send(bin2bcd(dt.day()));
|
||||
TinyWireM.send(bin2bcd(dt.month()));
|
||||
TinyWireM.send(bin2bcd(dt.year() - 2000));
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
}
|
||||
|
||||
DateTime RTC_DS1307::now() {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
|
||||
TinyWireM.requestFrom(DS1307_ADDRESS, 7);
|
||||
uint8_t ss = bcd2bin(TinyWireM.receive() & 0x7F);
|
||||
uint8_t mm = bcd2bin(TinyWireM.receive());
|
||||
uint8_t hh = bcd2bin(TinyWireM.receive());
|
||||
TinyWireM.receive();
|
||||
uint8_t d = bcd2bin(TinyWireM.receive());
|
||||
uint8_t m = bcd2bin(TinyWireM.receive());
|
||||
uint16_t y = bcd2bin(TinyWireM.receive()) + 2000;
|
||||
|
||||
return DateTime (y, m, d, hh, mm, ss);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
uint8_t RTC_DS1307::isrunning(void) {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
|
||||
TinyWireM.requestFrom(DS1307_ADDRESS, 1);
|
||||
uint8_t ss = TinyWireM.receive();
|
||||
return !(ss>>7);
|
||||
}
|
||||
|
||||
void RTC_DS1307::adjust(const DateTime& dt) {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.send(bin2bcd(dt.second()));
|
||||
TinyWireM.send(bin2bcd(dt.minute()));
|
||||
TinyWireM.send(bin2bcd(dt.hour()));
|
||||
TinyWireM.send(bin2bcd(0));
|
||||
TinyWireM.send(bin2bcd(dt.day()));
|
||||
TinyWireM.send(bin2bcd(dt.month()));
|
||||
TinyWireM.send(bin2bcd(dt.year() - 2000));
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
}
|
||||
|
||||
DateTime RTC_DS1307::now() {
|
||||
TinyWireM.beginTransmission(DS1307_ADDRESS);
|
||||
TinyWireM.send(i);
|
||||
TinyWireM.endTransmission();
|
||||
|
||||
TinyWireM.requestFrom(DS1307_ADDRESS, 7);
|
||||
uint8_t ss = bcd2bin(TinyWireM.receive() & 0x7F);
|
||||
uint8_t mm = bcd2bin(TinyWireM.receive());
|
||||
uint8_t hh = bcd2bin(TinyWireM.receive());
|
||||
TinyWireM.receive();
|
||||
uint8_t d = bcd2bin(TinyWireM.receive());
|
||||
uint8_t m = bcd2bin(TinyWireM.receive());
|
||||
uint16_t y = bcd2bin(TinyWireM.receive()) + 2000;
|
||||
|
||||
return DateTime (y, m, d, hh, mm, ss);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// RTC_Millis implementation
|
||||
|
||||
long RTC_Millis::offset = 0;
|
||||
|
||||
void RTC_Millis::adjust(const DateTime& dt) {
|
||||
offset = dt.unixtime() - millis() / 1000;
|
||||
}
|
||||
|
||||
DateTime RTC_Millis::now() {
|
||||
return (uint32_t)(offset + millis() / 1000);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
51
hardware/digistump/avr/libraries/TinyRTClib/TinyRTClib.h
Normal file
51
hardware/digistump/avr/libraries/TinyRTClib/TinyRTClib.h
Normal file
@@ -0,0 +1,51 @@
|
||||
// Code by JeeLabs http://news.jeelabs.org/code/
|
||||
// Released to the public domain! Enjoy!
|
||||
|
||||
// --Refactored by nGoline http://arduino.ngoline.com
|
||||
// --to fit Digispark and the attiny85
|
||||
|
||||
// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
|
||||
class DateTime {
|
||||
public:
|
||||
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);
|
||||
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;
|
||||
|
||||
// 32-bit times as seconds since 1/1/2000
|
||||
long secondstime() const;
|
||||
// 32-bit times as seconds since 1/1/1970
|
||||
uint32_t unixtime(void) const;
|
||||
|
||||
protected:
|
||||
uint8_t yOff, m, d, hh, mm, ss;
|
||||
};
|
||||
|
||||
// RTC based on the DS1307 chip connected via I2C and the Wire library
|
||||
// -- Now using the TinyWireM library
|
||||
class RTC_DS1307 {
|
||||
public:
|
||||
static uint8_t begin(void);
|
||||
static void adjust(const DateTime& dt);
|
||||
uint8_t isrunning(void);
|
||||
static DateTime now();
|
||||
};
|
||||
|
||||
// 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 (>49d?)
|
||||
class RTC_Millis {
|
||||
public:
|
||||
static void begin(const DateTime& dt) { adjust(dt); }
|
||||
static void adjust(const DateTime& dt);
|
||||
static DateTime now();
|
||||
|
||||
protected:
|
||||
static long offset;
|
||||
};
|
@@ -0,0 +1,65 @@
|
||||
// Simple date conversions and calculations
|
||||
|
||||
#include <TinyWireM.h>
|
||||
#include "TinyRTClib.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 setup () {
|
||||
Serial.begin(115200);
|
||||
|
||||
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.unixtime() + 7 * 86400L); // one week later
|
||||
showDate("dt9", dt9);
|
||||
}
|
||||
|
||||
void loop () {
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
|
||||
|
||||
#include <TinyWireM.h>
|
||||
#include "TinyRTClib.h"
|
||||
|
||||
RTC_DS1307 RTC;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(115200);
|
||||
TinyWireM.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();
|
||||
|
||||
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);
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
// Date and time functions using just software, based on millis() & timer
|
||||
|
||||
#include <TinyWireM.h>
|
||||
#include "TinyRTClib.h"
|
||||
|
||||
RTC_Millis RTC;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(115200);
|
||||
// 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.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);
|
||||
}
|
34
hardware/digistump/avr/libraries/TinyRTClib/keywords.txt
Normal file
34
hardware/digistump/avr/libraries/TinyRTClib/keywords.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For RTC
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
DateTime KEYWORD1
|
||||
RTC_DS1307 KEYWORD1
|
||||
RTC_Millis KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
year KEYWORD2
|
||||
month KEYWORD2
|
||||
day KEYWORD2
|
||||
hour KEYWORD2
|
||||
minute KEYWORD2
|
||||
second KEYWORD2
|
||||
dayOfWeek KEYWORD2
|
||||
secondstime KEYWORD2
|
||||
unixtime KEYWORD2
|
||||
begin KEYWORD2
|
||||
adjust KEYWORD2
|
||||
isrunning KEYWORD2
|
||||
now KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
Reference in New Issue
Block a user