Components and supplies
1
RGB Diffused Common Cathode
1
Pushbutton switch 12mm
1
Resistor 1k ohm
1
DigiSpark
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Drivers
Project description
Code
time.h
c_cpp
This is the library needed to use the time functions
1/* 2 time.h - low level time and date functions 3*/ 4 5/* 6 July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) 7 - fixed daysToTime_t macro (thanks maniacbug) 8*/ 9 10#ifndef _Time_h 11#ifdef __cplusplus 12#define _Time_h 13 14#include <inttypes.h> 15#ifndef __AVR__ 16#include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h 17#endif 18 19 20#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc 21typedef unsigned long time_t; 22#endif 23 24 25// This ugly hack allows us to define C++ overloaded functions, when included 26// from within an extern "C", as newlib's sys/stat.h does. Actually it is 27// intended to include "time.h" from the C library (on ARM, but AVR does not 28// have that file at all). On Mac and Windows, the compiler will find this 29// "Time.h" instead of the C library "time.h", so we may cause other weird 30// and unpredictable effects by conflicting with the C library header "time.h", 31// but at least this hack lets us define C++ functions as intended. Hopefully 32// nothing too terrible will result from overriding the C library header?! 33extern "C++" { 34typedef enum {timeNotSet, timeNeedsSync, timeSet 35} timeStatus_t ; 36 37typedef enum { 38 dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday 39} timeDayOfWeek_t; 40 41typedef enum { 42 tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields 43} tmByteFields; 44 45typedef struct { 46 uint8_t Second; 47 uint8_t Minute; 48 uint8_t Hour; 49 uint8_t Wday; // day of week, sunday is day 1 50 uint8_t Day; 51 uint8_t Month; 52 uint8_t Year; // offset from 1970; 53} tmElements_t, TimeElements, *tmElementsPtr_t; 54 55//convenience macros to convert to and from tm years 56#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year 57#define CalendarYrToTm(Y) ((Y) - 1970) 58#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 59#define y2kYearToTm(Y) ((Y) + 30) 60 61typedef time_t(*getExternalTime)(); 62//typedef void (*setExternalTime)(const time_t); // not used in this version 63 64 65/*==============================================================================*/ 66/* Useful Constants */ 67#define SECS_PER_MIN ((time_t)(60UL)) 68#define SECS_PER_HOUR ((time_t)(3600UL)) 69#define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL)) 70#define DAYS_PER_WEEK ((time_t)(7UL)) 71#define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK)) 72#define SECS_PER_YEAR ((time_t)(SECS_PER_WEEK * 52UL)) 73#define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k 74 75/* Useful Macros for getting elapsed time */ 76#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) 77#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) 78#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) 79#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday 80#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 81#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight 82// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 83// Always set the correct time before settting alarms 84#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day 85#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day 86#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY) ) // note that week starts on day 1 87#define previousSunday(_time_) (_time_ - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time 88#define nextSunday(_time_) ( previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time 89 90 91/* Useful Macros for converting elapsed time to a time_t */ 92#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) 93#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) 94#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 95#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) 96 97/*============================================================================*/ 98/* time and date functions */ 99int hour(); // the hour now 100int hour(time_t t); // the hour for the given time 101int hourFormat12(); // the hour now in 12 hour format 102int hourFormat12(time_t t); // the hour for the given time in 12 hour format 103uint8_t isAM(); // returns true if time now is AM 104uint8_t isAM(time_t t); // returns true the given time is AM 105uint8_t isPM(); // returns true if time now is PM 106uint8_t isPM(time_t t); // returns true the given time is PM 107int minute(); // the minute now 108int minute(time_t t); // the minute for the given time 109int second(); // the second now 110int second(time_t t); // the second for the given time 111int day(); // the day now 112int day(time_t t); // the day for the given time 113int weekday(); // the weekday now (Sunday is day 1) 114int weekday(time_t t); // the weekday for the given time 115int month(); // the month now (Jan is month 1) 116int month(time_t t); // the month for the given time 117int year(); // the full four digit year: (2009, 2010 etc) 118int year(time_t t); // the year for the given time 119 120time_t now(); // return the current time as seconds since Jan 1 1970 121void setTime(time_t t); 122void setTime(int hr,int min,int sec,int day, int month, int yr); 123void adjustTime(long adjustment); 124 125/* date strings */ 126#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) 127char* monthStr(uint8_t month); 128char* dayStr(uint8_t day); 129char* monthShortStr(uint8_t month); 130char* dayShortStr(uint8_t day); 131 132/* time sync functions */ 133timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized 134void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider 135void setSyncInterval(time_t interval); // set the number of seconds between re-sync 136 137/* low level functions to convert to and from system time */ 138void breakTime(time_t time, tmElements_t &tm); // break time_t into elements 139time_t makeTime(tmElements_t &tm); // convert time elements into time_t 140 141} // extern "C++" 142#endif // __cplusplus 143#endif /* _Time_h */
EEPROM.h
c_cpp
It is the library needed to manipulate EEPROM
1/* 2 EEPROM.h - EEPROM library 3 Original Copyright (c) 2006 David A. Mellis. All right reserved. 4 New version by Christopher Andrews 2015. 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19*/ 20 21#ifndef EEPROM_h 22#define EEPROM_h 23 24#include <inttypes.h> 25#include <avr/eeprom.h> 26#include <avr/io.h> 27 28/*** 29 EERef class. 30 31 This object references an EEPROM cell. 32 Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. 33 This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. 34***/ 35 36struct EERef{ 37 38 EERef( const int index ) 39 : index( index ) {} 40 41 //Access/read members. 42 uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); } 43 operator const uint8_t() const { return **this; } 44 45 //Assignment/write members. 46 EERef &operator=( const EERef &ref ) { return *this = *ref; } 47 EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; } 48 EERef &operator +=( uint8_t in ) { return *this = **this + in; } 49 EERef &operator -=( uint8_t in ) { return *this = **this - in; } 50 EERef &operator *=( uint8_t in ) { return *this = **this * in; } 51 EERef &operator /=( uint8_t in ) { return *this = **this / in; } 52 EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } 53 EERef &operator %=( uint8_t in ) { return *this = **this % in; } 54 EERef &operator &=( uint8_t in ) { return *this = **this & in; } 55 EERef &operator |=( uint8_t in ) { return *this = **this | in; } 56 EERef &operator <<=( uint8_t in ) { return *this = **this << in; } 57 EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } 58 59 EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } 60 61 /** Prefix increment/decrement **/ 62 EERef& operator++() { return *this += 1; } 63 EERef& operator--() { return *this -= 1; } 64 65 /** Postfix increment/decrement **/ 66 uint8_t operator++ (int){ 67 uint8_t ret = **this; 68 return ++(*this), ret; 69 } 70 71 uint8_t operator-- (int){ 72 uint8_t ret = **this; 73 return --(*this), ret; 74 } 75 76 int index; //Index of current EEPROM cell. 77}; 78 79/*** 80 EEPtr class. 81 82 This object is a bidirectional pointer to EEPROM cells represented by EERef objects. 83 Just like a normal pointer type, this can be dereferenced and repositioned using 84 increment/decrement operators. 85***/ 86 87struct EEPtr{ 88 89 EEPtr( const int index ) 90 : index( index ) {} 91 92 operator const int() const { return index; } 93 EEPtr &operator=( int in ) { return index = in, *this; } 94 95 //Iterator functionality. 96 bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } 97 EERef operator*() { return index; } 98 99 /** Prefix & Postfix increment/decrement **/ 100 EEPtr& operator++() { return ++index, *this; } 101 EEPtr& operator--() { return --index, *this; } 102 EEPtr operator++ (int) { return index++; } 103 EEPtr operator-- (int) { return index--; } 104 105 int index; //Index of current EEPROM cell. 106}; 107 108/*** 109 EEPROMClass class. 110 111 This object represents the entire EEPROM space. 112 It wraps the functionality of EEPtr and EERef into a basic interface. 113 This class is also 100% backwards compatible with earlier Arduino core releases. 114***/ 115 116struct EEPROMClass{ 117 118 //Basic user access methods. 119 EERef operator[]( const int idx ) { return idx; } 120 uint8_t read( int idx ) { return EERef( idx ); } 121 void write( int idx, uint8_t val ) { (EERef( idx )) = val; } 122 void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } 123 124 //STL and C++11 iteration capability. 125 EEPtr begin() { return 0x00; } 126 EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. 127 uint16_t length() { return E2END + 1; } 128 129 //Functionality to 'get' and 'put' objects to and from EEPROM. 130 template< typename T > T &get( int idx, T &t ){ 131 EEPtr e = idx; 132 uint8_t *ptr = (uint8_t*) &t; 133 for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; 134 return t; 135 } 136 137 template< typename T > const T &put( int idx, const T &t ){ 138 EEPtr e = idx; 139 const uint8_t *ptr = (const uint8_t*) &t; 140 for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); 141 return t; 142 } 143}; 144 145static EEPROMClass EEPROM; 146#endif
time.h
c_cpp
This is the library needed to use the time functions
1/* 2 time.h - low level time and date functions 3*/ 4 5/* 6 July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) 7 - fixed daysToTime_t macro (thanks maniacbug) 8*/ 9 10#ifndef _Time_h 11#ifdef __cplusplus 12#define _Time_h 13 14#include <inttypes.h> 15#ifndef __AVR__ 16#include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h 17#endif 18 19 20#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc 21typedef unsigned long time_t; 22#endif 23 24 25// This ugly hack allows us to define C++ overloaded functions, when included 26// from within an extern "C", as newlib's sys/stat.h does. Actually it is 27// intended to include "time.h" from the C library (on ARM, but AVR does not 28// have that file at all). On Mac and Windows, the compiler will find this 29// "Time.h" instead of the C library "time.h", so we may cause other weird 30// and unpredictable effects by conflicting with the C library header "time.h", 31// but at least this hack lets us define C++ functions as intended. Hopefully 32// nothing too terrible will result from overriding the C library header?! 33extern "C++" { 34typedef enum {timeNotSet, timeNeedsSync, timeSet 35} timeStatus_t ; 36 37typedef enum { 38 dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday 39} timeDayOfWeek_t; 40 41typedef enum { 42 tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields 43} tmByteFields; 44 45typedef struct { 46 uint8_t Second; 47 uint8_t Minute; 48 uint8_t Hour; 49 uint8_t Wday; // day of week, sunday is day 1 50 uint8_t Day; 51 uint8_t Month; 52 uint8_t Year; // offset from 1970; 53} tmElements_t, TimeElements, *tmElementsPtr_t; 54 55//convenience macros to convert to and from tm years 56#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year 57#define CalendarYrToTm(Y) ((Y) - 1970) 58#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 59#define y2kYearToTm(Y) ((Y) + 30) 60 61typedef time_t(*getExternalTime)(); 62//typedef void (*setExternalTime)(const time_t); // not used in this version 63 64 65/*==============================================================================*/ 66/* Useful Constants */ 67#define SECS_PER_MIN ((time_t)(60UL)) 68#define SECS_PER_HOUR ((time_t)(3600UL)) 69#define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL)) 70#define DAYS_PER_WEEK ((time_t)(7UL)) 71#define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK)) 72#define SECS_PER_YEAR ((time_t)(SECS_PER_WEEK * 52UL)) 73#define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k 74 75/* Useful Macros for getting elapsed time */ 76#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) 77#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) 78#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) 79#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday 80#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 81#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight 82// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 83// Always set the correct time before settting alarms 84#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day 85#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day 86#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY) ) // note that week starts on day 1 87#define previousSunday(_time_) (_time_ - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time 88#define nextSunday(_time_) ( previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time 89 90 91/* Useful Macros for converting elapsed time to a time_t */ 92#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) 93#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) 94#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 95#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) 96 97/*============================================================================*/ 98/* time and date functions */ 99int hour(); // the hour now 100int hour(time_t t); // the hour for the given time 101int hourFormat12(); // the hour now in 12 hour format 102int hourFormat12(time_t t); // the hour for the given time in 12 hour format 103uint8_t isAM(); // returns true if time now is AM 104uint8_t isAM(time_t t); // returns true the given time is AM 105uint8_t isPM(); // returns true if time now is PM 106uint8_t isPM(time_t t); // returns true the given time is PM 107int minute(); // the minute now 108int minute(time_t t); // the minute for the given time 109int second(); // the second now 110int second(time_t t); // the second for the given time 111int day(); // the day now 112int day(time_t t); // the day for the given time 113int weekday(); // the weekday now (Sunday is day 1) 114int weekday(time_t t); // the weekday for the given time 115int month(); // the month now (Jan is month 1) 116int month(time_t t); // the month for the given time 117int year(); // the full four digit year: (2009, 2010 etc) 118int year(time_t t); // the year for the given time 119 120time_t now(); // return the current time as seconds since Jan 1 1970 121void setTime(time_t t); 122void setTime(int hr,int min,int sec,int day, int month, int yr); 123void adjustTime(long adjustment); 124 125/* date strings */ 126#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) 127char* monthStr(uint8_t month); 128char* dayStr(uint8_t day); 129char* monthShortStr(uint8_t month); 130char* dayShortStr(uint8_t day); 131 132/* time sync functions */ 133timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized 134void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider 135void setSyncInterval(time_t interval); // set the number of seconds between re-sync 136 137/* low level functions to convert to and from system time */ 138void breakTime(time_t time, tmElements_t &tm); // break time_t into elements 139time_t makeTime(tmElements_t &tm); // convert time elements into time_t 140 141} // extern "C++" 142#endif // __cplusplus 143#endif /* _Time_h */
Keyboard
c_cpp
It is the main program
1#include "EEPROM.h" 2#include "DigiKeyboard.h" 3#include "time.h" 4 5#define BLUEPIN 0 6#define GREENPIN 1 7#define REDPIN 2 8 9// NO SPECIAL CHARS 10char caracteres[] = {0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}; 11char motdepasse[21]; 12 13void setup() { 14 pinMode(REDPIN, OUTPUT); 15 pinMode(GREENPIN, OUTPUT); 16 pinMode(BLUEPIN, INPUT); 17 18 DigiKeyboard.sendKeyStroke(0); 19 20 DigiKeyboard.delay(1000); 21 22 int adressReg = 0; 23 int i = 0; 24 char carr; 25 26 while (i != 20) { 27 carr = EEPROM.read(adressReg); 28 29 if ((carr & 0b10000000) == 0b10000000) DigiKeyboard.sendKeyStroke(carr & 0b01111111, MOD_SHIFT_LEFT); 30 31 else DigiKeyboard.sendKeyStroke(carr & 0b01111111); 32 33 digitalWrite(GREENPIN, LOW); 34 35 adressReg++; 36 i++; 37 } 38} 39 40void loop() { 41 while (digitalRead(0) == HIGH); 42 43 DigiKeyboard.delay(2000); 44 45 if (digitalRead(0) == LOW) newPass(); 46} 47 48void newPass() { 49 int adressEeprom = 0; 50 int i = 0; 51 52 digitalWrite(GREENPIN, HIGH); 53 54 DigiKeyboard.print("Nouvequ ;ot de pqsse en cours de generqtion"); 55 DigiKeyboard.println(); 56 DigiKeyboard.delay(50); 57 58 srand(millis()); // initialisation de rand 59 60 while (i != 20) { 61 digitalWrite(REDPIN, HIGH); 62 63 int randomChar = (rand() % 35); 64 int randomChoice = (rand() % 2) + 1; 65 66 char caractere = caracteres[randomChar]; 67 if (randomChar > 26) caractere = caractere | 0b10000000; 68 if ((randomChoice == 1) && (randomChar <= 26)) caractere = caractere | 0b10000000; 69 70 EEPROM.update(adressEeprom, caractere); 71 adressEeprom ++; 72 i++; 73 74 digitalWrite(REDPIN, LOW); 75 } 76 digitalWrite(GREENPIN, LOW); 77 78 DigiKeyboard.println("Generqtion ter;inee 6 Voici le ;ot de pqsse ."); 79 DigiKeyboard.delay(50); 80 81 setup(); 82 83 while (digitalRead(0) == LOW); 84}
EEPROM.h
c_cpp
It is the library needed to manipulate EEPROM
1/* 2 EEPROM.h - EEPROM library 3 Original Copyright (c) 2006 David A. Mellis. All right reserved. 4 New version by Christopher Andrews 2015. 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19*/ 20 21#ifndef EEPROM_h 22#define EEPROM_h 23 24#include <inttypes.h> 25#include <avr/eeprom.h> 26#include <avr/io.h> 27 28/*** 29 EERef class. 30 31 This object references an EEPROM cell. 32 Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. 33 This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. 34***/ 35 36struct EERef{ 37 38 EERef( const int index ) 39 : index( index ) {} 40 41 //Access/read members. 42 uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); } 43 operator const uint8_t() const { return **this; } 44 45 //Assignment/write members. 46 EERef &operator=( const EERef &ref ) { return *this = *ref; } 47 EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; } 48 EERef &operator +=( uint8_t in ) { return *this = **this + in; } 49 EERef &operator -=( uint8_t in ) { return *this = **this - in; } 50 EERef &operator *=( uint8_t in ) { return *this = **this * in; } 51 EERef &operator /=( uint8_t in ) { return *this = **this / in; } 52 EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } 53 EERef &operator %=( uint8_t in ) { return *this = **this % in; } 54 EERef &operator &=( uint8_t in ) { return *this = **this & in; } 55 EERef &operator |=( uint8_t in ) { return *this = **this | in; } 56 EERef &operator <<=( uint8_t in ) { return *this = **this << in; } 57 EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } 58 59 EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } 60 61 /** Prefix increment/decrement **/ 62 EERef& operator++() { return *this += 1; } 63 EERef& operator--() { return *this -= 1; } 64 65 /** Postfix increment/decrement **/ 66 uint8_t operator++ (int){ 67 uint8_t ret = **this; 68 return ++(*this), ret; 69 } 70 71 uint8_t operator-- (int){ 72 uint8_t ret = **this; 73 return --(*this), ret; 74 } 75 76 int index; //Index of current EEPROM cell. 77}; 78 79/*** 80 EEPtr class. 81 82 This object is a bidirectional pointer to EEPROM cells represented by EERef objects. 83 Just like a normal pointer type, this can be dereferenced and repositioned using 84 increment/decrement operators. 85***/ 86 87struct EEPtr{ 88 89 EEPtr( const int index ) 90 : index( index ) {} 91 92 operator const int() const { return index; } 93 EEPtr &operator=( int in ) { return index = in, *this; } 94 95 //Iterator functionality. 96 bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } 97 EERef operator*() { return index; } 98 99 /** Prefix & Postfix increment/decrement **/ 100 EEPtr& operator++() { return ++index, *this; } 101 EEPtr& operator--() { return --index, *this; } 102 EEPtr operator++ (int) { return index++; } 103 EEPtr operator-- (int) { return index--; } 104 105 int index; //Index of current EEPROM cell. 106}; 107 108/*** 109 EEPROMClass class. 110 111 This object represents the entire EEPROM space. 112 It wraps the functionality of EEPtr and EERef into a basic interface. 113 This class is also 100% backwards compatible with earlier Arduino core releases. 114***/ 115 116struct EEPROMClass{ 117 118 //Basic user access methods. 119 EERef operator[]( const int idx ) { return idx; } 120 uint8_t read( int idx ) { return EERef( idx ); } 121 void write( int idx, uint8_t val ) { (EERef( idx )) = val; } 122 void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } 123 124 //STL and C++11 iteration capability. 125 EEPtr begin() { return 0x00; } 126 EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. 127 uint16_t length() { return E2END + 1; } 128 129 //Functionality to 'get' and 'put' objects to and from EEPROM. 130 template< typename T > T &get( int idx, T &t ){ 131 EEPtr e = idx; 132 uint8_t *ptr = (uint8_t*) &t; 133 for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; 134 return t; 135 } 136 137 template< typename T > const T &put( int idx, const T &t ){ 138 EEPtr e = idx; 139 const uint8_t *ptr = (const uint8_t*) &t; 140 for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); 141 return t; 142 } 143}; 144 145static EEPROMClass EEPROM; 146#endif
Downloadable files
Wiring
Wiring

Wiring
Wiring

Comments
Only logged in users can leave comments