This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
/
ScreenTask.cpp
142 lines (121 loc) · 3.03 KB
/
ScreenTask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
//
//
#include "ScreenTask.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
ScreenTask::ScreenTask() :
Task() {
if (u8g.getMode() == U8G_MODE_R3G3B2) {
u8g.setColorIndex(255); // white
}
else if (u8g.getMode() == U8G_MODE_GRAY2BIT) {
u8g.setColorIndex(3); // max intensity
}
else if (u8g.getMode() == U8G_MODE_BW) {
u8g.setColorIndex(1); // pixel on
}
else if (u8g.getMode() == U8G_MODE_HICOLOR) {
u8g.setHiColorByRGB(255, 255, 255);
}
// init
this->clear();
}
void ScreenTask::setAutoFlush(bool autoFlush){
this->autoFlush = autoFlush;
}
void ScreenTask::execute(String command){
String cmd = "SCREEN";
String clearcmd = "CLEAR";
if (command.substring(0, cmd.length()) == cmd){
String originalCommand = command.substring(0);
String message = command.substring(cmd.length());
message.trim();
this->println(message);
}
else if (command.substring(0, clearcmd.length()) == clearcmd){
this->clear();
}
}
int ScreenTask::print(String string, int position){
if (string.length() > (SCREEN_WIDTH / CHAR_WIDTH)){
string = string.substring(0, 22) + "...";
}
int targetPosition = this->currentRow;
if (position != 0)
targetPosition = position;
if (0 == string.compareTo(this->content[targetPosition]))
return targetPosition;
this->content[targetPosition] = string;
// if auto-flush = true => draw the content , else need call flush method to draw
if (this->autoFlush)
this->draw();
if (position == 0)
return this->currentRow;
return position;
}
void ScreenTask::println(String string){
if (this->currentRow == 9){ // last row
for (int i = 0; i < 8; i++)
this->content[i] = this->content[i + 1];
this->currentRow = 8;
this->print(string);
this->currentRow = 9;
}
else{
this->print(string);
this->currentRow = this->currentRow + 1;
}
}
void ScreenTask::out(String string){
while (-1 != string.indexOf('\n')){
String line = string.substring(0, string.indexOf('\n'));
string = string.substring(string.indexOf('\n') + 1);
line.trim();
this->println(line);
}
// one line or last line
string.trim();
if (string.length() > 0){
this->println(string);
}
}
void ScreenTask::debug(String string){
while (string.length() > 0){
String line = string.substring(0, 25);
string = string.substring(25);
line.trim();
this->println(line);
}
// one line or last line
string.trim();
if (string.length() > 0){
this->println(string);
}
}
void ScreenTask::clear(){
u8g.firstPage();
do {} while (u8g.nextPage());
for (int i = 0; i < 9; i++){
this->content[i] = String("");
}
this->currentRow = 0;
}
void ScreenTask::flush(){
this->draw();
}
void ScreenTask::draw(){
//u8g.setFont(u8g_font_unifont);
u8g.setFont(u8g_font_5x7);
// print message
u8g.firstPage();
do {
for (int i = 0; i < 9; i++){
if (NULL != this->content[i] ){
String msg = this->content[i];
char convertedMessage[msg.length() + 1];
msg.toCharArray(convertedMessage, msg.length() + 1);
u8g.drawStr(0, (i*CHAR_HEIGHT) + CHAR_HEIGHT, convertedMessage);
}
}
} while (u8g.nextPage());
}