-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathSprite_drawPixel.ino
More file actions
137 lines (106 loc) · 4.25 KB
/
Sprite_drawPixel.ino
File metadata and controls
137 lines (106 loc) · 4.25 KB
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
/*
Sketch to show how a Sprite is created, how to draw pixels
and text within the Sprite and then push the Sprite onto
the display screen.
Example for library:
https://github.com/Bodmer/TFT_eSPI
A Sprite is notionally an invisible graphics screen that is
kept in the processors RAM. Graphics can be drawn into the
Sprite just as it can be drawn directly to the screen. Once
the Sprite is completed it can be plotted onto the screen in
any position. If there is sufficient RAM then the Sprite can
be the same size as the screen and used as a frame buffer.
A 16 bit Sprite occupies (2 * width * height) bytes in RAM.
On a ESP8266 Sprite sizes up to 126 x 160 can be accomodated,
this size requires 40kBytes of RAM for a 16 bit colour depth.
When 8 bit colour depth sprites are created they occupy
(width * height) bytes in RAM, so larger sprites can be
created, or the RAM required is halved.
*/
// Set delay after plotting the sprite
#define DELAY 1000
// Width and height of sprite
#define WIDTH 128
#define HEIGHT 128
#include <M5Unified.h>
M5Canvas spr(&M5.Lcd);
void setup() {
Serial.begin(250000);
Serial.println();
// Initialise the TFT registers
M5.begin();
M5.Power.begin();
// Optionally set colour depth to 8 or 16 bits, default is 16 if not
// spedified spr.setColorDepth(8);
// Create a sprite of defined size
spr.createSprite(WIDTH, HEIGHT);
// Clear the TFT screen to blue
M5.Lcd.fillScreen(TFT_BLUE);
}
void loop(void) {
// Fill the whole sprite with black (Sprite is in memory so not visible yet)
spr.fillSprite(TFT_BLACK);
// Number of pixels to draw
uint16_t n = 100;
// Draw 100 random colour pixels at random positions in sprite
while (n--) {
uint16_t colour = random(0x10000); // Returns colour 0 - 0xFFFF
int16_t x = random(WIDTH); // Random x coordinate
int16_t y = random(HEIGHT); // Random y coordinate
spr.drawPixel(x, y, colour); // Draw pixel in sprite
}
// Draw some lines
spr.drawLine(1, 0, WIDTH, HEIGHT - 1, TFT_GREEN);
spr.drawLine(0, 0, WIDTH, HEIGHT, TFT_GREEN);
spr.drawLine(0, 1, WIDTH - 1, HEIGHT, TFT_GREEN);
spr.drawLine(0, HEIGHT - 1, WIDTH - 1, 0, TFT_RED);
spr.drawLine(0, HEIGHT, WIDTH, 0, TFT_RED);
spr.drawLine(1, HEIGHT, WIDTH, 1, TFT_RED);
// Draw some text with Middle Centre datum
spr.setTextDatum(MC_DATUM);
spr.drawString("Sprite", WIDTH / 2, HEIGHT / 2, &fonts::Font4);
//spr.drawString("Sprite", WIDTH / 2, HEIGHT / 2, 4);
// Now push the sprite to the TFT at position 0,0 on screen
spr.pushSprite(-40, -40);
spr.pushSprite(M5.Lcd.width() / 2 - WIDTH / 2,
M5.Lcd.height() / 2 - HEIGHT / 2);
spr.pushSprite(M5.Lcd.width() - WIDTH + 40, M5.Lcd.height() - HEIGHT + 40);
delay(DELAY);
// Fill TFT screen with blue
M5.Lcd.fillScreen(TFT_BLUE);
// Draw a blue rectangle in sprite so when we move it 1 pixel it does not
// leave a trail on the blue screen background
spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLUE);
int x = M5.Lcd.width() / 2 - WIDTH / 2;
int y = M5.Lcd.height() / 2 - HEIGHT / 2;
uint32_t updateTime = 0; // time for next update
while (true) {
// Random movement direction
int dx = 1;
if (random(2)) dx = -1;
int dy = 1;
if (random(2)) dy = -1;
// Pull it back onto screen if it wanders off
if (x < -WIDTH / 2) dx = 1;
if (x >= M5.Lcd.width() - WIDTH / 2) dx = -1;
if (y < -HEIGHT / 2) dy = 1;
if (y >= M5.Lcd.height() - HEIGHT / 2) dy = -1;
// Draw it 50 time, moving in random direct or staying still
n = 50;
int wait = random(50);
while (n) {
if (updateTime <= millis()) {
// Use time delay so sprtie does not move fast when not all on
// screen
updateTime = millis() + wait;
// Push the sprite to the TFT screen
spr.pushSprite(x, y);
// Change coord for next loop
x += dx;
y += dy;
n--;
yield(); // Stop watchdog reset
}
}
} // Infinite while, will not exit!
}