繁体   English   中英

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

[英]How display a static map on TFT in arduino

我正在研究在 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: %s\n", 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 **传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM