1 package org.rpi.plugin.lcddisplay;
2
3 import org.apache.log4j.Logger;
4
5 import com.pi4j.util.StringUtil;
6
7 public class LCDRow {
8
9 private static Logger log = Logger.getLogger(LCDRow.class);
10
11 private int LCD_WIDTH = 20;
12 private String current_text = "";
13 private String next_text = "";
14 private int position = 0;
15
16 public int getLCD_WIDTH() {
17 return LCD_WIDTH;
18 }
19
20
21
22
23
24 public void setLCD_WIDTH(int lCD_WIDTH) {
25 LCD_WIDTH = lCD_WIDTH;
26 }
27
28
29
30
31
32 public String getText() {
33 UpdateText();
34 if (current_text.length() <= LCD_WIDTH)
35 return current_text;
36 else
37 return getNextString(current_text);
38 }
39
40
41
42
43
44 public void setText(String text) {
45 if (text.length() <= LCD_WIDTH) {
46 next_text = StringUtil.padCenter(text, LCD_WIDTH);
47 } else {
48 next_text = StringUtil.padLeft("", LCD_WIDTH) + text + StringUtil.padRight("", LCD_WIDTH);
49 }
50 }
51
52
53
54
55 public void UpdateText() {
56 if (current_text != next_text) {
57 if (next_text.length() <= LCD_WIDTH) {
58 position = 0;
59 } else {
60 }
61 current_text = next_text;
62 }
63 }
64
65
66
67
68
69
70 private String getNextString(String in) {
71 String res = "";
72 position++;
73 if (position + LCD_WIDTH > in.length()) {
74 position = 0;
75 }
76 if (in.length() > LCD_WIDTH) {
77 try {
78 res = in.substring(position, position + LCD_WIDTH);
79 } catch (Exception e) {
80 log.error("Error getNextString. Position: " + position + " Text:" + in, e);
81 position = 0;
82 }
83 }
84 return res;
85 }
86
87 }