My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LiquidCrystalRus.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #define __PROG_TYPES_COMPAT__
  2. #include "LiquidCrystalRus.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <inttypes.h>
  6. #include <avr/pgmspace.h>
  7. #if defined(ARDUINO) && ARDUINO >= 100
  8. #include "Arduino.h"
  9. #else
  10. #include "WProgram.h"
  11. #endif
  12. // it is a russian alphabet translation
  13. // except 0401 --> 0xa2 = ╗, 0451 --> 0xb5
  14. const PROGMEM prog_uchar utf_recode[] =
  15. { 0x41,0xa0,0x42,0xa1,0xe0,0x45,0xa3,0xa4,0xa5,0xa6,0x4b,0xa7,0x4d,0x48,0x4f,
  16. 0xa8,0x50,0x43,0x54,0xa9,0xaa,0x58,0xe1,0xab,0xac,0xe2,0xad,0xae,0x62,0xaf,0xb0,0xb1,
  17. 0x61,0xb2,0xb3,0xb4,0xe3,0x65,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0x6f,
  18. 0xbe,0x70,0x63,0xbf,0x79,0xe4,0x78,0xe5,0xc0,0xc1,0xe6,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7
  19. };
  20. // When the display powers up, it is configured as follows:
  21. //
  22. // 1. Display clear
  23. // 2. Function set:
  24. // DL = 1; 8-bit interface data
  25. // N = 0; 1-line display
  26. // F = 0; 5x8 dot character font
  27. // 3. Display on/off control:
  28. // D = 0; Display off
  29. // C = 0; Cursor off
  30. // B = 0; Blinking off
  31. // 4. Entry mode set:
  32. // I/D = 1; Increment by 1
  33. // S = 0; No shift
  34. //
  35. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  36. // can't assume that its in that state when a sketch starts (and the
  37. // LiquidCrystal constructor is called).
  38. //
  39. // modified 27 Jul 2011
  40. // by Ilya V. Danilov http://mk90.ru/
  41. LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
  42. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  43. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  44. {
  45. init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  46. }
  47. LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t enable,
  48. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  49. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  50. {
  51. init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  52. }
  53. LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
  54. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  55. {
  56. init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  57. }
  58. LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t enable,
  59. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  60. {
  61. init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  62. }
  63. void LiquidCrystalRus::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
  64. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  65. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  66. {
  67. _rs_pin = rs;
  68. _rw_pin = rw;
  69. _enable_pin = enable;
  70. _data_pins[0] = d0;
  71. _data_pins[1] = d1;
  72. _data_pins[2] = d2;
  73. _data_pins[3] = d3;
  74. _data_pins[4] = d4;
  75. _data_pins[5] = d5;
  76. _data_pins[6] = d6;
  77. _data_pins[7] = d7;
  78. pinMode(_rs_pin, OUTPUT);
  79. // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
  80. if (_rw_pin != 255) {
  81. pinMode(_rw_pin, OUTPUT);
  82. }
  83. pinMode(_enable_pin, OUTPUT);
  84. if (fourbitmode)
  85. _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  86. else
  87. _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
  88. begin(16, 1);
  89. }
  90. void LiquidCrystalRus::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  91. if (lines > 1) {
  92. _displayfunction |= LCD_2LINE;
  93. }
  94. _numlines = lines;
  95. _currline = 0;
  96. // for some 1 line displays you can select a 10 pixel high font
  97. if ((dotsize != 0) && (lines == 1)) {
  98. _displayfunction |= LCD_5x10DOTS;
  99. }
  100. // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  101. // according to datasheet, we need at least 40ms after power rises above 2.7V
  102. // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  103. delayMicroseconds(50000);
  104. // Now we pull both RS and R/W low to begin commands
  105. digitalWrite(_rs_pin, LOW);
  106. digitalWrite(_enable_pin, LOW);
  107. if (_rw_pin != 255) {
  108. digitalWrite(_rw_pin, LOW);
  109. }
  110. //put the LCD into 4 bit or 8 bit mode
  111. if (! (_displayfunction & LCD_8BITMODE)) {
  112. // this is according to the hitachi HD44780 datasheet
  113. // figure 24, pg 46
  114. // we start in 8bit mode, try to set 4 bit mode
  115. writeNbits(0x03,4);
  116. delayMicroseconds(4500); // wait min 4.1ms
  117. // second try
  118. writeNbits(0x03,4);
  119. delayMicroseconds(4500); // wait min 4.1ms
  120. // third go!
  121. writeNbits(0x03,4);
  122. delayMicroseconds(150);
  123. // finally, set to 8-bit interface
  124. writeNbits(0x02,4);
  125. } else {
  126. // this is according to the hitachi HD44780 datasheet
  127. // page 45 figure 23
  128. // Send function set command sequence
  129. command(LCD_FUNCTIONSET | _displayfunction);
  130. delayMicroseconds(4500); // wait more than 4.1ms
  131. // second try
  132. command(LCD_FUNCTIONSET | _displayfunction);
  133. delayMicroseconds(150);
  134. // third go
  135. command(LCD_FUNCTIONSET | _displayfunction);
  136. }
  137. // finally, set # lines, font size, etc.
  138. command(LCD_FUNCTIONSET | _displayfunction);
  139. // turn the display on with no cursor or blinking default
  140. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
  141. display();
  142. // clear it off
  143. clear();
  144. // Initialize to default text direction (for romance languages)
  145. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  146. // set the entry mode
  147. command(LCD_ENTRYMODESET | _displaymode);
  148. }
  149. void LiquidCrystalRus::setDRAMModel(uint8_t model) {
  150. _dram_model = model;
  151. }
  152. /********** high level commands, for the user! */
  153. void LiquidCrystalRus::clear()
  154. {
  155. command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
  156. delayMicroseconds(2000); // this command takes a long time!
  157. }
  158. void LiquidCrystalRus::home()
  159. {
  160. command(LCD_RETURNHOME); // set cursor position to zero
  161. delayMicroseconds(2000); // this command takes a long time!
  162. }
  163. void LiquidCrystalRus::setCursor(uint8_t col, uint8_t row)
  164. {
  165. int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  166. if ( row >= _numlines ) {
  167. row = _numlines-1; // we count rows starting w/0
  168. }
  169. command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  170. }
  171. // Turn the display on/off (quickly)
  172. void LiquidCrystalRus::noDisplay() {
  173. _displaycontrol &= ~LCD_DISPLAYON;
  174. command(LCD_DISPLAYCONTROL | _displaycontrol);
  175. }
  176. void LiquidCrystalRus::display() {
  177. _displaycontrol |= LCD_DISPLAYON;
  178. command(LCD_DISPLAYCONTROL | _displaycontrol);
  179. }
  180. // Turns the underline cursor on/off
  181. void LiquidCrystalRus::noCursor() {
  182. _displaycontrol &= ~LCD_CURSORON;
  183. command(LCD_DISPLAYCONTROL | _displaycontrol);
  184. }
  185. void LiquidCrystalRus::cursor() {
  186. _displaycontrol |= LCD_CURSORON;
  187. command(LCD_DISPLAYCONTROL | _displaycontrol);
  188. }
  189. // Turn on and off the blinking cursor
  190. void LiquidCrystalRus::noBlink() {
  191. _displaycontrol &= ~LCD_BLINKON;
  192. command(LCD_DISPLAYCONTROL | _displaycontrol);
  193. }
  194. void LiquidCrystalRus::blink() {
  195. _displaycontrol |= LCD_BLINKON;
  196. command(LCD_DISPLAYCONTROL | _displaycontrol);
  197. }
  198. // These commands scroll the display without changing the RAM
  199. void LiquidCrystalRus::scrollDisplayLeft(void) {
  200. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  201. }
  202. void LiquidCrystalRus::scrollDisplayRight(void) {
  203. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  204. }
  205. // This is for text that flows Left to Right
  206. void LiquidCrystalRus::leftToRight(void) {
  207. _displaymode |= LCD_ENTRYLEFT;
  208. command(LCD_ENTRYMODESET | _displaymode);
  209. }
  210. // This is for text that flows Right to Left
  211. void LiquidCrystalRus::rightToLeft(void) {
  212. _displaymode &= ~LCD_ENTRYLEFT;
  213. command(LCD_ENTRYMODESET | _displaymode);
  214. }
  215. // This will 'right justify' text from the cursor
  216. void LiquidCrystalRus::autoscroll(void) {
  217. _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  218. command(LCD_ENTRYMODESET | _displaymode);
  219. }
  220. // This will 'left justify' text from the cursor
  221. void LiquidCrystalRus::noAutoscroll(void) {
  222. _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  223. command(LCD_ENTRYMODESET | _displaymode);
  224. }
  225. // Allows us to fill the first 8 CGRAM locations
  226. // with custom characters
  227. void LiquidCrystalRus::createChar(uint8_t location, uint8_t charmap[]) {
  228. location &= 0x7; // we only have 8 locations 0-7
  229. command(LCD_SETCGRAMADDR | (location << 3));
  230. for (int i=0; i<8; i++) {
  231. write(charmap[i]);
  232. }
  233. }
  234. /*********** mid level commands, for sending data/cmds */
  235. inline void LiquidCrystalRus::command(uint8_t value) {
  236. send(value, LOW);
  237. }
  238. #if defined(ARDUINO) && ARDUINO >= 100
  239. size_t LiquidCrystalRus::write(uint8_t value)
  240. #else
  241. void LiquidCrystalRus::write(uint8_t value)
  242. #endif
  243. {
  244. uint8_t out_char=value;
  245. if (_dram_model == LCD_DRAM_WH1601) {
  246. uint8_t ac=recv(LOW) & 0x7f;
  247. if (ac>7 && ac<0x14) command(LCD_SETDDRAMADDR | (0x40+ac-8));
  248. }
  249. if (value>=0x80) { // UTF-8 handling
  250. if (value >= 0xc0) {
  251. utf_hi_char = value - 0xd0;
  252. } else {
  253. value &= 0x3f;
  254. if (!utf_hi_char && (value == 1))
  255. send(0xa2,HIGH); // ╗
  256. else if ((utf_hi_char == 1) && (value == 0x11))
  257. send(0xb5,HIGH); // ╦
  258. else
  259. send(pgm_read_byte_near(utf_recode + value + (utf_hi_char<<6) - 0x10), HIGH);
  260. }
  261. } else send(out_char, HIGH);
  262. #if defined(ARDUINO) && ARDUINO >= 100
  263. return 1; // assume sucess
  264. #endif
  265. }
  266. /************ low level data pushing commands **********/
  267. // write either command or data, with automatic 4/8-bit selection
  268. void LiquidCrystalRus::send(uint8_t value, uint8_t mode) {
  269. digitalWrite(_rs_pin, mode);
  270. // if there is a RW pin indicated, set it low to Write
  271. if (_rw_pin != 255) {
  272. digitalWrite(_rw_pin, LOW);
  273. }
  274. if (_displayfunction & LCD_8BITMODE) {
  275. writeNbits(value,8);
  276. } else {
  277. writeNbits(value>>4,4);
  278. writeNbits(value,4);
  279. }
  280. }
  281. // read data, with automatic 4/8-bit selection
  282. uint8_t LiquidCrystalRus::recv(uint8_t mode) {
  283. uint8_t retval;
  284. digitalWrite(_rs_pin, mode);
  285. // if there is a RW pin indicated, set it low to Write
  286. if (_rw_pin != 255) {
  287. digitalWrite(_rw_pin, HIGH);
  288. }
  289. if (_displayfunction & LCD_8BITMODE) {
  290. retval = readNbits(8);
  291. } else {
  292. retval = readNbits(4) << 4;
  293. retval |= readNbits(4);
  294. }
  295. return retval;
  296. }
  297. void LiquidCrystalRus::pulseEnable() {
  298. digitalWrite(_enable_pin, LOW);
  299. delayMicroseconds(1);
  300. digitalWrite(_enable_pin, HIGH);
  301. delayMicroseconds(1); // enable pulse must be >450ns
  302. digitalWrite(_enable_pin, LOW);
  303. delayMicroseconds(100); // commands need > 37us to settle
  304. }
  305. void LiquidCrystalRus::writeNbits(uint8_t value, uint8_t n) {
  306. for (int i = 0; i < n; i++) {
  307. pinMode(_data_pins[i], OUTPUT);
  308. digitalWrite(_data_pins[i], (value >> i) & 0x01);
  309. }
  310. pulseEnable();
  311. }
  312. uint8_t LiquidCrystalRus::readNbits(uint8_t n) {
  313. uint8_t retval=0;
  314. for (int i = 0; i < n; i++) {
  315. pinMode(_data_pins[i], INPUT);
  316. }
  317. digitalWrite(_enable_pin, LOW);
  318. delayMicroseconds(1);
  319. digitalWrite(_enable_pin, HIGH);
  320. delayMicroseconds(1); // enable pulse must be >450ns
  321. for (int i = 0; i < n; i++) {
  322. retval |= (digitalRead(_data_pins[i]) == HIGH)?(1 << i):0;
  323. }
  324. digitalWrite(_enable_pin, LOW);
  325. return retval;
  326. }