lcd_i2c.h (4248B)
1 #ifndef LCD_I2C_H 2 #define LCD_I2C_H 3 // ------------------------------------------------------------------------------ 4 // Copyright 2018 Stephen Stebbing. telecnatron.com 5 // 6 // Licensed under the Telecnatron License, Version 1.0 (the “License”); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // https://telecnatron.com/software/licenses/ 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an “AS IS” BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 // ------------------------------------------------------------------------------ 18 19 #include <stdint.h> 20 21 //! Structure for containing lcd state data 22 typedef struct { 23 // i2c file descriptor/identifier 24 int fd; 25 // current pcf8574 output 26 uint8_t output; 27 // number of rows 28 uint8_t rows; 29 // number of columns 30 uint8_t cols; 31 // row and column coord of current cursor position 32 // for a 1602 display, bottom right is (15,1), home is (0,0) 33 uint8_t x,y; 34 } lcd_i2c_t; 35 36 // convenience macros 37 #define LCD_I2C_BACKLIGHT_ON(lcd_p) lcd_i2c_backlight(lcd_p, 1) 38 #define LCD_I2C_BACKLIGHT_OFF(lcd_p) lcd_i2c_backlight(lcd_p, 0) 39 // Display, cursor and blink on/off control. 40 #define LCD_I2C_DISPLAY_CONTROL(lcd_p, display, cursor, blink) lcd_i2c_write_i ( lcd_p, 0x08 | (display << 2) | (cursor << 1) | (blink) ) 41 #define LCD_I2C_CURSOR_ON(lcd_p) LCD_I2C_DISPLAY_CONTROL(lcd_p,1,1,0) 42 #define LCD_I2C_CURSOR_OFF(lcd_p) LCD_I2C_DISPLAY_CONTROL(lcd_p,1,0,0) 43 #define LCD_I2C_CURSOR_BLINK_ON(lcd_p) LCD_I2C_DISPLAY_CONTROL(lcd_p,1,1,1) 44 #define LCD_I2C_CURSOR_BLINK_OFF(lcd_p) LCD_I2C_DISPLAY_CONTROL(lcd_p, 1,1,0) 45 46 47 // default i2c address 48 #define LCD_I2C_PCF8574_ADDRESS_DEFAULT 0x27 49 50 /** 51 * Initialise i2c and lcd_i2c_t structure. 52 * @param address i2c address of the pcf8574 that controls the LCD. Use PC LCD_I2C_PCF8574_ADDRESS_DEFAULT for default 53 * @param lcd Pointer to an lcd_i2c_t structure that will be used to keep lcd variables 54 * @return The 'file descriptor' for the device (as returned by wiringPiI2CSetup) on success, -1 on error with errno set 55 */ 56 int lcd_i2c_setup(lcd_i2c_t *lcd,int address); 57 58 /** 59 * Initalise the lcd by doing a software reset, clearing and positioning cursor in home position 60 * @param lcd Pointer to lcd_i2c_t structure 61 */ 62 void lcd_i2c_init(lcd_i2c_t *lcd); 63 64 /** 65 * Write an instruction byte to the lcd 66 * @param lcd Pointer to lcd_i2c_t structure 67 * @param data The instruction byte 68 */ 69 void lcd_i2c_write_i(lcd_i2c_t *lcd, uint8_t data); 70 71 /** 72 * Write a data byte to the lcd 73 * @param lcd Pointer to lcd_i2c_t structure 74 * @param data The data byte 75 */ 76 void lcd_i2c_write_d(lcd_i2c_t *lcd, uint8_t data); 77 78 /** 79 * Move the cursor location to specified coords. 80 * Note that no range checking is done 81 * @param lcd Pointer to lcd_i2c_t structure 82 * @param x The x coord 83 * @param y The y coord 84 */ 85 void lcd_i2c_gotoxy(lcd_i2c_t *lcd, uint8_t x, uint8_t y); 86 87 /** 88 * Write to lcd with printf like functionallity 89 * @param lcd Pointer to lcd_i2c_t structure 90 * @param format_p The format string, followed by the parameters 91 */ 92 void lcd_i2c_printf(lcd_i2c_t *lcd, char* format_p, ...); 93 94 /** 95 * Clear the lcd 96 * @param lcd Pointer to lcd_i2c_t structure 97 */ 98 void lcd_i2c_clear(lcd_i2c_t *lcd); 99 100 /** 101 * Move curson to home position 102 * @param lcd Pointer to lcd_i2c_t structure 103 */ 104 void lcd_i2c_home(lcd_i2c_t *lcd); 105 106 /** 107 * Display passed character at current position 108 * @param lcd Pointer to lcd_i2c_t structure 109 * @param c The character to be displayed 110 */ 111 void lcd_i2c_putc(lcd_i2c_t *lcd, char c); 112 113 /** 114 * Display passed string at current position 115 * @param lcd Pointer to lcd_i2c_t structure 116 * @param str Pointer to the string to be displayed 117 */ 118 void lcd_i2c_puts(lcd_i2c_t *lcd, const char* str); 119 120 /** 121 * Turn backlight on or off 122 * @param ct lcd_i2c_t control structure for the device 123 * @param on Turn on if non-zero, off otherwise 124 */ 125 void lcd_i2c_backlight(lcd_i2c_t *lcd, uint8_t on); 126 127 128 129 #endif