LiquidCrystal_Prusa.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "LiquidCrystal_Prusa.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include "Arduino.h"
  6. // When the display powers up, it is configured as follows:
  7. //
  8. // 1. Display clear
  9. // 2. Function set:
  10. // DL = 1; 8-bit interface data
  11. // N = 0; 1-line display
  12. // F = 0; 5x8 dot character font
  13. // 3. Display on/off control:
  14. // D = 0; Display off
  15. // C = 0; Cursor off
  16. // B = 0; Blinking off
  17. // 4. Entry mode set:
  18. // I/D = 1; Increment by 1
  19. // S = 0; No shift
  20. //
  21. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  22. // can't assume that it's in that state when a sketch starts (and the
  23. // LiquidCrystal_Prusa constructor is called).
  24. LiquidCrystal_Prusa::LiquidCrystal_Prusa(uint8_t rs, uint8_t rw, uint8_t enable,
  25. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  26. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  27. {
  28. init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  29. }
  30. LiquidCrystal_Prusa::LiquidCrystal_Prusa(uint8_t rs, uint8_t enable,
  31. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  32. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  33. {
  34. init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  35. }
  36. LiquidCrystal_Prusa::LiquidCrystal_Prusa(uint8_t rs, uint8_t rw, uint8_t enable,
  37. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  38. {
  39. init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  40. }
  41. LiquidCrystal_Prusa::LiquidCrystal_Prusa(uint8_t rs, uint8_t enable,
  42. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  43. {
  44. init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  45. }
  46. void LiquidCrystal_Prusa::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
  47. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  48. uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  49. {
  50. _rs_pin = rs;
  51. _rw_pin = rw;
  52. _enable_pin = enable;
  53. _data_pins[0] = d0;
  54. _data_pins[1] = d1;
  55. _data_pins[2] = d2;
  56. _data_pins[3] = d3;
  57. _data_pins[4] = d4;
  58. _data_pins[5] = d5;
  59. _data_pins[6] = d6;
  60. _data_pins[7] = d7;
  61. pinMode(_rs_pin, OUTPUT);
  62. // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
  63. if (_rw_pin != 255) {
  64. pinMode(_rw_pin, OUTPUT);
  65. }
  66. pinMode(_enable_pin, OUTPUT);
  67. if (fourbitmode)
  68. _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  69. else
  70. _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
  71. begin(16, 1);
  72. }
  73. void LiquidCrystal_Prusa::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  74. if (lines > 1) {
  75. _displayfunction |= LCD_2LINE;
  76. }
  77. _numlines = lines;
  78. _currline = 0;
  79. // for some 1 line displays you can select a 10 pixel high font
  80. if ((dotsize != 0) && (lines == 1)) {
  81. _displayfunction |= LCD_5x10DOTS;
  82. }
  83. // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  84. // according to datasheet, we need at least 40ms after power rises above 2.7V
  85. // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  86. delayMicroseconds(50000);
  87. // Now we pull both RS and R/W low to begin commands
  88. digitalWrite(_rs_pin, LOW);
  89. digitalWrite(_enable_pin, LOW);
  90. if (_rw_pin != 255) {
  91. digitalWrite(_rw_pin, LOW);
  92. }
  93. //put the LCD into 4 bit or 8 bit mode
  94. if (! (_displayfunction & LCD_8BITMODE)) {
  95. // this is according to the hitachi HD44780 datasheet
  96. // figure 24, pg 46
  97. // we start in 8bit mode, try to set 4 bit mode
  98. write4bits(0x03);
  99. delayMicroseconds(4500); // wait min 4.1ms
  100. // second try
  101. write4bits(0x03);
  102. delayMicroseconds(4500); // wait min 4.1ms
  103. // third go!
  104. write4bits(0x03);
  105. delayMicroseconds(150);
  106. // finally, set to 4-bit interface
  107. write4bits(0x02);
  108. } else {
  109. // this is according to the hitachi HD44780 datasheet
  110. // page 45 figure 23
  111. // Send function set command sequence
  112. command(LCD_FUNCTIONSET | _displayfunction);
  113. delayMicroseconds(4500); // wait more than 4.1ms
  114. // second try
  115. command(LCD_FUNCTIONSET | _displayfunction);
  116. delayMicroseconds(150);
  117. // third go
  118. command(LCD_FUNCTIONSET | _displayfunction);
  119. }
  120. // finally, set # lines, font size, etc.
  121. command(LCD_FUNCTIONSET | _displayfunction);
  122. delayMicroseconds(60);
  123. // turn the display on with no cursor or blinking default
  124. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
  125. display();
  126. delayMicroseconds(60);
  127. // clear it off
  128. clear();
  129. delayMicroseconds(3000);
  130. // Initialize to default text direction (for romance languages)
  131. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  132. // set the entry mode
  133. command(LCD_ENTRYMODESET | _displaymode);
  134. delayMicroseconds(60);
  135. _escape[0] = 0;
  136. }
  137. void LiquidCrystal_Prusa::begin_noclear(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  138. if (lines > 1) {
  139. _displayfunction |= LCD_2LINE;
  140. }
  141. _numlines = lines;
  142. _currline = 0;
  143. // for some 1 line displays you can select a 10 pixel high font
  144. if ((dotsize != 0) && (lines == 1)) {
  145. _displayfunction |= LCD_5x10DOTS;
  146. }
  147. // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  148. // according to datasheet, we need at least 40ms after power rises above 2.7V
  149. // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  150. delayMicroseconds(50000);
  151. // Now we pull both RS and R/W low to begin commands
  152. digitalWrite(_rs_pin, LOW);
  153. digitalWrite(_enable_pin, LOW);
  154. if (_rw_pin != 255) {
  155. digitalWrite(_rw_pin, LOW);
  156. }
  157. //put the LCD into 4 bit or 8 bit mode
  158. if (! (_displayfunction & LCD_8BITMODE)) {
  159. // this is according to the hitachi HD44780 datasheet
  160. // figure 24, pg 46
  161. // we start in 8bit mode, try to set 4 bit mode
  162. write4bits(0x03);
  163. delayMicroseconds(4500); // wait min 4.1ms
  164. // second try
  165. write4bits(0x03);
  166. delayMicroseconds(4500); // wait min 4.1ms
  167. // third go!
  168. write4bits(0x03);
  169. delayMicroseconds(150);
  170. // finally, set to 4-bit interface
  171. write4bits(0x02);
  172. } else {
  173. // this is according to the hitachi HD44780 datasheet
  174. // page 45 figure 23
  175. // Send function set command sequence
  176. command(LCD_FUNCTIONSET | _displayfunction);
  177. delayMicroseconds(4500); // wait more than 4.1ms
  178. // second try
  179. command(LCD_FUNCTIONSET | _displayfunction);
  180. delayMicroseconds(150);
  181. // third go
  182. command(LCD_FUNCTIONSET | _displayfunction);
  183. }
  184. // finally, set # lines, font size, etc.
  185. command(LCD_FUNCTIONSET | _displayfunction);
  186. delayMicroseconds(60);
  187. // turn the display on with no cursor or blinking default
  188. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
  189. display();
  190. delayMicroseconds(60);
  191. // clear it off
  192. //clear();
  193. home();
  194. delayMicroseconds(1600);
  195. // Initialize to default text direction (for romance languages)
  196. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  197. // set the entry mode
  198. command(LCD_ENTRYMODESET | _displaymode);
  199. delayMicroseconds(60);
  200. setCursor(8,0);
  201. print(" ");
  202. setCursor(8,1);
  203. print(" ");
  204. setCursor(6,2);
  205. print(" ");
  206. }
  207. /********** high level commands, for the user! */
  208. void LiquidCrystal_Prusa::clear()
  209. {
  210. command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
  211. delayMicroseconds(1600); // this command takes a long time
  212. }
  213. void LiquidCrystal_Prusa::home()
  214. {
  215. command(LCD_RETURNHOME); // set cursor position to zero
  216. delayMicroseconds(1600); // this command takes a long time!
  217. }
  218. void LiquidCrystal_Prusa::setCursor(uint8_t col, uint8_t row)
  219. {
  220. int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  221. if ( row >= _numlines ) {
  222. row = _numlines-1; // we count rows starting w/0
  223. }
  224. _currline = row;
  225. command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  226. }
  227. // Turn the display on/off (quickly)
  228. void LiquidCrystal_Prusa::noDisplay() {
  229. _displaycontrol &= ~LCD_DISPLAYON;
  230. command(LCD_DISPLAYCONTROL | _displaycontrol);
  231. }
  232. void LiquidCrystal_Prusa::display() {
  233. _displaycontrol |= LCD_DISPLAYON;
  234. command(LCD_DISPLAYCONTROL | _displaycontrol);
  235. }
  236. // Turns the underline cursor on/off
  237. void LiquidCrystal_Prusa::noCursor() {
  238. _displaycontrol &= ~LCD_CURSORON;
  239. command(LCD_DISPLAYCONTROL | _displaycontrol);
  240. }
  241. void LiquidCrystal_Prusa::cursor() {
  242. _displaycontrol |= LCD_CURSORON;
  243. command(LCD_DISPLAYCONTROL | _displaycontrol);
  244. }
  245. // Turn on and off the blinking cursor
  246. void LiquidCrystal_Prusa::noBlink() {
  247. _displaycontrol &= ~LCD_BLINKON;
  248. command(LCD_DISPLAYCONTROL | _displaycontrol);
  249. }
  250. void LiquidCrystal_Prusa::blink() {
  251. _displaycontrol |= LCD_BLINKON;
  252. command(LCD_DISPLAYCONTROL | _displaycontrol);
  253. }
  254. // These commands scroll the display without changing the RAM
  255. void LiquidCrystal_Prusa::scrollDisplayLeft(void) {
  256. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  257. }
  258. void LiquidCrystal_Prusa::scrollDisplayRight(void) {
  259. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  260. }
  261. // This is for text that flows Left to Right
  262. void LiquidCrystal_Prusa::leftToRight(void) {
  263. _displaymode |= LCD_ENTRYLEFT;
  264. command(LCD_ENTRYMODESET | _displaymode);
  265. }
  266. // This is for text that flows Right to Left
  267. void LiquidCrystal_Prusa::rightToLeft(void) {
  268. _displaymode &= ~LCD_ENTRYLEFT;
  269. command(LCD_ENTRYMODESET | _displaymode);
  270. }
  271. // This will 'right justify' text from the cursor
  272. void LiquidCrystal_Prusa::autoscroll(void) {
  273. _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  274. command(LCD_ENTRYMODESET | _displaymode);
  275. }
  276. // This will 'left justify' text from the cursor
  277. void LiquidCrystal_Prusa::noAutoscroll(void) {
  278. _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  279. command(LCD_ENTRYMODESET | _displaymode);
  280. }
  281. // Allows us to fill the first 8 CGRAM locations
  282. // with custom characters
  283. void LiquidCrystal_Prusa::createChar_P(uint8_t location, const uint8_t* charmap)
  284. {
  285. location &= 0x7; // we only have 8 locations 0-7
  286. command(LCD_SETCGRAMADDR | (location << 3));
  287. for (int i=0; i<8; i++)
  288. send(pgm_read_byte(&charmap[i]), HIGH);
  289. }
  290. /*********** mid level commands, for sending data/cmds */
  291. inline void LiquidCrystal_Prusa::command(uint8_t value) {
  292. send(value, LOW);
  293. }
  294. inline size_t LiquidCrystal_Prusa::write(uint8_t value) {
  295. if (value == '\n')
  296. {
  297. if (_currline > 3) _currline = -1;
  298. setCursor(0, _currline + 1); // LF
  299. return 1;
  300. }
  301. if (_escape[0] || (value == 0x1b))
  302. return escape_write(value);
  303. send(value, HIGH);
  304. return 1; // assume sucess
  305. }
  306. //Supported VT100 escape codes:
  307. //EraseScreen "\x1b[2J"
  308. //CursorHome "\x1b[%d;%dH"
  309. //CursorShow "\x1b[?25h"
  310. //CursorHide "\x1b[?25l"
  311. inline size_t LiquidCrystal_Prusa::escape_write(uint8_t chr)
  312. {
  313. #define escape_cnt (_escape[0]) //escape character counter
  314. #define is_num_msk (_escape[1]) //numeric character bit mask
  315. #define chr_is_num (is_num_msk & 0x01) //current character is numeric
  316. #define e_2_is_num (is_num_msk & 0x04) //escape char 2 is numeric
  317. #define e_3_is_num (is_num_msk & 0x08) //...
  318. #define e_4_is_num (is_num_msk & 0x10)
  319. #define e_5_is_num (is_num_msk & 0x20)
  320. #define e_6_is_num (is_num_msk & 0x40)
  321. #define e_7_is_num (is_num_msk & 0x80)
  322. #define e2_num (_escape[2] - '0') //number from character 2
  323. #define e3_num (_escape[3] - '0') //number from character 3
  324. #define e23_num (10*e2_num+e3_num) //number from characters 2 and 3
  325. #define e4_num (_escape[4] - '0') //number from character 4
  326. #define e5_num (_escape[5] - '0') //number from character 5
  327. #define e45_num (10*e4_num+e5_num) //number from characters 4 and 5
  328. #define e6_num (_escape[6] - '0') //number from character 6
  329. #define e56_num (10*e5_num+e6_num) //number from characters 5 and 6
  330. if (escape_cnt > 1) // escape length > 1 = "\x1b["
  331. {
  332. _escape[escape_cnt] = chr; // store current char
  333. if ((chr >= '0') && (chr <= '9')) // char is numeric
  334. is_num_msk |= (1 | (1 << escape_cnt)); //set mask
  335. else
  336. is_num_msk &= ~1; //clear mask
  337. }
  338. switch (escape_cnt++)
  339. {
  340. case 0:
  341. if (chr == 0x1b) return 1; // escape = "\x1b"
  342. break;
  343. case 1:
  344. is_num_msk = 0x00; // reset 'is number' bit mask
  345. if (chr == '[') return 1; // escape = "\x1b["
  346. break;
  347. case 2:
  348. switch (chr)
  349. {
  350. case '2': return 1; // escape = "\x1b[2"
  351. case '?': return 1; // escape = "\x1b[?"
  352. default:
  353. if (chr_is_num) return 1; // escape = "\x1b[%1d"
  354. }
  355. break;
  356. case 3:
  357. switch (_escape[2])
  358. {
  359. case '?': // escape = "\x1b[?"
  360. if (chr == '2') return 1; // escape = "\x1b[?2"
  361. break;
  362. case '2':
  363. if (chr == 'J') // escape = "\x1b[2J"
  364. { clear(); _currline = 0; break; } // EraseScreen
  365. default:
  366. if (e_2_is_num && // escape = "\x1b[%1d"
  367. ((chr == ';') || // escape = "\x1b[%1d;"
  368. chr_is_num)) // escape = "\x1b[%2d"
  369. return 1;
  370. }
  371. break;
  372. case 4:
  373. switch (_escape[2])
  374. {
  375. case '?': // "\x1b[?"
  376. if ((_escape[3] == '2') && (chr == '5')) return 1; // escape = "\x1b[?25"
  377. break;
  378. default:
  379. if (e_2_is_num) // escape = "\x1b[%1d"
  380. {
  381. if ((_escape[3] == ';') && chr_is_num) return 1; // escape = "\x1b[%1d;%1d"
  382. else if (e_3_is_num && (chr == ';')) return 1; // escape = "\x1b[%2d;"
  383. }
  384. }
  385. break;
  386. case 5:
  387. switch (_escape[2])
  388. {
  389. case '?':
  390. if ((_escape[3] == '2') && (_escape[4] == '5')) // escape = "\x1b[?25"
  391. switch (chr)
  392. {
  393. case 'h': // escape = "\x1b[?25h"
  394. void cursor(); // CursorShow
  395. break;
  396. case 'l': // escape = "\x1b[?25l"
  397. noCursor(); // CursorHide
  398. break;
  399. }
  400. break;
  401. default:
  402. if (e_2_is_num) // escape = "\x1b[%1d"
  403. {
  404. if ((_escape[3] == ';') && e_4_is_num) // escape = "\x1b%1d;%1dH"
  405. {
  406. if (chr == 'H') // escape = "\x1b%1d;%1dH"
  407. setCursor(e4_num, e2_num); // CursorHome
  408. else if (chr_is_num)
  409. return 1; // escape = "\x1b%1d;%2d"
  410. }
  411. else if (e_3_is_num && (_escape[4] == ';') && chr_is_num)
  412. return 1; // escape = "\x1b%2d;%1d"
  413. }
  414. }
  415. break;
  416. case 6:
  417. if (e_2_is_num) // escape = "\x1b[%1d"
  418. {
  419. if ((_escape[3] == ';') && e_4_is_num && e_5_is_num && (chr == 'H')) // escape = "\x1b%1d;%2dH"
  420. setCursor(e45_num, e2_num); // CursorHome
  421. else if (e_3_is_num && (_escape[4] == ';') && e_5_is_num) // escape = "\x1b%2d;%1d"
  422. {
  423. if (chr == 'H') // escape = "\x1b%2d;%1dH"
  424. setCursor(e5_num, e23_num); // CursorHome
  425. else if (chr_is_num) // "\x1b%2d;%2d"
  426. return 1;
  427. }
  428. }
  429. break;
  430. case 7:
  431. if (e_2_is_num && e_3_is_num && (_escape[4] == ';')) // "\x1b[%2d;"
  432. if (e_5_is_num && e_6_is_num && (chr == 'H')) // "\x1b[%2d;%2dH"
  433. setCursor(e56_num, e23_num); // CursorHome
  434. break;
  435. }
  436. escape_cnt = 0; // reset escape
  437. end:
  438. return 1; // assume sucess
  439. }
  440. /************ low level data pushing commands **********/
  441. // write either command or data, with automatic 4/8-bit selection
  442. void LiquidCrystal_Prusa::send(uint8_t value, uint8_t mode) {
  443. digitalWrite(_rs_pin, mode);
  444. // if there is a RW pin indicated, set it low to Write
  445. if (_rw_pin != 255) {
  446. digitalWrite(_rw_pin, LOW);
  447. }
  448. if (_displayfunction & LCD_8BITMODE) {
  449. write8bits(value);
  450. } else {
  451. write4bits(value>>4);
  452. write4bits(value);
  453. }
  454. }
  455. void LiquidCrystal_Prusa::pulseEnable(void) {
  456. digitalWrite(_enable_pin, LOW);
  457. delayMicroseconds(1);
  458. digitalWrite(_enable_pin, HIGH);
  459. delayMicroseconds(1); // enable pulse must be >450ns
  460. digitalWrite(_enable_pin, LOW);
  461. delayMicroseconds(100); // commands need > 37us to settle
  462. }
  463. void LiquidCrystal_Prusa::write4bits(uint8_t value) {
  464. for (int i = 0; i < 4; i++) {
  465. pinMode(_data_pins[i], OUTPUT);
  466. digitalWrite(_data_pins[i], (value >> i) & 0x01);
  467. }
  468. pulseEnable();
  469. }
  470. void LiquidCrystal_Prusa::write8bits(uint8_t value) {
  471. for (int i = 0; i < 8; i++) {
  472. pinMode(_data_pins[i], OUTPUT);
  473. digitalWrite(_data_pins[i], (value >> i) & 0x01);
  474. }
  475. pulseEnable();
  476. }
  477. void LiquidCrystal_Prusa::print(const char* s)
  478. {
  479. while (*s) write(*(s++));
  480. }
  481. void LiquidCrystal_Prusa::print(char c, int base)
  482. {
  483. print((long) c, base);
  484. }
  485. void LiquidCrystal_Prusa::print(unsigned char b, int base)
  486. {
  487. print((unsigned long) b, base);
  488. }
  489. void LiquidCrystal_Prusa::print(int n, int base)
  490. {
  491. print((long) n, base);
  492. }
  493. void LiquidCrystal_Prusa::print(unsigned int n, int base)
  494. {
  495. print((unsigned long) n, base);
  496. }
  497. void LiquidCrystal_Prusa::print(long n, int base)
  498. {
  499. if (base == 0) {
  500. write(n);
  501. } else if (base == 10) {
  502. if (n < 0) {
  503. print('-');
  504. n = -n;
  505. }
  506. printNumber(n, 10);
  507. } else {
  508. printNumber(n, base);
  509. }
  510. }
  511. void LiquidCrystal_Prusa::print(unsigned long n, int base)
  512. {
  513. if (base == 0) write(n);
  514. else printNumber(n, base);
  515. }
  516. void LiquidCrystal_Prusa::print(double n, int digits)
  517. {
  518. printFloat(n, digits);
  519. }
  520. void LiquidCrystal_Prusa::println(void)
  521. {
  522. print('\r');
  523. print('\n');
  524. }
  525. /*void LiquidCrystal_Prusa::println(const String &s)
  526. {
  527. print(s);
  528. println();
  529. }*/
  530. void LiquidCrystal_Prusa::println(const char c[])
  531. {
  532. print(c);
  533. println();
  534. }
  535. void LiquidCrystal_Prusa::println(char c, int base)
  536. {
  537. print(c, base);
  538. println();
  539. }
  540. void LiquidCrystal_Prusa::println(unsigned char b, int base)
  541. {
  542. print(b, base);
  543. println();
  544. }
  545. void LiquidCrystal_Prusa::println(int n, int base)
  546. {
  547. print(n, base);
  548. println();
  549. }
  550. void LiquidCrystal_Prusa::println(unsigned int n, int base)
  551. {
  552. print(n, base);
  553. println();
  554. }
  555. void LiquidCrystal_Prusa::println(long n, int base)
  556. {
  557. print(n, base);
  558. println();
  559. }
  560. void LiquidCrystal_Prusa::println(unsigned long n, int base)
  561. {
  562. print(n, base);
  563. println();
  564. }
  565. void LiquidCrystal_Prusa::println(double n, int digits)
  566. {
  567. print(n, digits);
  568. println();
  569. }
  570. void LiquidCrystal_Prusa::printNumber(unsigned long n, uint8_t base)
  571. {
  572. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  573. unsigned long i = 0;
  574. if (n == 0) {
  575. print('0');
  576. return;
  577. }
  578. while (n > 0) {
  579. buf[i++] = n % base;
  580. n /= base;
  581. }
  582. for (; i > 0; i--)
  583. print((char) (buf[i - 1] < 10 ?
  584. '0' + buf[i - 1] :
  585. 'A' + buf[i - 1] - 10));
  586. }
  587. void LiquidCrystal_Prusa::printFloat(double number, uint8_t digits)
  588. {
  589. // Handle negative numbers
  590. if (number < 0.0)
  591. {
  592. print('-');
  593. number = -number;
  594. }
  595. // Round correctly so that print(1.999, 2) prints as "2.00"
  596. double rounding = 0.5;
  597. for (uint8_t i=0; i<digits; ++i)
  598. rounding /= 10.0;
  599. number += rounding;
  600. // Extract the integer part of the number and print it
  601. unsigned long int_part = (unsigned long)number;
  602. double remainder = number - (double)int_part;
  603. print(int_part);
  604. // Print the decimal point, but only if there are digits beyond
  605. if (digits > 0)
  606. print(".");
  607. // Extract digits from the remainder one at a time
  608. while (digits-- > 0)
  609. {
  610. remainder *= 10.0;
  611. int toPrint = int(remainder);
  612. print(toPrint);
  613. remainder -= toPrint;
  614. }
  615. }