add: image
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
|
||||
set(target frame)
|
||||
|
||||
set(src
|
||||
src/main.cpp
|
||||
src/Image.hpp
|
||||
src/Image.cpp
|
||||
src/display/IDisplay.hpp
|
||||
src/display/EPD_7in5_V2.hpp
|
||||
src/display/EPD_7in5_V2.cpp
|
||||
)
|
||||
|
||||
add_executable(${target} main.cpp)
|
||||
add_executable(${target} ${src})
|
||||
target_link_libraries(${target} PRIVATE waveshare)
|
||||
33
frame/src/Image.cpp
Normal file
33
frame/src/Image.cpp
Normal 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
35
frame/src/Image.hpp
Normal 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
10
frame/src/Size.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace frame
|
||||
{
|
||||
struct Size
|
||||
{
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
};
|
||||
} // namespace frame
|
||||
69
frame/src/display/EPD_7in5_V2.cpp
Normal file
69
frame/src/display/EPD_7in5_V2.cpp
Normal 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
|
||||
23
frame/src/display/EPD_7in5_V2.hpp
Normal file
23
frame/src/display/EPD_7in5_V2.hpp
Normal 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
|
||||
35
frame/src/display/IDisplay.hpp
Normal file
35
frame/src/display/IDisplay.hpp
Normal 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
|
||||
@@ -1,10 +1,38 @@
|
||||
#include <iostream>
|
||||
#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>
|
||||
|
||||
|
||||
@@ -35,7 +63,7 @@ int main()
|
||||
|
||||
color = ~color;
|
||||
}
|
||||
|
||||
|
||||
EPD_7IN5_V2_Display(image.data());
|
||||
|
||||
std::this_thread::sleep_for(10s);
|
||||
@@ -44,4 +72,5 @@ int main()
|
||||
EPD_7IN5_V2_Sleep();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user