ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LCD.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 "LCD.h"
24 #include <avr/pgmspace.h>
25 #if defined(ARDUINO) && ARDUINO >= 100
26 #include <Arduino.h>
27 #else
28 #include <WProgram.h>
29 #endif
30 
31 #define LCD_BACK_LIGHT 3 // Default LCD backlight is on D3
32 #define LCD_BUTTON_PIN A0 // Button state is on A0
33 
34 #define DEBOUNCE_DELAY 10 // Delay in ms to debounce buttons
35 
145 void LCD::init()
146 {
147  // The Freetronics display is 16x2.
148  begin(16, 2);
149 
150  // Configure the backlight pin, but don't activate it yet in
151  // case the application sets it to something else during setup().
152  // Initialization will be forced in the first call to getButton().
153  _backlightPin = LCD_BACK_LIGHT;
154  backlightInit = false;
155 
156  // Initialise button input.
157  pinMode(LCD_BUTTON_PIN, INPUT);
158  digitalWrite(LCD_BUTTON_PIN, LOW);
159  prevButton = LCD_BUTTON_NONE;
160  debounceButton = LCD_BUTTON_NONE;
161  lastDebounce = 0;
162  eatRelease = false;
163 
164  // Initialize screen saver.
165  timeout = 0;
166  lastRestore = millis();
167  screenSaved = false;
168  mode = DisplayOff;
169 }
170 
197 void LCD::setBacklightPin(uint8_t pin)
198 {
199  if (_backlightPin != pin) {
200  if (backlightInit) {
201  // Restore the previous backlight pin to input, floating.
202  pinMode(_backlightPin, INPUT);
203  digitalWrite(_backlightPin, LOW);
204 
205  // Need to re-initialize the backlight at the earliest opportunity.
206  backlightInit = false;
207  }
208  _backlightPin = pin;
209  }
210 }
211 
222 {
223  LiquidCrystal::display();
224  pinMode(_backlightPin, OUTPUT);
225  digitalWrite(_backlightPin, HIGH);
226  screenSaved = false;
227  backlightInit = true;
228  lastRestore = millis();
229 }
230 
239 {
240  if (mode == DisplayOff)
241  LiquidCrystal::noDisplay();
242  pinMode(_backlightPin, OUTPUT);
243  digitalWrite(_backlightPin, LOW);
244  screenSaved = true;
245  backlightInit = true;
246 }
247 
284 {
285  if (this->mode != mode) {
286  this->mode = mode;
287  if (screenSaved)
288  noDisplay();
289  else
290  display();
291  }
292 }
293 
309 void LCD::enableScreenSaver(int timeoutSecs)
310 {
311  if (timeoutSecs < 0)
312  timeout = 0;
313  else
314  timeout = ((unsigned long)timeoutSecs) * 1000;
315  display();
316 }
317 
324 {
325  timeout = 0;
326  display();
327 }
328 
336 // Button mapping table generated by genlookup.c
337 static unsigned char const buttonMappings[] PROGMEM = {
338  2, 0, 0, 0, 3, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 1,
339  1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0
340 };
341 #define mapButton(value) (pgm_read_byte(&(buttonMappings[(value) >> 5])))
342 
369 {
370  // Initialize the backlight for the first time if necessary.
371  if (!backlightInit)
372  display();
373 
374  // Read the currently pressed button.
375  int button = mapButton(analogRead(LCD_BUTTON_PIN));
376 
377  // Debounce the button state.
378  unsigned long currentTime = millis();
379  if (button != debounceButton)
380  lastDebounce = currentTime;
381  debounceButton = button;
382  if ((currentTime - lastDebounce) < DEBOUNCE_DELAY)
383  button = prevButton;
384 
385  // Process the button event if the state has changed.
386  if (prevButton == LCD_BUTTON_NONE && button != LCD_BUTTON_NONE) {
387  prevButton = button;
388  if (screenSaved) {
389  // Button pressed when screen saver active.
390  if (mode == BacklightOnSelect) {
391  // Turn on the back light only if Select was pressed.
392  if (button == LCD_BUTTON_SELECT) {
393  pinMode(_backlightPin, OUTPUT);
394  digitalWrite(_backlightPin, HIGH);
395  screenSaved = false;
396  backlightInit = true;
397  }
398  } else if (mode == DisplayOff) {
399  display();
400  eatRelease = true;
401  return LCD_BUTTON_NONE;
402  } else {
403  display();
404  }
405  } else if (mode == BacklightOnSelect && button != LCD_BUTTON_SELECT) {
406  eatRelease = false;
407  return button;
408  }
409  eatRelease = false;
410  lastRestore = currentTime;
411  return button;
412  } else if (prevButton != LCD_BUTTON_NONE && button == LCD_BUTTON_NONE) {
413  button = -prevButton;
414  prevButton = LCD_BUTTON_NONE;
415  lastRestore = currentTime;
416  if (eatRelease) {
417  eatRelease = false;
418  return LCD_BUTTON_NONE;
419  }
420  return button;
421  } else {
422  if (!screenSaved && prevButton == LCD_BUTTON_NONE &&
423  timeout != 0 && (currentTime - lastRestore) >= timeout)
424  noDisplay(); // Activate screen saver.
425  return LCD_BUTTON_NONE;
426  }
427 }
void setScreenSaverMode(ScreenSaverMode mode)
Sets the current screen saver mode.
Definition: LCD.cpp:283
int getButton()
Gets the next button press, release, or idle event.
Definition: LCD.cpp:368
Same as BacklightOff but the screen saver is only deactivated when Select is pressed; other buttons h...
Definition: LCD.h:66
ScreenSaverMode
Screen saver mode that controls the display and back light.
Definition: LCD.h:62
void setBacklightPin(uint8_t pin)
Sets the back light pin for the LCD shield.
Definition: LCD.cpp:197
void enableScreenSaver(int timeoutSecs=10)
Enables the screen saver and causes it to activate after timeoutSecs of inactivity on the buttons...
Definition: LCD.cpp:309
void noDisplay()
Turns off the display of text on the LCD and the back light.
Definition: LCD.cpp:238
Turn off both the display and the backlight when the screen saver is activated.
Definition: LCD.h:64
void disableScreenSaver()
Disables the screen saver.
Definition: LCD.cpp:323
void display()
Turns on the display of text on the LCD and the back light.
Definition: LCD.cpp:221