/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * settings_major_mode.cc
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 *
 * This file is part of propeller-clock (hereafter referred to as "this
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 * information.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "Arduino.h"
#include "settings_major_mode.h"
#include "text.h"
#include "text_renderer.h"
#include "time.h"
#include "common.h"


#define FONT_SET_IDX 0
#define TIME_SET_IDX 1
#define DATE_SET_IDX 2


SettingsMajorMode::SettingsMajorMode( Button &button )
	:
	_button( button )
{
}


void SettingsMajorMode::draw( int segment )
{
	Text::draw( 0, segment );
}


void SettingsMajorMode::draw_reset()
{
	bool flash = ( ::millis() % 1000 ) > 600;
	PString str0( Text::_messages[ 0 ], MESSAGE_LEN * 2 );
	str0.begin();

	switch( _item ) {
	case TIME_SET_IDX:
		if( _part == 0 && flash )
			str0.print( ( Time::get_hours() % 12 ) < 10? " " : "  " );
		else
			str0.format( "%d", Time::get_hours() % 12 );
		str0.print( ':' );
		if( _part == 1 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_minutes() );
		str0.print( ':' );
		if( _part == 2 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_seconds() );
		str0.print( ' ' );
		if( _part == 0 && flash )
			str0.print( "  " );
		else
			str0.print( Time::get_hours() > 11? "pm" : "am" );
		break;

	case DATE_SET_IDX:
		if( _part == 0 && flash )
			str0.print( "    " );
		else
			str0.format( "%04d", Time::get_year() );
		str0.print( '-' );
		if( _part == 1 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_month() );
		str0.print( '-' );
		if( _part == 2 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_day() );
		break;

	case FONT_SET_IDX:
		str0.print( "font " );
		if( flash )
			str0.print( ' ' );
		else
			str0.format( "%d", TextRenderer::get_font() );
		break;
	}

	Text::set_message( 0, str0 );
}


void SettingsMajorMode::activate()
{
	_item = _part = 0;

	reset_messages();

	_button.set_press_mode( false );
}


void SettingsMajorMode::deactivate()
{
	_button.set_press_mode( true );

	// save time

}


void SettingsMajorMode::press()
{
	switch( _item )
	{
	case TIME_SET_IDX:
		switch( _part ) {
		case 0: Time::inc_hours(); break;
		case 1: Time::inc_minutes(); break;
		case 2: Time::reset_seconds(); break;
		}
		break;
	case DATE_SET_IDX:
		switch( _part ) {
		case 0: Time::inc_year(); break;
		case 1: Time::inc_month(); break;
		case 2: Time::inc_day(); break;
		}
		break;
	case FONT_SET_IDX:
		TextRenderer::inc_font();
		break;
	}
}


void SettingsMajorMode::long_press()
{
	// how many parts does this item have?
	int max = 1;
	switch( _item ) {
	case DATE_SET_IDX: // fall through
	case TIME_SET_IDX: max = 3; break;
	}

	// inc part and item
	if( ++_part >= max ) {
		_part = 0;
		if( ++_item >= 3 )
			_item = 0;
	}

	reset_messages();
}


void SettingsMajorMode::reset_messages()
{
	Text::reset_message( 0, Text::MODE_TOP | Text::MODE_ALL );
}