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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include <Arduino.h> #include <AsyncTCP.h> #include <TFT_eSPI.h> #include <TJpg_Decoder.h> #include <WiFi.h> #include <Wire.h>
#define ssid "XXXX" #define password "XXXX"
TFT_eSPI tft;
TFT_eSprite imageSprite = TFT_eSprite(&tft);
TFT_eSprite headerSprite = TFT_eSprite(&tft);
WiFiClient client;
uint16_t imageSize;
uint16_t readBytesSize = -1;
uint8_t *imageBuffer = (uint8_t *)heap_caps_malloc(16 * 1024, MALLOC_CAP_8BIT);
int16_t headerSpriteOffset = 0; String header; enum ParseState { PARSE_REQ_START, PARSE_REQ_HEADERS, PARSE_REQ_BODY, PARSE_REQ_END, PARSE_REQ_FAIL, PARSE_REQ_PENDING };
ParseState _parseState;
uint16_t extractContentLengthFromResponseRaw(String &responseBody, uint16_t &bodyBeginIndex) { int index = responseBody.indexOf("Content-Length:"); int eIndex = responseBody.indexOf('\n', index); String value = responseBody.substring(index + 16, eIndex); bodyBeginIndex = responseBody.indexOf("\r\n\r\n") + 4; return atoi(value.c_str()); }
size_t copyBytes(uint8_t *source, uint8_t *buffer, size_t length, size_t SrcOffset, size_t bufferOffset) { size_t count = 0; while (count + SrcOffset < length) { *(buffer + count + bufferOffset) = source[count + SrcOffset]; count++; } return count; }
uint16_t extractBodyChunkFromResponseRaw(uint8_t *frameData, uint8_t *bodyData, uint16_t length, uint16_t srcOffset, size_t bufferOffset) { copyBytes(frameData, bodyData, length, srcOffset, bufferOffset); return length - srcOffset; }
void asyncReqeust() { static AsyncClient *aClient; if (aClient) { return; } aClient = new AsyncClient(); if (!aClient) { return; } aClient->onError( [&](void *arg, AsyncClient *client, int error) { Serial.println("getImageAsync:Connect Error"); aClient = NULL; delete client; }, NULL); aClient->onConnect( [&](void *arg, AsyncClient *client) { Serial.println("getImageAsync:Connected"); aClient->onError(NULL, NULL); client->onDisconnect( [&](void *arg, AsyncClient *c) { aClient = NULL; delete c; Serial.printf("getImageAsync:Disconnected\n"); _parseState = PARSE_REQ_START; }, NULL); client->onData( [&](void *arg, AsyncClient *c, void *data, size_t len) { uint8_t *d = (uint8_t *)data; if (_parseState == PARSE_REQ_START) { String content = String((char *)data); _parseState = PARSE_REQ_HEADERS; uint16_t bodyBeginIndex = 0; imageSize = extractContentLengthFromResponseRaw(content, bodyBeginIndex); Serial.printf("Content-Length:%d\n", imageSize); header = content.substring(0, bodyBeginIndex); if (imageSize > 0) { _parseState = PARSE_REQ_BODY; readBytesSize = extractBodyChunkFromResponseRaw( d, imageBuffer, len, bodyBeginIndex, 0); }
} else if (_parseState == PARSE_REQ_BODY) { readBytesSize += extractBodyChunkFromResponseRaw( d, imageBuffer, len, 0, readBytesSize); Serial.printf("extract size:%d\n", readBytesSize); } if (readBytesSize == imageSize) { Serial.printf("Body Received Done.received %d bytes\n", readBytesSize); _parseState = PARSE_REQ_END; } }, NULL); client->write( "GET /response.jpg HTTP/1.1\r\n" "Host: chaosgoo-pic.oss-cn-shanghai.aliyuncs.com\r\n" "Accept: */*\r\n" "Accept-Encoding: gzip, deflate, br" "Connection: keep-alive" "User-Agent: PostmanRuntime/7.26.10\r\n\r\n"); }, NULL); if (!aClient->connect("chaosgoo-pic.oss-cn-shanghai.aliyuncs.com", 80)) { Serial.println("Connect Fail"); AsyncClient *client = aClient; aClient = NULL; delete client; } }
bool drawCallback(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) { if (imageSize == readBytesSize) { if (y >= 135) { return true; } imageSprite.pushImage(x, y, w, h, bitmap); } return true; }
void pushToTFT(int offset) { headerSprite.pushSprite(0, offset); imageSprite.pushSprite(0, offset + 135); }
void setup() { Serial.begin(115200); client.setTimeout(1); WiFi.begin(ssid, password); WiFi.setAutoReconnect(true); tft.init(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); imageSprite.createSprite(240, 32); headerSprite.createSprite(240, 135); TJpgDec.setJpgScale(1); TJpgDec.setSwapBytes(true); TJpgDec.setCallback(drawCallback); delay(3000); asyncReqeust(); }
void loop() { if (_parseState == PARSE_REQ_END) { TJpgDec.drawJpg(0, 0, imageBuffer, imageSize); _parseState = PARSE_REQ_PENDING; headerSprite.setTextFont(1); headerSprite.setTextColor(TFT_WHITE); for (int i = 0; i < header.length(); i++) { headerSprite.print(header[i]); Serial.print(header[i]); headerSprite.pushSprite(0, 0); } } pushToTFT(headerSpriteOffset); delay(100); if (headerSpriteOffset > -32) { headerSpriteOffset -= headerSprite.fontHeight(1); } }
|