ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BoolField.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 "BoolField.h"
24 
77 BoolField::BoolField(const String &label)
78  : Field(label)
79  , _printLen(0)
80  , _value(false)
81 {
82 }
83 
94 BoolField::BoolField(Form &form, const String &label, const String &trueLabel, const String &falseLabel, bool value)
95  : Field(form, label)
96  , _trueLabel(trueLabel)
97  , _falseLabel(falseLabel)
98  , _printLen(0)
99  , _value(value)
100 {
101 }
102 
103 int BoolField::dispatch(int event)
104 {
105  if (event == LCD_BUTTON_UP || event == LCD_BUTTON_DOWN) {
106  setValue(!_value);
107  return FORM_CHANGED;
108  } else {
109  return -1;
110  }
111 }
112 
113 void BoolField::enterField(bool reverse)
114 {
115  Field::enterField(reverse);
116  printValue();
117 }
118 
131 void BoolField::setValue(bool value)
132 {
133  if (value != _value) {
134  _value = value;
135  if (isCurrent())
136  printValue();
137  }
138 }
139 
153 void BoolField::setTrueLabel(const String &trueLabel)
154 {
155  _trueLabel = trueLabel;
156  if (isCurrent())
157  printValue();
158 }
159 
173 void BoolField::setFalseLabel(const String &falseLabel)
174 {
175  _falseLabel = falseLabel;
176  if (isCurrent())
177  printValue();
178 }
179 
180 void BoolField::printValue()
181 {
182  unsigned int len;
183  lcd()->setCursor(0, 1);
184  if (_value) {
185  lcd()->print(_trueLabel);
186  len = _trueLabel.length();
187  while (len++ < _printLen)
188  lcd()->write(' ');
189  _printLen = _trueLabel.length();
190  } else {
191  lcd()->print(_falseLabel);
192  len = _falseLabel.length();
193  while (len++ < _printLen)
194  lcd()->write(' ');
195  _printLen = _falseLabel.length();
196  }
197 }
BoolField(const String &label)
Constructs a new boolean field with a specific label.
Definition: BoolField.cpp:77
void setValue(bool value)
Sets the current value of this field to value.
Definition: BoolField.cpp:131
const String & trueLabel() const
Returns the string that is displayed when value() is true.
Definition: BoolField.h:40
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 setTrueLabel(const String &trueLabel)
Sets the string that is displayed when value() is true to trueLabel.
Definition: BoolField.cpp:153
LiquidCrystal * lcd() const
Returns the LCD that this field is being drawn on.
Definition: Field.h:47
void setFalseLabel(const String &falseLabel)
Sets the string that is displayed when value() is false to falseLabel.
Definition: BoolField.cpp:173
int dispatch(int event)
Dispatches event via this field.
Definition: BoolField.cpp:103
void enterField(bool reverse)
Enters the field due to form navigation.
Definition: BoolField.cpp:113
const String & falseLabel() const
Returns the string that is displayed when value() is false.
Definition: BoolField.h:43
bool value() const
Returns the current value of this field, true or false.
Definition: BoolField.h:37
bool isCurrent() const
Returns true if this field is the currently-displayed field in its owning form; false otherwise...
Definition: Field.cpp:169