ArduinoLibs
BoolField.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 "BoolField.h"
00024 
00077 BoolField::BoolField(const String &label)
00078     : Field(label)
00079     , _printLen(0)
00080     , _value(false)
00081 {
00082 }
00083 
00094 BoolField::BoolField(Form &form, const String &label, const String &trueLabel, const String &falseLabel, bool value)
00095     : Field(form, label)
00096     , _trueLabel(trueLabel)
00097     , _falseLabel(falseLabel)
00098     , _printLen(0)
00099     , _value(value)
00100 {
00101 }
00102 
00103 int BoolField::dispatch(int event)
00104 {
00105     if (event == LCD_BUTTON_UP || event == LCD_BUTTON_DOWN) {
00106         setValue(!_value);
00107         return FORM_CHANGED;
00108     } else {
00109         return -1;
00110     }
00111 }
00112 
00113 void BoolField::enterField(bool reverse)
00114 {
00115     Field::enterField(reverse);
00116     printValue();
00117 }
00118 
00131 void BoolField::setValue(bool value)
00132 {
00133     if (value != _value) {
00134         _value = value;
00135         if (isCurrent())
00136             printValue();
00137     }
00138 }
00139 
00153 void BoolField::setTrueLabel(const String &trueLabel)
00154 {
00155     _trueLabel = trueLabel;
00156     if (isCurrent())
00157         printValue();
00158 }
00159 
00173 void BoolField::setFalseLabel(const String &falseLabel)
00174 {
00175     _falseLabel = falseLabel;
00176     if (isCurrent())
00177         printValue();
00178 }
00179 
00180 void BoolField::printValue()
00181 {
00182     unsigned int len;
00183     lcd()->setCursor(0, 1);
00184     if (_value) {
00185         lcd()->print(_trueLabel);
00186         len = _trueLabel.length();
00187         while (len++ < _printLen)
00188             lcd()->write(' ');
00189         _printLen = _trueLabel.length();
00190     } else {
00191         lcd()->print(_falseLabel);
00192         len = _falseLabel.length();
00193         while (len++ < _printLen)
00194             lcd()->write(' ');
00195         _printLen = _falseLabel.length();
00196     }
00197 }
 All Classes Files Functions Variables Enumerations Enumerator