From f92e654a5e7dc6f6e8bc9543f315ec0e08d82d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sun, 7 May 2017 15:53:20 +0200 Subject: [PATCH] add time adjustment --- main.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index ee1e687..c9f9541 100644 --- a/main.c +++ b/main.c @@ -98,7 +98,6 @@ struct number_t numbers[] = { {0, 5, 4}, }; - #define POWER_SWITCH_PORT B /**< port to switch power of all devices (including this micro-controller) */ #define POWER_SWITCH_PIN 8 /**< pin to switch power of all devices (including this micro-controller) */ #define POWER_BUTTON_PORT B /**< port to detect power switching activity (to keep alive) */ @@ -674,9 +673,9 @@ void main(void) uint16_t buttons_diff = buttons^buttons_new; // get difference buttons = buttons_new; // save new state if (buttons_diff) { // only do something if there is a change - printf("button pressed: %016b\n", buttons); + //printf("button pressed: %016b\n", buttons); for (uint8_t number=0; number=60) { + rtc_ds1307_time[0] -= 60; + rtc_ds1307_time[1] += 1; + } + while (rtc_ds1307_time[1]>=60) { + rtc_ds1307_time[1] -= 60; + rtc_ds1307_time[2] += 1; + } + while (rtc_ds1307_time[2]>=24) { + rtc_ds1307_time[2] -= 24; + } + } + } else if (buttons_diff&(1<<8) && buttons&(1<<8)) { // down pressed + if (!(0==rtc_ds1307_time[2] && 0==rtc_ds1307_time[1] && 0==rtc_ds1307_time[0])) { // don't handle date changes since it's too complex + if (rtc_ds1307_time[0]>0) { + rtc_ds1307_time[0] -= 1; + } else { + rtc_ds1307_time[0] = 59; + if (rtc_ds1307_time[1]>0) { + rtc_ds1307_time[1] -= 1; + } else { + rtc_ds1307_time[1] = 59; + rtc_ds1307_time[2] -= 1; // it can't be 0 + } + } + } + } + rtc_seconds = rtc_ds1307_time[0]; // save seconds since this is only updated every minute + if (!rtc_ds1307_write_time(rtc_ds1307_time[0], rtc_ds1307_time[1], rtc_ds1307_time[2], rtc_ds1307_time[3], rtc_ds1307_time[4], rtc_ds1307_time[5], rtc_ds1307_time[6])) { + printf("could not set time on DS1307: resetting\n"); + rtc_ds1307_setup(); // resetting peripheral + } + } + } } // display time and frame number