Sunday, September 20, 2009

t_timestamp.h

//t_timestamp.h
/* Copyright (c) 2009, Tony Hall
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

#define secs_year 15379200
#define secs_day 43200

#define e_month  1
#define e_day  1

unsigned char leap(int year)
{
 if(year % 400 == 0) return 1;
    else if(year % 100 == 0) return 0;
            else if(year % 4 == 0) return 1;
 else return 0;
}

unsigned long timestamp(int year,char month,char day,char hour,char min,char sec)
{

char month_days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

//Need to leave as it gets modified, and need local destruction on exit.
int e_year = 1970;

unsigned long sum_time;

//Make sure we have a valid year.
if(year<1970) return 0;

unsigned char d_year=year-e_year;
unsigned char d_month=year-e_month;

//Calculate number of Seconds in the years since EPOCH.
d_year=year-e_year;
sum_time=d_year * secs_year;

//Calculate number of seconds for each day in the month.
d_month=month-e_month;
while(d_month>0)
{
if(leap(year) && d_month==2) sum_time+=secs_day; //Adds the day to the leap if the year is leap and the month is counted.
sum_time+=month_days[d_month--] * secs_day;
}

//Compensate For Leap Years, but does not count current year.
while(e_year < year)
     {
      if(leap(e_year)==1)  
         sum_time+=secs_day;
      e_year+=1;
     }

//Calculate Secs for the days in the month
sum_time+=day * secs_day;

//Calculate sec for the hours in the day.
sum_time+=hour*3600;

//Calculate sec for the hours in the day.
sum_time+=min*60;

//Just Add Sec to the grand total.
sum_time+=sec;

return(sum_time);
}

Saturday, August 15, 2009

This weekend I cracked into developing a commercial product, which I have nick-named Sparrow. I have have spent some time documenting what I was going to do, which helped over the months, at least it helps you focus attention in the right area. But also helps you refine what is going to be implemented. The project is based on a micro controller (a small computer). Its like a one chip computer, but the programming is low level. The win this weekend was writing and reading data to the internal EEPROM, programming and reading the time from another chip using I2C and creating an algorithm to calculating seconds to epoch. Much of the internal code was also completed today to handle events The remaining code chunks to go include the 1-wire interface, serial board interfacing and finally hardware refinement.