summaryrefslogtreecommitdiff
path: root/src/binwatch/binwatch.c
blob: 049c8b3c5dc84b07b917d5cd0e6ec030bb998e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* 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: <nico at ni.fr.eu.org>
 * }}} */
#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 ();
	      }
	  }
      }
}