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:
Sat Mar 05 19:17:23 2016 +0000
Revision:
3:e16488b64ef7
Parent:
2:9673849ef2e9
update docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:1d03652e9f7a 1 /*
Hotboards 1:b777b6147d99 2 Hotboards_SpiLcd.cpp - Library to control and write an lcd with spi interface and 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 #ifndef Hotboards_SpiLcd_h
Hotboards 0:1d03652e9f7a 17 #define Hotboards_SpiLcd_h
Hotboards 0:1d03652e9f7a 18
Hotboards 0:1d03652e9f7a 19 #include "mbed.h"
Hotboards 0:1d03652e9f7a 20
Hotboards 0:1d03652e9f7a 21 // commands
Hotboards 0:1d03652e9f7a 22 #define HT_SPILCD_CLEARDISPLAY 0x01
Hotboards 0:1d03652e9f7a 23 #define HT_SPILCD_RETURNHOME 0x02
Hotboards 0:1d03652e9f7a 24 #define HT_SPILCD_ENTRYMODESET 0x04
Hotboards 0:1d03652e9f7a 25 #define HT_SPILCD_DISPLAYCONTROL 0x08
Hotboards 0:1d03652e9f7a 26 #define HT_SPILCD_CURSORSHIFT 0x10
Hotboards 0:1d03652e9f7a 27 #define HT_SPILCD_FUNCTIONSET 0x20
Hotboards 0:1d03652e9f7a 28 #define HT_SPILCD_SETCGRAMADDR 0x40
Hotboards 0:1d03652e9f7a 29 #define HT_SPILCD_SETDDRAMADDR 0x80
Hotboards 0:1d03652e9f7a 30
Hotboards 0:1d03652e9f7a 31 // flags for display entry mode
Hotboards 0:1d03652e9f7a 32 #define HT_SPILCD_ENTRYRIGHT 0x00
Hotboards 0:1d03652e9f7a 33 #define HT_SPILCD_ENTRYLEFT 0x02
Hotboards 0:1d03652e9f7a 34 #define HT_SPILCD_ENTRYSHIFTINCREMENT 0x01
Hotboards 0:1d03652e9f7a 35 #define HT_SPILCD_ENTRYSHIFTDECREMENT 0x00
Hotboards 0:1d03652e9f7a 36
Hotboards 0:1d03652e9f7a 37 // flags for display on/off control
Hotboards 0:1d03652e9f7a 38 #define HT_SPILCD_DISPLAYON 0x04
Hotboards 0:1d03652e9f7a 39 #define HT_SPILCD_DISPLAYOFF 0x00
Hotboards 0:1d03652e9f7a 40 #define HT_SPILCD_CURSORON 0x02
Hotboards 0:1d03652e9f7a 41 #define HT_SPILCD_CURSOROFF 0x00
Hotboards 0:1d03652e9f7a 42 #define HT_SPILCD_BLINKON 0x01
Hotboards 0:1d03652e9f7a 43 #define HT_SPILCD_BLINKOFF 0x00
Hotboards 0:1d03652e9f7a 44
Hotboards 0:1d03652e9f7a 45 // flags for display/cursor shift
Hotboards 0:1d03652e9f7a 46 #define HT_SPILCD_DISPLAYMOVE 0x08
Hotboards 0:1d03652e9f7a 47 #define HT_SPILCD_CURSORMOVE 0x00
Hotboards 0:1d03652e9f7a 48 #define HT_SPILCD_MOVERIGHT 0x04
Hotboards 0:1d03652e9f7a 49 #define HT_SPILCD_MOVELEFT 0x00
Hotboards 0:1d03652e9f7a 50
Hotboards 0:1d03652e9f7a 51 // flags for function set
Hotboards 0:1d03652e9f7a 52 #define HT_SPILCD_8BITMODE 0x10
Hotboards 0:1d03652e9f7a 53 #define HT_SPILCD_4BITMODE 0x00
Hotboards 0:1d03652e9f7a 54 #define HT_SPILCD_2LINE 0x08
Hotboards 0:1d03652e9f7a 55 #define HT_SPILCD_1LINE 0x00
Hotboards 0:1d03652e9f7a 56 #define HT_SPILCD_5x16DOTS 0x04
Hotboards 0:1d03652e9f7a 57 #define HT_SPILCD_5x8DOTS 0x00
Hotboards 0:1d03652e9f7a 58 #define HT_SPILCD_EXTINST 0x01
Hotboards 0:1d03652e9f7a 59 #define HT_SPILCD_NORMINST 0x00
Hotboards 0:1d03652e9f7a 60
Hotboards 0:1d03652e9f7a 61
Hotboards 2:9673849ef2e9 62 /** Hotboards_SpiLcd class.
Hotboards 2:9673849ef2e9 63 * Used to control lcd with spi serial interface
Hotboards 2:9673849ef2e9 64 *
Hotboards 2:9673849ef2e9 65 * Example:
Hotboards 2:9673849ef2e9 66 * @code
Hotboards 2:9673849ef2e9 67 * #include "mbed.h"
Hotboards 2:9673849ef2e9 68 * #include "Hotboards_SpiLcd.h"
Hotboards 2:9673849ef2e9 69 *
Hotboards 3:e16488b64ef7 70 * SPI device( PB_5, NC, PB_3 ); // mosi, miso, sclk
Hotboards 3:e16488b64ef7 71 * Hotboards_SpiLcd lcd( device, PC_0, PC_1, PC_2 ); //spi, cs, rs, rst
Hotboards 2:9673849ef2e9 72 *
Hotboards 2:9673849ef2e9 73 * int main( void )
Hotboards 2:9673849ef2e9 74 * {
Hotboards 2:9673849ef2e9 75 * lcd.init( );
Hotboards 2:9673849ef2e9 76 * lcd.printf( "Hola mundo" );
Hotboards 2:9673849ef2e9 77 * }
Hotboards 2:9673849ef2e9 78 * @endcode
Hotboards 2:9673849ef2e9 79 */
Hotboards 0:1d03652e9f7a 80 class Hotboards_SpiLcd : public Stream
Hotboards 0:1d03652e9f7a 81 {
Hotboards 0:1d03652e9f7a 82 public:
Hotboards 0:1d03652e9f7a 83
Hotboards 2:9673849ef2e9 84 /** Create Hotboards_SpiLcd instance
Hotboards 3:e16488b64ef7 85 * @param spi spi peripheral to be use to control the lcd
Hotboards 3:e16488b64ef7 86 * @param cs pin to control the chip select signal
Hotboards 3:e16488b64ef7 87 * @param rs pin to control the data/command signal
Hotboards 3:e16488b64ef7 88 * @param rst pin to control the reset signal
Hotboards 3:e16488b64ef7 89 *
Hotboards 3:e16488b64ef7 90 * Example:
Hotboards 3:e16488b64ef7 91 * @code
Hotboards 3:e16488b64ef7 92 * // we need to init an spi peripheral first
Hotboards 3:e16488b64ef7 93 * SPI device( PB_5, NC, PB_3 ); // mosi, miso, sclk
Hotboards 3:e16488b64ef7 94 * // then we can create the instance
Hotboards 3:e16488b64ef7 95 * Hotboards_SpiLcd lcd( device, PC_0, PC_1, PC_2 ); //spi, cs, rs, rst
Hotboards 3:e16488b64ef7 96 * @endcode
Hotboards 3:e16488b64ef7 97 */
Hotboards 0:1d03652e9f7a 98 Hotboards_SpiLcd( SPI &spi, PinName cs, PinName rs, PinName rst );
Hotboards 2:9673849ef2e9 99
Hotboards 3:e16488b64ef7 100 /** Send commands to initialize internal lcd controller
Hotboards 3:e16488b64ef7 101 *
Hotboards 3:e16488b64ef7 102 * Example:
Hotboards 3:e16488b64ef7 103 * @code
Hotboards 3:e16488b64ef7 104 * // After create the instance we init
Hotboards 3:e16488b64ef7 105 * lcd.begin();
Hotboards 3:e16488b64ef7 106 * @endcode
Hotboards 2:9673849ef2e9 107 */
Hotboards 0:1d03652e9f7a 108 void init( void );
Hotboards 2:9673849ef2e9 109
Hotboards 2:9673849ef2e9 110 /** Clears the LCD screen and positions the cursor in the upper-left corner.
Hotboards 3:e16488b64ef7 111 *
Hotboards 3:e16488b64ef7 112 * Example:
Hotboards 3:e16488b64ef7 113 * @code
Hotboards 3:e16488b64ef7 114 * lcd.clear();
Hotboards 3:e16488b64ef7 115 * @endcode
Hotboards 2:9673849ef2e9 116 */
Hotboards 0:1d03652e9f7a 117 void clear( void );
Hotboards 2:9673849ef2e9 118
Hotboards 2:9673849ef2e9 119 /** Positions the cursor in the upper-left of the LCD. That is, use that location
Hotboards 2:9673849ef2e9 120 * in outputting subsequent text to the display. To also clear the display, use
Hotboards 2:9673849ef2e9 121 * the clear() function instead
Hotboards 3:e16488b64ef7 122 *
Hotboards 3:e16488b64ef7 123 * Example:
Hotboards 3:e16488b64ef7 124 * @code
Hotboards 3:e16488b64ef7 125 * lcd.home();
Hotboards 3:e16488b64ef7 126 * @endcode
Hotboards 2:9673849ef2e9 127 */
Hotboards 0:1d03652e9f7a 128 void home( void );
Hotboards 0:1d03652e9f7a 129
Hotboards 2:9673849ef2e9 130 /** Turns off the LCD display, without losing the text currently shown on it.
Hotboards 3:e16488b64ef7 131 *
Hotboards 3:e16488b64ef7 132 * Example:
Hotboards 3:e16488b64ef7 133 * @code
Hotboards 3:e16488b64ef7 134 * lcd.noDisplay();
Hotboards 3:e16488b64ef7 135 * @endcode
Hotboards 2:9673849ef2e9 136 */
Hotboards 0:1d03652e9f7a 137 void noDisplay( void );
Hotboards 2:9673849ef2e9 138
Hotboards 2:9673849ef2e9 139 /** Turns on the LCD display, after it's been turned off with noDisplay().
Hotboards 2:9673849ef2e9 140 * This will restore the text (and cursor) that was on the display.
Hotboards 3:e16488b64ef7 141 *
Hotboards 3:e16488b64ef7 142 * Example:
Hotboards 3:e16488b64ef7 143 * @code
Hotboards 3:e16488b64ef7 144 * lcd.display();
Hotboards 3:e16488b64ef7 145 * @endcode
Hotboards 2:9673849ef2e9 146 */
Hotboards 0:1d03652e9f7a 147 void display( void );
Hotboards 2:9673849ef2e9 148
Hotboards 2:9673849ef2e9 149 /** Turns off the blinking LCD cursor.
Hotboards 3:e16488b64ef7 150 *
Hotboards 3:e16488b64ef7 151 * Example:
Hotboards 3:e16488b64ef7 152 * @code
Hotboards 3:e16488b64ef7 153 * lcd.noBlink();
Hotboards 3:e16488b64ef7 154 * @endcode
Hotboards 2:9673849ef2e9 155 */
Hotboards 0:1d03652e9f7a 156 void noBlink( void );
Hotboards 2:9673849ef2e9 157
Hotboards 2:9673849ef2e9 158 /** Display the blinking LCD cursor. If used in combination with the cursor() function,
Hotboards 2:9673849ef2e9 159 * the result will depend on the particular display.
Hotboards 3:e16488b64ef7 160 *
Hotboards 3:e16488b64ef7 161 * Example:
Hotboards 3:e16488b64ef7 162 * @code
Hotboards 3:e16488b64ef7 163 * lcd.blink();
Hotboards 3:e16488b64ef7 164 * @endcode
Hotboards 2:9673849ef2e9 165 */
Hotboards 0:1d03652e9f7a 166 void blink( void );
Hotboards 2:9673849ef2e9 167
Hotboards 2:9673849ef2e9 168 /** Hides the LCD cursor.
Hotboards 3:e16488b64ef7 169 *
Hotboards 3:e16488b64ef7 170 * Example:
Hotboards 3:e16488b64ef7 171 * @code
Hotboards 3:e16488b64ef7 172 * lcd.noCursor();
Hotboards 3:e16488b64ef7 173 * @endcode
Hotboards 2:9673849ef2e9 174 */
Hotboards 0:1d03652e9f7a 175 void noCursor( void );
Hotboards 2:9673849ef2e9 176
Hotboards 2:9673849ef2e9 177 /** Display the LCD cursor: an underscore (line) at the position to which the
Hotboards 2:9673849ef2e9 178 * next character will be written.
Hotboards 3:e16488b64ef7 179 *
Hotboards 3:e16488b64ef7 180 * Example:
Hotboards 3:e16488b64ef7 181 * @code
Hotboards 3:e16488b64ef7 182 * lcd.cursor();
Hotboards 3:e16488b64ef7 183 * @endcode
Hotboards 2:9673849ef2e9 184 */
Hotboards 0:1d03652e9f7a 185 void cursor( void );
Hotboards 2:9673849ef2e9 186
Hotboards 2:9673849ef2e9 187 /** Scrolls the contents of the display (text and cursor) one space to the left.
Hotboards 3:e16488b64ef7 188 *
Hotboards 3:e16488b64ef7 189 * Example:
Hotboards 3:e16488b64ef7 190 * @code
Hotboards 3:e16488b64ef7 191 * lcd.scrollDisplayLeft();
Hotboards 3:e16488b64ef7 192 * @endcode
Hotboards 2:9673849ef2e9 193 */
Hotboards 0:1d03652e9f7a 194 void scrollDisplayLeft( void );
Hotboards 2:9673849ef2e9 195
Hotboards 2:9673849ef2e9 196 /** Scrolls the contents of the display (text and cursor) one space to the right.
Hotboards 3:e16488b64ef7 197 *
Hotboards 3:e16488b64ef7 198 * Example:
Hotboards 3:e16488b64ef7 199 * @code
Hotboards 3:e16488b64ef7 200 * lcd.scrollDisplayRight();
Hotboards 3:e16488b64ef7 201 * @endcode
Hotboards 2:9673849ef2e9 202 */
Hotboards 0:1d03652e9f7a 203 void scrollDisplayRight( void );
Hotboards 2:9673849ef2e9 204
Hotboards 2:9673849ef2e9 205 /** Set the direction for text written to the LCD to left-to-right, the default.
Hotboards 2:9673849ef2e9 206 * This means that subsequent characters written to the display will go from left to right,
Hotboards 2:9673849ef2e9 207 * but does not affect previously-output text.
Hotboards 3:e16488b64ef7 208 *
Hotboards 3:e16488b64ef7 209 * Example:
Hotboards 3:e16488b64ef7 210 * @code
Hotboards 3:e16488b64ef7 211 * lcd.leftToRight();
Hotboards 3:e16488b64ef7 212 * @endcode
Hotboards 2:9673849ef2e9 213 */
Hotboards 0:1d03652e9f7a 214 void leftToRight( void );
Hotboards 2:9673849ef2e9 215
Hotboards 2:9673849ef2e9 216 /** Set the direction for text written to the LCD to right-to-left (the default is left-to-right).
Hotboards 2:9673849ef2e9 217 * This means that subsequent characters written to the display will go from right to left,
Hotboards 2:9673849ef2e9 218 * but does not affect previously-output text.
Hotboards 3:e16488b64ef7 219 *
Hotboards 3:e16488b64ef7 220 * Example:
Hotboards 3:e16488b64ef7 221 * @code
Hotboards 3:e16488b64ef7 222 * lcd.rightToLeft();
Hotboards 3:e16488b64ef7 223 * @endcode
Hotboards 2:9673849ef2e9 224 */
Hotboards 0:1d03652e9f7a 225 void rightToLeft( void );
Hotboards 2:9673849ef2e9 226
Hotboards 2:9673849ef2e9 227 /** Turns on automatic scrolling of the LCD. This causes each character
Hotboards 2:9673849ef2e9 228 * output to the display to push previous characters over by one space.
Hotboards 2:9673849ef2e9 229 * If the current text direction is left-to-right (the default),
Hotboards 2:9673849ef2e9 230 * the display scrolls to the left; if the current direction is right-to-left,
Hotboards 2:9673849ef2e9 231 * the display scrolls to the right. This has the effect of outputting
Hotboards 2:9673849ef2e9 232 * each new character to the same location on the LCD.
Hotboards 3:e16488b64ef7 233 *
Hotboards 3:e16488b64ef7 234 * Example:
Hotboards 3:e16488b64ef7 235 * @code
Hotboards 3:e16488b64ef7 236 * lcd.autoscroll();
Hotboards 3:e16488b64ef7 237 * @endcode
Hotboards 2:9673849ef2e9 238 */
Hotboards 0:1d03652e9f7a 239 void autoscroll( void );
Hotboards 2:9673849ef2e9 240
Hotboards 2:9673849ef2e9 241 /** Turns off automatic scrolling of the LCD.
Hotboards 3:e16488b64ef7 242 *
Hotboards 3:e16488b64ef7 243 * Example:
Hotboards 3:e16488b64ef7 244 * @code
Hotboards 3:e16488b64ef7 245 * lcd.noAutoscroll();
Hotboards 3:e16488b64ef7 246 * @endcode
Hotboards 2:9673849ef2e9 247 */
Hotboards 0:1d03652e9f7a 248 void noAutoscroll( void );
Hotboards 0:1d03652e9f7a 249
Hotboards 0:1d03652e9f7a 250 //void setRowOffsets( int row1, int row2 );
Hotboards 2:9673849ef2e9 251
Hotboards 0:1d03652e9f7a 252 //void createChar( uint8_t, uint8_t[] );
Hotboards 2:9673849ef2e9 253
Hotboards 2:9673849ef2e9 254 /** Position the LCD cursor; that is, set the location at which subsequent
Hotboards 2:9673849ef2e9 255 * text written to the LCD will be displayed.
Hotboards 2:9673849ef2e9 256 * @param col: the column at which to position the cursor (with 0 being the first column)
Hotboards 2:9673849ef2e9 257 * @param row: the row at which to position the cursor (with 0 being the first row)
Hotboards 3:e16488b64ef7 258 *
Hotboards 3:e16488b64ef7 259 * Example:
Hotboards 3:e16488b64ef7 260 * @code
Hotboards 3:e16488b64ef7 261 * lcd.setCursor( 3, 1 );
Hotboards 3:e16488b64ef7 262 * @endcode
Hotboards 2:9673849ef2e9 263 */
Hotboards 2:9673849ef2e9 264 void setCursor( uint8_t col, uint8_t row );
Hotboards 2:9673849ef2e9 265
Hotboards 3:e16488b64ef7 266 /** Send a command to the LCD.
Hotboards 3:e16488b64ef7 267 * @data: cmd command to send
Hotboards 3:e16488b64ef7 268 *
Hotboards 3:e16488b64ef7 269 * Example:
Hotboards 3:e16488b64ef7 270 * @code
Hotboards 3:e16488b64ef7 271 * // clear screen command
Hotboards 3:e16488b64ef7 272 * lcd.command( 0x30 );
Hotboards 3:e16488b64ef7 273 * @endcode
Hotboards 2:9673849ef2e9 274 */
Hotboards 0:1d03652e9f7a 275 void command( uint8_t );
Hotboards 0:1d03652e9f7a 276 #if DOXYGEN_ONLY
Hotboards 2:9673849ef2e9 277 /** Write a character to the LCD.
Hotboards 2:9673849ef2e9 278 * @param c: character to display
Hotboards 3:e16488b64ef7 279 *
Hotboards 3:e16488b64ef7 280 * Example:
Hotboards 3:e16488b64ef7 281 * @code
Hotboards 3:e16488b64ef7 282 * lcd.putc();
Hotboards 3:e16488b64ef7 283 * @endcode
Hotboards 2:9673849ef2e9 284 */
Hotboards 2:9673849ef2e9 285 int putc(int c);
Hotboards 2:9673849ef2e9 286
Hotboards 2:9673849ef2e9 287 /** Write a formatted string of characters to the LCD.
Hotboards 3:e16488b64ef7 288 * @param format: formatted string
Hotboards 3:e16488b64ef7 289 *
Hotboards 3:e16488b64ef7 290 * Example:
Hotboards 3:e16488b64ef7 291 * @code
Hotboards 3:e16488b64ef7 292 * lcd.printf( "number %d: ", 34 );
Hotboards 3:e16488b64ef7 293 * @endcode
Hotboards 3:e16488b64ef7 294 */
Hotboards 0:1d03652e9f7a 295 int printf(const char* format, ...);
Hotboards 0:1d03652e9f7a 296 #endif
Hotboards 0:1d03652e9f7a 297
Hotboards 0:1d03652e9f7a 298
Hotboards 0:1d03652e9f7a 299 protected:
Hotboards 0:1d03652e9f7a 300
Hotboards 0:1d03652e9f7a 301 void send( uint8_t, uint8_t );
Hotboards 0:1d03652e9f7a 302
Hotboards 0:1d03652e9f7a 303 // redirection for the printf and putc functions
Hotboards 0:1d03652e9f7a 304 virtual int _putc( int value );
Hotboards 0:1d03652e9f7a 305 virtual int _getc( void );
Hotboards 0:1d03652e9f7a 306
Hotboards 0:1d03652e9f7a 307 // internal objects
Hotboards 0:1d03652e9f7a 308 SPI _spi;
Hotboards 0:1d03652e9f7a 309
Hotboards 0:1d03652e9f7a 310 DigitalOut _cs_pin;
Hotboards 0:1d03652e9f7a 311 DigitalOut _rs_pin;
Hotboards 0:1d03652e9f7a 312 DigitalOut _rst_pin;
Hotboards 0:1d03652e9f7a 313
Hotboards 0:1d03652e9f7a 314 uint8_t _displayfunction;
Hotboards 0:1d03652e9f7a 315 uint8_t _displaycontrol;
Hotboards 0:1d03652e9f7a 316 uint8_t _displaymode;
Hotboards 0:1d03652e9f7a 317 };
Hotboards 0:1d03652e9f7a 318
Hotboards 0:1d03652e9f7a 319 #endif /* Hotboards_SpiLcd_h */