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

Merge "feature/keyboard" #94

Merged
merged 12 commits into from
Sep 6, 2024
Merged
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
77 changes: 69 additions & 8 deletions lib/graphics/src/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ namespace graphics
&m_sprite,
x,
y,
m_transparent_color);
surface->m_transparent_color);
}
else
{
Expand Down Expand Up @@ -174,7 +174,7 @@ namespace graphics
0,
scale,
scale,
m_transparent_color);
surface->m_transparent_color);
}
else
{
Expand Down Expand Up @@ -310,7 +310,7 @@ namespace graphics

int width = sprite.width();
int height = sprite.height();

// Open the file for writing
std::ofstream outFile(filename.str(), std::ios::binary);
if (!outFile.is_open()) {
Expand Down Expand Up @@ -361,7 +361,7 @@ namespace graphics
}
}
}

// Add MCU to JPEG
rc = jpg.addMCU(&jpe, ucMCU, 8);
if (rc != 0) {
Expand All @@ -374,7 +374,7 @@ namespace graphics

// Finish encoding and get the compressed data
int jpegSize = jpg.close();

// Write JPEG data to file
outFile.write(reinterpret_cast<const char*>(jpe.pOutput), jpegSize);
outFile.close();
Expand Down Expand Up @@ -561,17 +561,55 @@ namespace graphics
drawText(text, textPositionX, textPositionY, color);
}

void Surface::blur(uint8_t radius)
Surface Surface::clone() const {
auto output = Surface(getWidth(), getHeight());

output.m_sprite.setBuffer(m_sprite.getBuffer(), m_sprite.width(), m_sprite.height());

return output;
}

void * Surface::getBuffer() const {
return m_sprite.getBuffer();
}

void Surface::setBuffer(void *buffer, int32_t w, int32_t h) {
if (w == -1) {
w = getWidth();
}
if (h == -1) {
h = getHeight();
}

m_sprite.setBuffer(buffer, w, h, m_sprite.getColorDepth());
}

void Surface::applyFilter(const Filter filter, const int32_t intensity) {
switch (filter) {
case BLUR:
blur(intensity);
break;
case LIGHTEN:
lighten(intensity);
break;
case DARKEN:
darken(intensity);
break;
default:;
}
}

void Surface::blur(const int32_t radius)
{
// Copy
auto copy = lgfx::LGFX_Sprite();
copy.setColorDepth(m_color_depth);
copy.createSprite(getWidth(), getHeight());

// Apply blur effect
for (uint16_t x = radius; x < getWidth() - radius; x++)
for (int32_t x = radius; x < m_sprite.width(); x++)
{
for (uint16_t y = radius; y < getHeight() - radius; y++)
for (int32_t y = radius; y < m_sprite.height(); y++)
{
uint64_t sumR = 0;
uint64_t sumG = 0;
Expand Down Expand Up @@ -608,4 +646,27 @@ namespace graphics
// Update the Surface
copy.pushSprite(&m_sprite, 0, 0);
}

void Surface::fastBlur(int32_t radius) {
// TODO
}

void Surface::lighten(const int32_t intensity) {
for (int32_t x = 0; x < getWidth(); x++) {
for (int32_t y = 0; y < getHeight(); y++) {
uint8_t r, g, b;
unpackRGB565(m_sprite.readPixel(x, y), &r, &g, &b);

r = std::clamp(r + intensity, 0, 255);
g = std::clamp(g + intensity, 0, 255);
b = std::clamp(b + intensity, 0, 255);

m_sprite.writePixel(x, y, packRGB565(r, g, b));
}
}
}

void Surface::darken(const int32_t intensity) {
lighten(-intensity);
}
} // graphics
19 changes: 18 additions & 1 deletion lib/graphics/src/Surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,24 @@ namespace graphics
// w and h are the width and height of the bounding box where the text will be centered
void drawTextCentered(std::string &text, const int16_t x, const int16_t y, const uint16_t w = -1, const uint16_t h = -1, const bool horizontallyCentered = true, const bool verticallyCentered = true, const std::optional<color_t> color = std::nullopt);

void blur(uint8_t radius);
[[nodiscard]] Surface clone() const;

[[nodiscard]] void * getBuffer() const;
void setBuffer(void* buffer, int32_t w = -1, int32_t h = -1);

enum Filter {
BLUR,
LIGHTEN,
DARKEN
};

void applyFilter(Filter filter, int32_t intensity);

private:
void blur(int32_t radius);
void fastBlur(int32_t radius);
void lighten(int32_t intensity);
void darken(int32_t intensity);
};
} // graphics

Expand Down
4 changes: 2 additions & 2 deletions lib/graphics/src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ graphics::GraphicsInitCode graphics::init()
if (i2cDevicesCount == 0) {
return ERROR_NO_TOUCHSCREEN;
}

if (i2cDevicesCount >= 2) {
return ERROR_FAULTY_TOUCHSCREEN;
}
Expand Down Expand Up @@ -370,7 +370,7 @@ void graphics::setScreenOrientation(const EScreenOrientation screenOrientation)
}
}

LGFX* graphics::getLGFX() {
LGFX* graphics::getLCD() {
return lcd.get();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/graphics/src/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace graphics
EScreenOrientation getScreenOrientation();
void setScreenOrientation(EScreenOrientation screenOrientation);

LGFX* getLGFX();
LGFX* getLCD();
#ifdef ESP_PLATFORM
FT6236G* getTouchController();
#endif
Expand Down
20 changes: 14 additions & 6 deletions lib/gui/src/ElementBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ bool gui::ElementBase::updateAll()
}

update();


if(this->m_parent == nullptr)
graphics::touchIsRead();

Expand Down Expand Up @@ -233,7 +233,7 @@ bool gui::ElementBase::update()
bool isScrollingX = abs(m_lastTouchX - touchX) > SCROLL_STEP;
bool isScrollingY = abs(m_lastTouchY - touchY) > SCROLL_STEP;
bool isScrolling = isScrollingX || isScrollingY;

if(isScrollingX)
{
gui::ElementBase* nearScrollableObject = getHigestXScrollableParent();
Expand Down Expand Up @@ -310,7 +310,7 @@ bool gui::ElementBase::update()
lastEventTouchY = originTouchY;
onReleased();
}

globalPressedState = NOT_PRESSED;
widgetPressed = nullptr;

Expand Down Expand Up @@ -599,7 +599,7 @@ void gui::ElementBase::free()
m_surface.reset();

setParentNotRendered();

for (auto child : m_children)
{
child->free();
Expand All @@ -621,4 +621,12 @@ bool gui::ElementBase::isInside()
return false;

return true;
}
}

std::shared_ptr<graphics::Surface> gui::ElementBase::getSurface() {
return getAndSetSurface();
}

void gui::ElementBase::forceUpdate() {
localGraphicalUpdate();
}
6 changes: 6 additions & 0 deletions lib/gui/src/ElementBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ namespace gui
static int16_t lastEventTouchX, lastEventTouchY;

uint16_t m_x, m_y;

// WARNING : Don't ever expose this to the Lua API
std::shared_ptr<graphics::Surface> getSurface();

void forceUpdate();

protected:
// variables générales
uint16_t m_width, m_height;
Expand Down
44 changes: 44 additions & 0 deletions lib/gui/src/elements/Filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "Filter.hpp"

#include <iostream>
#include <standby.hpp>

#include "graphics.hpp"

namespace gui::elements
{
Filter::Filter(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height)
{
this->m_x = x;
this->m_y = y;
this->m_width = width;
this->m_height = height;

m_screenSurface = std::make_shared<graphics::Surface>(m_width, m_height);
}

void Filter::render() {
std::cout << "RENDER FILTER" << std::endl;

m_surface->pushSurface(m_screenSurface.get(), 0, 0);
}

void Filter::apply() const {
std::cout << "APPLY FILTER" << std::endl;

StandbyMode::triggerPower();

// Get screen
LGFX* lcd = graphics::getLCD();

// Copy screen zone to buffer
lcd->readRect(m_x, m_y, m_width, m_height, static_cast<uint16_t *>(m_screenSurface->getBuffer()));

// Apply filter
m_screenSurface->applyFilter(graphics::Surface::LIGHTEN, 100);
m_screenSurface->applyFilter(graphics::Surface::DARKEN, 200);
m_screenSurface->applyFilter(graphics::Surface::BLUR, 3);
}

Filter::~Filter() = default;
}
29 changes: 29 additions & 0 deletions lib/gui/src/elements/Filter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef FILTER_HPP
#define FILTER_HPP

#include "../ElementBase.hpp"
#include <filestream.hpp>

namespace gui::elements
{
/**
* @deprecated Not compatible with device.
*/
class Filter final : public ElementBase
{
public:
typedef std::pair<int16_t, int16_t> point_t;

Filter(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
~Filter() override;

void render() override;

void apply() const;

private:
std::shared_ptr<graphics::Surface> m_screenSurface;
};
} // gui::elements

#endif //FILTER_HPP
Loading
Loading