/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/text.cc

  • Committer: edam
  • Date: 2012-01-01 14:05:28 UTC
  • Revision ID: edam@waxworlds.org-20120101140528-ldqldo67nil1xf1s
added arduino.mk to the project

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * text.cc
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 "text.h"
24
 
#include "text_renderer.h"
25
 
#include "config.h"
26
 
#include "Arduino.h"
27
 
#include <avr/pgmspace.h>
28
 
 
29
 
 
30
 
// the buffer for the message
31
 
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];
32
 
 
33
 
// message buffer lengths
34
 
static int _message_lens[ NUM_MESSAGE_BUFFERS ];
35
 
 
36
 
// the mode that each message represents
37
 
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
38
 
 
39
 
// mode-specific message parameters
40
 
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
41
 
 
42
 
// time last frame
43
 
static unsigned long _last_millis;
44
 
 
45
 
// duration last frame (in case we can't calculate it this frame!)
46
 
static unsigned long _duration;
47
 
 
48
 
// recalculate message params, based on _duration
49
 
static bool _do_draw_reset;
50
 
 
51
 
 
52
 
void reset_message_param( int message_num )
53
 
{
54
 
        if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
55
 
        {
56
 
                switch( _message_modes[ message_num ] & 0x1c )
57
 
                {
58
 
                case Text::MODE_ONEQUARTER:
59
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 4; break;
60
 
                case Text::MODE_ONETHIRD:
61
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 3; break;
62
 
                case Text::MODE_HALF:
63
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 2; break;
64
 
                case Text::MODE_TWOTHIRDS:
65
 
                        _message_params[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
66
 
                case Text::MODE_THREEQUARTERS:
67
 
                        _message_params[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
68
 
                case Text::MODE_ALL:
69
 
                        _message_params[ message_num ] = -NUM_SEGMENTS; break;
70
 
                }
71
 
        }
72
 
        else
73
 
        {
74
 
                switch( _message_modes[ message_num ] & 0x1c )
75
 
                {
76
 
                case Text::MODE_ONEQUARTER:
77
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 4; break;
78
 
                case Text::MODE_ONETHIRD:
79
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 3; break;
80
 
                case Text::MODE_HALF:
81
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 2; break;
82
 
                case Text::MODE_TWOTHIRDS:
83
 
                        _message_params[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
84
 
                case Text::MODE_THREEQUARTERS:
85
 
                        _message_params[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
86
 
                case Text::MODE_ALL:
87
 
                        _message_params[ message_num ] = NUM_SEGMENTS; break;
88
 
                }
89
 
                _message_params[ message_num ] = -( _message_params[ message_num ] -
90
 
                        TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
91
 
        }
92
 
}
93
 
 
94
 
 
95
 
void Text::set_message( int message_num, PString &pstring )
96
 
{
97
 
        // update message
98
 
        _message_lens[ message_num ] = pstring.length();
99
 
}
100
 
 
101
 
 
102
 
void Text::reset_message( int message_num, char mode )
103
 
{
104
 
        _message_modes[ message_num ] = mode;
105
 
        reset_message_param( message_num );
106
 
}
107
 
 
108
 
 
109
 
void Text::reset()
110
 
{
111
 
        // reset renderer
112
 
        _duration = 0;
113
 
        _last_millis = ::millis();
114
 
        _do_draw_reset = false;
115
 
 
116
 
        // reset the text renderer's output buffer
117
 
        TextRenderer::reset_buffer();
118
 
}
119
 
 
120
 
 
121
 
void Text::draw_reset()
122
 
{
123
 
        // how many milliseconds have elapsed since last frame?
124
 
        unsigned long millis = ::millis();
125
 
        if( millis > _last_millis )
126
 
                _duration = millis - _last_millis;
127
 
        _last_millis = millis;
128
 
 
129
 
        _do_draw_reset = true;
130
 
}
131
 
 
132
 
 
133
 
void Text::draw( int message_num, int segment )
134
 
{
135
 
        // perform a mid-frame reset, as necessary
136
 
        if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
137
 
                _do_draw_reset = false;
138
 
 
139
 
                if( _message_modes[ message_num ] & MODE_HSCROLL )
140
 
                {
141
 
                        // add to horizontal scroll position
142
 
                        _message_params[ message_num ] +=
143
 
                                _duration * TEXT_SCROLL_SPEED / 1000;
144
 
                        long excess = _message_params[ message_num ] -
145
 
                                TextRenderer::get_width( _message_lens[ message_num ] );
146
 
                        if( excess >= 0 ) {
147
 
                                reset_message_param( message_num );
148
 
                                _message_params[ message_num ] += excess;
149
 
                        }
150
 
                }
151
 
        }
152
 
 
153
 
        // calculate the width
154
 
        int width;
155
 
        switch( _message_modes[ message_num ] & 0x1c )
156
 
        {
157
 
        case MODE_ONEQUARTER:
158
 
                width = NUM_SEGMENTS / 4;
159
 
                break;
160
 
        case MODE_ONETHIRD:
161
 
                width = NUM_SEGMENTS / 3;
162
 
                break;
163
 
        case MODE_HALF:
164
 
                width = NUM_SEGMENTS / 2;
165
 
                break;
166
 
        case MODE_TWOTHIRDS:
167
 
                width = NUM_SEGMENTS * 2 / 3;
168
 
                break;
169
 
        case MODE_THREEQUARTERS:
170
 
                width = NUM_SEGMENTS * 3 / 4;
171
 
                break;
172
 
        case MODE_ALL:
173
 
                width = NUM_SEGMENTS;
174
 
                break;
175
 
        }
176
 
 
177
 
        // convert segment to display-space
178
 
        switch( _message_modes[ message_num ] & 0x03 )
179
 
        {
180
 
        case MODE_TOP:
181
 
                if( segment < width / 2 )
182
 
                        segment += width / 2;
183
 
                else if( segment >= NUM_SEGMENTS - width / 2 )
184
 
                        segment -= ( NUM_SEGMENTS - width / 2 );
185
 
                else
186
 
                        return;
187
 
                break;
188
 
        case MODE_BOTTOM:
189
 
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
190
 
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
191
 
                {
192
 
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
193
 
                }               
194
 
                else
195
 
                        return;
196
 
                break;
197
 
        }
198
 
 
199
 
        // render
200
 
        TextRenderer::render(
201
 
                _messages[ message_num ], _message_lens[ message_num ],
202
 
                (long)segment + _message_params[ message_num ],
203
 
                ( _message_modes[ message_num ] & 1 ) == 0, 0 );
204
 
}