/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
1
/*
87 by edam
switched back to using classes for modes
2
 * settings_major_mode.cc
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
3
 *
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
5
 *
6
 * This file is part of propeller-clock (hereafter referred to as "this
7
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
8
 * information.
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Lesser General Public License as published
12
 * by the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
#include "Arduino.h"
87 by edam
switched back to using classes for modes
24
#include "settings_major_mode.h"
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
25
#include "text.h"
26
#include "text_renderer.h"
27
#include "time.h"
28
#include "common.h"
29
30
86 by Tim Marston
various tweaks, a (failed) attempt to fix text reset bug and added TODO
31
#define FONT_SET_IDX 0
32
#define TIME_SET_IDX 1
33
#define DATE_SET_IDX 2
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
34
35
89 by Tim Marston
moved rount Time a bit, and passed _button to SettingsMajorMode so that it can
36
SettingsMajorMode::SettingsMajorMode( Button &button )
37
	:
38
	_button( button )
39
{
40
}
41
42
87 by edam
switched back to using classes for modes
43
void SettingsMajorMode::draw( int segment )
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
44
{
45
	Text::draw( 0, segment );
46
}
47
48
87 by edam
switched back to using classes for modes
49
void SettingsMajorMode::draw_reset()
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
50
{
51
	bool flash = ( ::millis() % 1000 ) > 600;
52
	PString str0( Text::_messages[ 0 ], MESSAGE_LEN * 2 );
53
	str0.begin();
54
55
	switch( _item ) {
56
	case TIME_SET_IDX:
57
		if( _part == 0 && flash )
75 by Tim Marston
fixed time centring and display in settings mode
58
			str0.print( ( Time::get_hours() % 12 ) < 10? " " : "  " );
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
59
		else
75 by Tim Marston
fixed time centring and display in settings mode
60
			str0.format( "%d", Time::get_hours() % 12 );
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
61
		str0.print( ':' );
62
		if( _part == 1 && flash )
63
			str0.print( "  " );
64
		else
65
			str0.format( "%02d", Time::get_minutes() );
66
		str0.print( ':' );
67
		if( _part == 2 && flash )
68
			str0.print( "  " );
69
		else
70
			str0.format( "%02d", Time::get_seconds() );
75 by Tim Marston
fixed time centring and display in settings mode
71
		str0.print( ' ' );
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
72
		if( _part == 0 && flash )
73
			str0.print( "  " );
74
		else
75
			str0.print( Time::get_hours() > 11? "pm" : "am" );
76
		break;
77
78
	case DATE_SET_IDX:
79
		if( _part == 0 && flash )
80
			str0.print( "    " );
81
		else
82
			str0.format( "%04d", Time::get_year() );
83
		str0.print( '-' );
84
		if( _part == 1 && flash )
85
			str0.print( "  " );
86
		else
87
			str0.format( "%02d", Time::get_month() );
88
		str0.print( '-' );
89
		if( _part == 2 && flash )
90
			str0.print( "  " );
91
		else
92
			str0.format( "%02d", Time::get_day() );
93
		break;
94
95
	case FONT_SET_IDX:
96
		str0.print( "font " );
97
		if( flash )
98
			str0.print( ' ' );
99
		else
100
			str0.format( "%d", TextRenderer::get_font() );
101
		break;
102
	}
103
104
	Text::set_message( 0, str0 );
105
}
106
107
87 by edam
switched back to using classes for modes
108
void SettingsMajorMode::activate()
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
109
{
110
	_item = _part = 0;
111
87 by edam
switched back to using classes for modes
112
	reset_messages();
89 by Tim Marston
moved rount Time a bit, and passed _button to SettingsMajorMode so that it can
113
114
	_button.set_press_mode( false );
87 by edam
switched back to using classes for modes
115
}
116
117
118
void SettingsMajorMode::deactivate()
119
{
89 by Tim Marston
moved rount Time a bit, and passed _button to SettingsMajorMode so that it can
120
	_button.set_press_mode( true );
121
122
	// save time
123
87 by edam
switched back to using classes for modes
124
}
125
126
127
void SettingsMajorMode::press()
128
{
129
	switch( _item )
130
	{
131
	case TIME_SET_IDX:
132
		switch( _part ) {
133
		case 0: Time::inc_hours(); break;
134
		case 1: Time::inc_minutes(); break;
135
		case 2: Time::reset_seconds(); break;
136
		}
137
		break;
138
	case DATE_SET_IDX:
139
		switch( _part ) {
140
		case 0: Time::inc_year(); break;
141
		case 1: Time::inc_month(); break;
142
		case 2: Time::inc_day(); break;
143
		}
144
		break;
145
	case FONT_SET_IDX:
146
		TextRenderer::inc_font();
147
		break;
148
	}
149
}
150
151
152
void SettingsMajorMode::long_press()
153
{
154
	// how many parts does this item have?
155
	int max = 1;
156
	switch( _item ) {
157
	case DATE_SET_IDX: // fall through
158
	case TIME_SET_IDX: max = 3; break;
159
	}
160
161
	// inc part and item
162
	if( ++_part >= max ) {
163
		_part = 0;
164
		if( ++_item >= 3 )
165
			_item = 0;
166
	}
167
168
	reset_messages();
169
}
170
171
172
void SettingsMajorMode::reset_messages()
173
{
174
	Text::reset_message( 0, Text::MODE_TOP | Text::MODE_ALL );
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
175
}