John Bailey / freetronicsLCDShield

Fork of freetronicsLCDShield by Koen Kempeneers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers freetronicsLCDShield.cpp Source File

freetronicsLCDShield.cpp

00001 /* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers
00002  * kkempeneers(at)skynet.be
00003  * Improved button support added by John Bailey 
00004  * jdb__mbed(at)anotherdimension.net
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 #include "mbed.h"
00026 #include "freetronicsLCDShield.h"
00027 
00028 #define PULSE_E     wait(0.000001f);    \
00029                     _e = 0;             \
00030                     wait(0.000001f);    \
00031                     _e = 1;
00032 
00033 const float freetronicsLCDShield::buttonThresholds[FREETRONICSLCDSHIELD_BUTTON_COUNT] = { 0.95, 0.65, 0.4, 0.15, -0.5 }; /* None, Left, Down, Up, Right */
00034 
00035 freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) 
00036             : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
00037     // Class constructor
00038     // Init the display, wait 15ms to insure the power is up
00039     _e = true;
00040     _rs = false;
00041     wait(0.015f);
00042     
00043     for (int i = 0; i < 3; i++){
00044         writeByte(0x3);
00045         wait(0.00164f);  // this command takes 1.64ms, so wait for it
00046     }
00047 
00048     writeByte(0x2);     // 4-bit mode
00049     writeCommand(0x28); // Function set 001 BW N F - -
00050     writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off 
00051     writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00052     cls();
00053     
00054     // Set the PWM period to 20ms
00055     _bl.period_ms(1);
00056     _bl.write(0.0);
00057 }
00058 
00059 void freetronicsLCDShield::setCursorPosition (int line, int col) {
00060     // Set the new cursorposition
00061     writeCommand(0x80 + (line * 0x40) + col);
00062 }
00063 
00064 void freetronicsLCDShield::setBackLight (bool blStatus) {
00065     // Switch the backlight on (true) or off (false)
00066     _bl = (blStatus) ? 1.0 : 0.0;
00067 }
00068 
00069 void freetronicsLCDShield::setBackLight (float blIntensity) {
00070     // Switch the backlight on (true) or off (false)
00071     _bl = blIntensity;
00072 }
00073 
00074 void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
00075     int tmp = 0;
00076     
00077     if (blink) tmp = 0x01;
00078     if (cStatus) tmp |= 0x02; 
00079     writeCommand(0x0C + tmp);
00080 }
00081 
00082 void freetronicsLCDShield::shift(bool direction) {
00083     if(direction == LEFT) shiftLeft();
00084     else shiftRight();
00085 }
00086 
00087 void freetronicsLCDShield::shiftLeft(void) {
00088     writeCommand(0x18 + 0x04);
00089 }
00090 
00091 void freetronicsLCDShield::shiftRight(void) {
00092     writeCommand(0x18);
00093 }
00094 
00095 void freetronicsLCDShield::cls(void) {
00096     // Clear the display and place the cursor at 0, 0
00097     writeCommand(0x01);
00098     wait(0.00164f);
00099 }
00100 
00101 void freetronicsLCDShield::home(void) {
00102     // Undo shift operations and place cursor at 0,0
00103     writeCommand(0x02);
00104     wait(0.00164f);
00105 }
00106 
00107 void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) {
00108     // Write the address only once, it is autoincremented 
00109     writeCommand(0x40 | address);
00110 
00111     // Write the data
00112     for(int i = 0; i < nbytes; i++) {
00113         writeData(*ptr++);
00114     }
00115 }
00116 
00117 // Low level output functions
00118 void freetronicsLCDShield::writeByte (int byte) {
00119     // Split the byte in high and low nibble, write high nibble first
00120     _d = byte >> 4;
00121     PULSE_E;
00122     _d = byte >> 0;
00123     PULSE_E;
00124     // Most instructions take 40us
00125     wait(0.000040f);  
00126 }
00127 
00128 void freetronicsLCDShield::writeData (int data) {
00129     _rs = true;
00130     writeByte(data);
00131 } 
00132 
00133 void freetronicsLCDShield::writeCommand (int command) {
00134     _rs = false;
00135     writeByte(command);
00136 } 
00137 
00138 float freetronicsLCDShield::readButton(void) {
00139     return(_a0.read());
00140 }
00141 
00142 freetronicsLCDShield::ShieldButton freetronicsLCDShield::pressedButton(void) {
00143     freetronicsLCDShield::ShieldButton retVal = freetronicsLCDShield::None;
00144     float buttonVal = readButton();
00145     for( unsigned i = 0; i < FREETRONICSLCDSHIELD_BUTTON_COUNT; i++ )
00146     {
00147         if( buttonVal > buttonThresholds[ i ] )
00148         {
00149             retVal = (ShieldButton)i;
00150             break;
00151         }
00152     }    
00153     return retVal;
00154 }
00155 
00156 // Virtual functions for stream
00157 int freetronicsLCDShield::_putc(int value) {   
00158     writeData(value);
00159     return value;
00160 }
00161 
00162 int freetronicsLCDShield::_getc() {
00163     return -1;
00164 }