Compare View

switch
from
...
to
 
Commits (5)
ino/lib/LCD/LCD.cpp 0 → 100644
... ... @@ -0,0 +1,105 @@
  1 +#include "LCD.h"
  2 +
  3 +LCD::LCD() {
  4 + lcd_addr = LCD_DEFAULT_ADDRESS >> 1;
  5 + Wire.begin();
  6 +}
  7 +
  8 +LCD::LCD(byte lcd_addr) {
  9 + this->lcd_addr = lcd_addr >> 1;
  10 + Wire.begin();
  11 +}
  12 +
  13 +bool LCD::is_alive() {
  14 + Wire.beginTransmission(lcd_addr);
  15 + return Wire.endTransmission() == 0;
  16 +}
  17 +
  18 +void LCD::command(byte cmd) {
  19 + Wire.write(LCD_COMMAND);
  20 + Wire.write(cmd);
  21 +}
  22 +
  23 +void LCD::backlight(bool state) {
  24 + Wire.beginTransmission(lcd_addr);
  25 + if (state) {
  26 + command(LCD_BACKLIGHT_ON);
  27 + } else {
  28 + command(LCD_BACKLIGHT_OFF);
  29 + }
  30 + Wire.endTransmission();
  31 +}
  32 +
  33 +void LCD::set_type(byte type) {
  34 + Wire.beginTransmission(lcd_addr);
  35 + command(LCD_SET_DISPLAY_TYPE);
  36 + Wire.write(type);
  37 + Wire.endTransmission();
  38 +}
  39 +
  40 +void LCD::change_address(byte addr) {
  41 + Wire.beginTransmission(lcd_addr);
  42 + command(LCD_CHANGE_ADDRESS);
  43 + command(LCD_CHANGE_ADDRESS_1);
  44 + command(LCD_CHANGE_ADDRESS_2);
  45 + command(LCD_CHANGE_ADDRESS_3);
  46 + Wire.write(addr);
  47 + Wire.endTransmission();
  48 +
  49 + lcd_addr = addr;
  50 +}
  51 +
  52 +void LCD::print(char *string) {
  53 + Wire.beginTransmission(lcd_addr);
  54 + Wire.write(LCD_COMMAND);
  55 + uint8_t length = strlen(string);
  56 + for (int i = 0; i < length; i++) {
  57 + Wire.write(*string++);
  58 + }
  59 + Wire.endTransmission();
  60 +}
  61 +
  62 +void LCD::clear_screen() {
  63 + Wire.beginTransmission(lcd_addr);
  64 + command(LCD_CLEAR_SCREEN);
  65 + Wire.endTransmission();
  66 +}
  67 +
  68 +void LCD::cursor_home() {
  69 + Wire.beginTransmission(lcd_addr);
  70 + command(LCD_CURSOR_HOME);
  71 + Wire.endTransmission();
  72 +}
  73 +
  74 +void LCD::set_cursor(byte x, byte y) {
  75 + Wire.beginTransmission(lcd_addr);
  76 + command(LCD_SET_CURSOR_COORDS);
  77 + Wire.write(y);
  78 + Wire.write(x);
  79 + Wire.endTransmission();
  80 +}
  81 +
  82 +void LCD::hide_cursor() {
  83 + Wire.beginTransmission(lcd_addr);
  84 + command(LCD_HIDE_CURSOR);
  85 + Wire.endTransmission();
  86 +}
  87 +
  88 +void LCD::underline_cursor() {
  89 + Wire.beginTransmission(lcd_addr);
  90 + command(LCD_SHOW_UNDERLINE_CURSOR);
  91 + Wire.endTransmission();
  92 +}
  93 +
  94 +void LCD::blink_cursor() {
  95 + Wire.beginTransmission(lcd_addr);
  96 + command(LCD_SHOW_BLINKING_CURSOR);
  97 + Wire.endTransmission();
  98 +}
  99 +
  100 +void LCD::backspace() {
  101 + Wire.beginTransmission(lcd_addr);
  102 + command(LCD_BACKSPACE);
  103 + Wire.endTransmission();
  104 +}
  105 +
... ...
ino/lib/LCD/LCD.h 0 → 100644
... ... @@ -0,0 +1,76 @@
  1 +/*
  2 + Heisenwave's LCD05
  3 +*/
  4 +
  5 +#ifndef LCD_H
  6 +#define LCD_H
  7 +
  8 +#include "Arduino.h"
  9 +#include "Wire.h"
  10 +
  11 +// Commands LCD05
  12 +
  13 +#define LCD_COMMAND byte(0)
  14 +#define LCD_CURSOR_HOME byte(1)
  15 +#define LCD_SET_CURSOR byte(2)
  16 +#define LCD_SET_CURSOR_COORDS byte(3)
  17 +#define LCD_HIDE_CURSOR byte(4)
  18 +#define LCD_SHOW_UNDERLINE_CURSOR byte(5)
  19 +#define LCD_SHOW_BLINKING_CURSOR byte(6)
  20 +#define LCD_BACKSPACE byte(8)
  21 +#define LCD_HORIZONTAL_TAB byte(9)
  22 +#define LCD_SMART_LINE_FEED byte(10)
  23 +#define LCD_VERTICAL_TAB byte(11)
  24 +#define LCD_CLEAR_SCREEN byte(12)
  25 +#define LCD_CARRIAGE_RETURN byte(13)
  26 +#define LCD_FIFO_STATUS byte(14)
  27 +#define LCD_CLEAR_COLUMN byte(17)
  28 +#define LCD_TAB_SET byte(18)
  29 +#define LCD_BACKLIGHT_ON byte(19)
  30 +#define LCD_BACKLIGHT_OFF byte(20)
  31 +#define LCD_DISABLE_STARTUP_MESSAGE byte(21)
  32 +#define LCD_ENABLE_STARTUP_MESSAGE byte(22)
  33 +#define LCD_SAVE_AS_STARTUP_SCREEN byte(23)
  34 +#define LCD_SET_DISPLAY_TYPE byte(24)
  35 +#define LCD_CHANGE_ADDRESS byte(25)
  36 +#define LCD_CUSTOM_CHAR_GENERATOR byte(27)
  37 +#define LCD_DOUBLE_KEYPAD_SCAN_RATE byte(28)
  38 +#define LCD_NORMAL_KEYPAD_SCAN_RATE byte(29)
  39 +#define LCD_CONTRAST_SET byte(30)
  40 +#define LCD_BRIGHTNESS_SET byte(31)
  41 +#define LCD_CHANGE_ADDRESS_1 byte(160)
  42 +#define LCD_CHANGE_ADDRESS_3 byte(165)
  43 +#define LCD_CHANGE_ADDRESS_2 byte(170)
  44 +
  45 +#define LCD_DEFAULT_ADDRESS byte(0xC6)
  46 +#define LCD_MAX_CHANCE 500
  47 +
  48 +class LCD {
  49 + public:
  50 + static const byte GLCD_20X4 = 3;
  51 + static const byte BLCD_20X4 = 4;
  52 + static const byte GLCD_16X2 = 5;
  53 + static const byte BLCD_16X2 = 6;
  54 +
  55 + LCD();
  56 + LCD(uint8_t lcd_addr);
  57 +
  58 + bool is_alive();
  59 + void backlight(bool state);
  60 + void set_type(byte type);
  61 + void change_address(byte addr);
  62 + void print(char *string);
  63 + void clear_screen();
  64 + void cursor_home();
  65 + void set_cursor(byte x, byte y);
  66 + void hide_cursor();
  67 + void blink_cursor();
  68 + void underline_cursor();
  69 + void backspace();
  70 + private:
  71 + uint8_t lcd_addr;
  72 +
  73 + void command(byte cmd);
  74 +};
  75 +
  76 +#endif
... ...
ino/lib/Time/DateStrings.cpp 0 → 100644
... ... @@ -0,0 +1,97 @@
  1 +/* DateStrings.cpp
  2 + * Definitions for date strings for use with the Time library
  3 + *
  4 + * Updated for Arduino 1.5.7 18 July 2014
  5 + *
  6 + * No memory is consumed in the sketch if your code does not call any of the string methods
  7 + * You can change the text of the strings, make sure the short strings are each exactly 3 characters
  8 + * the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in Time.h
  9 + *
  10 + */
  11 +
  12 +#if defined(__AVR__)
  13 +#include <avr/pgmspace.h>
  14 +#else
  15 +// for compatiblity with Arduino Due and Teensy 3.0 and maybe others?
  16 +#define PROGMEM
  17 +#define PGM_P const char *
  18 +#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  19 +#define pgm_read_word(addr) (*(const unsigned char **)(addr))
  20 +#define strcpy_P(dest, src) strcpy((dest), (src))
  21 +#endif
  22 +#include <string.h> // for strcpy_P or strcpy
  23 +#include "Time.h"
  24 +
  25 +// the short strings for each day or month must be exactly dt_SHORT_STR_LEN
  26 +#define dt_SHORT_STR_LEN 3 // the length of short strings
  27 +
  28 +static char buffer[dt_MAX_STRING_LEN+1]; // must be big enough for longest string and the terminating null
  29 +
  30 +const char monthStr0[] PROGMEM = "";
  31 +const char monthStr1[] PROGMEM = "January";
  32 +const char monthStr2[] PROGMEM = "February";
  33 +const char monthStr3[] PROGMEM = "March";
  34 +const char monthStr4[] PROGMEM = "April";
  35 +const char monthStr5[] PROGMEM = "May";
  36 +const char monthStr6[] PROGMEM = "June";
  37 +const char monthStr7[] PROGMEM = "July";
  38 +const char monthStr8[] PROGMEM = "August";
  39 +const char monthStr9[] PROGMEM = "September";
  40 +const char monthStr10[] PROGMEM = "October";
  41 +const char monthStr11[] PROGMEM = "November";
  42 +const char monthStr12[] PROGMEM = "December";
  43 +
  44 +const PROGMEM char * const PROGMEM monthNames_P[] =
  45 +{
  46 + monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
  47 + monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
  48 +};
  49 +
  50 +const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec";
  51 +
  52 +const char dayStr0[] PROGMEM = "Err";
  53 +const char dayStr1[] PROGMEM = "Sunday";
  54 +const char dayStr2[] PROGMEM = "Monday";
  55 +const char dayStr3[] PROGMEM = "Tuesday";
  56 +const char dayStr4[] PROGMEM = "Wednesday";
  57 +const char dayStr5[] PROGMEM = "Thursday";
  58 +const char dayStr6[] PROGMEM = "Friday";
  59 +const char dayStr7[] PROGMEM = "Saturday";
  60 +
  61 +const PROGMEM char * const PROGMEM dayNames_P[] =
  62 +{
  63 + dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7
  64 +};
  65 +
  66 +const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";
  67 +
  68 +/* functions to return date strings */
  69 +
  70 +char* monthStr(uint8_t month)
  71 +{
  72 + strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
  73 + return buffer;
  74 +}
  75 +
  76 +char* monthShortStr(uint8_t month)
  77 +{
  78 + for (int i=0; i < dt_SHORT_STR_LEN; i++)
  79 + buffer[i] = pgm_read_byte(&(monthShortNames_P[i+ (month*dt_SHORT_STR_LEN)]));
  80 + buffer[dt_SHORT_STR_LEN] = 0;
  81 + return buffer;
  82 +}
  83 +
  84 +char* dayStr(uint8_t day)
  85 +{
  86 + strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day])));
  87 + return buffer;
  88 +}
  89 +
  90 +char* dayShortStr(uint8_t day)
  91 +{
  92 + uint8_t index = day*dt_SHORT_STR_LEN;
  93 + for (int i=0; i < dt_SHORT_STR_LEN; i++)
  94 + buffer[i] = pgm_read_byte(&(dayShortNames_P[index + i]));
  95 + buffer[dt_SHORT_STR_LEN] = 0;
  96 + return buffer;
  97 +}
... ...
ino/lib/Time/Time.cpp 0 → 100644
... ... @@ -0,0 +1,319 @@
  1 +/*
  2 + time.c - low level time and date functions
  3 + Copyright (c) Michael Margolis 2009-2014
  4 +
  5 + This library is free software; you can redistribute it and/or
  6 + modify it under the terms of the GNU Lesser General Public
  7 + License as published by the Free Software Foundation; either
  8 + version 2.1 of the License, or (at your option) any later version.
  9 +
  10 + This library is distributed in the hope that it will be useful,
  11 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13 + Lesser General Public License for more details.
  14 +
  15 + You should have received a copy of the GNU Lesser General Public
  16 + License along with this library; if not, write to the Free Software
  17 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18 +
  19 + 1.0 6 Jan 2010 - initial release
  20 + 1.1 12 Feb 2010 - fixed leap year calculation error
  21 + 1.2 1 Nov 2010 - fixed setTime bug (thanks to Korman for this)
  22 + 1.3 24 Mar 2012 - many edits by Paul Stoffregen: fixed timeStatus() to update
  23 + status, updated examples for Arduino 1.0, fixed ARM
  24 + compatibility issues, added TimeArduinoDue and TimeTeensy3
  25 + examples, add error checking and messages to RTC examples,
  26 + add examples to DS1307RTC library.
  27 + 1.4 5 Sep 2014 - compatibility with Arduino 1.5.7
  28 +*/
  29 +
  30 +#if ARDUINO >= 100
  31 +#include <Arduino.h>
  32 +#else
  33 +#include <WProgram.h>
  34 +#endif
  35 +
  36 +#include "Time.h"
  37 +
  38 +static tmElements_t tm; // a cache of time elements
  39 +static time_t cacheTime; // the time the cache was updated
  40 +static uint32_t syncInterval = 300; // time sync will be attempted after this many seconds
  41 +
  42 +void refreshCache(time_t t) {
  43 + if (t != cacheTime) {
  44 + breakTime(t, tm);
  45 + cacheTime = t;
  46 + }
  47 +}
  48 +
  49 +int hour() { // the hour now
  50 + return hour(now());
  51 +}
  52 +
  53 +int hour(time_t t) { // the hour for the given time
  54 + refreshCache(t);
  55 + return tm.Hour;
  56 +}
  57 +
  58 +int hourFormat12() { // the hour now in 12 hour format
  59 + return hourFormat12(now());
  60 +}
  61 +
  62 +int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
  63 + refreshCache(t);
  64 + if( tm.Hour == 0 )
  65 + return 12; // 12 midnight
  66 + else if( tm.Hour > 12)
  67 + return tm.Hour - 12 ;
  68 + else
  69 + return tm.Hour ;
  70 +}
  71 +
  72 +uint8_t isAM() { // returns true if time now is AM
  73 + return !isPM(now());
  74 +}
  75 +
  76 +uint8_t isAM(time_t t) { // returns true if given time is AM
  77 + return !isPM(t);
  78 +}
  79 +
  80 +uint8_t isPM() { // returns true if PM
  81 + return isPM(now());
  82 +}
  83 +
  84 +uint8_t isPM(time_t t) { // returns true if PM
  85 + return (hour(t) >= 12);
  86 +}
  87 +
  88 +int minute() {
  89 + return minute(now());
  90 +}
  91 +
  92 +int minute(time_t t) { // the minute for the given time
  93 + refreshCache(t);
  94 + return tm.Minute;
  95 +}
  96 +
  97 +int second() {
  98 + return second(now());
  99 +}
  100 +
  101 +int second(time_t t) { // the second for the given time
  102 + refreshCache(t);
  103 + return tm.Second;
  104 +}
  105 +
  106 +int day(){
  107 + return(day(now()));
  108 +}
  109 +
  110 +int day(time_t t) { // the day for the given time (0-6)
  111 + refreshCache(t);
  112 + return tm.Day;
  113 +}
  114 +
  115 +int weekday() { // Sunday is day 1
  116 + return weekday(now());
  117 +}
  118 +
  119 +int weekday(time_t t) {
  120 + refreshCache(t);
  121 + return tm.Wday;
  122 +}
  123 +
  124 +int month(){
  125 + return month(now());
  126 +}
  127 +
  128 +int month(time_t t) { // the month for the given time
  129 + refreshCache(t);
  130 + return tm.Month;
  131 +}
  132 +
  133 +int year() { // as in Processing, the full four digit year: (2009, 2010 etc)
  134 + return year(now());
  135 +}
  136 +
  137 +int year(time_t t) { // the year for the given time
  138 + refreshCache(t);
  139 + return tmYearToCalendar(tm.Year);
  140 +}
  141 +
  142 +/*============================================================================*/
  143 +/* functions to convert to and from system time */
  144 +/* These are for interfacing with time serivces and are not normally needed in a sketch */
  145 +
  146 +// leap year calulator expects year argument as years offset from 1970
  147 +#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
  148 +
  149 +static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
  150 +
  151 +void breakTime(time_t timeInput, tmElements_t &tm){
  152 +// break the given time_t into time components
  153 +// this is a more compact version of the C library localtime function
  154 +// note that year is offset from 1970 !!!
  155 +
  156 + uint8_t year;
  157 + uint8_t month, monthLength;
  158 + uint32_t time;
  159 + unsigned long days;
  160 +
  161 + time = (uint32_t)timeInput;
  162 + tm.Second = time % 60;
  163 + time /= 60; // now it is minutes
  164 + tm.Minute = time % 60;
  165 + time /= 60; // now it is hours
  166 + tm.Hour = time % 24;
  167 + time /= 24; // now it is days
  168 + tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1
  169 +
  170 + year = 0;
  171 + days = 0;
  172 + while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
  173 + year++;
  174 + }
  175 + tm.Year = year; // year is offset from 1970
  176 +
  177 + days -= LEAP_YEAR(year) ? 366 : 365;
  178 + time -= days; // now it is days in this year, starting at 0
  179 +
  180 + days=0;
  181 + month=0;
  182 + monthLength=0;
  183 + for (month=0; month<12; month++) {
  184 + if (month==1) { // february
  185 + if (LEAP_YEAR(year)) {
  186 + monthLength=29;
  187 + } else {
  188 + monthLength=28;
  189 + }
  190 + } else {
  191 + monthLength = monthDays[month];
  192 + }
  193 +
  194 + if (time >= monthLength) {
  195 + time -= monthLength;
  196 + } else {
  197 + break;
  198 + }
  199 + }
  200 + tm.Month = month + 1; // jan is month 1
  201 + tm.Day = time + 1; // day of month
  202 +}
  203 +
  204 +time_t makeTime(tmElements_t &tm){
  205 +// assemble time elements into time_t
  206 +// note year argument is offset from 1970 (see macros in time.h to convert to other formats)
  207 +// previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9
  208 +
  209 + int i;
  210 + uint32_t seconds;
  211 +
  212 + // seconds from 1970 till 1 jan 00:00:00 of the given year
  213 + seconds= tm.Year*(SECS_PER_DAY * 365);
  214 + for (i = 0; i < tm.Year; i++) {
  215 + if (LEAP_YEAR(i)) {
  216 + seconds += SECS_PER_DAY; // add extra days for leap years
  217 + }
  218 + }
  219 +
  220 + // add days for this year, months start from 1
  221 + for (i = 1; i < tm.Month; i++) {
  222 + if ( (i == 2) && LEAP_YEAR(tm.Year)) {
  223 + seconds += SECS_PER_DAY * 29;
  224 + } else {
  225 + seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0
  226 + }
  227 + }
  228 + seconds+= (tm.Day-1) * SECS_PER_DAY;
  229 + seconds+= tm.Hour * SECS_PER_HOUR;
  230 + seconds+= tm.Minute * SECS_PER_MIN;
  231 + seconds+= tm.Second;
  232 + return (time_t)seconds;
  233 +}
  234 +/*=====================================================*/
  235 +/* Low level system time functions */
  236 +
  237 +static uint32_t sysTime = 0;
  238 +static uint32_t prevMillis = 0;
  239 +static uint32_t nextSyncTime = 0;
  240 +static timeStatus_t Status = timeNotSet;
  241 +
  242 +getExternalTime getTimePtr; // pointer to external sync function
  243 +//setExternalTime setTimePtr; // not used in this version
  244 +
  245 +#ifdef TIME_DRIFT_INFO // define this to get drift data
  246 +time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
  247 +#endif
  248 +
  249 +
  250 +time_t now() {
  251 + while (millis() - prevMillis >= 1000){
  252 + sysTime++;
  253 + prevMillis += 1000;
  254 +#ifdef TIME_DRIFT_INFO
  255 + sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift
  256 +#endif
  257 + }
  258 + if (nextSyncTime <= sysTime) {
  259 + if (getTimePtr != 0) {
  260 + time_t t = getTimePtr();
  261 + if (t != 0) {
  262 + setTime(t);
  263 + } else {
  264 + nextSyncTime = sysTime + syncInterval;
  265 + Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync;
  266 + }
  267 + }
  268 + }
  269 + return (time_t)sysTime;
  270 +}
  271 +
  272 +void setTime(time_t t) {
  273 +#ifdef TIME_DRIFT_INFO
  274 + if(sysUnsyncedTime == 0)
  275 + sysUnsyncedTime = t; // store the time of the first call to set a valid Time
  276 +#endif
  277 +
  278 + sysTime = (uint32_t)t;
  279 + nextSyncTime = (uint32_t)t + syncInterval;
  280 + Status = timeSet;
  281 + prevMillis = millis(); // restart counting from now (thanks to Korman for this fix)
  282 +}
  283 +
  284 +void setTime(int hr,int min,int sec,int dy, int mnth, int yr){
  285 + // year can be given as full four digit year or two digts (2010 or 10 for 2010);
  286 + //it is converted to years since 1970
  287 + if( yr > 99)
  288 + yr = yr - 1970;
  289 + else
  290 + yr += 30;
  291 + tm.Year = yr;
  292 + tm.Month = mnth;
  293 + tm.Day = dy;
  294 + tm.Hour = hr;
  295 + tm.Minute = min;
  296 + tm.Second = sec;
  297 + setTime(makeTime(tm));
  298 +}
  299 +
  300 +void adjustTime(long adjustment) {
  301 + sysTime += adjustment;
  302 +}
  303 +
  304 +// indicates if time has been set and recently synchronized
  305 +timeStatus_t timeStatus() {
  306 + now(); // required to actually update the status
  307 + return Status;
  308 +}
  309 +
  310 +void setSyncProvider( getExternalTime getTimeFunction){
  311 + getTimePtr = getTimeFunction;
  312 + nextSyncTime = sysTime;
  313 + now(); // this will sync the clock
  314 +}
  315 +
  316 +void setSyncInterval(time_t interval){ // set the number of seconds between re-sync
  317 + syncInterval = (uint32_t)interval;
  318 + nextSyncTime = sysTime + syncInterval;
  319 +}
... ...
ino/lib/Time/Time.h 0 → 100644
... ... @@ -0,0 +1,144 @@
  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
  21 +typedef 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?!
  33 +extern "C++" {
  34 +typedef enum {timeNotSet, timeNeedsSync, timeSet
  35 +} timeStatus_t ;
  36 +
  37 +typedef enum {
  38 + dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday
  39 +} timeDayOfWeek_t;
  40 +
  41 +typedef enum {
  42 + tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields
  43 +} tmByteFields;
  44 +
  45 +typedef 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 +
  61 +typedef 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 (60UL)
  68 +#define SECS_PER_HOUR (3600UL)
  69 +#define SECS_PER_DAY (SECS_PER_HOUR * 24UL)
  70 +#define DAYS_PER_WEEK (7UL)
  71 +#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
  72 +#define SECS_PER_YEAR (SECS_PER_WEEK * 52UL)
  73 +#define SECS_YR_2000 (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 */
  99 +int hour(); // the hour now
  100 +int hour(time_t t); // the hour for the given time
  101 +int hourFormat12(); // the hour now in 12 hour format
  102 +int hourFormat12(time_t t); // the hour for the given time in 12 hour format
  103 +uint8_t isAM(); // returns true if time now is AM
  104 +uint8_t isAM(time_t t); // returns true the given time is AM
  105 +uint8_t isPM(); // returns true if time now is PM
  106 +uint8_t isPM(time_t t); // returns true the given time is PM
  107 +int minute(); // the minute now
  108 +int minute(time_t t); // the minute for the given time
  109 +int second(); // the second now
  110 +int second(time_t t); // the second for the given time
  111 +int day(); // the day now
  112 +int day(time_t t); // the day for the given time
  113 +int weekday(); // the weekday now (Sunday is day 1)
  114 +int weekday(time_t t); // the weekday for the given time
  115 +int month(); // the month now (Jan is month 1)
  116 +int month(time_t t); // the month for the given time
  117 +int year(); // the full four digit year: (2009, 2010 etc)
  118 +int year(time_t t); // the year for the given time
  119 +
  120 +time_t now(); // return the current time as seconds since Jan 1 1970
  121 +void setTime(time_t t);
  122 +void setTime(int hr,int min,int sec,int day, int month, int yr);
  123 +void adjustTime(long adjustment);
  124 +
  125 +/* date strings */
  126 +#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null)
  127 +char* monthStr(uint8_t month);
  128 +char* dayStr(uint8_t day);
  129 +char* monthShortStr(uint8_t month);
  130 +char* dayShortStr(uint8_t day);
  131 +
  132 +/* time sync functions */
  133 +timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized
  134 +void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider
  135 +void setSyncInterval(time_t interval); // set the number of seconds between re-sync
  136 +
  137 +/* low level functions to convert to and from system time */
  138 +void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
  139 +time_t makeTime(tmElements_t &tm); // convert time elements into time_t
  140 +
  141 +} // extern "C++"
  142 +#endif // __cplusplus
  143 +#endif /* _Time_h */
  144 +
... ...
ino/lib/heisenwave/heisenwave.h 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +#ifndef COMMANDS_H
  2 +#define COMMANDS_H
  3 +
  4 +#define HW_INO_VERSION 1
  5 +
  6 +// COMMANDS
  7 +
  8 +#define HW_ACK 1
  9 +#define HW_NACK 2
  10 +#define HW_HELO 3
  11 +#define HW_SMPL_RATE 4
  12 +#define HW_GET_DATA 5
  13 +#define HW_DTIME_SET 6
  14 +#define HW_DTIME_GET 7
  15 +#define HW_SLEEP 8
  16 +
  17 +// COMMANDS SIZE
  18 +
  19 +#define HW_HELO_SIZE 16
  20 +#define HW_DATE_SIZE 4
  21 +
  22 +#endif
... ...
ino/src/sketch.ino
1   -void setup()
2   -{
  1 +#include <heisenwave.h>
  2 +#include <LCD.h>
  3 +#include <Time.h>
  4 +
  5 +uint8_t unix_time[4] = {0,0,0,0};
  6 +char ino_alias[16];
  7 +LCD lcd;
  8 +
  9 +void setup() {
  10 + Serial.begin(115200);
  11 +
  12 + lcd.clear_screen();
  13 + lcd.backlight(true);
  14 + lcd.print("Waiting HELO...");
  15 +
  16 + while (Serial.available() < 1);
  17 +
  18 + if (Serial.read() == HW_HELO) {
  19 + while (Serial.available() < HW_HELO_SIZE);
  20 +
  21 + for (byte i = 0; i < HW_HELO_SIZE; i++) {
  22 + ino_alias[i] = Serial.read();
  23 + }
  24 +
  25 + Serial.write(HW_ACK);
  26 + Serial.write("HW");
  27 + Serial.write(HW_INO_VERSION);
  28 + } else {
  29 + // This should never happen
  30 + //asm volatile ("jmp 0");
  31 + lcd.clear_screen();
  32 + lcd.print("Byte malo :'(");
  33 + }
  34 +
  35 + lcd.clear_screen();
  36 + lcd.print("APP says I'm:");
  37 + lcd.set_cursor(1, 2);
  38 + lcd.print(ino_alias);
3 39 }
4 40  
5   -void loop()
6   -{
  41 +void loop() {
  42 + while (Serial.available() < 1);
  43 +
  44 + switch (Serial.read()) {
  45 + case HW_SMPL_RATE:
  46 +
  47 + break;
  48 + case HW_DTIME_GET:
  49 +
  50 + break;
  51 + case HW_DTIME_SET:
  52 + while (Serial.available() < HW_DATE_SIZE);
  53 +
  54 + for (uint8_t i = 0; i < HW_DATE_SIZE; i++) {
  55 + unix_time[i] = Serial.read();
  56 + }
  57 +
  58 + break;
  59 + case HW_SLEEP:
  60 +
  61 + break;
  62 + case HW_GET_DATA:
  63 +
  64 + break;
  65 + }
7 66 }
... ...