Demo of LCD modified to use interrupts not delays.

Dependencies:   mbed

Fork of LCD_nonblocking_demo by Oliver Broad

This is an experiment in controlling an LCD from callbacks independently of the main program loop. As it runs from callbacks it is necessary to check if the library is "busy", which is currently indicated by an integer called "busy".

Busy checking should be converted into a method so that the criteria for being "busy" can be changed without changing the API.

As this is rough I have not issued it as a library ... yet.

Also due to my inexperience using the repository manager this is currently listed as a fork of itself. Sorry.

Committer:
oliverb
Date:
Wed Nov 20 12:52:19 2013 +0000
Revision:
3:f18aa98bc9a8
Moved classes out of main

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oliverb 3:f18aa98bc9a8 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
oliverb 3:f18aa98bc9a8 2 * Copyright (c) 2013, oliverb, http://mbed.org
oliverb 3:f18aa98bc9a8 3 * Copyright (c) 2007-2010, sford, http://mbed.org
oliverb 3:f18aa98bc9a8 4 *
oliverb 3:f18aa98bc9a8 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
oliverb 3:f18aa98bc9a8 6 * of this software and associated documentation files (the "Software"), to deal
oliverb 3:f18aa98bc9a8 7 * in the Software without restriction, including without limitation the rights
oliverb 3:f18aa98bc9a8 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
oliverb 3:f18aa98bc9a8 9 * copies of the Software, and to permit persons to whom the Software is
oliverb 3:f18aa98bc9a8 10 * furnished to do so, subject to the following conditions:
oliverb 3:f18aa98bc9a8 11 *
oliverb 3:f18aa98bc9a8 12 * The above copyright notice and this permission notice shall be included in
oliverb 3:f18aa98bc9a8 13 * all copies or substantial portions of the Software.
oliverb 3:f18aa98bc9a8 14 *
oliverb 3:f18aa98bc9a8 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
oliverb 3:f18aa98bc9a8 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
oliverb 3:f18aa98bc9a8 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
oliverb 3:f18aa98bc9a8 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
oliverb 3:f18aa98bc9a8 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
oliverb 3:f18aa98bc9a8 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
oliverb 3:f18aa98bc9a8 21 * THE SOFTWARE.
oliverb 3:f18aa98bc9a8 22 */
oliverb 3:f18aa98bc9a8 23
oliverb 3:f18aa98bc9a8 24 #ifndef MBED_LCDNB_H
oliverb 3:f18aa98bc9a8 25 #define MBED_LCDNB_H
oliverb 3:f18aa98bc9a8 26
oliverb 3:f18aa98bc9a8 27 #include "mbed.h"
oliverb 3:f18aa98bc9a8 28
oliverb 3:f18aa98bc9a8 29 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
oliverb 3:f18aa98bc9a8 30 *
oliverb 3:f18aa98bc9a8 31 * Currently supports 16x2, 20x2 and 20x4 panels
oliverb 3:f18aa98bc9a8 32 *
oliverb 3:f18aa98bc9a8 33 * Implements a "Busy" flag to indicate when display is unavailable
oliverb 3:f18aa98bc9a8 34 * Busy is currently set by a timer and not by polling actual module
oliverb 3:f18aa98bc9a8 35 *
oliverb 3:f18aa98bc9a8 36 *
oliverb 3:f18aa98bc9a8 37 * @code
oliverb 3:f18aa98bc9a8 38 * #include "mbed.h"
oliverb 3:f18aa98bc9a8 39 * #include "TextLCD.h"
oliverb 3:f18aa98bc9a8 40 *
oliverb 3:f18aa98bc9a8 41 * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7
oliverb 3:f18aa98bc9a8 42 *
oliverb 3:f18aa98bc9a8 43 * int main() {
oliverb 3:f18aa98bc9a8 44 * lcd.printf("Hello World!\n");
oliverb 3:f18aa98bc9a8 45 * }
oliverb 3:f18aa98bc9a8 46 * @endcode
oliverb 3:f18aa98bc9a8 47 */
oliverb 3:f18aa98bc9a8 48 class TextLCD : public Stream
oliverb 3:f18aa98bc9a8 49 {
oliverb 3:f18aa98bc9a8 50 public:
oliverb 3:f18aa98bc9a8 51
oliverb 3:f18aa98bc9a8 52 /** LCD panel format */
oliverb 3:f18aa98bc9a8 53 enum LCDType {
oliverb 3:f18aa98bc9a8 54 LCD16x2 /**< 16x2 LCD panel (default) */
oliverb 3:f18aa98bc9a8 55 , LCD16x2B /**< 16x2 LCD panel alternate addressing */
oliverb 3:f18aa98bc9a8 56 , LCD20x2 /**< 20x2 LCD panel */
oliverb 3:f18aa98bc9a8 57 , LCD20x4 /**< 20x4 LCD panel */
oliverb 3:f18aa98bc9a8 58 };
oliverb 3:f18aa98bc9a8 59
oliverb 3:f18aa98bc9a8 60 /** Create a TextLCD interface
oliverb 3:f18aa98bc9a8 61 *
oliverb 3:f18aa98bc9a8 62 * @param rs Instruction/data control line
oliverb 3:f18aa98bc9a8 63 * @param e Enable line (clock)
oliverb 3:f18aa98bc9a8 64 * @param d4-d7 Data lines for using as a 4-bit interface
oliverb 3:f18aa98bc9a8 65 * @param type Sets the panel size/addressing mode (default = LCD16x2)
oliverb 3:f18aa98bc9a8 66 */
oliverb 3:f18aa98bc9a8 67 TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
oliverb 3:f18aa98bc9a8 68
oliverb 3:f18aa98bc9a8 69 void init();
oliverb 3:f18aa98bc9a8 70
oliverb 3:f18aa98bc9a8 71
oliverb 3:f18aa98bc9a8 72 #if DOXYGEN_ONLY
oliverb 3:f18aa98bc9a8 73 /** Write a character to the LCD
oliverb 3:f18aa98bc9a8 74 *
oliverb 3:f18aa98bc9a8 75 * @param c The character to write to the display
oliverb 3:f18aa98bc9a8 76 */
oliverb 3:f18aa98bc9a8 77 int putc(int c);
oliverb 3:f18aa98bc9a8 78
oliverb 3:f18aa98bc9a8 79 /** Write a formated string to the LCD
oliverb 3:f18aa98bc9a8 80 *
oliverb 3:f18aa98bc9a8 81 * @param format A printf-style format string, followed by the
oliverb 3:f18aa98bc9a8 82 * variables to use in formating the string.
oliverb 3:f18aa98bc9a8 83 */
oliverb 3:f18aa98bc9a8 84 volatile int printf(const char* format, ...);
oliverb 3:f18aa98bc9a8 85 #endif
oliverb 3:f18aa98bc9a8 86
oliverb 3:f18aa98bc9a8 87 /** Locate to a screen column and row
oliverb 3:f18aa98bc9a8 88 *
oliverb 3:f18aa98bc9a8 89 * @param column The horizontal position from the left, indexed from 0
oliverb 3:f18aa98bc9a8 90 * @param row The vertical position from the top, indexed from 0
oliverb 3:f18aa98bc9a8 91 */
oliverb 3:f18aa98bc9a8 92 void locate(int column, int row);
oliverb 3:f18aa98bc9a8 93
oliverb 3:f18aa98bc9a8 94 /** Clear the screen and locate to 0,0 */
oliverb 3:f18aa98bc9a8 95 void cls();
oliverb 3:f18aa98bc9a8 96
oliverb 3:f18aa98bc9a8 97 int rows();
oliverb 3:f18aa98bc9a8 98 int columns();
oliverb 3:f18aa98bc9a8 99 /// Status of display, wait for zero before writing
oliverb 3:f18aa98bc9a8 100 volatile int busy;
oliverb 3:f18aa98bc9a8 101
oliverb 3:f18aa98bc9a8 102 protected:
oliverb 3:f18aa98bc9a8 103 Timeout timer;
oliverb 3:f18aa98bc9a8 104
oliverb 3:f18aa98bc9a8 105 // callbacks used by "init" to carry out steps of initialisation
oliverb 3:f18aa98bc9a8 106 void init2();
oliverb 3:f18aa98bc9a8 107 void init2b();
oliverb 3:f18aa98bc9a8 108
oliverb 3:f18aa98bc9a8 109 void init3();
oliverb 3:f18aa98bc9a8 110 void init4();
oliverb 3:f18aa98bc9a8 111 void init5();
oliverb 3:f18aa98bc9a8 112 void init6();
oliverb 3:f18aa98bc9a8 113 void init7();
oliverb 3:f18aa98bc9a8 114 void init8();
oliverb 3:f18aa98bc9a8 115 // void init9();
oliverb 3:f18aa98bc9a8 116 // Stream implementation functions
oliverb 3:f18aa98bc9a8 117 virtual int _putc(int value);
oliverb 3:f18aa98bc9a8 118 virtual int _getc();
oliverb 3:f18aa98bc9a8 119
oliverb 3:f18aa98bc9a8 120 int address(int column, int row);
oliverb 3:f18aa98bc9a8 121 // void character(int column, int row, int c);
oliverb 3:f18aa98bc9a8 122 void writeByte(int value);
oliverb 3:f18aa98bc9a8 123 void writeCommand(int command);
oliverb 3:f18aa98bc9a8 124 void writeData(int data);
oliverb 3:f18aa98bc9a8 125 void callback();
oliverb 3:f18aa98bc9a8 126 void locate_cb();
oliverb 3:f18aa98bc9a8 127
oliverb 3:f18aa98bc9a8 128 DigitalOut _rs, _e;
oliverb 3:f18aa98bc9a8 129 BusOut _d;
oliverb 3:f18aa98bc9a8 130 LCDType _type;
oliverb 3:f18aa98bc9a8 131
oliverb 3:f18aa98bc9a8 132 int _column;
oliverb 3:f18aa98bc9a8 133 int _row;
oliverb 3:f18aa98bc9a8 134 char _queue[40];
oliverb 3:f18aa98bc9a8 135 volatile int _head;
oliverb 3:f18aa98bc9a8 136 volatile int _tail;
oliverb 3:f18aa98bc9a8 137 // int _loop;
oliverb 3:f18aa98bc9a8 138 };
oliverb 3:f18aa98bc9a8 139
oliverb 3:f18aa98bc9a8 140 #endif