Skip to content

Commit d61f570

Browse files
committed
Added Arduino Robot LCD library subset
1 parent 69dd55c commit d61f570

File tree

3 files changed

+170
-66
lines changed

3 files changed

+170
-66
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ SD card, and needs to be included in all sketches.
2222
See also:
2323
* [Adafruit GFX Graphics Library](https://learn.adafruit.com/adafruit-gfx-graphics-library)
2424

25-
Processing subset:
25+
Arduino Robot LCD library subset:
26+
* [debugPrint()](https://www.arduino.cc/en/Reference/RobotDebugPrint)
27+
* [clearScreen()](https://www.arduino.cc/en/Reference/RobotClearScreen)
28+
* [drawCompass()](https://www.arduino.cc/en/Reference/RobotDrawCompass)
29+
* [drawBMP()](https://www.arduino.cc/en/Reference/RobotDrawBMP)
30+
31+
Processing library subset:
2632
* [PImage](https://processing.org/reference/PImage.html)
2733
* [loadImage()](https://processing.org/reference/loadImage_.html)
2834
* [image()](https://processing.org/reference/image_.html)

src/LcdScreen.cpp

+132-57
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,59 @@ void LcdScreen::begin() {
5050
#endif
5151
}
5252

53+
// Arduino Robot library compatibility.
54+
void LcdScreen::debugPrint(long value, uint8_t x, uint8_t y) {
55+
static long oldVal = 0;
56+
stroke(backgroundColor);
57+
text(oldVal, x, y);
58+
stroke(strokeColor);
59+
text(value, x, y);
60+
oldVal = value;
61+
}
62+
63+
void LcdScreen::clearScreen() {
64+
background(backgroundColor);
65+
}
66+
67+
void drawCompassBase(LcdScreen* lcd, color strokeColor) {
68+
lcd->drawCircle(64, 80, 50, strokeColor);
69+
lcd->drawLine(64, 30, 64, 20, strokeColor);
70+
}
71+
72+
void drawCompassDire(LcdScreen* lcd, int16_t dire, color backgroundColor) {
73+
static uint8_t x_old;
74+
static uint8_t y_old;
75+
static uint8_t x_t_old;
76+
static uint8_t y_t_old;
77+
78+
uint8_t x = 60 * sin(dire / 360.0*6.28) + 64;
79+
uint8_t x_t = 40 * sin(dire / 360.0*6.28) + 64;
80+
uint8_t y = 60 * cos(dire / 360.0*6.28) + 80;
81+
uint8_t y_t = 40 * cos(dire / 360.0*6.28) + 80;
82+
83+
lcd->drawLine(x_t_old, y_t_old, x_old, y_old, backgroundColor);
84+
lcd->drawLine(x_t, y_t, x, y, ILI9163_RED);
85+
86+
x_old = x;
87+
y_old = y;
88+
x_t_old = x_t;
89+
y_t_old = y_t;
90+
}
91+
92+
void LcdScreen::drawCompass(uint16_t value) {
93+
drawCompassBase(this, strokeColor);
94+
drawCompassDire(this, value, backgroundColor);
95+
debugPrint(value, 57, 76);
96+
}
97+
5398
// Arduino TFT library compatibility.
5499

55100
void LcdScreen::background(uint8_t red, uint8_t green, uint8_t blue) {
56101
background(Color565(red, green, blue));
57102
}
58103

59104
void LcdScreen::background(color c) {
105+
backgroundColor = c;
60106
fillScreen(c);
61107
}
62108

@@ -88,85 +134,114 @@ void LcdScreen::fill(color c) {
88134
}
89135

90136
void LcdScreen::point(int16_t x, int16_t y) {
91-
if (!useStroke)
92-
return;
137+
if (!useStroke)
138+
return;
139+
140+
drawPixel(x, y, strokeColor);
141+
}
142+
143+
void LcdScreen::text(char value, uint8_t x, uint8_t y) {
144+
if (!useStroke)
145+
return;
146+
147+
setTextWrap(false);
148+
setTextColor(strokeColor);
149+
setCursor(x, y);
150+
print(value);
151+
}
152+
153+
void LcdScreen::text(int value, uint8_t x, uint8_t y) {
154+
if (!useStroke)
155+
return;
156+
157+
setTextWrap(false);
158+
setTextColor(strokeColor);
159+
setCursor(x, y);
160+
print(value);
161+
}
162+
void LcdScreen::text(long value, uint8_t x, uint8_t y) {
163+
if (!useStroke)
164+
return;
93165

94-
drawPixel(x, y, strokeColor);
166+
setTextWrap(false);
167+
setTextColor(strokeColor);
168+
setCursor(x, y);
169+
print(value);
95170
}
96171

97172
void LcdScreen::text(const char * text, int16_t x, int16_t y) {
98-
if (!useStroke)
99-
return;
173+
if (!useStroke)
174+
return;
100175

101-
setTextWrap(false);
102-
setTextColor(strokeColor);
103-
setCursor(x, y);
104-
print(text);
176+
setTextWrap(false);
177+
setTextColor(strokeColor);
178+
setCursor(x, y);
179+
print(text);
105180
}
106181

107182
void LcdScreen::textSize(uint8_t size) {
108-
setTextSize(size);
183+
setTextSize(size);
109184
}
110185

111186
void LcdScreen::line(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
112-
if (!useStroke)
113-
return;
114-
115-
if (x1 == x2) {
116-
if (y1 < y2)
117-
drawFastVLine(x1, y1, y2 - y1, strokeColor);
118-
else
119-
drawFastVLine(x1, y2, y1 - y2, strokeColor);
120-
}
121-
else if (y1 == y2) {
122-
if (x1 < x2)
123-
drawFastHLine(x1, y1, x2 - x1, strokeColor);
124-
else
125-
drawFastHLine(x2, y1, x1 - x2, strokeColor);
126-
}
127-
else {
128-
drawLine(x1, y1, x2, y2, strokeColor);
129-
}
187+
if (!useStroke)
188+
return;
189+
190+
if (x1 == x2) {
191+
if (y1 < y2)
192+
drawFastVLine(x1, y1, y2 - y1, strokeColor);
193+
else
194+
drawFastVLine(x1, y2, y1 - y2, strokeColor);
195+
}
196+
else if (y1 == y2) {
197+
if (x1 < x2)
198+
drawFastHLine(x1, y1, x2 - x1, strokeColor);
199+
else
200+
drawFastHLine(x2, y1, x1 - x2, strokeColor);
201+
}
202+
else {
203+
drawLine(x1, y1, x2, y2, strokeColor);
204+
}
130205
}
131206

132207
void LcdScreen::rect(int16_t x, int16_t y, int16_t width, int16_t height) {
133-
if (useFill) {
134-
fillRect(x, y, width, height, fillColor);
135-
}
136-
if (useStroke) {
137-
drawRect(x, y, width, height, strokeColor);
138-
}
208+
if (useFill) {
209+
fillRect(x, y, width, height, fillColor);
210+
}
211+
if (useStroke) {
212+
drawRect(x, y, width, height, strokeColor);
213+
}
139214
}
140215

141216
void LcdScreen::rect(int16_t x, int16_t y, int16_t width, int16_t height, int16_t radius) {
142-
if (radius == 0) {
143-
rect(x, y, width, height);
144-
}
145-
if (useFill) {
146-
fillRoundRect(x, y, width, height, radius, fillColor);
147-
}
148-
if (useStroke) {
149-
drawRoundRect(x, y, width, height, radius, strokeColor);
150-
}
217+
if (radius == 0) {
218+
rect(x, y, width, height);
219+
}
220+
if (useFill) {
221+
fillRoundRect(x, y, width, height, radius, fillColor);
222+
}
223+
if (useStroke) {
224+
drawRoundRect(x, y, width, height, radius, strokeColor);
225+
}
151226
}
152227

153228
void LcdScreen::circle(int16_t x, int16_t y, int16_t r) {
154-
if (r == 0)
155-
return;
229+
if (r == 0)
230+
return;
156231

157-
if (useFill) {
158-
fillCircle(x, y, r, fillColor);
159-
}
160-
if (useStroke) {
161-
drawCircle(x, y, r, strokeColor);
162-
}
232+
if (useFill) {
233+
fillCircle(x, y, r, fillColor);
234+
}
235+
if (useStroke) {
236+
drawCircle(x, y, r, strokeColor);
237+
}
163238
}
164239

165240
void LcdScreen::triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3) {
166-
if (useFill) {
167-
fillTriangle(x1, y1, x2, y2, x3, y3, fillColor);
168-
}
169-
if (useStroke) {
170-
drawTriangle(x1, y1, x2, y2, x3, y3, strokeColor);
171-
}
241+
if (useFill) {
242+
fillTriangle(x1, y1, x2, y2, x3, y3, fillColor);
243+
}
244+
if (useStroke) {
245+
drawTriangle(x1, y1, x2, y2, x3, y3, strokeColor);
246+
}
172247
}

src/LcdScreen.h

+31-8
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6969
#if defined(__SD_H__) // Arduino SD library
7070
#include "utility/PImage.h"
7171
#else
72-
#warning "The SD library was not found. loadImage() and image() won't be supported."
72+
#warning "The SD library was not found. loadImage() image() and drawBMP() won't be supported."
7373
#endif
7474

7575
typedef uint16_t color;
@@ -88,10 +88,23 @@ class LcdScreen : public Arduino_ILI9163 {
8888

8989
#if defined(__SD_H__) // Arduino SD library
9090
PImage loadImage(const char * fileName) { return PImage::loadImage(fileName); }
91-
9291
void image(PImage & img, uint16_t x, uint16_t y);
92+
93+
// https://www.arduino.cc/en/Reference/RobotDrawBMP
94+
void drawBMP(const char* filename, uint8_t x, uint8_t y);
9395
#endif
9496

97+
// Arduino Robot library compatibility.
98+
void
99+
// https://www.arduino.cc/en/Reference/RobotDebugPrint
100+
debugPrint(long value, uint8_t x = 0, uint8_t y = 0),
101+
102+
// https://www.arduino.cc/en/Reference/RobotClearScreen
103+
clearScreen(),
104+
105+
// https://www.arduino.cc/en/Reference/RobotDrawCompass
106+
drawCompass(uint16_t value);
107+
95108
// Arduino TFT library compatibility.
96109
void
97110
// http://processing.org/reference/background_.html
@@ -114,12 +127,14 @@ class LcdScreen : public Arduino_ILI9163 {
114127

115128
// https://processing.org/reference/text_.html
116129
text(const char * text, int16_t x, int16_t y),
130+
text(char value, uint8_t x, uint8_t y),
131+
text(int value, uint8_t x, uint8_t y),
132+
text(long value, uint8_t x, uint8_t y),
117133

118134
// https://processing.org/reference/textSize_.html
119135
textSize(uint8_t size),
120136

121-
// similar to ellipse() in Processing, but with
122-
// a single radius.
137+
// similar to ellipse() in Processing, but with a single radius.
123138
// http://processing.org/reference/ellipse_.html
124139
circle(int16_t x, int16_t y, int16_t r),
125140
point(int16_t x, int16_t y),
@@ -128,15 +143,16 @@ class LcdScreen : public Arduino_ILI9163 {
128143
rect(int16_t x, int16_t y, int16_t width, int16_t height),
129144
rect(int16_t x, int16_t y, int16_t width, int16_t height, int16_t radius),
130145
triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3);
131-
;
146+
132147
protected:
133148
/*
134149
* Processing-style graphics state
135150
*/
136-
color strokeColor = 0x0000;
151+
color strokeColor = ILI9163_BLACK;
137152
bool useStroke = false;
138-
color fillColor = 0x0000;
153+
color fillColor = ILI9163_BLACK;
139154
bool useFill = false;
155+
color backgroundColor = ILI9163_WHITE;
140156
};
141157

142158
/// Esplora boards have hard-wired connections with
@@ -203,7 +219,7 @@ void LcdScreen::image(PImage & img, uint16_t x, uint16_t y) {
203219

204220
} // end pixel
205221
} // end scanline
206-
222+
img._bmpFile.close();
207223
}
208224

209225

@@ -319,6 +335,13 @@ PImage PImage::loadImage(const char * fileName) {
319335
return PImage(bmpFile, bmpWidth, bmpHeight, bmpDepth, bmpImageoffset, rowSize, flip);
320336
}
321337

338+
void LcdScreen::drawBMP(const char* filename, uint8_t x, uint8_t y) {
339+
PImage bmp = loadImage(filename);
340+
if (bmp.isValid()) {
341+
image(bmp, x, y);
342+
}
343+
}
344+
322345
#endif // __SD_H__
323346

324347
#endif // _ARDUINO_LCD_SCREEN_H

0 commit comments

Comments
 (0)