-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapptft.ino
213 lines (154 loc) · 6.03 KB
/
apptft.ino
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "fsx.h"
#include "SPI.h"
#include <TFT_eSPI.h>
#include <TJpg_Decoder.h>
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite img = TFT_eSprite(&tft);
TaskHandle_t tftshowTask;
SemaphoreHandle_t tftSemaphore;
/*
* #define TFT_BACKLIGHT_ON HIGH
* #define TFT_MOSI 23
* #define TFT_SCLK 19 // was 18
* #define TFT_CS 5 // from 15 to avoid sd card conflict Chip select control pin
* #define TFT_DC 0 // from 2 to avoid sd card conflict Data Command control pin
* #define TFT_RST 22 // Reset pin (could connect to RST pin)
* #define TFT_BL 21
*/
//---------------------------------------------------------------------
void tft_message( const char *message1, const char *message2 ){
img.createSprite( tft.width(), 100);
img.setTextColor( TFT_WHITE, TFT_BLACK );
img.setTextSize(2);
img.fillSprite(TFT_BLACK);
img.drawString( message1, 0,10, 2);
img.drawString( message2, 0,60, 2);
xSemaphoreTake( updateSemaphore, portMAX_DELAY);
img.pushSprite( 0, tft.height()/2 - 50);
delay( 10000 );
xSemaphoreGive( updateSemaphore);
img.deleteSprite();
}
//---------------------------------------------------------------------
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
// Stop further decoding as image is running off bottom of screen
if ( y >= tft.height() ) return 0;
// This function will clip the image block rendering automatically at the TFT boundaries
tft.pushImage(x, y, w, h, bitmap);
// This might work instead if you adapt the sketch to use the Adafruit_GFX library
// tft.drawRGBBitmap(x, y, bitmap, w, h);
// Return 1 to decode next block
return 1;
}
//-----------------------------------------------------------------------
void fsjpgsize( fs::FS &fs, const char *filename, uint16_t *w, uint16_t *h){
fs::File jpgfile = fs.open( filename);
TJpgDec.getFsJpgSize(w,h, jpgfile);
// file should be closed by getFsJpgSize
}
//-----------------------------------------------------------------------
void fsdrawjpg( fs::FS &fs, const char *filename, uint32_t x_offset, uint32_t y_offset ){
fs::File jpgfile = fs.open( filename);
TJpgDec.drawFsJpg (x_offset, y_offset, jpgfile);
// file should be closed by drawFsJpg
}
//-----------------------------------------------------------------------
void showpicture( fs::FS &fs, const char *filename){
uint16_t w = 0, h = 0, scale;
uint32_t xo = 0, yo = 0;
//TJpgDec.getFsJpgSize(&w, &h, filename); // Note name preceded with "/"
fsjpgsize( fs, filename, &w, &h);
if ( w==0 && h ==0 ) return;
tft.setRotation( settings.portrait );
if( settings.portland )tft.setRotation( settings.landscape );
if ( settings.adapt_rotation ){
if ( w > h ){
if ( !settings.portland )tft.setRotation( settings.landscape);
}else{
if ( settings.portland )tft.setRotation( settings.portrait);
}
}
for (scale = 1; scale <= 8; scale <<= 1) {
// if ( w < ((tft.width() * scale* 2)/3) && scale > 1 )scale>>=1;
//if ( w < ((tft.width() * scale* 2)/3) && scale > 1 ){
if ( w < ((tft.width() * scale* 2)/3) && scale > 1 ){
scale>>=1;
break;
}
}
TJpgDec.setJpgScale(scale);
// if picture is smaller, then center
// if ( w < tft.width()*scale ) xo = (tft.width() - w/scale)/2;
//if ( h < tft.height()*scale ) yo = (tft.height() - h/scale)/2;
if ( w < tft.width()*scale ) xo = ( tft.width() - w/scale)/2;
if ( h < tft.height()*scale ) yo = ( tft.height() - h/scale)/2;
if ( xo || yo)tft.fillScreen(TFT_BLACK);
//Serial.printf("%s Width = %d,height %d xo = %d, yo = %d\n",filename, w/scale,h/scale, xo,yo);
// Draw the image, top left at xo, yo
//TJpgDec.drawFsJpg(xo, yo, filename);
xSemaphoreTake( updateSemaphore, portMAX_DELAY);
fsdrawjpg( fs, filename, xo, yo );
xSemaphoreGive( updateSemaphore);
delay( settings.pauseseconds * 1000 );
return;
}
//-----------------------------------------------------------------------------
void traversefs( fs::FS &fs, const char *dirname ){
fs::File dir = fs.open( dirname );
if(!dir.isDirectory()){
//Serial.println(" - not a directory");
return;
}
fs::File entry;
while ( (entry = dir.openNextFile()) ){
xSemaphoreTake( updateSemaphore, portMAX_DELAY);
xSemaphoreGive( updateSemaphore);
char *tmpname= strdup( entry.name() );
if ( entry.isDirectory() ) {
entry.close();
traversefs( fs, tmpname );
}else{
entry.close();
showpicture ( fs, tmpname );
}
free( tmpname );
}
dir.close();
}
//-------------------------------------------------------
void initTFT(){
init_config();
tft.begin();
tft.setTextColor(0xFFFF, 0x0000);
tft.fillScreen(TFT_BLACK);
tft.setSwapBytes(true); // We need to swap the colour bytes (endianess)
tft.setRotation( settings.portrait );
// The jpeg image can be scaled by a factor of 1, 2, 4, or 8
TJpgDec.setJpgScale(1);
// The decoder must be given the exact name of the rendering function above
TJpgDec.setCallback(tft_output);
showpicture ( *(fsdlist.back().f) , "/logo.jpg" );
}
//---------------------------------------------------------------------------------------
void runtftShow( void *param){
initTFT();
while(1){
refresh_fsd_usage();
for ( auto &efes : fsdlist ){
Serial.printf ("Now traversing fs %s\n", efes.fsname);
traversefs( *(efes.f), "/" );
}
}
}
//---------------------------------------------------------------------------------------
void startTFT(){
xTaskCreatePinnedToCore(
runtftShow, // Task to handle special functions.
"TFTshower", // name of task.
1024*4, // Stack size of task
NULL, // parameter of the task
2, // priority of the task
&tftshowTask, // Task handle to keep track of created task
1); //core to run it on
}