Ice Tube with GPS and DST mods

Tick Tock Clock Kits

Ice Tube with GPS and DST mods

I love the Ice Tube clock, and have been experimenting with the different firmware versions available. I added the \$40 GPS module and that works great, but I also wanted automatic DST adjustment... That's when it got interesting.

In looking at the DST mod, I was surprised to find that it will really only work if the clock is running when the transitions are crossed. If you happen to reboot your clock, as you would do when making firmware mods, and the current date is outside of the DST start "window", it won't pick it up. Has no one else noticed this?

So... I've rewritten the DST code so it can be run at any time. I need to do some testing but once I've checked it if anyone else is interested out I'll post it here.

William

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Thanks, William. That sounds like a worthwhile improvement.

Posts: 16027
Joined: Sat Feb 07, 2009 9:11 am

Re: Ice Tube with GPS and DST mods

I decided to use the number of seconds since the beginning of the year for my DST calculations. I started out wanting to use Julian day number but then learned that the at168's float doesn't have enough precision - it would work but there would be some odd delays at times.

This is the "yearSeconds" sub (this is pretty basic, there is nothing really new here):
Code: Select all
`static const uint16_t monthDays[]={0,31,59,90,120,151,181,212,243,273,304,334}; // Number of days at the beginning of the month if not leap yearlong yearSeconds(uint8_t yr, uint8_t mo, uint8_t da, uint8_t h, uint8_t m, uint8_t s){  long secs;  secs = monthDays[(mo-1)]+da;  // # days so far if not leap year  if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)  // if leap year    secs ++;  // add 1 day  secs = secs * 86400 + h*3600 + m*60 + s;  return secs;} `

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

using the yearSeconds function, one can then calculate the time of DST start and DST end. I took the DST code from caitsith2 as a staring point, but modified the rule structure a bit. so we have something like this:
Code: Select all
`  #ifdef DST_USA    if(dst_on == DST_USA)  //Check daylight saving time.   setDSToffset(3,1,2,2,11,1,1,2,1);  // USA DST starts second sunday in march at 2am, ends first sunday in november at 2am  #endif`

The parameters are: month, day of the week (1=sunday), which occurrence (5 for "last"), and hour, repeated for DST end, and time offset in hours.

The function to compute the time of a DST event given these "rules" is:
Code: Select all
`long DSTseconds(uint8_t month, uint8_t doftw, uint8_t n, uint8_t hour){   uint8_t dom = monthDays[month-1];   if ( (date_y %4 == 0) && (month == 2) )      dom ++;  // february has 29 days this year   uint8_t dw = dotw(date_y, month, 1);  // DOW for 1st day of month for DST event   uint8_t day = doftw - dw;  // number of days until 1st dotw in given month   if (day<1)  day += 7;  // make sure it's positive   day = 1 + day + (n-1)*7;  // date of dotw for this year   while (day > dom)  // handles "last DOW" case      day -= 7;  return yearSeconds(date_y,month,day,hour,0,0);  // seconds til DST event this year}`

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

To put it all together - this sub calculates DST start and end times, then checks to see if the current time is within that window:

Code: Select all
`void setDSToffset(uint8_t month1, uint8_t dotw1, uint8_t n1, uint8_t hour1, uint8_t month2, uint8_t dotw2, uint8_t n2, uint8_t hour2, uint8_t offset){  // if current time & date is at or past the first DST rule and before the second, set dst_offset, otherwise reset dst_offset  long seconds1 = DSTseconds(month1,dotw1, n1, hour1);  // seconds til start of DST this year  long seconds2 = DSTseconds(month2,dotw2, n2, hour2);  // seconds til end of DST this year  long seconds_now = yearSeconds(date_y, date_m, date_d, time_h, time_m, time_s);   if ((seconds_now >= seconds1) && (seconds_now < seconds2))   {  // spring ahead      if (dst_offset == 0)  time_h += offset;  // if this is the first time, bump the hour      dst_offset = offset;   }   else   {  // fall back      if (dst_offset >0 )  time_h -= offset;  // if this is the first time, bump the hour      dst_offset = 0;   }}`

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Now all you need to do is use the GPS data to figure out what the local DST schedule is (if any)

Posts: 16027
Joined: Sat Feb 07, 2009 9:11 am

Re: Ice Tube with GPS and DST mods

Seems like we should be looking up the time zone from the GPS coordinates first. Usually this is done by just looking at the longitude, that would be simple enough.
How about a lookup table with geographic regions, time zones, and DST rules?
I was planning on looking at ramping the volume for the alarm next though...

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

wbp wrote:Seems like we should be looking up the time zone from the GPS coordinates first. Usually this is done by just looking at the longitude, that would be simple enough.
How about a lookup table with geographic regions, time zones, and DST rules?

Not to rain on your parade, but I'm not sure it's worthwhile to try to figure out all the time zones. The latest release of the "official" time zone database has 410 different time zones, and it changes pretty often - 6 times so far this year. Time zones follow political boundaries more than lines of longitude so it's not so simple to figure out which one you're in. Even Apple gets this stuff wrong.

If it were me I'd just compile in the current DST settings for my time zone and hope they don't change again this year.

Len17

Posts: 393
Joined: Sat Mar 14, 2009 6:20 pm

Re: Ice Tube with GPS and DST mods

You're not raining on my parade - I have no plans to implement such a thing anyway! I really just wanted to fix the DST code so it worked all the time, and I've done that.

I am considering exposing the DST rules in the settings menu, which would not be all that hard to do - with that and the Zone setting people can configure the clock once for where they live and be done with it.

William

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Yeah, that would be more convenient than recompiling the firmware, for most people.

Len17

Posts: 393
Joined: Sat Mar 14, 2009 6:20 pm

Re: Ice Tube with GPS and DST mods

OK, I've got all my mods working now. GPS + DST, with DST rules settable in the menu, and DST offset re-calculated once a minute. I also put the low and high values used for the automatic brightness calculation in the menu so you can play with them if you like.

Anybody care to do some beta testing?

William

wbp

Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm