esp32 arduino example #5
Replies: 3 comments
-
This is nice! I might add it to Examples later. String(esp_get_free_heap_size()) <-- this is not Qentem::String right? You can use: StringStream<char> ss;
Template::Render(content, value, ss); less code and does not invoke move nor allocate a new heap if the current one is big enough. Thanks for sharing! |
Beta Was this translation helpful? Give feedback.
-
Note: I'm working on making a faster engine, but it needs time and I don't want to rush it, after that I'm planing on using string-view for Json, which will reduce the memory to almost nothing, after all of that, a fast memory allocator should make things faster. |
Beta Was this translation helpful? Give feedback.
-
I think I over engineered it: #define QENTEM_SIZE_T SizeT16
#define QENTEM_AUTO_ESCAPE_HTML 0
#include "JSON.hpp"
#include "Template.hpp"
using Qentem::Digit;
using Qentem::SizeT;
using Qentem::Template;
void setup() {
Serial.begin(115200);
Qentem::StringStream<char> stream;
const char *APP_p1 = "\n[APP] Free memory: ";
const char *APP_p2 = " bytes\n";
const SizeT APP_p1_length = Qentem::StringUtils::Count(APP_p1);
const SizeT APP_p2_length = Qentem::StringUtils::Count(APP_p2);
// Convert current used memory to a string.
// Opinion: I think this is much faster than String(esp_get_free_heap_size())
Digit::NumberToString(stream, esp_get_free_heap_size());
Serial.write(APP_p1, APP_p1_length);
Serial.write(stream.First(), stream.Length());
Serial.write(APP_p2, APP_p2_length);
// Reset will release the used memory, and Clear will only reset the index.
// Clear is better for faster preformence since it does not reallocate. but ...
// every bit counts here, Right?
stream.Reset();
const char *content = R"(<html>
<body>
<div>
<h2>Students' list:</h2>
<loop value="department_val">
<h3>Major: {var:department_val[major]}</h3>
<ul>
<loop set="department_val[students]" value="student_val">
<li>
<span>Name: {var:student_val[Name]}</span>
<span>
GPA: {var:student_val[GPA]}
<if case="{var:student_val[GPA]} < 2.5"> (Inform adviser!)
<elseif case="{var:student_val[GPA]} >= 3.5" /> (President's List!)
<elseif case="{var:student_val[GPA]} >= 3.0" /> (Dean's List!)
</if>
</span>
</li>
</loop>
</ul>
</loop>
</div>
</body>
</html>)";
const SizeT content_length = Qentem::StringUtils::Count(content);
Qentem::Value<char> value = Qentem::JSON::Parse(R"(
[
{
"major": "Computer Science",
"students": [
{ "Name": "Student1", "GPA": 3.2 },
{ "Name": "Student2", "GPA": 3.8 },
{ "Name": "Student3", "GPA": 2.8 }
]
},
{
"major": "Math",
"students": [
{ "Name": "Student4", "GPA": 3.0 },
{ "Name": "Student5", "GPA": 2.5 },
{ "Name": "Student6", "GPA": 2.4 }
]
}
]
)");
// This line will check if there is an excess used of memory, and release it by writing to a smaller heap.
value.Compress();
// Render 'content' with 'value' and store it in 'stream'
Template::Render(content, content_length, value, stream);
// Print the rendered template
Serial.write(stream.First(), stream.Length());
stream.Reset();
Serial.println("\n-----end------\n");
Digit::NumberToString(stream, esp_get_free_heap_size());
Serial.write(APP_p1, APP_p1_length);
Serial.write(stream.First(), stream.Length());
Serial.write(APP_p2, APP_p2_length);
stream.Reset();
Template::Render(content, content_length, value, stream);
Serial.write(stream.First(), stream.Length());
stream.Reset();
Serial.println("\n-----end------\n");
Digit::NumberToString(stream, esp_get_free_heap_size());
Serial.write(APP_p1, APP_p1_length);
Serial.write(stream.First(), stream.Length());
Serial.write(APP_p2, APP_p2_length);
// stream.Reset(); // It will auto destruct after this point, unless there is more code after.
}
void loop() {
} |
Beta Was this translation helpful? Give feedback.
-
Great work!! someone might want to test this on esp8266 or esp32 which makes this the best template engine for this devices, low memory usage with fast engine perfect for embedded webserver template engine. Here are my sample esp32 code for those who might want to test this. Cheers
Beta Was this translation helpful? Give feedback.
All reactions