Advertisement
Guest User

Untitled

a guest
May 24th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /* global setInterval: false, setTimeout: false, clearInterval: false, clearTimeout:false, module: false, console: false */
  2.  
  3. /**
  4. * Module exports.
  5. */
  6.  
  7. module.exports = LcdTextHelper;
  8.  
  9. /**
  10. * LcdTextHelper constructor.
  11. *
  12. * @param {Jhd1313m1} UPM I2C LCD instance
  13. * @api public
  14. */
  15. function LcdTextHelper(lcd) {
  16.  
  17. var TOTAL_LCD_ROWS = 2;
  18. var TOTAL_LCD_COLUMNS = 16;
  19. var KEEP_TEXT_ON_SCREEN_DELAY = 2000; // in ms
  20. var TEXT_UPDATE_INTERVAL = 550; // in ms, should be less than TEXT_DRAW_INTERVAL
  21. var TEXT_DRAW_INTERVAL = 500; // in ms, should be more than TEXT_UPDATE_INTERVAL
  22. var REPEATING_TEXT_PADDING = " "; // whitespace between looping message when text is scrolling
  23. var BLANK_ROW_TEXT = " "; // exactly 16 space characters
  24.  
  25. var lcdRows = []; //var EXAMPLE_ROW_OBJECT = { displayText: BLANK_ROW_TEXT, updateDisplayTextIntervalId: 0 };
  26. var startScrollingTimeoutId = 0;
  27. var drawIntervalId = 0;
  28.  
  29. /**
  30. * Takes array of message strings to display.
  31. * Later uses string lengths to set text on LCD with or without scrolling.
  32. *
  33. * @param {string[]} messages - Each string in the array corresponds to a row on the LCD.
  34. * @api public
  35. */
  36. function set(messages){
  37.  
  38. reset();
  39.  
  40. messages = messages || []; // default, empty array
  41. if (messages.length > TOTAL_LCD_ROWS) {
  42. console.log("This LCD screen only supports %s rows of text but you tried to give me %s", TOTAL_LCD_ROWS, messages.length);
  43. messages.length = TOTAL_LCD_ROWS; // truncate array, if needed
  44. }
  45.  
  46. for (var row=0; row<messages.length; row++) {
  47. var msg = messages[row];
  48. if (msg.length < TOTAL_LCD_COLUMNS) {
  49. msg = msg + BLANK_ROW_TEXT.slice(msg.length); // fill in any remaining gaps with spaces
  50. } else if (msg.length > TOTAL_LCD_COLUMNS) {
  51. msg = msg + REPEATING_TEXT_PADDING; // add padding for looping text
  52. }
  53.  
  54. // create a new slot in lcdRows to save the display string
  55. // (and later save any scrolling interval timer IDs)
  56. lcdRows[row] = {displayText: msg};
  57. }
  58.  
  59. // immediately draw text to LCD then, after a slight startup delay, start scrolling effect
  60. draw();
  61. startScrollingTimeoutId = setTimeout(startScrolling, KEEP_TEXT_ON_SCREEN_DELAY);
  62. }
  63.  
  64. /**
  65. * Checks length of display text strings and, if long enough for scrolling, will:
  66. * - Add a timer interval for each row to update the string value to give illusion of scrolling.
  67. * - Add a timer interval to re-draw all strings to the LCD.
  68. *
  69. * @api private
  70. */
  71. function startScrolling() {
  72. var totalScrolling = 0;
  73.  
  74. for (var r=0; r<lcdRows.length; r++) {
  75. if (lcdRows[r].displayText.length > TOTAL_LCD_COLUMNS) {
  76. lcdRows[r].updateDisplayTextIntervalId = setInterval(updateScrollingRow, TEXT_UPDATE_INTERVAL, r);
  77. totalScrolling++;
  78. }
  79. }
  80.  
  81. if (totalScrolling > 0) {
  82. drawIntervalId = setInterval(draw, TEXT_DRAW_INTERVAL);
  83. }
  84. }
  85.  
  86. /**
  87. * Takes the first character of the display string and moves it to the end
  88. * of the string to give the illusion of scrolling.
  89. *
  90. * @api private
  91. */
  92. function updateScrollingRow(row){
  93. var currentText = lcdRows[row].displayText;
  94. var shiftedText = currentText.slice(1) + currentText.slice(0, 1);
  95. lcdRows[row].displayText = shiftedText;
  96. }
  97.  
  98. /**
  99. * Draws all display strings to the LCD
  100. *
  101. * @api private
  102. */
  103. function draw() {
  104. //console.log("----------------");
  105. for (var r=0; r<lcdRows.length; r++) {
  106. var text = lcdRows[r].displayText.slice(0, TOTAL_LCD_COLUMNS);
  107. //console.log(text);
  108. lcd.setCursor(r, 0);
  109. lcd.write(text);
  110. }
  111. }
  112.  
  113. /**
  114. * Clear LCD screen and clear any timer events that might try to write to the screen
  115. *
  116. * @api public
  117. */
  118. function reset() {
  119.  
  120. // write blank rows instead of lcd.clear()
  121. // which takes too long to execute and messes up drawn text sometimes
  122. lcd.setCursor(0, 0);
  123. lcd.write(BLANK_ROW_TEXT);
  124. lcd.setCursor(1, 0);
  125. lcd.write(BLANK_ROW_TEXT);
  126.  
  127. clearInterval(drawIntervalId);
  128. clearTimeout(startScrollingTimeoutId);
  129. for (var r=0; r<lcdRows.length; r++) {
  130. var rowObject = lcdRows[r];
  131. clearInterval(rowObject.updateDisplayTextIntervalId);
  132. }
  133. lcdRows.length = 0;
  134. }
  135.  
  136. /**
  137. * Return public methods for module.exports.
  138. */
  139. return {
  140. set: set,
  141. reset: reset
  142. };
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement