1 package org.rpi.plugin.lcddisplay;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7 import java.util.concurrent.ConcurrentHashMap;
8
9 import org.apache.log4j.Logger;
10 import org.rpi.os.OSManager;
11
12 import com.pi4j.system.SystemInfo;
13 import com.pi4j.wiringpi.Lcd;
14
15 public class LCDScroller extends Thread {
16
17 private static Logger log = Logger.getLogger(LCDScroller.class);
18 private int lcdHandle = -1;
19 private int LCD_COLUMNS = 20;
20 private int LCD_ROWS = 2;
21 private boolean bStandBy = true;
22 private boolean bStartup = true;
23 private boolean bReset = true;
24 private ConcurrentHashMap<String, String> values = new ConcurrentHashMap<String, String>();
25 private ArrayList<RowDefinition> row_definition = new ArrayList<RowDefinition>();
26 private List<LCDRow> rows = new ArrayList<LCDRow>();
27 private ArrayList<RowDefinition> standby_definition = new ArrayList<RowDefinition>();
28
29
30
31
32
33
34
35 public LCDScroller(int lcd_rows, int lcd_columns, ArrayList<RowDefinition> row_definition, ArrayList<RowDefinition> standby_definition) {
36 LCD_ROWS = lcd_rows;
37 LCD_COLUMNS = lcd_columns;
38 for (int i = 0; i < LCD_ROWS; i++) {
39 LCDRow row = new LCDRow();
40 row.setLCD_WIDTH(LCD_COLUMNS);
41 rows.add(row);
42 }
43 this.row_definition = row_definition;
44 this.standby_definition = standby_definition;
45 init();
46 }
47
48
49
50
51 private void init() {
52 log.debug("Initialize Values");
53 updateValues("[VOLUME]", "100");
54 updateValues("[TIME]", "0:00");
55 updateValues("[FULL_DETAILS]", "");
56 updateValues("[ALBUM]", "");
57 updateValues("[ARTIST]", "");
58 updateValues("[TRACK]", "");
59 updateValues("[TITLE]", "");
60 updateValues("[COMPOSER]", "");
61 updateValues("[CONDUCTOR]", "");
62 updateValues("[DATE]", "");
63 }
64
65 public void run() {
66 while (true) {
67 try {
68 if (isReset()) {
69 if (lcdHandle != -1) {
70 Lcd.lcdClear(lcdHandle);
71 }
72 for(int i=0;i<LCD_ROWS;i++)
73 {
74 setText("", i);
75 }
76 bReset = false;
77 }
78 if (bStandBy) {
79 if (bStartup) {
80
81 } else {
82
83 }
84
85 updateRows();
86 } else {
87 bStartup = false;
88 }
89 for (int i = 0; i < LCD_ROWS; i++) {
90 LCDRow row = rows.get(i);
91 LCDWrite(row.getText(), i);
92 }
93
94 } catch (Exception e) {
95 log.error(e);
96 }
97 try {
98
99 Thread.sleep(200);
100 } catch (InterruptedException e) {
101 }
102 }
103 }
104
105 private void standbyMessage() {
106 Date now = new Date();
107 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
108 String dateStr = formatter.format(now);
109 String cpuTemp = "";
110 try {
111 cpuTemp = "CPU Temp:" + SystemInfo.getCpuTemperature() + "C";
112 } catch (Exception e) {
113 log.error("Error Getting CPU Temp", e);
114 }
115 setText(cpuTemp, 0);
116 setText(dateStr, 1);
117 }
118
119
120
121
122 private void welcomeMessage() {
123
124 String sWelcome = "Welcome";
125 String sStatus = "";
126 try {
127 sStatus = "CPU Temp:" + SystemInfo.getCpuTemperature() + " Memory Free:" + convertBytes(SystemInfo.getMemoryFree()) + " Memory Used:" + convertBytes(SystemInfo.getMemoryUsed());
128 } catch (Exception e) {
129 log.error(e);
130 }
131 setText(sWelcome, 0);
132 setText(sStatus, 1);
133 }
134
135 private final String[] Q = new String[] { "", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb" };
136
137 public String convertBytes(long bytes) {
138 for (int i = 6; i > 0; i--) {
139 double step = Math.pow(1024, i);
140 if (bytes > step)
141 return String.format("%3.1f %s", bytes / step, Q[i]);
142 }
143 return Long.toString(bytes);
144 }
145
146
147
148
149
150 public int getLCDHandle() {
151 return lcdHandle;
152 }
153
154
155
156
157
158 public void setLCDHandle(int lcdHandle) {
159 this.lcdHandle = lcdHandle;
160 }
161
162
163
164
165
166
167 public synchronized void LCDWrite(String s, int Row) {
168 if (lcdHandle != -1) {
169 try {
170 Lcd.lcdPosition(lcdHandle, 0, Row);
171 byte[] data = s.getBytes("UTF-8");
172 for (byte b : data)
173 Lcd.lcdPutchar(lcdHandle, b);
174 } catch (Exception e) {
175 log.error(e);
176 }
177 }
178 }
179
180
181
182
183
184
185 public synchronized void setText(String text, int row) {
186
187 try {
188 rows.get(row).setText(text);
189 } catch (Exception e) {
190 log.error(e);
191 }
192 }
193
194 public synchronized boolean isStandBy() {
195 return bStandBy;
196 }
197
198 public synchronized void setStandBy(boolean bStandBy) {
199 this.bStandBy = bStandBy;
200 updateRows();
201 }
202
203 public synchronized boolean isReset() {
204 return bReset;
205 }
206
207
208
209
210 public synchronized void setReset() {
211 this.bReset = true;
212 }
213
214
215
216
217
218
219
220 public synchronized void updateValues(String name, String value) {
221 if (values.containsKey(name)) {
222 values.remove(name);
223 }
224 values.put(name, value);
225 updateRows();
226 }
227
228
229
230
231 private void updateRows() {
232 int i = 0;
233 ArrayList<RowDefinition> list = new ArrayList<RowDefinition>();
234 if(bStandBy)
235 {
236 list = standby_definition;
237 }
238 else
239 {
240 list = row_definition;
241 }
242 for (RowDefinition def : list) {
243 String text = def.getText();
244
245 for (KeyDefinition rd : def.getKeys()) {
246
247 String value = "";
248
249 if (values.containsKey(rd.getKey())) {
250
251 value = values.get(rd.getKey());
252 }
253 if (rd.isFormatted()) {
254 try {
255
256 Date date = new SimpleDateFormat("yyyy-MM-dd").parse(value);
257 value = new SimpleDateFormat(rd.getFormat()).format(date);
258
259
260 } catch (Exception e) {
261
262 }
263 }
264
265 text = text.replace(rd.getName(), value);
266 }
267
268 if (OSManager.getInstance().isRaspi()) {
269 for (KeyDefinition rd : def.getSystemKeys()) {
270 String key = rd.getKey();
271 try {
272 if (key.contains("[SYS_MEMORY_USED]")) {
273 text = text.replace(rd.getName(), convertBytes(SystemInfo.getMemoryUsed()));
274 }
275 else if(key.contains("[SYS_CPU_TEMP")){
276 text = text.replace(rd.getName(), ""+SystemInfo.getCpuTemperature());
277 }
278 else if(key.contains("[SYS_MEMORY_FREE]"))
279 {
280 text = text.replace(rd.getName(), convertBytes(SystemInfo.getMemoryFree()));
281 }
282 else if(key.contains("[SYS_DATE]"))
283 {
284 try
285 {
286 Date date = new Date();
287 text = text.replace(rd.getName(),new SimpleDateFormat(rd.getFormat()).format(date));
288 }
289 catch(Exception e)
290 {
291
292 }
293 }
294 else if(key.contains("[SYS_BOARD_TYPE]"))
295 {
296 text = text.replace(rd.getName(),SystemInfo.getBoardType().name());
297 }
298 } catch (Exception e) {
299 log.error("Error gettting SystimInfo:", e);
300 }
301 }
302 }
303
304 rows.get(i).setText(text);
305 i++;
306 }
307
308 }
309 }