#include "Grove_LCD_rgb_V2.0.h" #include #include #include #include static int _displayfunction = 0; static int _displaycontrol = 0; static int _displaymode = 0; extern struct i2c_client *i2c_client_lcd; extern struct i2c_client *i2c_client_rgb; //--- Mid level commands, for sending data/cmds -------- inline void lcd_command(int value) { i2c_smbus_write_byte_data(i2c_client_lcd, 0x80, value); } // send data int lcd_write(char value) { i2c_smbus_write_byte_data(i2c_client_lcd, 0x40, value); return 0; } void lcd_setReg(unsigned char addr, unsigned char dta) { i2c_smbus_write_byte_data(i2c_client_rgb, addr, dta); } void lcd_setRGB(unsigned char r, unsigned char g, unsigned char b) { lcd_setReg(REG_RED, r); lcd_setReg(REG_GREEN, g); lcd_setReg(REG_BLUE, b); } void lcd_setColor(enum color c) { lcd_setRGB((c &0xFF0000)>>16, (c &0x00FF00)>>8, c &0x0000FF); } //--- high level commands, for the user! --------------------- void lcd_clear(void) { lcd_command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero mdelay(2); // this command takes a long time! } void lcd_home(void) { lcd_command(LCD_RETURNHOME); // set cursor position to zero mdelay(2); // this command takes a long time! } void lcd_setCursor(int col, int row) { col = (row == 0 ? col|0x80 : col|0xc0); i2c_smbus_write_byte_data(i2c_client_lcd, 0x80, col); } // Turn the display on/off (quickly) void lcd_noDisplay(void) { _displaycontrol &= ~LCD_DISPLAYON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_display(void) { _displaycontrol |= LCD_DISPLAYON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turns the underline cursor on/off void lcd_noCursor(void) { _displaycontrol &= ~LCD_CURSORON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_cursor(void) { _displaycontrol |= LCD_CURSORON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turn on and off the blinking cursor void lcd_noBlink(void) { _displaycontrol &= ~LCD_BLINKON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_blink(void) { _displaycontrol |= LCD_BLINKON; lcd_command(LCD_DISPLAYCONTROL | _displaycontrol); } // These commands scroll the display without changing the RAM void lcd_scrollDisplayLeft(void) { lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); } void lcd_scrollDisplayRight(void) { lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); } // This is for text that flows Left to Right void lcd_leftToRight(void) { _displaymode |= LCD_ENTRYLEFT; lcd_command(LCD_ENTRYMODESET | _displaymode); } // This is for text that flows Right to Left void lcd_rightToLeft(void) { _displaymode &= ~LCD_ENTRYLEFT; lcd_command(LCD_ENTRYMODESET | _displaymode); } // This will 'right justify' text from the cursor void lcd_autoscroll(void) { _displaymode |= LCD_ENTRYSHIFTINCREMENT; lcd_command(LCD_ENTRYMODESET | _displaymode); } // This will 'left justify' text from the cursor void lcd_noAutoscroll(void) { _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; lcd_command(LCD_ENTRYMODESET | _displaymode); } // Allows us to fill the first 8 CGRAM locations // with custom characters void lcd_createChar(int location, int charmap[]) { unsigned char dta[8]; int i; location &= 0x7; // we only have 8 locations 0-7 lcd_command(LCD_SETCGRAMADDR | (location << 3)); for(i=0; i<8; i++) { dta[i] = charmap[i]; } i2c_smbus_write_block_data(i2c_client_lcd, 0x40, 8, dta); } // Control the backlight LED blinking void lcd_blinkLED(void) { lcd_setReg(0x07, 0x17); // blink every second lcd_setReg(0x06, 0x7f); // half on, half off } void lcd_noBlinkLED(void) { lcd_setReg(0x07, 0x00); lcd_setReg(0x06, 0xff); } void lcd_begin(void) { _displayfunction |= LCD_5x8DOTS; _displayfunction |= LCD_2LINE; // Send function set command sequence lcd_command(LCD_FUNCTIONSET | _displayfunction); mdelay(5); // wait more than 4.1ms // second try lcd_command(LCD_FUNCTIONSET | _displayfunction); mdelay(1); // third go lcd_command(LCD_FUNCTIONSET | _displayfunction); // finally, set # lines, font size, etc. lcd_command(LCD_FUNCTIONSET | _displayfunction); // turn the display on with no cursor or blinking default _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; lcd_display(); // clear it off lcd_clear(); // Initialize to default text direction (for romance languages) _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; // set the entry mode lcd_command(LCD_ENTRYMODESET | _displaymode); // backlight init lcd_setReg(REG_MODE1, 0); // set LEDs controllable by both PWM and GRPPWM registers lcd_setReg(REG_OUTPUT, 0xFF); // set MODE2 values // 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode) lcd_setReg(REG_MODE2, 0x20); lcd_setColor(WHITE); } //--- Comand for printing ------------------------------- void lcd_print(char c[]) { int n; for (n=0; c[n] != '\0'; n++) lcd_write(c[n]); }