Ice Tube with GPS and DST mods

Tick Tock Clock Kits

Moderators: adafruit_support_bill, adafruit

Ice Tube with GPS and DST mods

Postby wbp » Tue Apr 19, 2011 1:22 pm

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
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby adafruit_support_bill » Tue Apr 19, 2011 8:47 pm

Thanks, William. That sounds like a worthwhile improvement. :D
User avatar
adafruit_support_bill
 
Posts: 15962
Joined: Sat Feb 07, 2009 9:11 am

Re: Ice Tube with GPS and DST mods

Postby wbp » Thu Apr 21, 2011 1:04 am

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 year

long 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;
}
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby wbp » Thu Apr 21, 2011 1:29 am

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
}
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby wbp » Thu Apr 21, 2011 2:01 am

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;
   }
}
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby adafruit_support_bill » Thu Apr 21, 2011 5:09 am

Now all you need to do is use the GPS data to figure out what the local DST schedule is (if any) :mrgreen:
User avatar
adafruit_support_bill
 
Posts: 15962
Joined: Sat Feb 07, 2009 9:11 am

Re: Ice Tube with GPS and DST mods

Postby wbp » Thu Apr 21, 2011 10:41 am

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...
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby Len17 » Thu Apr 21, 2011 7:58 pm

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. :(
User avatar
Len17
 
Posts: 393
Joined: Sat Mar 14, 2009 6:20 pm

Re: Ice Tube with GPS and DST mods

Postby wbp » Thu Apr 21, 2011 9:10 pm

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
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm

Re: Ice Tube with GPS and DST mods

Postby Len17 » Fri Apr 22, 2011 1:07 pm

Yeah, that would be more convenient than recompiling the firmware, for most people. :)
User avatar
Len17
 
Posts: 393
Joined: Sat Mar 14, 2009 6:20 pm

Re: Ice Tube with GPS and DST mods

Postby wbp » Wed Apr 27, 2011 2:41 am

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
User avatar
wbp
 
Posts: 149
Joined: Mon Mar 07, 2011 12:18 pm


Return to Clocks

Who is online

Users browsing this forum: No registered users and 0 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [100]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]