ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IntField.cpp
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "IntField.h"
24 
71 IntField::IntField(const String &label)
72  : Field(label)
73  , _minValue(0)
74  , _maxValue(100)
75  , _stepValue(1)
76  , _value(0)
77  , _printLen(0)
78 {
79 }
80 
88 IntField::IntField(Form &form, const String &label, int minValue, int maxValue, int stepValue, int value)
89  : Field(form, label)
90  , _minValue(minValue)
91  , _maxValue(maxValue)
92  , _stepValue(stepValue)
93  , _value(value)
94  , _printLen(0)
95 {
96 }
97 
103 IntField::IntField(Form &form, const String &label, int minValue, int maxValue, int stepValue, int value, const String &suffix)
104  : Field(form, label)
105  , _minValue(minValue)
106  , _maxValue(maxValue)
107  , _stepValue(stepValue)
108  , _value(value)
109  , _printLen(0)
110  , _suffix(suffix)
111 {
112 }
113 
114 int IntField::dispatch(int event)
115 {
116  if (event == LCD_BUTTON_UP) {
117  setValue(_value + _stepValue);
118  return FORM_CHANGED;
119  } else if (event == LCD_BUTTON_DOWN) {
120  setValue(_value - _stepValue);
121  return FORM_CHANGED;
122  }
123  return -1;
124 }
125 
126 void IntField::enterField(bool reverse)
127 {
128  Field::enterField(reverse);
129  printValue();
130 }
131 
198 void IntField::setValue(int value)
199 {
200  if (value < _minValue)
201  value = _minValue;
202  else if (value > _maxValue)
203  value = _maxValue;
204  if (value != _value) {
205  _value = value;
206  if (isCurrent())
207  printValue();
208  }
209 }
210 
231 void IntField::setSuffix(const String &suffix)
232 {
233  _suffix = suffix;
234  if (isCurrent())
235  printValue();
236 }
237 
238 void IntField::printValue()
239 {
240  String str(_value);
241  if (_suffix.length())
242  str += _suffix;
243  lcd()->setCursor(0, 1);
244  lcd()->print(str);
245  unsigned int len = str.length();
246  while (len++ < _printLen)
247  lcd()->write(' ');
248  _printLen = str.length();
249 }
Manages a single data input/output field within a Form.
Definition: Field.h:28
Manager for a form containing data input/output fields.
Definition: Form.h:32
virtual void enterField(bool reverse)
Enters the field due to form navigation.
Definition: Field.cpp:116
void setValue(int value)
Sets the current value of this field.
Definition: IntField.cpp:198
int dispatch(int event)
Dispatches event via this field.
Definition: IntField.cpp:114
LiquidCrystal * lcd() const
Returns the LCD that this field is being drawn on.
Definition: Field.h:47
const String & suffix() const
Returns the suffix string to be displayed after the field's value.
Definition: IntField.h:50
int value() const
Returns the current value of this field.
Definition: IntField.h:47
IntField(const String &label)
Constructs a new integer field with a specific label.
Definition: IntField.cpp:71
void setSuffix(const String &suffix)
Sets the suffix string to be displayed after the field's value.
Definition: IntField.cpp:231
bool isCurrent() const
Returns true if this field is the currently-displayed field in its owning form; false otherwise...
Definition: Field.cpp:169
void enterField(bool reverse)
Enters the field due to form navigation.
Definition: IntField.cpp:126