ArduinoLibs
IntField.cpp
00001 /*
00002  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020  * DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #include "IntField.h"
00024 
00071 IntField::IntField(const String &label)
00072     : Field(label)
00073     , _minValue(0)
00074     , _maxValue(100)
00075     , _stepValue(1)
00076     , _value(0)
00077     , _printLen(0)
00078 {
00079 }
00080 
00088 IntField::IntField(Form &form, const String &label, int minValue, int maxValue, int stepValue, int value)
00089     : Field(form, label)
00090     , _minValue(minValue)
00091     , _maxValue(maxValue)
00092     , _stepValue(stepValue)
00093     , _value(value)
00094     , _printLen(0)
00095 {
00096 }
00097 
00103 IntField::IntField(Form &form, const String &label, int minValue, int maxValue, int stepValue, int value, const String &suffix)
00104     : Field(form, label)
00105     , _minValue(minValue)
00106     , _maxValue(maxValue)
00107     , _stepValue(stepValue)
00108     , _value(value)
00109     , _printLen(0)
00110     , _suffix(suffix)
00111 {
00112 }
00113 
00114 int IntField::dispatch(int event)
00115 {
00116     if (event == LCD_BUTTON_UP) {
00117         setValue(_value + _stepValue);
00118         return FORM_CHANGED;
00119     } else if (event == LCD_BUTTON_DOWN) {
00120         setValue(_value - _stepValue);
00121         return FORM_CHANGED;
00122     }
00123     return -1;
00124 }
00125 
00126 void IntField::enterField(bool reverse)
00127 {
00128     Field::enterField(reverse);
00129     printValue();
00130 }
00131 
00198 void IntField::setValue(int value)
00199 {
00200     if (value < _minValue)
00201         value = _minValue;
00202     else if (value > _maxValue)
00203         value = _maxValue;
00204     if (value != _value) {
00205         _value = value;
00206         if (isCurrent())
00207             printValue();
00208     }
00209 }
00210 
00231 void IntField::setSuffix(const String &suffix)
00232 {
00233     _suffix = suffix;
00234     if (isCurrent())
00235         printValue();
00236 }
00237 
00238 void IntField::printValue()
00239 {
00240     String str(_value);
00241     if (_suffix.length())
00242         str += _suffix;
00243     lcd()->setCursor(0, 1);
00244     lcd()->print(str);
00245     unsigned int len = str.length();
00246     while (len++ < _printLen)
00247         lcd()->write(' ');
00248     _printLen = str.length();
00249 }
 All Classes Files Functions Variables Enumerations Enumerator