Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

application Notes #115

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.vscode
CMakeFiles
CMakeCache.txt
extern/SDL2-2.28.5
extern/SDL2-2.28.5
storage/apps/notes/data/notes.dat
2 changes: 1 addition & 1 deletion lib/graphics/src/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ namespace graphics
void Surface::fillRoundRectWithBorder(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, int16_t bs, uint16_t Backcolor, uint16_t Bordercolor)
{
m_sprite.fillSmoothRoundRect(x, y, w, h, r, Bordercolor);
m_sprite.fillSmoothRoundRect(x + bs, y + bs, w - 2 * bs, h - 2 * bs, r - bs, Backcolor);
m_sprite.fillSmoothRoundRect(x + bs, y + bs, w - 2 * bs, h - 2 * bs, r, Backcolor);
}

void Surface::drawRoundRect(const int16_t x, const int16_t y, const uint16_t w, const uint16_t h, const uint16_t r, const color_t color)
Expand Down
16 changes: 15 additions & 1 deletion lib/gsm/src/gsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace GSM
std::vector<Message> messages;
State state;
uint16_t seconds, minutes, hours, days, months, years = 0;
std::vector<float> battery_voltage_history;
float voltage = -1;
int networkQuality = 0;
bool flightMode = false;
Expand Down Expand Up @@ -994,6 +995,19 @@ namespace GSM
try
{
voltage = std::stof(voltage_str);

battery_voltage_history.push_back(voltage);
if (battery_voltage_history.size() > 24)
battery_voltage_history.erase(battery_voltage_history.begin());

if (battery_voltage_history.size() > 0) {
double sum = 0;
for (auto v : battery_voltage_history)
sum += v;
voltage = sum / battery_voltage_history.size();

std::cout << "Battery voltage average: " << voltage << std::endl;
}
}
catch (std::exception)
{
Expand All @@ -1010,7 +1024,7 @@ namespace GSM
// Thanks NumWorks for the regression app
const double batteryLevel = 3.083368 * std::pow(voltage, 3) - 37.21203 * std::pow(voltage, 2) + 150.5735 * voltage - 203.3347;

std::cout << "Battery level: " << batteryLevel << std::endl;
//std::cout << "Battery level: " << batteryLevel << std::endl;

return std::clamp(batteryLevel, 0.0, 1.0);

Expand Down
24 changes: 24 additions & 0 deletions lib/gui/src/ElementBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ void gui::ElementBase::renderAll(bool onScreen)
m_surface = nullptr;

if (m_surface == nullptr)
{
freeRamFor(m_width * m_height, this->getMaster());
m_surface = std::make_shared<graphics::Surface>(m_width, m_height);
}

// Render the element
render();
Expand Down Expand Up @@ -647,3 +650,24 @@ gui::ElementBase *gui::ElementBase::getElementAt(int index) {
return nullptr;

}

#include "elements/Window.hpp"

void gui::ElementBase::freeRamFor(uint32_t size, ElementBase* window)
{
#ifdef ESP_PLATFORM
size_t free = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);

if (free < size + 1000000)
{
std::cout << "Not enough RAM, free : " << free << " need : " << size << "\n -> will free other windows" << std::endl;
for (auto i : gui::elements::Window::windows)
{
if(i != window)
{
i->free();
}
}
}
#endif
}
2 changes: 2 additions & 0 deletions lib/gui/src/ElementBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace gui
void forceUpdate();

protected:
void freeRamFor(uint32_t size, ElementBase* window);

// variables générales
uint16_t m_width, m_height;

Expand Down
114 changes: 70 additions & 44 deletions lib/gui/src/elements/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include <Surface.hpp>
#include <iostream>

namespace gui::elements {
namespace gui::elements
{
Label::Label(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height)
: ElementBase(),
m_text(""),
m_fontSize(18),
m_textColor(COLOR_DARK),
m_textVerticalAlignment(UP),
m_textHorizontalAlignment(LEFT)
m_text(""),
m_fontSize(18),
m_textColor(COLOR_DARK),
m_textVerticalAlignment(UP),
m_textHorizontalAlignment(LEFT)
{
m_x = x;
m_y = y;
Expand All @@ -25,6 +26,7 @@ namespace gui::elements {

m_hasCursor = false;
m_cursorIndex = 0;
m_strikeOut = false;
}

Label::~Label() = default;
Expand All @@ -34,7 +36,7 @@ namespace gui::elements {
m_surface->clear(COLOR_WHITE);
m_surface->fillRoundRectWithBorder(0, 0, m_width, m_height, m_borderRadius, m_borderSize, m_backgroundColor, m_borderColor);

m_surface->setTextColor((this->m_textColor == 0)?(1):(this->m_textColor));
m_surface->setTextColor((this->m_textColor == 0) ? (1) : (this->m_textColor));
m_surface->setColor(this->m_backgroundColor);
m_surface->setFontSize(this->m_fontSize);

Expand All @@ -45,40 +47,40 @@ namespace gui::elements {
int x;
switch (int(m_textHorizontalAlignment))
{
case Alignement::LEFT:
x = getRadius()/2 + getBorderSize();
case Alignement::LEFT:
x = getRadius() / 2 + getBorderSize();
break;
case Alignement::CENTER:
x = getRadius()/2 + getBorderSize() + getUsableWidth()/2 - m_surface->getTextWidth(lines[i])/2;
case Alignement::CENTER:
x = getRadius() / 2 + getBorderSize() + getUsableWidth() / 2 - m_surface->getTextWidth(lines[i]) / 2;
break;
case Alignement::RIGHT:
x = getRadius()/2 + getBorderSize() + getUsableWidth() - m_surface->getTextWidth(lines[i]);
case Alignement::RIGHT:
x = getRadius() / 2 + getBorderSize() + getUsableWidth() - m_surface->getTextWidth(lines[i]);
break;
};

int y;
switch (int(m_textVerticalAlignment))
{
case Alignement::UP:
y = getRadius()/2 + getBorderSize() + (m_surface->getTextHeight() + LINE_SPACING) * i;
case Alignement::UP:
y = getRadius() / 2 + getBorderSize() + (m_surface->getTextHeight() + LINE_SPACING) * i;
break;
case Alignement::CENTER:
y = getRadius()/2 + getBorderSize() + getUsableHeight()/2
- ((m_surface->getTextHeight() + LINE_SPACING) * lines.size()) / 2
+ (m_surface->getTextHeight() + LINE_SPACING) * i;
case Alignement::CENTER:
y = getRadius() / 2 + getBorderSize() + getUsableHeight() / 2 - ((m_surface->getTextHeight() + LINE_SPACING) * lines.size()) / 2 + (m_surface->getTextHeight() + LINE_SPACING) * i;
break;
case Alignement::DOWN:
y = getRadius()/2 + getBorderSize() + getUsableHeight()
- ((m_surface->getTextHeight() + LINE_SPACING) * lines.size())
+ (m_surface->getTextHeight() + LINE_SPACING) * i;
case Alignement::DOWN:
y = getRadius() / 2 + getBorderSize() + getUsableHeight() - ((m_surface->getTextHeight() + LINE_SPACING) * lines.size()) + (m_surface->getTextHeight() + LINE_SPACING) * i;
break;
};

m_surface->drawText(lines[i], x, y);
if (m_strikeOut)
{
m_surface->drawLine(x, y + m_surface->getTextHeight() / 2, x + m_surface->getTextWidth(lines[i]), y + m_surface->getTextHeight() / 2, m_textColor);
}
}
}

void Label::setText(const std::string& text)
void Label::setText(const std::string &text)
{
this->m_text = text;
localGraphicalUpdate();
Expand Down Expand Up @@ -106,9 +108,10 @@ namespace gui::elements {
uint16_t charIndex = 0; // Global text index

uint16_t lineCharIndex = 0; // X position
uint16_t lineIndex = 0; // Y position
uint16_t lineIndex = 0; // Y position

for (char c : m_text) {
for (char c : m_text)
{
// Save cursor pos
if (m_hasCursor)
{
Expand All @@ -124,41 +127,56 @@ namespace gui::elements {
}
}

if (c == '\n') {
if (c == '\n')
{
output.m_lines.push_back(currentLine);
currentLine = "";

lineIndex++;
lineCharIndex = 0;
} else if (m_surface->getTextWidth(currentLine + c) <= getUsableWidth()) {
}
else if (m_surface->getTextWidth(currentLine + c) <= getUsableWidth())
{
currentLine += c;

lineCharIndex++;
} else if (c == ' ') {
}
else if (c == ' ')
{
output.m_lines.push_back(currentLine);
currentLine = "";

lineIndex++;
lineCharIndex = 0;
} else {
if (currentLine.empty()) {
}
else
{
if (currentLine.empty())
{
currentLine += c;

lineCharIndex++;
} else if (currentLine.back() == ' ') {
}
else if (currentLine.back() == ' ')
{
currentLine += c;

lineCharIndex++;
} else {
}
else
{
std::size_t lastSpace = currentLine.find_last_of(' ');
if (lastSpace == std::string::npos) {
if (lastSpace == std::string::npos)
{
output.m_lines.push_back(currentLine);
currentLine = "";
currentLine += c;

lineIndex++;
lineCharIndex = 1;
} else {
}
else
{
std::string firstPart = currentLine.substr(0, lastSpace);
output.m_lines.push_back(firstPart);
currentLine = currentLine.substr(lastSpace + 1);
Expand All @@ -182,7 +200,8 @@ namespace gui::elements {
}
}

if (!currentLine.empty()) {
if (!currentLine.empty())
{
output.m_lines.push_back(currentLine);
}

Expand All @@ -191,12 +210,12 @@ namespace gui::elements {

uint16_t Label::getUsableWidth(void)
{
return getWidth()-getRadius()-2*getBorderSize();
return getWidth() - getRadius() - 2 * getBorderSize();
}

uint16_t Label::getUsableHeight(void)
{
return getHeight()-getRadius()-2*getBorderSize();
return getHeight() - getRadius() - 2 * getBorderSize();
}

void Label::setHorizontalAlignment(Alignement alignment)
Expand All @@ -217,7 +236,7 @@ namespace gui::elements {

uint16_t Label::getTextWidth()
{
if(m_surface == nullptr)
if (m_surface == nullptr)
m_surface = std::make_shared<graphics::Surface>(m_width, m_height);
m_surface->setFontSize(this->m_fontSize);
return m_surface->getTextWidth(m_text);
Expand All @@ -226,7 +245,7 @@ namespace gui::elements {
uint16_t Label::getTextHeight()
{
bool allocatedSprite = false;
if(m_surface == nullptr)
if (m_surface == nullptr)
{
m_surface = std::make_shared<graphics::Surface>(1, 1);
allocatedSprite = true;
Expand All @@ -235,9 +254,9 @@ namespace gui::elements {
m_surface->setFontSize(this->m_fontSize);

const auto [lines, cursorIndex, cursorLine] = parse();
uint16_t out = getRadius() + getBorderSize()*2 + (m_surface->getTextHeight() + LINE_SPACING) * lines.size();
uint16_t out = getRadius() + getBorderSize() * 2 + (m_surface->getTextHeight() + LINE_SPACING) * lines.size();

if(allocatedSprite)
if (allocatedSprite)
m_surface = nullptr;

return out;
Expand All @@ -262,11 +281,18 @@ namespace gui::elements {
{
m_cursorIndex = cursorIndex;

if (m_cursorIndex < 0) {
if (m_cursorIndex < 0)
{
m_cursorIndex = 0;
}
if (m_cursorIndex > m_text.length()) {
if (m_cursorIndex > m_text.length())
{
m_cursorIndex = static_cast<int16_t>(m_text.length());
}
}

void Label::setStrikeOut(bool strikeOut)
{
m_strikeOut = strikeOut;
}
} // gui::elements
Loading