Hotboards_SpiLcd.cpp - Library for write and control and lcd with spi interfaces and the ST7032 controller. Base on Arduino's Liquid Cristal library

Dependents:   Hotboards_SpiLcd-Hello_World Hotboards_SpiLcd-Writing_In_Diferent_Rows Hotboards_SpiLcd_Scrolling_Text Hotboards_SpiLcd_AutoScroll ... more

Committer:
Hotboards
Date:
Thu Jan 28 22:45:39 2016 +0000
Revision:
0:1d03652e9f7a
Child:
1:b777b6147d99
Hotboards_SpiLcd - Library for write and control and lcd with spi interfaces and the ST7032 controller. Base on Arduino's Liquid Cristal library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:1d03652e9f7a 1 /*
Hotboards 0:1d03652e9f7a 2 Hotboards_SpiLcd.cpp - Library for write and control and lcd with spi interfaces and the ST7032 controller.
Hotboards 0:1d03652e9f7a 3 base on Arduino's Liquid Cristal library
Hotboards 0:1d03652e9f7a 4 Library ported by diego from Hotboards January 16, 2016.
Hotboards 0:1d03652e9f7a 5 and originally cretaed by
Hotboards 0:1d03652e9f7a 6 by David A. Mellis
Hotboards 0:1d03652e9f7a 7 library modified 5 Jul 2009
Hotboards 0:1d03652e9f7a 8 by Limor Fried (http://www.ladyada.net)
Hotboards 0:1d03652e9f7a 9 example added 9 Jul 2009
Hotboards 0:1d03652e9f7a 10 by Tom Igoe
Hotboards 0:1d03652e9f7a 11 modified 22 Nov 2010
Hotboards 0:1d03652e9f7a 12 by Tom Igoe
Hotboards 0:1d03652e9f7a 13 Released into the public domain.
Hotboards 0:1d03652e9f7a 14 */
Hotboards 0:1d03652e9f7a 15
Hotboards 0:1d03652e9f7a 16 #include "Hotboards_SpiLcd.h"
Hotboards 0:1d03652e9f7a 17
Hotboards 0:1d03652e9f7a 18 Hotboards_SpiLcd::Hotboards_SpiLcd( SPI &spi, PinName cs, PinName rs, PinName rst )
Hotboards 0:1d03652e9f7a 19 : _spi(spi), _cs_pin(cs), _rs_pin(rs), _rst_pin(rst)
Hotboards 0:1d03652e9f7a 20 {
Hotboards 0:1d03652e9f7a 21 _cs_pin = 1; /*chip select deactivated*/
Hotboards 0:1d03652e9f7a 22 _rs_pin = 0;
Hotboards 0:1d03652e9f7a 23 _rst_pin = 1; /*reset not active*/
Hotboards 0:1d03652e9f7a 24 }
Hotboards 0:1d03652e9f7a 25
Hotboards 0:1d03652e9f7a 26 /*
Hotboards 0:1d03652e9f7a 27 * send commands to initialize internal lcd controller
Hotboards 0:1d03652e9f7a 28 */
Hotboards 0:1d03652e9f7a 29 void Hotboards_SpiLcd::init( void )
Hotboards 0:1d03652e9f7a 30 {
Hotboards 0:1d03652e9f7a 31 _rst_pin = 0;
Hotboards 0:1d03652e9f7a 32 wait( 0.002 );
Hotboards 0:1d03652e9f7a 33 _rst_pin = 1;
Hotboards 0:1d03652e9f7a 34 wait( 0.02 );
Hotboards 0:1d03652e9f7a 35 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 36 wait( 0.002 );
Hotboards 0:1d03652e9f7a 37 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 38 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 39
Hotboards 0:1d03652e9f7a 40 // configure default display functions, two lines, 5x8 charcters
Hotboards 0:1d03652e9f7a 41 _displayfunction = HT_SPILCD_8BITMODE | HT_SPILCD_2LINE | HT_SPILCD_5x8DOTS | HT_SPILCD_EXTINST;
Hotboards 0:1d03652e9f7a 42 command( HT_SPILCD_FUNCTIONSET | _displayfunction );
Hotboards 0:1d03652e9f7a 43
Hotboards 0:1d03652e9f7a 44 command( 0x14 );/*internaloscfrequency*/
Hotboards 0:1d03652e9f7a 45 command( 0x56 );/*powercontroll*/
Hotboards 0:1d03652e9f7a 46 command( 0x6d );/*followercontrol*/
Hotboards 0:1d03652e9f7a 47 wait( 0.2 );
Hotboards 0:1d03652e9f7a 48 command( 0x70 );/*constrast*/
Hotboards 0:1d03652e9f7a 49
Hotboards 0:1d03652e9f7a 50 // reenable shift functions disbling extended instructions
Hotboards 0:1d03652e9f7a 51 _displayfunction &= ~HT_SPILCD_EXTINST;
Hotboards 0:1d03652e9f7a 52 command( HT_SPILCD_FUNCTIONSET | _displayfunction );
Hotboards 0:1d03652e9f7a 53
Hotboards 0:1d03652e9f7a 54 // turn the display on with no cursor or blinking default
Hotboards 0:1d03652e9f7a 55 _displaycontrol = HT_SPILCD_DISPLAYON | HT_SPILCD_CURSOROFF | HT_SPILCD_BLINKOFF;
Hotboards 0:1d03652e9f7a 56 display( );
Hotboards 0:1d03652e9f7a 57
Hotboards 0:1d03652e9f7a 58 // Initialize to default text direction (for romance languages)
Hotboards 0:1d03652e9f7a 59 _displaymode = HT_SPILCD_ENTRYLEFT | HT_SPILCD_ENTRYSHIFTDECREMENT;
Hotboards 0:1d03652e9f7a 60 // set the entry mode
Hotboards 0:1d03652e9f7a 61 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 62 clear( );/*clearscreen*/
Hotboards 0:1d03652e9f7a 63 }
Hotboards 0:1d03652e9f7a 64
Hotboards 0:1d03652e9f7a 65 /*
Hotboards 0:1d03652e9f7a 66 * Clears the LCD screen and positions the cursor in the upper-left corner.
Hotboards 0:1d03652e9f7a 67 */
Hotboards 0:1d03652e9f7a 68 void Hotboards_SpiLcd::clear( void )
Hotboards 0:1d03652e9f7a 69 {
Hotboards 0:1d03652e9f7a 70 command( HT_SPILCD_CLEARDISPLAY ); // clear display, set cursor position to zero
Hotboards 0:1d03652e9f7a 71 wait( 0.002 ); // this command takes a long time!
Hotboards 0:1d03652e9f7a 72 }
Hotboards 0:1d03652e9f7a 73
Hotboards 0:1d03652e9f7a 74 /*
Hotboards 0:1d03652e9f7a 75 * Positions the cursor in the upper-left of the LCD. That is, use that location
Hotboards 0:1d03652e9f7a 76 * in outputting subsequent text to the display. To also clear the display, use
Hotboards 0:1d03652e9f7a 77 * the clear() function instead
Hotboards 0:1d03652e9f7a 78 */
Hotboards 0:1d03652e9f7a 79 void Hotboards_SpiLcd::home( void )
Hotboards 0:1d03652e9f7a 80 {
Hotboards 0:1d03652e9f7a 81 command( HT_SPILCD_RETURNHOME ); // set cursor position to zero
Hotboards 0:1d03652e9f7a 82 wait( 0.002 ); // this command takes a long time!
Hotboards 0:1d03652e9f7a 83 }
Hotboards 0:1d03652e9f7a 84
Hotboards 0:1d03652e9f7a 85 /*
Hotboards 0:1d03652e9f7a 86 * Turns off the LCD display, without losing the text currently shown on it.
Hotboards 0:1d03652e9f7a 87 */
Hotboards 0:1d03652e9f7a 88 void Hotboards_SpiLcd::noDisplay( void )
Hotboards 0:1d03652e9f7a 89 {
Hotboards 0:1d03652e9f7a 90 _displaycontrol &= ~HT_SPILCD_DISPLAYON;
Hotboards 0:1d03652e9f7a 91 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 92 }
Hotboards 0:1d03652e9f7a 93
Hotboards 0:1d03652e9f7a 94 /*
Hotboards 0:1d03652e9f7a 95 * Turns on the LCD display, after it's been turned off with noDisplay().
Hotboards 0:1d03652e9f7a 96 * This will restore the text (and cursor) that was on the display.
Hotboards 0:1d03652e9f7a 97 */
Hotboards 0:1d03652e9f7a 98 void Hotboards_SpiLcd::display( void )
Hotboards 0:1d03652e9f7a 99 {
Hotboards 0:1d03652e9f7a 100 _displaycontrol |= HT_SPILCD_DISPLAYON;
Hotboards 0:1d03652e9f7a 101 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 102 }
Hotboards 0:1d03652e9f7a 103
Hotboards 0:1d03652e9f7a 104 /*
Hotboards 0:1d03652e9f7a 105 * Turns off the blinking LCD cursor.
Hotboards 0:1d03652e9f7a 106 */
Hotboards 0:1d03652e9f7a 107 void Hotboards_SpiLcd::noBlink( void )
Hotboards 0:1d03652e9f7a 108 {
Hotboards 0:1d03652e9f7a 109 _displaycontrol &= ~HT_SPILCD_BLINKON;
Hotboards 0:1d03652e9f7a 110 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 111 }
Hotboards 0:1d03652e9f7a 112
Hotboards 0:1d03652e9f7a 113 /*
Hotboards 0:1d03652e9f7a 114 * Display the blinking LCD cursor. If used in combination with the cursor() function,
Hotboards 0:1d03652e9f7a 115 * the result will depend on the particular display.
Hotboards 0:1d03652e9f7a 116 */
Hotboards 0:1d03652e9f7a 117 void Hotboards_SpiLcd::blink( void )
Hotboards 0:1d03652e9f7a 118 {
Hotboards 0:1d03652e9f7a 119 _displaycontrol |= HT_SPILCD_BLINKON;
Hotboards 0:1d03652e9f7a 120 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 121 }
Hotboards 0:1d03652e9f7a 122
Hotboards 0:1d03652e9f7a 123
Hotboards 0:1d03652e9f7a 124 /*
Hotboards 0:1d03652e9f7a 125 * Hides the LCD cursor.
Hotboards 0:1d03652e9f7a 126 */
Hotboards 0:1d03652e9f7a 127 void Hotboards_SpiLcd::noCursor( void )
Hotboards 0:1d03652e9f7a 128 {
Hotboards 0:1d03652e9f7a 129 _displaycontrol &= ~HT_SPILCD_CURSORON;
Hotboards 0:1d03652e9f7a 130 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 131 }
Hotboards 0:1d03652e9f7a 132
Hotboards 0:1d03652e9f7a 133 /*
Hotboards 0:1d03652e9f7a 134 * Display the LCD cursor: an underscore (line) at the position to which the
Hotboards 0:1d03652e9f7a 135 * next character will be written.
Hotboards 0:1d03652e9f7a 136 */
Hotboards 0:1d03652e9f7a 137 void Hotboards_SpiLcd::cursor( void )
Hotboards 0:1d03652e9f7a 138 {
Hotboards 0:1d03652e9f7a 139 _displaycontrol |= HT_SPILCD_CURSORON;
Hotboards 0:1d03652e9f7a 140 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 141
Hotboards 0:1d03652e9f7a 142 }
Hotboards 0:1d03652e9f7a 143
Hotboards 0:1d03652e9f7a 144 /*
Hotboards 0:1d03652e9f7a 145 * Scrolls the contents of the display (text and cursor) one space to the left.
Hotboards 0:1d03652e9f7a 146 */
Hotboards 0:1d03652e9f7a 147 void Hotboards_SpiLcd::scrollDisplayLeft( void )
Hotboards 0:1d03652e9f7a 148 {
Hotboards 0:1d03652e9f7a 149 command( HT_SPILCD_CURSORSHIFT | HT_SPILCD_DISPLAYMOVE | HT_SPILCD_MOVELEFT );
Hotboards 0:1d03652e9f7a 150 }
Hotboards 0:1d03652e9f7a 151
Hotboards 0:1d03652e9f7a 152 /*
Hotboards 0:1d03652e9f7a 153 * Scrolls the contents of the display (text and cursor) one space to the right.
Hotboards 0:1d03652e9f7a 154 */
Hotboards 0:1d03652e9f7a 155 void Hotboards_SpiLcd::scrollDisplayRight( void )
Hotboards 0:1d03652e9f7a 156 {
Hotboards 0:1d03652e9f7a 157 command( HT_SPILCD_CURSORSHIFT | HT_SPILCD_DISPLAYMOVE | HT_SPILCD_MOVERIGHT );
Hotboards 0:1d03652e9f7a 158 }
Hotboards 0:1d03652e9f7a 159
Hotboards 0:1d03652e9f7a 160 /*
Hotboards 0:1d03652e9f7a 161 * Set the direction for text written to the LCD to left-to-right, the default.
Hotboards 0:1d03652e9f7a 162 * This means that subsequent characters written to the display will go from left to right,
Hotboards 0:1d03652e9f7a 163 * but does not affect previously-output text.
Hotboards 0:1d03652e9f7a 164 */
Hotboards 0:1d03652e9f7a 165 void Hotboards_SpiLcd::leftToRight( void )
Hotboards 0:1d03652e9f7a 166 {
Hotboards 0:1d03652e9f7a 167 _displaymode |= HT_SPILCD_ENTRYLEFT;
Hotboards 0:1d03652e9f7a 168 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 169 }
Hotboards 0:1d03652e9f7a 170
Hotboards 0:1d03652e9f7a 171 /*
Hotboards 0:1d03652e9f7a 172 * Set the direction for text written to the LCD to right-to-left (the default is left-to-right).
Hotboards 0:1d03652e9f7a 173 * This means that subsequent characters written to the display will go from right to left,
Hotboards 0:1d03652e9f7a 174 * but does not affect previously-output text.
Hotboards 0:1d03652e9f7a 175 */
Hotboards 0:1d03652e9f7a 176 void Hotboards_SpiLcd::rightToLeft( void )
Hotboards 0:1d03652e9f7a 177 {
Hotboards 0:1d03652e9f7a 178 _displaymode &= ~HT_SPILCD_ENTRYLEFT;
Hotboards 0:1d03652e9f7a 179 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 180 }
Hotboards 0:1d03652e9f7a 181
Hotboards 0:1d03652e9f7a 182 /*
Hotboards 0:1d03652e9f7a 183 * Turns on automatic scrolling of the LCD. This causes each character
Hotboards 0:1d03652e9f7a 184 * output to the display to push previous characters over by one space.
Hotboards 0:1d03652e9f7a 185 * If the current text direction is left-to-right (the default),
Hotboards 0:1d03652e9f7a 186 * the display scrolls to the left; if the current direction is right-to-left,
Hotboards 0:1d03652e9f7a 187 * the display scrolls to the right. This has the effect of outputting
Hotboards 0:1d03652e9f7a 188 * each new character to the same location on the LCD.
Hotboards 0:1d03652e9f7a 189 */
Hotboards 0:1d03652e9f7a 190 void Hotboards_SpiLcd::autoscroll( void )
Hotboards 0:1d03652e9f7a 191 {
Hotboards 0:1d03652e9f7a 192 _displaymode |= HT_SPILCD_ENTRYSHIFTINCREMENT;
Hotboards 0:1d03652e9f7a 193 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 194 }
Hotboards 0:1d03652e9f7a 195
Hotboards 0:1d03652e9f7a 196 /*
Hotboards 0:1d03652e9f7a 197 * Turns off automatic scrolling of the LCD.
Hotboards 0:1d03652e9f7a 198 */
Hotboards 0:1d03652e9f7a 199 void Hotboards_SpiLcd::noAutoscroll( void )
Hotboards 0:1d03652e9f7a 200 {
Hotboards 0:1d03652e9f7a 201 _displaymode &= ~HT_SPILCD_ENTRYSHIFTINCREMENT;
Hotboards 0:1d03652e9f7a 202 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 203 }
Hotboards 0:1d03652e9f7a 204
Hotboards 0:1d03652e9f7a 205 /*
Hotboards 0:1d03652e9f7a 206 * Position the LCD cursor; that is, set the location at which subsequent
Hotboards 0:1d03652e9f7a 207 * text written to the LCD will be displayed.
Hotboards 0:1d03652e9f7a 208 * col: the column at which to position the cursor (with 0 being the first column)
Hotboards 0:1d03652e9f7a 209 * row: the row at which to position the cursor (with 0 being the first row)
Hotboards 0:1d03652e9f7a 210 */
Hotboards 0:1d03652e9f7a 211 void Hotboards_SpiLcd::setCursor( uint8_t col, uint8_t row )
Hotboards 0:1d03652e9f7a 212 {
Hotboards 0:1d03652e9f7a 213 const uint8_t Buffer[ 2 ]= { 0x00u, 0x40u };
Hotboards 0:1d03652e9f7a 214 uint8_t address;
Hotboards 0:1d03652e9f7a 215
Hotboards 0:1d03652e9f7a 216 if( row > 1 )
Hotboards 0:1d03652e9f7a 217 {
Hotboards 0:1d03652e9f7a 218 row = 1;
Hotboards 0:1d03652e9f7a 219 }
Hotboards 0:1d03652e9f7a 220 address = Buffer[ row ] + col;
Hotboards 0:1d03652e9f7a 221 command( 0x80 | address );
Hotboards 0:1d03652e9f7a 222 }
Hotboards 0:1d03652e9f7a 223
Hotboards 0:1d03652e9f7a 224
Hotboards 0:1d03652e9f7a 225 /*********** mid level commands, for sending data/cmds */
Hotboards 0:1d03652e9f7a 226 /*
Hotboards 0:1d03652e9f7a 227 * Write a character to the LCD.
Hotboards 0:1d03652e9f7a 228 * data: the character to write to the display
Hotboards 0:1d03652e9f7a 229 */
Hotboards 0:1d03652e9f7a 230 inline void Hotboards_SpiLcd::command( uint8_t value )
Hotboards 0:1d03652e9f7a 231 {
Hotboards 0:1d03652e9f7a 232 _cs_pin = 0;
Hotboards 0:1d03652e9f7a 233 send( value, 0 );
Hotboards 0:1d03652e9f7a 234 _cs_pin = 1;
Hotboards 0:1d03652e9f7a 235 wait( 0.00003 ); // commands need > 27us to settle
Hotboards 0:1d03652e9f7a 236 }
Hotboards 0:1d03652e9f7a 237
Hotboards 0:1d03652e9f7a 238 /*
Hotboards 0:1d03652e9f7a 239 * Write a character to the LCD.
Hotboards 0:1d03652e9f7a 240 */
Hotboards 0:1d03652e9f7a 241 int Hotboards_SpiLcd::_putc(int value)
Hotboards 0:1d03652e9f7a 242 {
Hotboards 0:1d03652e9f7a 243 _cs_pin = 0;
Hotboards 0:1d03652e9f7a 244 send( value, 1 );
Hotboards 0:1d03652e9f7a 245 _cs_pin = 1;
Hotboards 0:1d03652e9f7a 246 wait( 0.00003 ); // commands need > 27us to settle
Hotboards 0:1d03652e9f7a 247 return 1;
Hotboards 0:1d03652e9f7a 248 }
Hotboards 0:1d03652e9f7a 249
Hotboards 0:1d03652e9f7a 250 int Hotboards_SpiLcd::_getc(void)
Hotboards 0:1d03652e9f7a 251 {
Hotboards 0:1d03652e9f7a 252 return -1;
Hotboards 0:1d03652e9f7a 253 }
Hotboards 0:1d03652e9f7a 254
Hotboards 0:1d03652e9f7a 255 /************ low level data pushing commands **********/
Hotboards 0:1d03652e9f7a 256
Hotboards 0:1d03652e9f7a 257 // write either command or data, with automatic 4/8-bit selection
Hotboards 0:1d03652e9f7a 258 void Hotboards_SpiLcd::send( uint8_t value, uint8_t mode )
Hotboards 0:1d03652e9f7a 259 {
Hotboards 0:1d03652e9f7a 260 _rs_pin = mode;
Hotboards 0:1d03652e9f7a 261 _spi.write( value );
Hotboards 0:1d03652e9f7a 262 }