Пример #1
0
bool SetTimeHelper::retrieveCompileTime() {
    int Hour, Min, Sec;
    if (sscanf(__TIME__, "%d:%d:%d", &Hour, &Min, &Sec) != 3)
        return false;

    compiledAt.Hour = Hour;
    compiledAt.Minute = Min;
    compiledAt.Second = Sec;

    char Month[12];
    int Day, Year;
    uint8_t monthIndex;

    if (sscanf(__DATE__, "%s %d %d", Month, &Day, &Year) != 3)
        return false;

    for (monthIndex = 0; monthIndex < 12; monthIndex++) {
        if (strcmp(Month, monthNames[monthIndex]) == 0)
            break;
    }

    if (monthIndex >= 12)
        return false;
    compiledAt.Day = Day;
    compiledAt.Month = monthIndex + 1;
    compiledAt.Year = CalendarYrToTm(Year);

    return true;
}
Пример #2
0
void PCF85xx::read(tmElementsWithMillis &tm) {
	this->wire.beginTransmission(this->READ_ADDR);
	this->wire.write(this->HUNDRETH_SEC_REG);
	this->wire.endTransmission();
	TIME time;

	this->wire.requestFrom(this->READ_ADDR, sizeof(time));
	this->wire.readBytes((char*) &time, sizeof(time));

	tm.Second = time.seconds.ten * 10L + time.seconds.unit;
	tm.Minute = time.minutes.ten * 10L + time.minutes.unit;
	//for hour the two msb are The "12 Hour flag" (bit 7)
	//  and the subordinant "pm flag" (bit 6)
	tm.Hour = time.hours.hour_t * 10L + time.hours.hour_u;
	tm.Day = time.day_year.day_t;
	tm.Day *= (uint8_t) 10;
	tm.Day += (uint8_t) time.day_year.day_u;

	uint16_t eeprom_year = year(this->load() / 1000);
	uint16_t year = eeprom_year + (uint16_t) (time.day_year.year_off);
	tm.Year = CalendarYrToTm(year);

	tm.Wday = time.wday_month.dow;
	tm.Month = time.wday_month.month_t * 10L + time.wday_month.month_u;

	tm.Milliseconds = 10
			* ((uint16_t) (time.hundredths.ten * 10 + time.hundredths.unit));
	//update eeprom if year_off > 0 so next time year_off=0
	// ensuring we're never overflow 3 bits.
	if (time.day_year.year_off > 0) {
		this->save(makeTimeMilli(tm));
		time.day_year.year_off = 0;
		this->writeByte(this->YEAR_REG, this->to_uint8(&time.day_year));
	}
}
Пример #3
0
bool getDate(const char *str) {
    char Month[12];
    int Day, Year;
    uint8_t monthIndex;

    if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
    for (monthIndex = 0; monthIndex < 12; monthIndex++) {
        if (strcmp(Month, monthName[monthIndex]) == 0) break;
    }
    if (monthIndex >= 12) return false;
    tm.Day = Day;
    tm.Month = monthIndex + 1;
    tm.Year = CalendarYrToTm(Year);
    return true;
}
Пример #4
0
/* = Parsers
--------------------------------------------------------------*/
boolean TimeKeeper::parseDateFromString(const char *str, tmElements_t tm){
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}
Пример #5
0
void PCF85xx::reset() {
	this->setup();
	this->clearSave();
	tmElements_t tm;
	tm.Year = CalendarYrToTm(2011);
	tm.Month = 12;
	tm.Day = 31;
	tm.Wday = 6;
	tm.Hour = 23;
	tm.Minute = 59;
	tm.Second = 59;

	this->write(tm);

}
Пример #6
0
void Clock::set(int y,int m,int d,int h,int n,int s)
{
  tmElements_t tm;
  time_t t;
  
  tm.Hour = h;  
  tm.Minute = n;
  tm.Second = s;
  tm.Month = m;
  tm.Day = d;
  tm.Year = CalendarYrToTm(y); //TM.Year is a byte so can't take year > 255
  t = makeTime(tm);
  
  this->_rtc->set(t);   // set the RTC and the system time to the received value
  setTime(t); 
}
Пример #7
0
bool SensorDs1307::getDate(const char *str, tmElements_t *tm) {
  const char *monthName[12] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm->Day = Day;
  tm->Month = monthIndex + 1;
  tm->Year = CalendarYrToTm(Year);
  return true;
}
Пример #8
0
enum ab1815_status ab1815_get_time(struct ab1815_t *clock, struct tmElements_t *time)
{
			enum ab1815_status to_ret = ab1815_status_ERROR;
			size_t length = (AB1815_REG_ALARM_HUNDREDTHS - AB1815_REG_TIME_HUNDREDTHS) ;
			uint8_t buffer[length];
			memset(buffer, 0, length);
			if(ab1815_read(clock, AB1815_REG_TIME_HUNDREDTHS, buffer, length) == ab1815_status_OK)
			{
				
				to_ret = ab1815_status_OK;
				time->Hundredth = bcd2bin(buffer[0]);
				time->Second = bcd2bin(0x7F & buffer[1]);
				time->Minute = bcd2bin(0x7F & buffer[2]);
				time->Hour = bcd2bin(0x3F & buffer[3]);
				time->Day = bcd2bin(0x3F & buffer[4]);
				time->Month = bcd2bin(0x1F & buffer[5]);
				time->Year = CalendarYrToTm(bcd2bin(buffer[6]) + 2000);
				time->Wday = bcd2bin(0x07 & buffer[7]);
			}
			return to_ret;		
}
Пример #9
0
//run the time setting state machine. must be called frequently while in set mode.
//returns true when setting is complete or has timed out.
bool GoldieClock::setClock(void)
{
    static setStates_t SET_STATE;
    static time_t utc, local;
    static uint8_t newTzIndex;
    static int y, mth, d, h, m, maxDays;
    static uint16_t pixel;
    static uint32_t rpt;
    bool retval = false;

    //see if user wants to cancel set mode or if set mode has timed out
    if ( SET_STATE != SET_INIT )
    {
        if ( btnSet.pressedFor(SET_LONGPRESS) )
        {
            SET_STATE = SET_INIT;
            displayClock( getUTC() );
            while ( btnSet.isPressed() ) btnSet.read();    //wait for user to release the button
            return true;
        }
        uint32_t ms = millis();
        if ( (ms - btnSet.lastChange() >= SET_TIMEOUT) && (ms - btnIncr.lastChange() >= SET_TIMEOUT) )
        {
            SET_STATE = SET_INIT;
            return true;
        }
    }

    switch ( SET_STATE )
    {
    case SET_INIT:
        SET_STATE = SET_TZ;
        rpt = REPEAT_FIRST;
        utc = getUTC();
        local = (*tz).toLocal(utc, &tcr);
        newTzIndex = tzIndex;
        displaySet( newTzIndex, WHITE );
        while ( btnSet.isPressed() ) btnSet.read();    //wait for user to release the button
        break;

    case SET_TZ:
        if ( btnSet.wasReleased() )                    //then move on to set year
        {
            SET_STATE = SET_YEAR;
            rpt = REPEAT_FIRST;
            if ( newTzIndex != tzIndex )
            {
                tzIndex = newTzIndex;
                tz = timezones[tzIndex];
                eeprom_write_byte( &ee_tzIndex, tzIndex);
                Serial << F("Time zone changed to ") << tzNames[tzIndex] << endl;
            }
            y = year(local);
            if ( y < 2015 || y > 2074 ) y = 2015;      //year must be in range 2015-2074
            pixel = y - 2000;                          //map to pixel
            if ( pixel > 60 ) pixel -= 60;
            displaySet( pixel, MAGENTA );
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++newTzIndex >= sizeof(tzNames) / sizeof(tzNames[0]) ) newTzIndex = 0;
            displaySet( newTzIndex, WHITE );
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++newTzIndex >= sizeof(tzNames) / sizeof(tzNames[0]) ) newTzIndex = 0;
            displaySet( newTzIndex, WHITE );
        }
        break;

    case SET_YEAR:
        if ( btnSet.wasReleased() )
        {
            SET_STATE = SET_MON;
            rpt = REPEAT_FIRST;
            mth = month(local);
            displaySet( mth, CYAN );
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++pixel > LAST_PIXEL ) pixel = 0;
            displaySet( pixel, MAGENTA );
            y = pixel < 15 ? 2060 + pixel : 2000 + pixel;    //map pixel to year
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++pixel > LAST_PIXEL ) pixel = 0;
            displaySet( pixel, MAGENTA );
            y = pixel < 15 ? 2060 + pixel : 2000 + pixel;    //map pixel to year
        }
        break;

    case SET_MON:
        if ( btnSet.wasReleased() )
        {
            SET_STATE = SET_DAY;
            rpt = REPEAT_FIRST;
            d = day(local);
            maxDays = monthDays[mth-1];                      //number of days in the month
            if (mth == 2 && isLeap(y)) ++maxDays;            //account for leap year
            if ( d > maxDays ) d = maxDays;
            displaySet( d, YELLOW );
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++mth > 12 ) mth = 1;                       //wrap from dec back to jan
            displaySet( mth, CYAN );
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++mth > 12 ) mth = 1;
            displaySet( mth, CYAN );
        }
        break;

    case SET_DAY:
        if ( btnSet.wasReleased() )
        {
            SET_STATE = SET_HOUR;
            rpt = REPEAT_FIRST;
            h = hour(local);
            displaySet( h, RED );
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++d > maxDays ) d = 1;
            displaySet( d, YELLOW );
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++d > maxDays ) d = 1;
            displaySet( d, YELLOW );
        }
        break;

    case SET_HOUR:
        if ( btnSet.wasReleased() )
        {
            SET_STATE = SET_MIN;
            rpt = REPEAT_FIRST;
            m = minute(local);
            displaySet( m, BLUE );
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++h > 23 ) h = 0;
            displaySet( h, RED );
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++h > 23 ) h = 0;
            displaySet( h, RED );
        }
        break;

    case SET_MIN:
        if ( btnSet.wasReleased() )
        {
            SET_STATE = SET_INIT;
            rpt = REPEAT_FIRST;
            tmElements_t tm;
            tm.Year = CalendarYrToTm(y);
            tm.Month = mth;
            tm.Day = d;
            tm.Hour = h;
            tm.Minute = m;
            tm.Second = 0;
            local = makeTime(tm);
            utc = (*tz).toUTC(local);
            RTC.set(utc);
            setUTC(utc);
            Serial << F("\nTime set to:\n");
            printDateTime(utc);
            Serial << F("UTC") << endl;
            printDateTime(local);
            Serial << tcr -> abbrev << endl;
            retval = true;
        }
        else if ( btnIncr.wasReleased() )
        {
            rpt = REPEAT_FIRST;
            if ( ++m > 59 ) m = 0;
            displaySet( m, BLUE );
        }
        else if ( btnIncr.pressedFor(rpt) )
        {
            rpt += REPEAT_INCR;
            if ( ++m > 59 ) m = 0;
            displaySet( m, BLUE );
        }
        break;
    }
    return retval;
}