如何在arduino的TFT上显示静态地图



我正在开发一个在TFT显示器上显示静态地图的功能(我使用ESP32.(。地图链接如下(http链接(:

http://osm-static-maps.herokuapp.com/?geojson=[{%22type%22:%22Point%22,%22coordinates%22:[32.37956,51.66127]}]&height=400&width=600&zoom=13&type=jpeg

我的代码如下:

#define WIFI_SSID "<your SSID>"
#define PASSWORD "<your password>"

#define LINK "http://osm-static-maps.herokuapp.com/?geojson=[{%22type%22:%22Point%22,%22coordinates%22:[32.37956,51.66127]}]&height=400&width=600&zoom=13&type=jpeg"
#include <TJpg_Decoder.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

#define BUFSIZE 40000 
uint8_t* jpgbuffer;
unsigned long jp = 0;

bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap) {
if ( y >= tft.height() ) return 0;
tft.pushImage(x, y, w, h, bitmap);
return 1;
}

void setup() {
jpgbuffer = (uint8_t *) malloc (BUFSIZE);
Serial.begin(115200);

tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);

TJpgDec.setJpgScale(1);
TJpgDec.setSwapBytes(true);
TJpgDec.setCallback(tft_output);

WiFi.begin(WIFI_SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
uint32_t t = millis();
bool loaded_ok = downFile(LINK, jpgbuffer); 
t = millis() - t;
if (loaded_ok) {
Serial.print("Downloaded in "); Serial.print(t); Serial.println(" ms."); 
}

t = millis();
TJpgDec.drawJpg(0, 0, jpgbuffer, jp);
t = millis() - t;
Serial.print("Decoding and rendering: "); Serial.print(t); Serial.println(" ms."); 
}

void loop() {

}

bool downFile(String url, uint8_t* jpgbuffer) {
Serial.println("Downloading file from " + url);
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
int total = http.getSize();
int len = total;
uint8_t buff[128] = { 0 };
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
memcpy (jpgbuffer + jp, buff, c);
jp = jp + c;
if (len > 0) {
len -= c;
}
}
yield();
}
Serial.println();
Serial.print("[HTTP] connection closed or file end.n");
}
}
else {
Serial.printf("[HTTP] GET... failed, error: %sn", http.errorToString(httpCode).c_str());
}
http.end();
}
return 1; 
}

当我使用下面的链接时,一切都可以,例如:

#define LINK "http://dellascolto.com/lorenz.jpg"

但是当我使用下面的链接时,上传代码后Arduino会挂起!

#define LINK "http://osm-static-maps.herokuapp.com/?geojson=[{%22type%22:%22Point%22,%22coordinates%22:[32.37956,51.66127]}]&height=400&width=600&zoom=13&type=jpeg"

显然,当JSON部分从映射链接中删除时,程序可以正确运行。如何解决问题。

图像不适合您的缓冲区。它的大小为78886字节。您只分配了40000个字节。

推迟分配,直到您知道大小:

total = http.getSize();
*jpgbuffer = malloc(total);

请注意,为了做到这一点,您需要将jpgbuffer传递为uint8_t **

最新更新