Mercurial > ~darius > hgwebdir.cgi > avr-lib
comparison ds1307.c @ 19:b5e4591b6570
- Satisfy new compiler
- Put strings in PROGMEM
- Add various time functions
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 31 Jan 2015 23:27:10 +1030 |
parents | 119688bb743f |
children |
comparison
equal
deleted
inserted
replaced
18:0876867347de | 19:b5e4591b6570 |
---|---|
472 | 472 |
473 return(1); | 473 return(1); |
474 } | 474 } |
475 | 475 |
476 /* | 476 /* |
477 * ds1307_printtime | 477 * ds1307_printnow |
478 * | 478 * |
479 * Print the time in rtime with trailer | 479 * Print the current time of day |
480 * | 480 * |
481 */ | 481 */ |
482 void | 482 void |
483 ds1307_printtime(char *leader, char *trailer) { | 483 ds1307_printnow(const char *leader, const char *trailer) { |
484 ds1307raw_t rtime; | 484 ds1307raw_t rtime; |
485 uint8_t hour; | 485 ds1307_t time; |
486 | 486 |
487 if (ds1307_gettod(&rtime) != 1) | 487 if (ds1307_gettod(&rtime) != 1) |
488 return; | 488 return; |
489 | 489 |
490 // Handle 12/24 hour time | 490 ds1307_cook(&rtime, &time); |
491 hour = rtime.split.hour10 * 10 + rtime.split.hour; | 491 ds1307_printtime(&time, leader, trailer); |
492 if (rtime.split.s1224) { | 492 } |
493 if (rtime.split.pmam) | 493 |
494 hour += 12; | 494 /* |
495 * ds1307_printtime | |
496 * | |
497 * Print time with header & trailer | |
498 * | |
499 */ | |
500 void | |
501 ds1307_printtime(ds1307_t *time, const char *leader, const char *trailer) { | |
502 printf_P(PSTR("%S%04d/%02d/%02d %02d:%02d:%02d%S"), leader, | |
503 1900 + time->year, time->month, time->day, | |
504 time->hour, time->min, time->sec, trailer); | |
505 } | |
506 | |
507 /* | |
508 * ds1307_cook | |
509 * | |
510 * Convert a ds1307raw_t to ds1307_t | |
511 */ | |
512 void | |
513 ds1307_cook(ds1307raw_t *raw, ds1307_t *cooked) { | |
514 cooked->year = raw->split.year + raw->split.year10 * 10; | |
515 cooked->month = raw->split.month + raw->split.month10 * 10; | |
516 cooked->day = raw->split.day + raw->split.day10 * 10; | |
517 | |
518 /* Handle 12/24 hour time */ | |
519 cooked->hour = raw->split.hour10 * 10 + raw->split.hour; | |
520 if (raw->split.s1224) { | |
521 if (raw->split.pmam) | |
522 cooked->hour += 12; | |
495 } else | 523 } else |
496 hour += (rtime.split.pmam << 1) * 10; | 524 cooked->hour += (raw->split.pmam << 1) * 10; |
497 | 525 |
498 printf_P(PSTR("%S%04d/%02d/%02d %02d:%02d:%02d%S"), leader, | 526 cooked->min = raw->split.min + raw->split.min10 * 10; |
499 1900 + rtime.split.year10 * 10 + rtime.split.year, | 527 cooked->sec = raw->split.sec + raw->split.sec10 * 10; |
500 rtime.split.month10 * 10 + rtime.split.month, | 528 } |
501 rtime.split.day10 * 10 + rtime.split.day, | 529 |
502 hour, | 530 /* |
503 rtime.split.min10 * 10 + rtime.split.min, | 531 * ds1307_totimet |
504 rtime.split.sec10 * 10 + rtime.split.sec, | 532 * |
505 trailer); | 533 * Convert a ds1307_t to time_t |
506 } | 534 * |
535 */ | |
536 #define SECONDS_PER_DAY 86400 | |
537 static uint8_t dayspermonth[] = { 31, 27, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
538 static uint8_t dayspermonth_leap[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
539 | |
540 static int | |
541 isleapyear(int year) { | |
542 return ((!(year % 4) && (year % 100)) || !(year % 400)) ? 1 : 0; | |
543 } | |
544 | |
545 time_t | |
546 ds1307_totimet(ds1307_t *time) { | |
547 time_t t = 0; | |
548 int16_t i; | |
549 | |
550 for (i = time->year + 1900; i > 1970; i--) | |
551 t += (isleapyear(i) ? 366 : 365) * SECONDS_PER_DAY; | |
552 for (i = time->month; i > 1; i--) | |
553 t += (isleapyear(i) ? dayspermonth_leap[i] : dayspermonth[i]) * SECONDS_PER_DAY; | |
554 t += (time->day - 1) * SECONDS_PER_DAY; | |
555 t += time->hour * 60 * 60; | |
556 t += time->min * 60; | |
557 t += time->sec; | |
558 t += 65535; // XXX: this is magical :( | |
559 return(t); | |
560 } | |
561 | |
562 /* | |
563 * ds1307_time | |
564 * | |
565 * Return TOD as a time_t | |
566 * | |
567 */ | |
568 time_t | |
569 ds1307_time(void) { | |
570 ds1307raw_t r; | |
571 ds1307_t c; | |
572 | |
573 if (ds1307_gettod(&r) == 0) | |
574 return -1; | |
575 | |
576 ds1307_cook(&r, &c); | |
577 return ds1307_totimet(&c); | |
578 } |