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
| #include "StreamingComponent.h"
StreamingComponent::StreamingComponent(WiFiClient &clt, TFT_eSPI &tft) { this->client = &clt; this->Tft = &tft; Serial.println("StreamingComponent Constuctor"); };
void StreamingComponent::enter() { status = RUNNING; };
void StreamingComponent::exit() { status = EXITING; };
void StreamingComponent::loop() { if (status == RUNNING) { Serial.println("StreamingComponent loop"); loopCost = millis(); onReceiveData(); Serial.printf("fps_avg:%f,loop cost:%d ms\n", fps_avg, millis() - loopCost); Tft->drawString(String(fps_avg), 0, 0, 2); } else if (status == EXITING) { } };
bool StreamingComponent::drawCallBack(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) { if (status == RUNNING) { if (y >= SCREEN_HEIGHT) return 0; if (dmaBufferSel) { dmaBufferPtr = dmaBuffer2; } else { dmaBufferPtr = dmaBuffer1; } dmaBufferSel = !dmaBufferSel; Tft->pushImageDMA(x, y, w, h, bitmap, dmaBufferPtr); } return true; }
void StreamingComponent::onReceiveData() { Serial.println("StreamingComponent onReceiveData"); StreamingComponent::client->write(PREPAREOK); Serial.println("StreamingComponent client.write(PREPAREOK);"); cost = millis(); if (headerBuffer == nullptr) { Serial.printf("headerBuffer is null.\n"); } else { client->readBytes(headerBuffer, headerFrameSize); Serial.printf("receive header cost:%d ms\n", millis() - cost); }
int sum = checkSum((const char *)headerBuffer, 8); if ((sum & 0xf) == c2i(headerBuffer[9]) && (sum >> 4) == c2i(headerBuffer[8])) { strncpy((char *)frameSizeBuffer, (char *)headerBuffer, 8); frameSizeBuffer[9] = '\0'; size = atoi((char *)frameSizeBuffer); } else { return; }
client->write(HEADEROK); cost = millis(); bSize = 0; if (wifiBuffer == NULL) { Serial.printf("wifiBuffer is null.\n"); Serial.printf("MALLOC_CAP_8BIT heap_caps_get_largest_free_block: %d.\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)); Serial.printf("MALLOC_CAP_32BIT heap_caps_get_largest_free_block: %d.\n", heap_caps_get_largest_free_block(MALLOC_CAP_32BIT)); Serial.printf("MALLOC_CAP_SPIRAM heap_caps_get_largest_free_block: %d.\n", heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)); Serial.printf("MALLOC_CAP_8BIT: %d.\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); Serial.printf("MALLOC_CAP_32BIT: %d.\n", heap_caps_get_free_size(MALLOC_CAP_32BIT)); Serial.printf("MALLOC_CAP_SPIRAM: %d.\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); } else { bSize = client->readBytes(wifiBuffer, size); Serial.printf("frame size: %d bytes, receive frame cost:%d ms\n", bSize, millis() - cost); }
if (bSize > 64 && bSize == size) { cost = millis(); Tft->startWrite(); TJpgDec.drawJpg(0, 0, wifiBuffer, bSize); Tft->endWrite(); frame_count++; sec = millis() / 1000; if (psec != sec) { psec = sec; fps = frame_count; fps_avg = (fps_avg + fps) / 2.0; frame_count = 0; }
Serial.printf("draw cost:%d ms\n", millis() - cost); } else { } client->write(FRAMEOK); }
|