【问题标题】:How display a static map on TFT in arduino如何在 arduino 中的 TFT 上显示静态地图
【发布时间】:2020-11-03 14:02:57
【问题描述】:

我正在开发一个在 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 部分从地图链接中删除时,程序可以正常运行。如何解决问题。

【问题讨论】:

    标签: c++ c arduino


    【解决方案1】:

    图像不适合您的缓冲区。它是 78886 字节强。你只分配了 40000 字节。

    推迟分配直到知道大小:

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

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

    【讨论】:

    • 感谢@user58697,我增加了缓冲空间,问题解决了。
    猜你喜欢
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多