A basic library for the Freetronics arduino LCD shield.

Fork of freetronicsLCDShield by Koen Kempeneers

Committer:
johnb
Date:
Sun Jul 02 15:26:57 2017 +0000
Revision:
7:56d8df0eb209
Parent:
6:ac481535732f
Updated threshold value & added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KKempeneers 1:ddcefddda4a7 1 /* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers
KKempeneers 4:3a0c74cef8e8 2 * kkempeneers(at)skynet.be
johnb 6:ac481535732f 3 * Improved button support added by John Bailey
johnb 6:ac481535732f 4 * jdb__mbed(at)anotherdimension.net
KKempeneers 1:ddcefddda4a7 5 *
KKempeneers 1:ddcefddda4a7 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
KKempeneers 1:ddcefddda4a7 7 * of this software and associated documentation files (the "Software"), to deal
KKempeneers 1:ddcefddda4a7 8 * in the Software without restriction, including without limitation the rights
KKempeneers 1:ddcefddda4a7 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
KKempeneers 1:ddcefddda4a7 10 * copies of the Software, and to permit persons to whom the Software is
KKempeneers 1:ddcefddda4a7 11 * furnished to do so, subject to the following conditions:
KKempeneers 1:ddcefddda4a7 12 *
KKempeneers 1:ddcefddda4a7 13 * The above copyright notice and this permission notice shall be included in
KKempeneers 1:ddcefddda4a7 14 * all copies or substantial portions of the Software.
KKempeneers 1:ddcefddda4a7 15 *
KKempeneers 1:ddcefddda4a7 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
KKempeneers 1:ddcefddda4a7 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
KKempeneers 1:ddcefddda4a7 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
KKempeneers 1:ddcefddda4a7 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
KKempeneers 1:ddcefddda4a7 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
KKempeneers 1:ddcefddda4a7 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
KKempeneers 1:ddcefddda4a7 22 * THE SOFTWARE.
KKempeneers 1:ddcefddda4a7 23 */
KKempeneers 1:ddcefddda4a7 24
KKempeneers 0:01f3d38f8b6d 25 #include "mbed.h"
KKempeneers 0:01f3d38f8b6d 26 #include "freetronicsLCDShield.h"
KKempeneers 0:01f3d38f8b6d 27
KKempeneers 0:01f3d38f8b6d 28 #define PULSE_E wait(0.000001f); \
KKempeneers 0:01f3d38f8b6d 29 _e = 0; \
KKempeneers 0:01f3d38f8b6d 30 wait(0.000001f); \
KKempeneers 0:01f3d38f8b6d 31 _e = 1;
KKempeneers 0:01f3d38f8b6d 32
johnb 7:56d8df0eb209 33 const float freetronicsLCDShield::buttonThresholds[FREETRONICSLCDSHIELD_BUTTON_COUNT] = { 0.95, 0.65, 0.4, 0.15, -0.5 }; /* None, Left, Down, Up, Right */
KKempeneers 0:01f3d38f8b6d 34
KKempeneers 0:01f3d38f8b6d 35 freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0)
KKempeneers 0:01f3d38f8b6d 36 : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
KKempeneers 0:01f3d38f8b6d 37 // Class constructor
KKempeneers 0:01f3d38f8b6d 38 // Init the display, wait 15ms to insure the power is up
KKempeneers 0:01f3d38f8b6d 39 _e = true;
KKempeneers 0:01f3d38f8b6d 40 _rs = false;
KKempeneers 0:01f3d38f8b6d 41 wait(0.015f);
KKempeneers 0:01f3d38f8b6d 42
KKempeneers 0:01f3d38f8b6d 43 for (int i = 0; i < 3; i++){
KKempeneers 0:01f3d38f8b6d 44 writeByte(0x3);
KKempeneers 0:01f3d38f8b6d 45 wait(0.00164f); // this command takes 1.64ms, so wait for it
KKempeneers 0:01f3d38f8b6d 46 }
KKempeneers 0:01f3d38f8b6d 47
KKempeneers 0:01f3d38f8b6d 48 writeByte(0x2); // 4-bit mode
KKempeneers 0:01f3d38f8b6d 49 writeCommand(0x28); // Function set 001 BW N F - -
KKempeneers 0:01f3d38f8b6d 50 writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off
KKempeneers 0:01f3d38f8b6d 51 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
KKempeneers 0:01f3d38f8b6d 52 cls();
KKempeneers 0:01f3d38f8b6d 53
KKempeneers 0:01f3d38f8b6d 54 // Set the PWM period to 20ms
KKempeneers 0:01f3d38f8b6d 55 _bl.period_ms(1);
KKempeneers 0:01f3d38f8b6d 56 _bl.write(0.0);
KKempeneers 0:01f3d38f8b6d 57 }
KKempeneers 0:01f3d38f8b6d 58
KKempeneers 0:01f3d38f8b6d 59 void freetronicsLCDShield::setCursorPosition (int line, int col) {
KKempeneers 0:01f3d38f8b6d 60 // Set the new cursorposition
KKempeneers 0:01f3d38f8b6d 61 writeCommand(0x80 + (line * 0x40) + col);
KKempeneers 0:01f3d38f8b6d 62 }
KKempeneers 0:01f3d38f8b6d 63
KKempeneers 0:01f3d38f8b6d 64 void freetronicsLCDShield::setBackLight (bool blStatus) {
KKempeneers 0:01f3d38f8b6d 65 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 66 _bl = (blStatus) ? 1.0 : 0.0;
KKempeneers 0:01f3d38f8b6d 67 }
KKempeneers 0:01f3d38f8b6d 68
KKempeneers 0:01f3d38f8b6d 69 void freetronicsLCDShield::setBackLight (float blIntensity) {
KKempeneers 0:01f3d38f8b6d 70 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 71 _bl = blIntensity;
KKempeneers 0:01f3d38f8b6d 72 }
KKempeneers 0:01f3d38f8b6d 73
KKempeneers 0:01f3d38f8b6d 74 void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
KKempeneers 0:01f3d38f8b6d 75 int tmp = 0;
KKempeneers 0:01f3d38f8b6d 76
KKempeneers 0:01f3d38f8b6d 77 if (blink) tmp = 0x01;
KKempeneers 0:01f3d38f8b6d 78 if (cStatus) tmp |= 0x02;
KKempeneers 0:01f3d38f8b6d 79 writeCommand(0x0C + tmp);
KKempeneers 0:01f3d38f8b6d 80 }
KKempeneers 0:01f3d38f8b6d 81
KKempeneers 1:ddcefddda4a7 82 void freetronicsLCDShield::shift(bool direction) {
KKempeneers 1:ddcefddda4a7 83 if(direction == LEFT) shiftLeft();
KKempeneers 1:ddcefddda4a7 84 else shiftRight();
KKempeneers 1:ddcefddda4a7 85 }
KKempeneers 1:ddcefddda4a7 86
KKempeneers 1:ddcefddda4a7 87 void freetronicsLCDShield::shiftLeft(void) {
KKempeneers 1:ddcefddda4a7 88 writeCommand(0x18 + 0x04);
KKempeneers 1:ddcefddda4a7 89 }
KKempeneers 1:ddcefddda4a7 90
KKempeneers 1:ddcefddda4a7 91 void freetronicsLCDShield::shiftRight(void) {
KKempeneers 1:ddcefddda4a7 92 writeCommand(0x18);
KKempeneers 0:01f3d38f8b6d 93 }
KKempeneers 0:01f3d38f8b6d 94
KKempeneers 0:01f3d38f8b6d 95 void freetronicsLCDShield::cls(void) {
KKempeneers 0:01f3d38f8b6d 96 // Clear the display and place the cursor at 0, 0
KKempeneers 0:01f3d38f8b6d 97 writeCommand(0x01);
KKempeneers 0:01f3d38f8b6d 98 wait(0.00164f);
KKempeneers 0:01f3d38f8b6d 99 }
KKempeneers 0:01f3d38f8b6d 100
KKempeneers 0:01f3d38f8b6d 101 void freetronicsLCDShield::home(void) {
KKempeneers 0:01f3d38f8b6d 102 // Undo shift operations and place cursor at 0,0
KKempeneers 0:01f3d38f8b6d 103 writeCommand(0x02);
KKempeneers 0:01f3d38f8b6d 104 wait(0.00164f);
KKempeneers 0:01f3d38f8b6d 105 }
KKempeneers 0:01f3d38f8b6d 106
KKempeneers 1:ddcefddda4a7 107 void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) {
KKempeneers 1:ddcefddda4a7 108 // Write the address only once, it is autoincremented
KKempeneers 1:ddcefddda4a7 109 writeCommand(0x40 | address);
KKempeneers 1:ddcefddda4a7 110
KKempeneers 1:ddcefddda4a7 111 // Write the data
KKempeneers 1:ddcefddda4a7 112 for(int i = 0; i < nbytes; i++) {
KKempeneers 1:ddcefddda4a7 113 writeData(*ptr++);
KKempeneers 1:ddcefddda4a7 114 }
KKempeneers 1:ddcefddda4a7 115 }
KKempeneers 1:ddcefddda4a7 116
KKempeneers 0:01f3d38f8b6d 117 // Low level output functions
KKempeneers 0:01f3d38f8b6d 118 void freetronicsLCDShield::writeByte (int byte) {
KKempeneers 0:01f3d38f8b6d 119 // Split the byte in high and low nibble, write high nibble first
KKempeneers 0:01f3d38f8b6d 120 _d = byte >> 4;
KKempeneers 0:01f3d38f8b6d 121 PULSE_E;
KKempeneers 0:01f3d38f8b6d 122 _d = byte >> 0;
KKempeneers 0:01f3d38f8b6d 123 PULSE_E;
KKempeneers 0:01f3d38f8b6d 124 // Most instructions take 40us
KKempeneers 0:01f3d38f8b6d 125 wait(0.000040f);
KKempeneers 0:01f3d38f8b6d 126 }
KKempeneers 0:01f3d38f8b6d 127
KKempeneers 0:01f3d38f8b6d 128 void freetronicsLCDShield::writeData (int data) {
KKempeneers 0:01f3d38f8b6d 129 _rs = true;
KKempeneers 0:01f3d38f8b6d 130 writeByte(data);
KKempeneers 0:01f3d38f8b6d 131 }
KKempeneers 0:01f3d38f8b6d 132
KKempeneers 0:01f3d38f8b6d 133 void freetronicsLCDShield::writeCommand (int command) {
KKempeneers 0:01f3d38f8b6d 134 _rs = false;
KKempeneers 0:01f3d38f8b6d 135 writeByte(command);
KKempeneers 0:01f3d38f8b6d 136 }
KKempeneers 0:01f3d38f8b6d 137
KKempeneers 0:01f3d38f8b6d 138 float freetronicsLCDShield::readButton(void) {
KKempeneers 0:01f3d38f8b6d 139 return(_a0.read());
KKempeneers 0:01f3d38f8b6d 140 }
KKempeneers 0:01f3d38f8b6d 141
johnb 6:ac481535732f 142 freetronicsLCDShield::ShieldButton freetronicsLCDShield::pressedButton(void) {
johnb 6:ac481535732f 143 freetronicsLCDShield::ShieldButton retVal = freetronicsLCDShield::None;
johnb 6:ac481535732f 144 float buttonVal = readButton();
johnb 6:ac481535732f 145 for( unsigned i = 0; i < FREETRONICSLCDSHIELD_BUTTON_COUNT; i++ )
johnb 6:ac481535732f 146 {
johnb 6:ac481535732f 147 if( buttonVal > buttonThresholds[ i ] )
johnb 6:ac481535732f 148 {
johnb 6:ac481535732f 149 retVal = (ShieldButton)i;
johnb 6:ac481535732f 150 break;
johnb 6:ac481535732f 151 }
johnb 6:ac481535732f 152 }
johnb 6:ac481535732f 153 return retVal;
johnb 6:ac481535732f 154 }
johnb 6:ac481535732f 155
KKempeneers 0:01f3d38f8b6d 156 // Virtual functions for stream
KKempeneers 0:01f3d38f8b6d 157 int freetronicsLCDShield::_putc(int value) {
KKempeneers 0:01f3d38f8b6d 158 writeData(value);
KKempeneers 0:01f3d38f8b6d 159 return value;
KKempeneers 0:01f3d38f8b6d 160 }
KKempeneers 0:01f3d38f8b6d 161
KKempeneers 0:01f3d38f8b6d 162 int freetronicsLCDShield::_getc() {
KKempeneers 0:01f3d38f8b6d 163 return -1;
KKempeneers 0:01f3d38f8b6d 164 }