-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrolling_sprites.h
153 lines (132 loc) · 4.38 KB
/
scrolling_sprites.h
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
142
143
144
145
146
147
148
149
150
151
152
153
// This contains the class for scrolling sprites
#include "globalInclude.h"
#include <TFT_eSPI.h>
#include "TFT_eSPI.h"
#include "Free_Fonts.h"
// Set up Pins - Only need DC and CS for hardware SPI
#define TFT_DC 16
#define TFT_CS 17
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
SemaphoreHandle_t tftMutex;
// The rollingSprite class
class rollingSprite
{
private:
unsigned int c_sprHeight;
unsigned int c_sprWidth;
String c_spriteMsg;
int c_scrollGap;
int c_scrollSpeed;
int c_txtWidth;
int c_spriteACount;
int c_spriteBCount;
bool c_spriteValid = false;
uint16_t c_x;
uint16_t c_y;
uint32_t c_txtColor;
SemaphoreHandle_t spriteMutex;
TFT_eSprite spr = TFT_eSprite(&tft); // Sprite object
public:
bool newSprite(String spriteMsg, unsigned int sprHeight, unsigned int sprWidth, int scrollSpeed, int scrollGap, uint32_t color, uint16_t x, uint16_t y) {
if (spriteMutex == NULL) {
spriteMutex = xSemaphoreCreateMutex();
}
if (xSemaphoreTake(spriteMutex, (TickType_t) 30) == pdTRUE ) {
// set up the class vairables
c_spriteMsg = spriteMsg;
c_sprHeight = sprHeight;
c_sprWidth = sprWidth;
c_scrollSpeed = scrollSpeed;
c_scrollGap = scrollGap;
c_txtColor = color;
c_x = x;
c_y = y;
c_spriteValid = false;
spr.setColorDepth(16);
spr.setFreeFont(FSS9);
spr.createSprite(c_sprWidth, c_sprHeight);
spr.fillSprite(TFT_BLACK);
spr.setTextColor(c_txtColor);
spr.setTextWrap(false);
xSemaphoreGive(spriteMutex);
}
else {
Serial.println("newSprite: Unable to update sprite: " + c_spriteMsg);
}
if (xSemaphoreTake(spriteMutex, (TickType_t) 40) == pdTRUE ) {
c_txtWidth = spr.textWidth(c_spriteMsg, GFXFF);
c_spriteACount = 0;
c_spriteBCount = c_scrollGap + c_txtWidth;
c_spriteValid = true;
xSemaphoreGive(spriteMutex);
return true;
}
else {
Serial.println("newSprite: Unable to update sprite: " + c_spriteMsg);
}
return false;
}
bool delSprite() {
if (spriteMutex != NULL || c_spriteValid == true) {
if (xSemaphoreTake(spriteMutex, (TickType_t) 50) == pdTRUE ) {
c_spriteValid = false;
c_x = 0;
c_y = 0;
c_spriteACount = 0;
c_spriteBCount = 0;
spr.deleteSprite();
c_spriteMsg = " ";
xSemaphoreGive(spriteMutex);
}
else {
Serial.print("delSprite: Unable to delete sprite: " + c_spriteMsg);
return false;
}
}
return true;
}
bool drawSprite() {
if (c_spriteValid == true) {
if (xSemaphoreTake(spriteMutex, (TickType_t) 5) == pdTRUE ) {
c_spriteACount -= c_scrollSpeed;
c_spriteBCount -= c_scrollSpeed;
spr.fillSprite(TFT_BLACK);
spr.setTextColor(c_txtColor);
spr.drawString(c_spriteMsg, c_spriteACount, 0, GFXFF);
spr.drawString(c_spriteMsg, c_spriteBCount, 0, GFXFF);
xSemaphoreGive(spriteMutex);
}
else {
Serial.println("drawSprite: Unable to update sprite: " + c_spriteMsg);
}
if (xSemaphoreTake(spriteMutex, (TickType_t) 5) == pdTRUE ) {
if (c_spriteACount < (-1 * (c_txtWidth))) {
c_spriteACount += ((c_txtWidth + c_scrollGap) * 2);
}
if (c_spriteBCount < (-1 * (c_txtWidth))) {
c_spriteBCount += ((c_txtWidth + c_scrollGap) * 2);
}
xSemaphoreGive(spriteMutex);
}
else {
Serial.println("drawSprite: Unable to reset sprite counts: " + c_spriteMsg);
}
if (xSemaphoreTake(tftMutex, (TickType_t) 40 / portTICK_PERIOD_MS) == pdTRUE ) {
spr.pushSprite(c_x, c_y);
xSemaphoreGive(tftMutex);
}
else {
Serial.println("drawSprite: Unable to push sprite: " + c_spriteMsg);
return false;
}
}
return true;
}
bool isValid() {
return c_spriteValid;
}
String currMsg() {
return c_spriteMsg;
}
};