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

73
.clang-format Normal file
View File

@@ -0,0 +1,73 @@
---
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Left
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'false'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: 'true'
CompactNamespaces: 'false'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'true'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'true'
IndentPPDirectives: AfterHash
IndentWidth: '4'
IndentWrappedFunctionNames: 'true'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PointerAlignment: Left
ReflowComments: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'false'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInSquareBrackets: 'false'
Standard: Auto
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterStruct: true
BeforeCatch: true
AfterControlStatement: "Always"
AfterFunction: true
AfterNamespace: true
AfterUnion: true
SplitEmptyFunction: true
SplitEmptyNamespace: true
SplitEmptyRecord: true

View File

@@ -1,6 +1,14 @@
set(target frame) 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) target_link_libraries(${target} PRIVATE waveshare)

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

View File

@@ -1,10 +1,38 @@
#include <iostream>
#include <chrono> #include <chrono>
#include <iostream>
#include <thread> #include <thread>
#include <vector> #include <vector>
using namespace std::chrono_literals; 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> #include <EPD_7in5_V2.h>
@@ -35,7 +63,7 @@ int main()
color = ~color; color = ~color;
} }
EPD_7IN5_V2_Display(image.data()); EPD_7IN5_V2_Display(image.data());
std::this_thread::sleep_for(10s); std::this_thread::sleep_for(10s);
@@ -44,4 +72,5 @@ int main()
EPD_7IN5_V2_Sleep(); EPD_7IN5_V2_Sleep();
return 0; return 0;
} }
*/