Skip to content

Commit

Permalink
Merge pull request #2 from khoih-prog/main
Browse files Browse the repository at this point in the history
Modified Cstr routine
  • Loading branch information
salasidis authored Oct 4, 2022
2 parents ca11d5d + 19f2546 commit 8fe690d
Show file tree
Hide file tree
Showing 52 changed files with 723 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Arduino IDE version: 1.8.19
`ArduinoCore-mbed` mbed_portenta core v3.3.0
Portenta_H7 Rev2 ABX00042
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using this library
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Arduino IDE version: 1.8.19
`ArduinoCore-mbed` mbed_portenta core v3.3.0
Portenta_H7 Rev2 ABX00042
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using this library
Expand All @@ -52,3 +52,4 @@ There are usually some outstanding feature requests in the [existing issues list
### Sending Pull Requests

Pull Requests with changes and fixes are also welcome!

211 changes: 187 additions & 24 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.4.0](#Releases-v140)
* [Releases v1.3.0](#Releases-v130)
* [Releases v1.2.1](#Releases-v121)
* [Releases v1.2.0](#Releases-v120)
Expand All @@ -23,6 +24,11 @@

## Changelog

### Releases v1.4.0

1. Support using `CString` in optional `SDRAM` to save heap to send `very large data`. Check [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8)
2. Add multiple examples to demo the new feature

### Releases v1.3.0

1. Fix issue with slow browsers or network. Check [Target stops responding after variable time when using Firefox on Windows 10 #3](https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#endif

#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
#define _PORTENTA_H7_AWS_LOGLEVEL_ 4

#define USE_ETHERNET_PORTENTA_H7 true

Expand Down Expand Up @@ -177,7 +177,7 @@ void setup()
digitalWrite(LED_BUILTIN, LED_OFF);

Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

delay(200);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************************************************************
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet
Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino - - Dead simple AsyncWebServer for Portenta_H7
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
Expand Down Expand Up @@ -53,6 +53,9 @@

#include <Portenta_H7_AsyncWebServer.h>

// In bytes
#define STRING_SIZE 40000

// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20

Expand Down Expand Up @@ -146,26 +149,49 @@ void handleNotFound(AsyncWebServerRequest *request)
digitalWrite(LED_BUILTIN, LED_OFF);
}

void PrintHeapData(String hIn){
mbed_stats_heap_t heap_stats;

Serial.print("HEAP DATA - ");
Serial.print(hIn);
void PrintHeapData(String hIn)
{
static mbed_stats_heap_t heap_stats;
static uint32_t maxHeapSize = 0;

mbed_stats_heap_get(&heap_stats);
Serial.print(" Cur heap: ");
Serial.print(heap_stats.current_size);
Serial.print(" Res Size: ");
Serial.print(heap_stats.reserved_size);
Serial.print(" Max heap: ");
Serial.println(heap_stats.max_size);

// Print and update only when different
if (maxHeapSize != heap_stats.max_size)
{
maxHeapSize = heap_stats.max_size;

Serial.print("\nHEAP DATA - ");
Serial.print(hIn);

Serial.print(" Cur heap: ");
Serial.print(heap_stats.current_size);
Serial.print(" Res Size: ");
Serial.print(heap_stats.reserved_size);
Serial.print(" Max heap: ");
Serial.println(heap_stats.max_size);
}
}

void PrintStringSize(String & out)
{
static uint32_t count = 0;

// Print only when cStr length too large and corrupting memory or every (20 * 5) s
if ( (out.length() >= STRING_SIZE) || (++count > 20) )
{
Serial.print("\nOut String Length=");
Serial.println(out.length());

count = 0;
}
}

void drawGraph(AsyncWebServerRequest *request)
{
String out;

out.reserve(40000);
out.reserve(STRING_SIZE);
char temp[70];

out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1810\" height=\"150\">\n";
Expand All @@ -180,19 +206,18 @@ void drawGraph(AsyncWebServerRequest *request)
out += temp;
y = y2;
}

out += "</g>\n</svg>\n";

PrintHeapData("Pre Send");

Serial.print("Out String Length=");
Serial.println(out.length());
PrintStringSize(out);

request->send(200, "image/svg+xml", out);

PrintHeapData("Post Send");
}


void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Expand Down Expand Up @@ -264,7 +289,6 @@ void setup()
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(Ethernet.localIP());


PrintHeapData("Pre Create Arduino String");

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************************************************************
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for Portenta_H7
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet
Expand Down Expand Up @@ -43,7 +43,7 @@
#endif

#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
#define _PORTENTA_H7_AWS_LOGLEVEL_ 0

#define USE_ETHERNET_PORTENTA_H7 true

Expand All @@ -53,7 +53,17 @@

#include <Portenta_H7_AsyncWebServer.h>

#include "SDRAM.h"
char *cStr;

// In bytes
#define CSTRING_SIZE 40000

// Select either cString is stored in SDRAM or not
#define USING_CSTRING_IN_SDRAM true

#if USING_CSTRING_IN_SDRAM
#include "SDRAM.h"
#endif

// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20
Expand Down Expand Up @@ -91,7 +101,6 @@ int reqCount = 0; // number of requests received
#define LED_OFF HIGH
#define LED_ON LOW


#define BUFFER_SIZE 768 // a little larger in case required for header shift (destructive send)
char temp[BUFFER_SIZE];

Expand Down Expand Up @@ -148,24 +157,35 @@ void handleNotFound(AsyncWebServerRequest *request)
digitalWrite(LED_BUILTIN, LED_OFF);
}

void PrintHeapData(String hIn){
mbed_stats_heap_t heap_stats;

Serial.print("HEAP DATA - ");
Serial.print(hIn);
void PrintHeapData(String hIn)
{
static mbed_stats_heap_t heap_stats;
static uint32_t maxHeapSize = 0;

mbed_stats_heap_get(&heap_stats);
Serial.print(" Cur heap: ");
Serial.print(heap_stats.current_size);
Serial.print(" Res Size: ");
Serial.print(heap_stats.reserved_size);
Serial.print(" Max heap: ");
Serial.println(heap_stats.max_size);
}


char *cStr;
// Print and update only when different
if (maxHeapSize != heap_stats.max_size)
{
maxHeapSize = heap_stats.max_size;

Serial.print("\nHEAP DATA - ");
Serial.print(hIn);

Serial.print(" Cur heap: ");
Serial.print(heap_stats.current_size);
Serial.print(" Res Size: ");
Serial.print(heap_stats.reserved_size);
Serial.print(" Max heap: ");
Serial.println(heap_stats.max_size);
}
}

void PrintStringSize(const char* cStr)
{
Serial.print("\nOut String Length=");
Serial.println(strlen(cStr));
}

void drawGraph(AsyncWebServerRequest *request)
{
Expand All @@ -190,15 +210,17 @@ void drawGraph(AsyncWebServerRequest *request)

PrintHeapData("Pre Send");

Serial.print("Out String Length=");
Serial.println(strlen(cStr));
// Print only when cStr length too large and corrupting memory
if ( (strlen(cStr) >= CSTRING_SIZE))
{
PrintStringSize(cStr);
}

request->send(200, "image/svg+xml", cStr, false);

PrintHeapData("Post Send");
}


void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Expand All @@ -209,17 +231,32 @@ void setup()

delay(200);

Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString on "); Serial.print(BOARD_NAME);
#if USING_CSTRING_IN_SDRAM
Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString using SDRAM on ");
#else
Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString on ");
#endif

Serial.print(BOARD_NAME);
Serial.print(" with "); Serial.println(SHIELD_TYPE);
Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);

Serial.print("TCP_MSS = "); Serial.print(TCP_MSS); Serial.print(", TCP_SND_BUF = "); Serial.println(TCP_SND_BUF);


#if USING_CSTRING_IN_SDRAM
SDRAM.begin();

cStr = (char *)SDRAM.malloc(100000); // make a little larger than required
cStr = (char *) SDRAM.malloc(CSTRING_SIZE); // make a little larger than required
#else
cStr = (char *) malloc(CSTRING_SIZE); // make a little larger than required
#endif

if (cStr == NULL) {
if (cStr == NULL)
{
Serial.println("Unable top Allocate RAM");

for(;;);
}

Expand Down Expand Up @@ -279,7 +316,6 @@ void setup()
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(Ethernet.localIP());


PrintHeapData("Pre Create Arduino String");

}
Expand All @@ -292,7 +328,8 @@ void heartBeatPrint()

if (num == 80)
{
Serial.println();
//Serial.println();
PrintStringSize(cStr);
num = 1;
}
else if (num++ % 10 == 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/Ethernet/Async_HelloServer/Async_HelloServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void setup()
digitalWrite(LED_BUILTIN, LED_OFF);

Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

delay(200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void setup()
digitalWrite(LED_BUILTIN, LED_OFF);

Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

delay(200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const char* www_password = "portenta";
void setup()
{
Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

delay(200);

Expand Down
2 changes: 1 addition & 1 deletion examples/Ethernet/Async_PostServer/Async_PostServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void handleNotFound(AsyncWebServerRequest *request)
void setup()
{
Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

delay(200);

Expand Down
2 changes: 1 addition & 1 deletion examples/Ethernet/MQTTClient_Auth/MQTTClient_Auth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

Serial.print("\nStart MQTTClient_Auth on "); Serial.print(BOARD_NAME);
Serial.print(" with "); Serial.println(SHIELD_TYPE);
Expand Down
2 changes: 1 addition & 1 deletion examples/Ethernet/MQTTClient_Basic/MQTTClient_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial);
while (!Serial && millis() < 5000);

Serial.print("\nStart MQTTClient_Basic on "); Serial.print(BOARD_NAME);
Serial.print(" with "); Serial.println(SHIELD_TYPE);
Expand Down
Loading

0 comments on commit 8fe690d

Please sign in to comment.