ArduinoLibs
ListField.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 "ListField.h"
00024 #include <string.h>
00025 
00064 ListField::ListField(const String &label)
00065     : Field(label)
00066     , _items(0)
00067     , _itemCount(0)
00068     , _value(-1)
00069     , _printLen(0)
00070 {
00071 }
00072 
00077 ListField::ListField(Form &form, const String &label, ListItems items, int value)
00078     : Field(form, label)
00079     , _items(0)
00080     , _itemCount(0)
00081     , _value(value)
00082     , _printLen(0)
00083 {
00084     setItems(items);
00085 }
00086 
00087 int ListField::dispatch(int event)
00088 {
00089     if (event == LCD_BUTTON_DOWN) {
00090         if (_value >= (_itemCount - 1))
00091             setValue(0);
00092         else
00093             setValue(_value + 1);
00094         return FORM_CHANGED;
00095     } else if (event == LCD_BUTTON_UP) {
00096         if (_value <= 0)
00097             setValue(_itemCount - 1);
00098         else
00099             setValue(_value - 1);
00100         return FORM_CHANGED;
00101     }
00102     return -1;
00103 }
00104 
00105 void ListField::enterField(bool reverse)
00106 {
00107     Field::enterField(reverse);
00108     _printLen = 0;
00109     printValue();
00110 }
00111 
00141 void ListField::setItems(ListItems items)
00142 {
00143     _items = items;
00144     _itemCount = 0;
00145     if (items) {
00146         for (;;) {
00147             ListItem item = (ListItem)pgm_read_word(items);
00148             if (!item)
00149                 break;
00150             ++items;
00151             ++_itemCount;
00152         }
00153     }
00154     if (_value >= _itemCount)
00155         _value = _itemCount - 1;
00156     if (isCurrent())
00157         printValue();
00158 }
00159 
00178 void ListField::setValue(int value)
00179 {
00180     if (_value != value) {
00181         _value = value;
00182         if (_value < 0)
00183             _value = 0;
00184         if (_value >= _itemCount)
00185             _value = _itemCount - 1;
00186         if (isCurrent())
00187             printValue();
00188     }
00189 }
00190 
00191 void ListField::printValue()
00192 {
00193     lcd()->setCursor(0, 1);
00194     int len = 0;
00195     if (_value >= 0) {
00196         ListItem str = (ListItem)pgm_read_word(&(_items[_value]));
00197         char ch;
00198         while ((ch = pgm_read_byte(str)) != 0) {
00199             lcd()->write(ch);
00200             ++len;
00201             ++str;
00202         }
00203     }
00204     while (_printLen-- > len)
00205         lcd()->write(' ');
00206     _printLen = len;
00207 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator