/* binwatch.c */ /* binwatch - Tiny binary wristwatch. {{{ * * Copyright (C) 2010 Nicolas Schodet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contact : * Web: http://ni.fr.eu.org/ * Email: * }}} */ #include "io.h" #include "common/led.h" #include "common/rtc.h" #include "common/button.h" #include "common/power.h" #define BINWATCH_TIME_DISPLAY_DUR (3 * LED_1S) #define BINWATCH_ANIM_SPEED (LED_100MS / 2) #define BINWATCH_UI_SPEED (LED_1S / 2) /** Format time in leds format. */ static uint16_t binwatch_format_time (uint8_t hour, uint8_t minute) { hour = hour % 12; if (hour == 0) hour = 12; return hour << 6 | minute; } /** Get time in leds format. */ static uint16_t binwatch_get_time (void) { uint8_t hour, minute, second; rtc_get_time (&hour, &minute, &second); return binwatch_format_time (hour, minute); } /** User interface to get an integer between a and b (included). */ static uint16_t binwatch_get_int_ui (uint16_t a, uint16_t b, uint16_t step, uint16_t always_on) { uint16_t input = a; while (1) { led_display (input | always_on, BINWATCH_UI_SPEED); if (button_pressed ()) { /* If button is pressed, user selected this integer. */ button_wait (); return input; } else { /* Else, propose the next integer. */ input += step; if (input > b) input = a; } } } /** User interface to set current time. */ static void binwatch_set_time_ui (void) { /* Input hour and minutes. */ uint8_t hour = binwatch_get_int_ui (0, 23, 1, 0x200); uint8_t minute = binwatch_get_int_ui (0, 59, 1, 0x100); /* Display entered time and wait button press. */ uint16_t time = binwatch_format_time (hour, minute); led_display (time, BINWATCH_TIME_DISPLAY_DUR); button_wait (); /* Now change time in RTC. */ rtc_stop (); rtc_set_time (hour, minute, 0); rtc_go (); /* Display time to acknowledge. */ led_display (time, BINWATCH_TIME_DISPLAY_DUR); } int main (void) { /* Initialise hardware. */ power_init (); led_init (); button_init (); /* Initialise RTC. */ uint8_t init; init = rtc_init (); if (!init) { /* Initialisation failed, blink! */ while (1) { led_display (0x3f, LED_100MS); led_display (0, 9 * LED_100MS); /* Sleep, do not eat all battery. */ button_wait (); } } else { /* Initialisation ok, display current time. */ uint16_t time; time = binwatch_get_time (); led_display (time, BINWATCH_TIME_DISPLAY_DUR); /* If low voltage detected, wait user press to let clock go. */ if (init == RTC_LOW_VOLTAGE) { button_wait (); rtc_go (); led_display (time, BINWATCH_TIME_DISPLAY_DUR); } /* Wait user press to display time. */ while (1) { uint16_t press_ms = button_wait (); if (press_ms < 5000) { time = binwatch_get_time (); led_animate (time, LED_ANIMATION_TRACE_CCW, BINWATCH_ANIM_SPEED); led_display (time, BINWATCH_TIME_DISPLAY_DUR); } else { binwatch_set_time_ui (); } } } }