Initial version for Modtronix LCD2S I2C and SPI serial LCD displays. For details, see http://modtronix.com/products-serial-lcd-board/

Committer:
modtronix
Date:
Tue Aug 04 18:54:37 2015 +1000
Revision:
1:429b7d3f7b95
Parent:
0:cea36bae632b
Child:
2:fe0c1e27f362
Added functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 1:429b7d3f7b95 1 /**
modtronix 1:429b7d3f7b95 2 * File: lcd2s_i2c.cpp
modtronix 1:429b7d3f7b95 3 *
modtronix 1:429b7d3f7b95 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 1:429b7d3f7b95 5 *
modtronix 1:429b7d3f7b95 6 * Description:
modtronix 1:429b7d3f7b95 7 *
modtronix 1:429b7d3f7b95 8 * Software License Agreement:
modtronix 1:429b7d3f7b95 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 1:429b7d3f7b95 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 1:429b7d3f7b95 11 * applications. If this is modified software, any license conditions from original
modtronix 1:429b7d3f7b95 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 1:429b7d3f7b95 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 1:429b7d3f7b95 14 *
modtronix 1:429b7d3f7b95 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 1:429b7d3f7b95 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 1:429b7d3f7b95 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 1:429b7d3f7b95 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 1:429b7d3f7b95 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 1:429b7d3f7b95 20 */
modtronix 1:429b7d3f7b95 21 #include "mbed.h"
modtronix 1:429b7d3f7b95 22 #include "lcd2s_i2c.h"
modtronix 1:429b7d3f7b95 23
modtronix 1:429b7d3f7b95 24
modtronix 1:429b7d3f7b95 25 // DEFINES ////////////////////////////////////////////////////////////////////
modtronix 1:429b7d3f7b95 26
modtronix 1:429b7d3f7b95 27
modtronix 1:429b7d3f7b95 28 // GLOBAL VARIABLES ///////////////////////////////////////////////////////////
modtronix 1:429b7d3f7b95 29
modtronix 1:429b7d3f7b95 30
modtronix 1:429b7d3f7b95 31 // Function Prototypes ////////////////////////////////////////////////////////
modtronix 1:429b7d3f7b95 32
modtronix 1:429b7d3f7b95 33
modtronix 1:429b7d3f7b95 34 LCD2S_I2C::LCD2S_I2C(I2C* i2c, uint8_t rows/* = 4*/, uint8_t columns/* = 20*/)
modtronix 1:429b7d3f7b95 35 : LCD2S(rows, columns)
modtronix 1:429b7d3f7b95 36 {
modtronix 1:429b7d3f7b95 37 pI2C = i2c;
modtronix 1:429b7d3f7b95 38 i2cAdr = 0x50; //Default I2C address of the LCD
modtronix 1:429b7d3f7b95 39 }
modtronix 1:429b7d3f7b95 40
modtronix 1:429b7d3f7b95 41 /** Initializes the LCD2S device with default values
modtronix 1:429b7d3f7b95 42 * - Interrupt pin is Open Collector output type
modtronix 1:429b7d3f7b95 43 * - Backlight On
modtronix 1:429b7d3f7b95 44 * - Display On
modtronix 1:429b7d3f7b95 45 * - Backlight 200 (value from 0 to 255)
modtronix 1:429b7d3f7b95 46 * - Cursor off, block cursor off
modtronix 1:429b7d3f7b95 47 * - Cursor moves forward
modtronix 1:429b7d3f7b95 48 * - Keypad is 4x4 button type (if present)
modtronix 1:429b7d3f7b95 49 * - OUT1 disabled
modtronix 1:429b7d3f7b95 50 * - GPIO1 to 3 disabled
modtronix 1:429b7d3f7b95 51 * - Keypad Buzzer off
modtronix 1:429b7d3f7b95 52 * - Keypad repeat rate = 320ms
modtronix 1:429b7d3f7b95 53 * - Keypad Repeat Delay = 1 second
modtronix 1:429b7d3f7b95 54 * - Keypad Debounce Time = 64ms
modtronix 1:429b7d3f7b95 55 *
modtronix 1:429b7d3f7b95 56 * @param contrast The display contrast, a value from 0 to 255
modtronix 1:429b7d3f7b95 57 */
modtronix 1:429b7d3f7b95 58 void LCD2S_I2C::initDefault(uint8_t contrast) {
modtronix 1:429b7d3f7b95 59 char cmd[8];
modtronix 1:429b7d3f7b95 60
modtronix 1:429b7d3f7b95 61 //Byte 1 = 0x95
modtronix 1:429b7d3f7b95 62 //Configure LCD using "Configure Device" command
modtronix 1:429b7d3f7b95 63 cmd[0] = LCD2S_CMD_CONFIG_DEVICE;
modtronix 1:429b7d3f7b95 64
modtronix 1:429b7d3f7b95 65 //Byte 2 (Display) of command
modtronix 1:429b7d3f7b95 66 //xx1x xxxx - Interrupt pin is Open Collector output type
modtronix 1:429b7d3f7b95 67 //xxx1 1xxx - Backlight On, Display On
modtronix 1:429b7d3f7b95 68 //xxxx x001 - Cursor off, block cursor off, Cursor moves forward
modtronix 1:429b7d3f7b95 69 //0011 1001 = 0x39
modtronix 1:429b7d3f7b95 70 cmd[1] = 0x39;
modtronix 1:429b7d3f7b95 71
modtronix 1:429b7d3f7b95 72 //Byte 3 (Contrast and Brightness) of command
modtronix 1:429b7d3f7b95 73 //01xx xxxx - Set Backlight to 100 (range is 0 to 255)
modtronix 1:429b7d3f7b95 74 //xx11 0111 - Set contrast to 220 (0x37 x 4)
modtronix 1:429b7d3f7b95 75 //0111 0111 = 0x77
modtronix 1:429b7d3f7b95 76 cmd[2] = 0x77;
modtronix 1:429b7d3f7b95 77
modtronix 1:429b7d3f7b95 78 //Byte 4 (keypad and IO) of command
modtronix 1:429b7d3f7b95 79 //0x0f Configures device for 4x4 keypad (if present), OUT1 disabled, GPIO1 to 3 disabled
modtronix 1:429b7d3f7b95 80 cmd[3] = 0x0f;
modtronix 1:429b7d3f7b95 81
modtronix 1:429b7d3f7b95 82 //Byte 5 (Keypad and Buzzer) of command
modtronix 1:429b7d3f7b95 83 //01xx xxxx - Keypad Buzzer off
modtronix 1:429b7d3f7b95 84 //xx10 xxxx - Keypad repeat rate = 320ms
modtronix 1:429b7d3f7b95 85 //xxxx 10xx - Keypad Repeat Delay = 1 second
modtronix 1:429b7d3f7b95 86 //xxxx xx10 - Keypad Debounce Time = 64ms
modtronix 1:429b7d3f7b95 87 //0110 1010 = 0x6A
modtronix 1:429b7d3f7b95 88 cmd[4] = 0x6a;
modtronix 1:429b7d3f7b95 89
modtronix 1:429b7d3f7b95 90 pI2C->write(i2cAdr, cmd, 5);
modtronix 1:429b7d3f7b95 91 }
modtronix 1:429b7d3f7b95 92
modtronix 1:429b7d3f7b95 93
modtronix 1:429b7d3f7b95 94 /** Causes the display to be updated with buffer content
modtronix 1:429b7d3f7b95 95 */
modtronix 1:429b7d3f7b95 96 void LCD2S_I2C::display(void) {
modtronix 1:429b7d3f7b95 97 uint8_t row, col;
modtronix 1:429b7d3f7b95 98 uint8_t colFirstDirty = 0xff;
modtronix 1:429b7d3f7b95 99 uint8_t colLastDirty = 0xff;
modtronix 1:429b7d3f7b95 100 uint32_t mask = 0;
modtronix 1:429b7d3f7b95 101 char cmd[32];
modtronix 1:429b7d3f7b95 102
modtronix 1:429b7d3f7b95 103 // bool changed = FALSE;
modtronix 1:429b7d3f7b95 104 //
modtronix 1:429b7d3f7b95 105 // //Check if anything changed
modtronix 1:429b7d3f7b95 106 // for (row=0; row<rows; row++) {
modtronix 1:429b7d3f7b95 107 // if(pDirtyRows[row] != 0) {
modtronix 1:429b7d3f7b95 108 // changed = TRUE;
modtronix 1:429b7d3f7b95 109 // break;
modtronix 1:429b7d3f7b95 110 // }
modtronix 1:429b7d3f7b95 111 // }
modtronix 1:429b7d3f7b95 112 //
modtronix 1:429b7d3f7b95 113 // if (changed == FALSE)
modtronix 1:429b7d3f7b95 114 // return;
modtronix 1:429b7d3f7b95 115
modtronix 1:429b7d3f7b95 116 //Write to dirty rows
modtronix 1:429b7d3f7b95 117 for (row=0; row<rows; row++) {
modtronix 1:429b7d3f7b95 118 if(pDirtyRows[row] != 0) {
modtronix 1:429b7d3f7b95 119 mask = 0x01;
modtronix 1:429b7d3f7b95 120 //Get first and last dirty characters in current row
modtronix 1:429b7d3f7b95 121 for(col=0; col<columns; col++) {
modtronix 1:429b7d3f7b95 122 //Is current char dirty?
modtronix 1:429b7d3f7b95 123 if ((pDirtyRows[row] & mask) != 0) {
modtronix 1:429b7d3f7b95 124 if(colFirstDirty == 0xff)
modtronix 1:429b7d3f7b95 125 colFirstDirty = col;
modtronix 1:429b7d3f7b95 126 colLastDirty=col;
modtronix 1:429b7d3f7b95 127 }
modtronix 1:429b7d3f7b95 128 mask = mask << 1;
modtronix 1:429b7d3f7b95 129 }
modtronix 1:429b7d3f7b95 130
modtronix 1:429b7d3f7b95 131 //Set cursor position
modtronix 1:429b7d3f7b95 132 cmd[0] = LCD2S_CMD_SET_CURSOR_POSITION;
modtronix 1:429b7d3f7b95 133 cmd[1] = row+1;
modtronix 1:429b7d3f7b95 134 cmd[2] = colFirstDirty+1;
modtronix 1:429b7d3f7b95 135 pI2C->write(this->i2cAdr, cmd, 3);
modtronix 1:429b7d3f7b95 136 wait_ms(5);
modtronix 1:429b7d3f7b95 137
modtronix 1:429b7d3f7b95 138 //Write string - only dirty characters are written
modtronix 1:429b7d3f7b95 139 cmd[0] = LCD2S_CMD_WRITE_STRING;
modtronix 1:429b7d3f7b95 140 for(col=colFirstDirty; col<=colLastDirty; col++) {
modtronix 1:429b7d3f7b95 141 cmd[col-colFirstDirty+1] = pBuf[(row*columns)+col];
modtronix 1:429b7d3f7b95 142 }
modtronix 1:429b7d3f7b95 143 pI2C->write(this->i2cAdr, cmd, colLastDirty-colFirstDirty+2);
modtronix 1:429b7d3f7b95 144 }
modtronix 1:429b7d3f7b95 145 }
modtronix 1:429b7d3f7b95 146 }
modtronix 1:429b7d3f7b95 147
modtronix 1:429b7d3f7b95 148 int LCD2S_I2C::writeChar(uint8_t c) {
modtronix 1:429b7d3f7b95 149 //Write to buffer
modtronix 1:429b7d3f7b95 150 pBuf[(cursor_row * columns) + cursor_col] = c;
modtronix 1:429b7d3f7b95 151
modtronix 1:429b7d3f7b95 152 //Set dirty flag
modtronix 1:429b7d3f7b95 153 pDirtyRows[cursor_row] |= 0x01 << cursor_col;
modtronix 1:429b7d3f7b95 154
modtronix 1:429b7d3f7b95 155 if (++cursor_col >= columns) {
modtronix 1:429b7d3f7b95 156 cursor_col = 0;
modtronix 1:429b7d3f7b95 157
modtronix 1:429b7d3f7b95 158 if (++cursor_row >= rows) {
modtronix 1:429b7d3f7b95 159 cursor_row = 0;
modtronix 1:429b7d3f7b95 160 }
modtronix 1:429b7d3f7b95 161 }
modtronix 1:429b7d3f7b95 162 return 0;
modtronix 1:429b7d3f7b95 163 }
modtronix 1:429b7d3f7b95 164
modtronix 1:429b7d3f7b95 165 //void LCD2S_I2C::write(const char* str) {
modtronix 1:429b7d3f7b95 166 // char cmd[32];
modtronix 1:429b7d3f7b95 167 //
modtronix 1:429b7d3f7b95 168 // cmd[0] = LCD2S_CMD_WRITE_STRING;
modtronix 1:429b7d3f7b95 169 // cmd[1] = 0x0c; //Clear display and go to first line
modtronix 1:429b7d3f7b95 170 // cmd[2] = 'H';
modtronix 1:429b7d3f7b95 171 // cmd[3] = 'o';
modtronix 1:429b7d3f7b95 172 // pI2C->write(this->i2cAdr, cmd, 4);
modtronix 1:429b7d3f7b95 173 //}