Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyubimov committed Jul 3, 2017
1 parent 4f7d133 commit dcd2174
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 29 deletions.
18 changes: 13 additions & 5 deletions Hydra_EVSE/Hydra_EVSE.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@
#ifndef ___HYDRA_EVSE_H___
#define ___HYDRA_EVSE_H___

// Standard Arduino types like boolean
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <avr/wdt.h>
#include <Wire.h>
#include <LiquidTWI2.h>
#include <PWM.h>
#include <EEPROM.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <Timezone.h>
//#include <Timezone.h>
#include "dst.h"

// SW version
#define SW_VERSION "2.4.1-dev"

// Define this for the basic unit tests run in a generica arduino uno board with a display shield.
//#define UNIT_TESTS
#define UNIT_TESTS

#define UINT_BITS (sizeof(unsigned int) << 3)
#define ULONG_BITS (sizeof(unsigned long) << 3)
Expand Down Expand Up @@ -479,7 +487,6 @@ struct persisted_struct {
void reset();
};


////////////////////////////////////////////////////////////
// This is stuff from Hydra_EVSE.ino that unit tests use

Expand All @@ -491,16 +498,17 @@ extern persisted_struct persisted;
extern void Delay(unsigned int);
extern void displayStatus(unsigned int status);
extern char errLetter(unsigned int status);

extern boolean &enable_dst;
extern Timezone dst;
extern car_struct cars[];
extern DSTRule dstr[2];


///////////////////////////////////////////////////////////
// Inline declarations
static inline time_t localTime()
{
return enable_dst ? dst.toLocal(now()) : now();
return toDST(dstr, now()); // for now
}


Expand Down
31 changes: 7 additions & 24 deletions Hydra_EVSE/Hydra_EVSE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,12 @@ unsigned char currentMenuChoices[] = { 12, 16, 20, 24, 28, 30, 32, 36, 40, 44, 5
#define FIRST_YEAR 2010
#define LAST_YEAR 2020

// We don't really need to support timezones. We just want to perform automatic DST switching.
// The following are the U.S. DST rules. If you live elsewhere, the customize these. The params
// are a descriptive string (not used, but must be 5 chars or less), a "descriptor" for which
// weekday in the active month is in the rule (first, second... last), the day of the week,
// the month, and the hour of the day. The last parameter is the offset in minutes. You should
// have the "winter" rule have a 0 offset so that turning off DST returns you to winter time.
// The summer offset should be relative to winter time (probably +60 minutes).
#if 1
// US rules
TimeChangeRule summer = { "DST", Second, Sun, Mar, 2, 60 };
TimeChangeRule winter = { "ST", First, Sun, Nov, 2, 0 };
#endif
#if 0
// European Union
TimeChangeRule summer = { "DST", Last, Sun, Mar, 1, 60 };
TimeChangeRule winter = { "ST", Last, Sun, Oct, 1, 0 };
#endif
#if 0
// Australia - note the reversal due to the Southern hemisphere
TimeChangeRule summer = { "DST", First, Sun, Oct, 2, 60 };
TimeChangeRule winter = { "ST", First, Sun, Apr, 2, 0 };
#endif
Timezone dst(summer, winter);

// Time zone rules.
// Use US_DST_RULES macro for US, EU_... for EU, and AU_... for Australia.
US_DST_RULES(dstr);
//EU_DST_RULES(dstr);
//AU_DST_RULES(dstr);

// Thanks to Gareth Evans at http://todbot.com/blog/2008/06/19/how-to-do-big-strings-in-arduino/
// Note that you must be careful not to use this macro more than once per "statement", lest you
Expand Down Expand Up @@ -1184,7 +1167,7 @@ void doClockMenu(boolean initialize)
// The underlying system clock is always winter time.
// Note that setting the time during the repeated hour in
// the fall will assume winter time - the hour will NOT repeat.
if (enable_dst) toSet = dst.toUTC(toSet);
// if (enable_dst) toSet = dst.toUTC(toSet);
setTime(toSet);
RTC.set(toSet);
doMenuFunc = doMenu;
Expand Down
56 changes: 56 additions & 0 deletions Hydra_EVSE/dst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
J1772 Hydra (EVSE variant) for Arduino
Copyright 2014 Nicholas W. Sayer, Dmitriy Lyubimov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "dst.h"

boolean isSummer(DSTRule* rules , time_t t) {

// The algorithm here is simple. in he current year,
// if we are past the second rule, then we take that rule.
// otherwise, if we are past the first rule, we take that rule.
// otherwise, we take second rule.
for (int i = 1; i >= 0; i--) {
if ( rules[i] < t) return rules[i].dst;
}
return rules[1].dst;
}

time_t toDST(DSTRule *rules, time_t t) {
return isSummer(rules, t)? t + 3600 : t;

}


boolean DSTRule::operator<(time_t& that) {

tmElements_t thatEls;
breakTime (that, thatEls);

// try to break based on the month.
if (month < thatEls.Month) return true;
else if ( month > thatEls.Month) return false;

// same month -- we will have to figure the day of week of the rule.
// TODO to be contd.


}


70 changes: 70 additions & 0 deletions Hydra_EVSE/dst.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
J1772 Hydra (EVSE variant) for Arduino
Copyright 2014 Nicholas W. Sayer, Dmitriy Lyubimov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef ___DST_H___
#define ___DST_H___

// Standard Arduino types like boolean
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <Time.h>

enum week_t {First, Second, Third, Fourth, Last};
enum dow_t {Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat};
enum month_t {Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
enum dst_t {summer, winter};

//structure to describe rules for when daylight/summer time begins,
//or when standard time begins.
struct DSTRule
{
dst_t dst; // Whether the rule switches to summer or winter time
uint8_t week; // First, Second, Third, Fourth, or Last week of the month
uint8_t dow; // day of week, here and on per Time.h
uint8_t month;
uint8_t hour;

boolean operator<(time_t& that);
};
// Exactly 2 rules are expected for the rules argument, in calendar succession.
extern boolean isSummer(DSTRule* rules , time_t);
extern time_t toDST(DSTRule *rules, time_t);

// We don't really need to support timezones. We just want to perform automatic DST switching.
// The following are the U.S. DST rules. If you live elsewhere, the customize these. The params
// are a descriptive string (not used, but must be 5 chars or less), a "descriptor" for which
// weekday in the active month is in the rule (first, second... last), the day of the week,
// the month, and the hour of the day. The last parameter is the offset in minutes. You should
// have the "winter" rule have a 0 offset so that turning off DST returns you to winter time.
// The summer offset should be relative to winter time (probably +60 minutes).

// US
#define US_DST_RULES(name) DSTRule name[] = {{ summer, Second, Sun, Mar, 2 }, { winter, First, Sun, Nov, 2 }}
// Europe
#define EU_DST_RULES(name) DSTRule name[] = {{ summer, Last, Sun, Mar, 1 }, { winter, Last, Sun, Oct, 1 }}
// Australia - note the reversal due to the Southern hemisphere
#define AU_DST_RULES(name) DSTRule name[] = {{ winter, First, Sun, Apr, 2 }, { summer, First, Sun, Oct, 2 }}


#endif // ___DST_H___

0 comments on commit dcd2174

Please sign in to comment.