LiquidCrystal_I2C.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Based on the work by DFRobot
  2. #include "LiquidCrystal_I2C.h"
  3. #include <inttypes.h>
  4. #if defined(ARDUINO) && ARDUINO >= 100
  5. #include "Arduino.h"
  6. #define printIIC(args) Wire.write(args)
  7. inline size_t LiquidCrystal_I2C::write(uint8_t value) {
  8. send(value, Rs);
  9. return 1;
  10. }
  11. #else
  12. #include "WProgram.h"
  13. #define printIIC(args) Wire.send(args)
  14. inline void LiquidCrystal_I2C::write(uint8_t value) {
  15. send(value, Rs);
  16. }
  17. #endif
  18. #include "Wire.h"
  19. // When the display powers up, it is configured as follows:
  20. //
  21. // 1. Display clear
  22. // 2. Function set:
  23. // DL = 1; 8-bit interface data
  24. // N = 0; 1-line display
  25. // F = 0; 5x8 dot character font
  26. // 3. Display on/off control:
  27. // D = 0; Display off
  28. // C = 0; Cursor off
  29. // B = 0; Blinking off
  30. // 4. Entry mode set:
  31. // I/D = 1; Increment by 1
  32. // S = 0; No shift
  33. //
  34. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  35. // can't assume that its in that state when a sketch starts (and the
  36. // LiquidCrystal constructor is called).
  37. LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
  38. {
  39. _Addr = lcd_Addr;
  40. _cols = lcd_cols;
  41. _rows = lcd_rows;
  42. _backlightval = LCD_NOBACKLIGHT;
  43. }
  44. void LiquidCrystal_I2C::oled_init(){
  45. _oled = true;
  46. init_priv();
  47. }
  48. void LiquidCrystal_I2C::init(){
  49. init_priv();
  50. }
  51. void LiquidCrystal_I2C::init_priv()
  52. {
  53. Wire.begin();
  54. _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  55. begin(_cols, _rows);
  56. }
  57. void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  58. if (lines > 1) {
  59. _displayfunction |= LCD_2LINE;
  60. }
  61. _numlines = lines;
  62. // for some 1 line displays you can select a 10 pixel high font
  63. if ((dotsize != 0) && (lines == 1)) {
  64. _displayfunction |= LCD_5x10DOTS;
  65. }
  66. // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  67. // according to datasheet, we need at least 40ms after power rises above 2.7V
  68. // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  69. delay(50);
  70. // Now we pull both RS and R/W low to begin commands
  71. expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1)
  72. delay(1000);
  73. //put the LCD into 4 bit mode
  74. // this is according to the hitachi HD44780 datasheet
  75. // figure 24, pg 46
  76. // we start in 8bit mode, try to set 4 bit mode
  77. write4bits(0x03 << 4);
  78. delayMicroseconds(4500); // wait min 4.1ms
  79. // second try
  80. write4bits(0x03 << 4);
  81. delayMicroseconds(4500); // wait min 4.1ms
  82. // third go!
  83. write4bits(0x03 << 4);
  84. delayMicroseconds(150);
  85. // finally, set to 4-bit interface
  86. write4bits(0x02 << 4);
  87. // set # lines, font size, etc.
  88. command(LCD_FUNCTIONSET | _displayfunction);
  89. // turn the display on with no cursor or blinking default
  90. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
  91. display();
  92. // clear it off
  93. clear();
  94. // Initialize to default text direction (for roman languages)
  95. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  96. // set the entry mode
  97. command(LCD_ENTRYMODESET | _displaymode);
  98. home();
  99. }
  100. /********** high level commands, for the user! */
  101. void LiquidCrystal_I2C::clear(){
  102. command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
  103. delayMicroseconds(2000); // this command takes a long time!
  104. if (_oled) setCursor(0,0);
  105. }
  106. void LiquidCrystal_I2C::home(){
  107. command(LCD_RETURNHOME); // set cursor position to zero
  108. delayMicroseconds(2000); // this command takes a long time!
  109. }
  110. void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){
  111. int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  112. if ( row > _numlines ) {
  113. row = _numlines-1; // we count rows starting w/0
  114. }
  115. command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  116. }
  117. // Turn the display on/off (quickly)
  118. void LiquidCrystal_I2C::noDisplay() {
  119. _displaycontrol &= ~LCD_DISPLAYON;
  120. command(LCD_DISPLAYCONTROL | _displaycontrol);
  121. }
  122. void LiquidCrystal_I2C::display() {
  123. _displaycontrol |= LCD_DISPLAYON;
  124. command(LCD_DISPLAYCONTROL | _displaycontrol);
  125. }
  126. // Turns the underline cursor on/off
  127. void LiquidCrystal_I2C::noCursor() {
  128. _displaycontrol &= ~LCD_CURSORON;
  129. command(LCD_DISPLAYCONTROL | _displaycontrol);
  130. }
  131. void LiquidCrystal_I2C::cursor() {
  132. _displaycontrol |= LCD_CURSORON;
  133. command(LCD_DISPLAYCONTROL | _displaycontrol);
  134. }
  135. // Turn on and off the blinking cursor
  136. void LiquidCrystal_I2C::noBlink() {
  137. _displaycontrol &= ~LCD_BLINKON;
  138. command(LCD_DISPLAYCONTROL | _displaycontrol);
  139. }
  140. void LiquidCrystal_I2C::blink() {
  141. _displaycontrol |= LCD_BLINKON;
  142. command(LCD_DISPLAYCONTROL | _displaycontrol);
  143. }
  144. // These commands scroll the display without changing the RAM
  145. void LiquidCrystal_I2C::scrollDisplayLeft(void) {
  146. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  147. }
  148. void LiquidCrystal_I2C::scrollDisplayRight(void) {
  149. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  150. }
  151. // This is for text that flows Left to Right
  152. void LiquidCrystal_I2C::leftToRight(void) {
  153. _displaymode |= LCD_ENTRYLEFT;
  154. command(LCD_ENTRYMODESET | _displaymode);
  155. }
  156. // This is for text that flows Right to Left
  157. void LiquidCrystal_I2C::rightToLeft(void) {
  158. _displaymode &= ~LCD_ENTRYLEFT;
  159. command(LCD_ENTRYMODESET | _displaymode);
  160. }
  161. // This will 'right justify' text from the cursor
  162. void LiquidCrystal_I2C::autoscroll(void) {
  163. _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  164. command(LCD_ENTRYMODESET | _displaymode);
  165. }
  166. // This will 'left justify' text from the cursor
  167. void LiquidCrystal_I2C::noAutoscroll(void) {
  168. _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  169. command(LCD_ENTRYMODESET | _displaymode);
  170. }
  171. // Allows us to fill the first 8 CGRAM locations
  172. // with custom characters
  173. void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) {
  174. location &= 0x7; // we only have 8 locations 0-7
  175. command(LCD_SETCGRAMADDR | (location << 3));
  176. for (int i=0; i<8; i++) {
  177. write(charmap[i]);
  178. }
  179. }
  180. //createChar with PROGMEM input
  181. void LiquidCrystal_I2C::createChar(uint8_t location, const char *charmap) {
  182. location &= 0x7; // we only have 8 locations 0-7
  183. command(LCD_SETCGRAMADDR | (location << 3));
  184. for (int i=0; i<8; i++) {
  185. write(pgm_read_byte_near(charmap++));
  186. }
  187. }
  188. // Turn the (optional) backlight off/on
  189. void LiquidCrystal_I2C::noBacklight(void) {
  190. _backlightval=LCD_NOBACKLIGHT;
  191. expanderWrite(0);
  192. }
  193. void LiquidCrystal_I2C::backlight(void) {
  194. _backlightval=LCD_BACKLIGHT;
  195. expanderWrite(0);
  196. }
  197. /*********** mid level commands, for sending data/cmds */
  198. inline void LiquidCrystal_I2C::command(uint8_t value) {
  199. send(value, 0);
  200. }
  201. /************ low level data pushing commands **********/
  202. // write either command or data
  203. void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
  204. uint8_t highnib=value&0xf0;
  205. uint8_t lownib=(value<<4)&0xf0;
  206. write4bits((highnib)|mode);
  207. write4bits((lownib)|mode);
  208. }
  209. void LiquidCrystal_I2C::write4bits(uint8_t value) {
  210. expanderWrite(value);
  211. pulseEnable(value);
  212. }
  213. void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
  214. Wire.beginTransmission(_Addr);
  215. printIIC((int)(_data) | _backlightval);
  216. Wire.endTransmission();
  217. }
  218. void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
  219. expanderWrite(_data | En); // En high
  220. delayMicroseconds(1); // enable pulse must be >450ns
  221. expanderWrite(_data & ~En); // En low
  222. delayMicroseconds(50); // commands need > 37us to settle
  223. }
  224. // Alias functions
  225. void LiquidCrystal_I2C::cursor_on(){
  226. cursor();
  227. }
  228. void LiquidCrystal_I2C::cursor_off(){
  229. noCursor();
  230. }
  231. void LiquidCrystal_I2C::blink_on(){
  232. blink();
  233. }
  234. void LiquidCrystal_I2C::blink_off(){
  235. noBlink();
  236. }
  237. void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){
  238. createChar(char_num, rows);
  239. }
  240. void LiquidCrystal_I2C::setBacklight(uint8_t new_val){
  241. if(new_val){
  242. backlight(); // turn backlight on
  243. }else{
  244. noBacklight(); // turn backlight off
  245. }
  246. }
  247. void LiquidCrystal_I2C::printstr(const char c[]){
  248. //This function is not identical to the function used for "real" I2C displays
  249. //it's here so the user sketch doesn't have to be changed
  250. print(c);
  251. }
  252. // unsupported API functions
  253. #pragma GCC diagnostic push
  254. #pragma GCC diagnostic ignored "-Wunused-parameter"
  255. void LiquidCrystal_I2C::off(){}
  256. void LiquidCrystal_I2C::on(){}
  257. void LiquidCrystal_I2C::setDelay (int cmdDelay,int charDelay) {}
  258. uint8_t LiquidCrystal_I2C::status(){return 0;}
  259. uint8_t LiquidCrystal_I2C::keypad (){return 0;}
  260. uint8_t LiquidCrystal_I2C::init_bargraph(uint8_t graphtype){return 0;}
  261. void LiquidCrystal_I2C::draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end){}
  262. void LiquidCrystal_I2C::draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_row_end){}
  263. void LiquidCrystal_I2C::setContrast(uint8_t new_val){}
  264. #pragma GCC diagnostic pop