Shows available RAM
This commit is contained in:
parent
0395fed907
commit
ab37d32b6d
4 changed files with 176 additions and 0 deletions
93
exercises/15_RAM/src/main.cpp
Normal file
93
exercises/15_RAM/src/main.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
// 20260403 ChatGPT
|
||||
// Exercise 15_RAM
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <U8g2lib.h>
|
||||
|
||||
#ifndef NODE_LABEL
|
||||
#define NODE_LABEL "RAM"
|
||||
#endif
|
||||
|
||||
#ifndef OLED_SDA
|
||||
#define OLED_SDA 17
|
||||
#endif
|
||||
#ifndef OLED_SCL
|
||||
#define OLED_SCL 18
|
||||
#endif
|
||||
#ifndef OLED_ADDR
|
||||
#define OLED_ADDR 0x3C
|
||||
#endif
|
||||
|
||||
static U8G2_SH1106_128X64_NONAME_F_HW_I2C g_oled(U8G2_R0, U8X8_PIN_NONE);
|
||||
|
||||
static void oledShowLines(const char *l1,
|
||||
const char *l2 = nullptr,
|
||||
const char *l3 = nullptr,
|
||||
const char *l4 = nullptr,
|
||||
const char *l5 = nullptr)
|
||||
{
|
||||
g_oled.clearBuffer();
|
||||
g_oled.setFont(u8g2_font_5x8_tf);
|
||||
if (l1) g_oled.drawUTF8(0, 12, l1);
|
||||
if (l2) g_oled.drawUTF8(0, 24, l2);
|
||||
if (l3) g_oled.drawUTF8(0, 36, l3);
|
||||
if (l4) g_oled.drawUTF8(0, 48, l4);
|
||||
if (l5) g_oled.drawUTF8(0, 60, l5);
|
||||
g_oled.sendBuffer();
|
||||
}
|
||||
|
||||
static size_t getAvailableRamBytes()
|
||||
{
|
||||
return ESP.getFreeHeap();
|
||||
}
|
||||
|
||||
static void printRamStatus()
|
||||
{
|
||||
const size_t freeBytes = getAvailableRamBytes();
|
||||
const size_t totalBytes = ESP.getHeapSize();
|
||||
const size_t maxAlloc = ESP.getMaxAllocHeap();
|
||||
|
||||
Serial.printf("RAM total=%u free=%u maxAlloc=%u\r\n", (unsigned)totalBytes, (unsigned)freeBytes, (unsigned)maxAlloc);
|
||||
|
||||
char line1[32];
|
||||
char line2[32];
|
||||
char line3[32];
|
||||
|
||||
snprintf(line1, sizeof(line1), "Exercise 15 RAM");
|
||||
snprintf(line2, sizeof(line2), "Node: %s", NODE_LABEL);
|
||||
snprintf(line3, sizeof(line3), "Free: %u KB", (unsigned)(freeBytes / 1024U));
|
||||
char line4[32];
|
||||
snprintf(line4, sizeof(line4), "Total: %u KB", (unsigned)(totalBytes / 1024U));
|
||||
char line5[32];
|
||||
snprintf(line5, sizeof(line5), "Max alloc: %u KB", (unsigned)(maxAlloc / 1024U));
|
||||
|
||||
oledShowLines(line1, line2, line3, line4, line5);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(800);
|
||||
Serial.println("Exercise 15_RAM boot");
|
||||
|
||||
Wire.begin(OLED_SDA, OLED_SCL);
|
||||
g_oled.setI2CAddress(OLED_ADDR << 1);
|
||||
g_oled.begin();
|
||||
oledShowLines("Exercise 15_RAM", "Node: " NODE_LABEL, "Booting...");
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static uint32_t lastMs = 0;
|
||||
const uint32_t now = millis();
|
||||
if (now - lastMs < 1000) {
|
||||
delay(10);
|
||||
return;
|
||||
}
|
||||
lastMs = now;
|
||||
|
||||
printRamStatus();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue