add: image

This commit is contained in:
2022-03-02 22:15:40 +01:00
parent a9bdbca571
commit 7a8e4f6096
9 changed files with 319 additions and 4 deletions

33
frame/src/Image.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Image.hpp"
namespace frame
{
Image::Image(uint32_t pWidth, uint32_t pHeight)
: mWidth(pWidth)
, mHeight(pHeight)
, mBuffer(mWidth * mHeight)
{
}
Image::Image(Size size)
: Image(size.width, size.height)
{
}
uint8_t& Image::at(uint32_t x, uint32_t y)
{
return mBuffer.at(toInternal(x, y));
}
uint8_t const& Image::at(uint32_t x, uint32_t y) const
{
return mBuffer.at(toInternal(x, y));
}
uint8_t Image::toInternal(uint32_t x, uint32_t y) const
{
return x + mWidth * y;
}
} // namespace frame

35
frame/src/Image.hpp Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "Size.hpp"
#include <vector>
namespace frame
{
class Image
{
uint32_t mWidth;
uint32_t mHeight;
std::vector<uint8_t> mBuffer;
public:
Image(uint32_t pWidth, uint32_t pHeight);
Image(Size size);
uint8_t& at(uint32_t x, uint32_t y);
uint8_t const& at(uint32_t x, uint32_t y) const;
auto getWidth() const
{
return mWidth;
}
auto getHeight() const
{
return mHeight;
}
private:
uint8_t toInternal(uint32_t x, uint32_t y) const;
};
} // namespace frame

10
frame/src/Size.hpp Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace frame
{
struct Size
{
uint32_t width;
uint32_t height;
};
} // namespace frame

View File

@@ -0,0 +1,69 @@
#include "EPD_7in5_V2.hpp"
#include <EPD_7in5_V2.h>
namespace frame::display
{
constexpr uint32_t mWidth = EPD_7IN5_V2_WIDTH;
constexpr uint32_t mHeight = EPD_7IN5_V2_HEIGHT;
EPD_7in5_V2::EPD_7in5_V2()
: IDisplay("EPD_7in5_V2", {mWidth, mHeight})
{
}
EPD_7in5_V2::~EPD_7in5_V2()
{
Sleep();
}
bool EPD_7in5_V2::Init()
{
return EPD_7IN5_V2_Init() == 0;
}
void EPD_7in5_V2::Clear(Color color)
{
if(color == Color::WHITE)
{
EPD_7IN5_V2_Clear()
} else
{
EPD_7IN5_V2_ClearBlack();
}
}
void EPD_7in5_V2::Display(Image const& image)
{
constexpr auto bytes_width = mWidth / 8;
std::vector<uint8_t> data(bytes_with * mHeight);
uint8_t bit_counter;
for(auto y = 0; y < mHeight; ++y)
{
for(auto x = 0; x < bytes_width; ++x)
{
uint8_t byte = 0;
for(auto bit = 0; bit < 8; ++bit)
{
if(image.at(x * 8 + bit, y) > 265 / 2)
{
byte ^= 1;
}
byte << 1;
}
data[bytes_width * y + x] = byte;
}
}
EPD_7IN5_V2_Display(data.data());
}
void EPD_7in5_V2::Sleep()
{
EPD_7IN5_V2_Sleep();
}
} // namespace frame::display

View File

@@ -0,0 +1,23 @@
#pragma once
#include "IDisplay.hpp"
namespace frame::display
{
class EPD_7in5_V2 : public IDisplay
{
public:
EPD_7in5_V2();
~EPD_7in5_V2();
bool Init() override;
void Clear(Color color) override;
void Display(Image const& image) override;
void Sleep() override;
};
} // namespace frame::display

View File

@@ -0,0 +1,35 @@
#pragma once
#include "../Image.hpp"
#include <string>
namespace frame::display
{
enum class Color { WHITE, BLACK };
class IDisplay
{
std::string mName;
Size mSize;
public:
IDisplay(std::string_view pName, Size pSize)
: mName(pName)
, mSize(pSize){};
virtual ~IDisplay() = default;
virtual bool Init() = 0;
virtual void Clear(Color color) = 0;
virtual void Display(Image const& image) = 0;
virtual void Sleep() = 0;
public:
Size const& getSize()
{
return size;
}
};
} // namespace frame::display

76
frame/src/main.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
#include "display/EPD_7in5_V2.hpp"
int main()
{
frame::display::EPD_7in5_V2 display{};
display.Init();
display.Clear(frame::display::Color::BLACK);
std::this_thread::sleep_for(2s);
display.Clear(frame::display::Color::WHITE);
std::this_thread::sleep_for(2s);
frame::Image image(display.getSize());
for(int x = 0; x < image.getWidth(); ++x)
{
image.at(x, 0) = 0xFF;
}
display.Display(image);
}
/*
#include <EPD_7in5_V2.h>
int main()
{
std::cout << "Hallo Welt" << std::endl;
EPD_7IN5_V2_Init();
EPD_7IN5_V2_ClearBlack();
std::this_thread::sleep_for(2s);
EPD_7IN5_V2_Clear();
std::this_thread::sleep_for(2s);
std::vector<uint8_t> image;
image.resize(EPD_7IN5_V2_HEIGHT * EPD_7IN5_V2_WIDTH);
uint8_t color = 0;
for(int x = 0; x < EPD_7IN5_V2_WIDTH; ++x)
{
for(int y = 0; y < EPD_7IN5_V2_HEIGHT; ++y)
{
image[EPD_7IN5_V2_WIDTH * y + x] = color;
}
color = ~color;
}
EPD_7IN5_V2_Display(image.data());
std::this_thread::sleep_for(10s);
EPD_7IN5_V2_Clear();
EPD_7IN5_V2_Sleep();
return 0;
}
*/