ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ListField.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 "ListField.h"
24 #include <string.h>
25 
64 ListField::ListField(const String &label)
65  : Field(label)
66  , _items(0)
67  , _itemCount(0)
68  , _value(-1)
69  , _printLen(0)
70 {
71 }
72 
77 ListField::ListField(Form &form, const String &label, ListItems items, int value)
78  : Field(form, label)
79  , _items(0)
80  , _itemCount(0)
81  , _value(value)
82  , _printLen(0)
83 {
84  setItems(items);
85 }
86 
87 int ListField::dispatch(int event)
88 {
89  if (event == LCD_BUTTON_DOWN) {
90  if (_value >= (_itemCount - 1))
91  setValue(0);
92  else
93  setValue(_value + 1);
94  return FORM_CHANGED;
95  } else if (event == LCD_BUTTON_UP) {
96  if (_value <= 0)
97  setValue(_itemCount - 1);
98  else
99  setValue(_value - 1);
100  return FORM_CHANGED;
101  }
102  return -1;
103 }
104 
105 void ListField::enterField(bool reverse)
106 {
107  Field::enterField(reverse);
108  _printLen = 0;
109  printValue();
110 }
111 
141 void ListField::setItems(ListItems items)
142 {
143  _items = items;
144  _itemCount = 0;
145  if (items) {
146  for (;;) {
147  ListItem item = (ListItem)pgm_read_word(items);
148  if (!item)
149  break;
150  ++items;
151  ++_itemCount;
152  }
153  }
154  if (_value >= _itemCount)
155  _value = _itemCount - 1;
156  if (isCurrent())
157  printValue();
158 }
159 
178 void ListField::setValue(int value)
179 {
180  if (_value != value) {
181  _value = value;
182  if (_value < 0)
183  _value = 0;
184  if (_value >= _itemCount)
185  _value = _itemCount - 1;
186  if (isCurrent())
187  printValue();
188  }
189 }
190 
191 void ListField::printValue()
192 {
193  lcd()->setCursor(0, 1);
194  int len = 0;
195  if (_value >= 0) {
196  ListItem str = (ListItem)pgm_read_word(&(_items[_value]));
197  char ch;
198  while ((ch = pgm_read_byte(str)) != 0) {
199  lcd()->write(ch);
200  ++len;
201  ++str;
202  }
203  }
204  while (_printLen-- > len)
205  lcd()->write(' ');
206  _printLen = len;
207 }
ListField(const String &label)
Constructs a new list field with a specific label.
Definition: ListField.cpp:64
Manages a single data input/output field within a Form.
Definition: Field.h:28
int value() const
Returns the value of this list; i.e. the index within items() of the selected item.
Definition: ListField.h:44
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 setItems(ListItems items)
Sets the array of items for this list.
Definition: ListField.cpp:141
LiquidCrystal * lcd() const
Returns the LCD that this field is being drawn on.
Definition: Field.h:47
ListItems items() const
Returns the array of items in this list.
Definition: ListField.h:41
void enterField(bool reverse)
Enters the field due to form navigation.
Definition: ListField.cpp:105
void setValue(int value)
Sets the value of this list; i.e. the index within items() of the selected item.
Definition: ListField.cpp:178
int dispatch(int event)
Dispatches event via this field.
Definition: ListField.cpp:87
bool isCurrent() const
Returns true if this field is the currently-displayed field in its owning form; false otherwise...
Definition: Field.cpp:169