From a31d83cbc56dfb5ee647460e6f544a9f51555195 Mon Sep 17 00:00:00 2001 From: Rosia E Evans Date: Sun, 30 Mar 2025 18:46:31 +0100 Subject: [PATCH] initial upload --- CMakeLists.txt | 11 ++ src/CMakeLists.txt | 9 ++ src/Structs.h | 1 + src/Utils.h | 10 ++ src/main.cpp | 20 ++++ src/screens/CMakeLists.txt | 2 + src/screens/Screen.h | 14 +++ src/screens/VirtualScreen/BufferIterator.cpp | 29 +++++ src/screens/VirtualScreen/BufferIterator.h | 19 +++ src/screens/VirtualScreen/VirtualScreen.cpp | 111 ++++++++++++++++++ src/screens/VirtualScreen/VirtualScreen.h | 37 ++++++ tests/CMakeLists.txt | 17 +++ .../VirtualScreens/test_BufferIterator.cpp | 32 +++++ tests/test_VirtualScreen.cpp | 0 14 files changed, 312 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/Structs.h create mode 100644 src/Utils.h create mode 100644 src/main.cpp create mode 100644 src/screens/CMakeLists.txt create mode 100644 src/screens/Screen.h create mode 100644 src/screens/VirtualScreen/BufferIterator.cpp create mode 100644 src/screens/VirtualScreen/BufferIterator.h create mode 100644 src/screens/VirtualScreen/VirtualScreen.cpp create mode 100644 src/screens/VirtualScreen/VirtualScreen.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/screens/VirtualScreens/test_BufferIterator.cpp create mode 100644 tests/test_VirtualScreen.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..92d8130 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.20) +set(CMAKE_BUILD_TYPE Debug) # Debug or Release +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) # export our compiler flags for clangd to read + +project(c-paper-terminal) + +add_subdirectory(src) +add_subdirectory(tests) + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..5360485 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable(c-paper-terminal main.cpp) + +add_subdirectory(screens) + +target_link_libraries(c-paper-terminal PRIVATE VirtualScreen) + +#add_library(VirtualScreen VirtualScreen/VirtualScreen.cpp VirtualScreen/VirtualScreen.h) +# target_include_directories(VirtualScreen PRIVATE "VirtualScreen") +# target_link_libraries(BlogParser PRIVATE md4c) use this to link a library to this diff --git a/src/Structs.h b/src/Structs.h new file mode 100644 index 0000000..78171ee --- /dev/null +++ b/src/Structs.h @@ -0,0 +1 @@ +typedef char UBYTE; diff --git a/src/Utils.h b/src/Utils.h new file mode 100644 index 0000000..83aadf9 --- /dev/null +++ b/src/Utils.h @@ -0,0 +1,10 @@ +#include "Structs.h" + +class EInkBufferReader +{ +private: + UBYTE* buffer; + +public: + bool getPixelAtIndex(int index); +}; diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..ff53615 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,20 @@ +#include "screens/VirtualScreen/VirtualScreen.h" +#include + +#include +#include + + +int main(int argc, char** args) +{ + VirtualScreen *vScreen = new VirtualScreen(); + vScreen->init(); + + while(1){ + printf("%d\n", vScreen->debug); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + } + + + return 0; +} diff --git a/src/screens/CMakeLists.txt b/src/screens/CMakeLists.txt new file mode 100644 index 0000000..057339d --- /dev/null +++ b/src/screens/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(VirtualScreen VirtualScreen/VirtualScreen.cpp VirtualScreen/VirtualScreen.h VirtualScreen/BufferIterator.cpp VirtualScreen/BufferIterator.h) +target_link_libraries(VirtualScreen SDL2) diff --git a/src/screens/Screen.h b/src/screens/Screen.h new file mode 100644 index 0000000..aba8155 --- /dev/null +++ b/src/screens/Screen.h @@ -0,0 +1,14 @@ +#include "../Structs.h" + +class Screen +{ +protected: + int width; + int height; + +public: + void init(); + void display(); + void displayPartial(); + void cleanUp(); +}; diff --git a/src/screens/VirtualScreen/BufferIterator.cpp b/src/screens/VirtualScreen/BufferIterator.cpp new file mode 100644 index 0000000..a25a06e --- /dev/null +++ b/src/screens/VirtualScreen/BufferIterator.cpp @@ -0,0 +1,29 @@ +#include "BufferIterator.h" + +BufferIterator::BufferIterator(UBYTE* buffer, int bufferLength) +{ + bytePointer = 0; + interBytePointer = 0; + this->buffer = buffer; + this->bufferLength = bufferLength; +} + +bool BufferIterator::next() +{ + if (bytePointer >= this->bufferLength) + { + //fprintf(stderr, "Warning! BufferIterator reached the end of a buffer and is now just returning false\n"); + return false; + } + + bool bitState = (buffer[bytePointer] & (128 >> interBytePointer)); + + interBytePointer++; + if (interBytePointer >= 8) + { + interBytePointer = 0; + bytePointer++; + } + + return bitState; +} diff --git a/src/screens/VirtualScreen/BufferIterator.h b/src/screens/VirtualScreen/BufferIterator.h new file mode 100644 index 0000000..a9daca5 --- /dev/null +++ b/src/screens/VirtualScreen/BufferIterator.h @@ -0,0 +1,19 @@ +#include "../../Structs.h" +#include +#include + +class BufferIterator +{ + + +public: + bool next(); + BufferIterator(UBYTE* buffer, int bufferLength); + +private: + int bytePointer; + int interBytePointer; + UBYTE* buffer; + int bufferLength; + +}; diff --git a/src/screens/VirtualScreen/VirtualScreen.cpp b/src/screens/VirtualScreen/VirtualScreen.cpp new file mode 100644 index 0000000..a2645ae --- /dev/null +++ b/src/screens/VirtualScreen/VirtualScreen.cpp @@ -0,0 +1,111 @@ +#include "VirtualScreen.h" + +void VirtualScreen::initSDL() +{ + SDL_Init(SDL_INIT_VIDEO < 0); + + updateWindowResolution(); + this->window = SDL_CreateWindow("E-Ink Dummy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, 0); + + this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED); + + UBYTE* buffer = (UBYTE*)calloc(sizeof(UBYTE), simulatedResolutionHeight*(simulatedResolutionWidth/8)); + replaceBuffer(buffer); + + windowThread = std::thread(&VirtualScreen::runWindowAsync, this); +} + +void VirtualScreen::runWindowAsync() +{ + for(;;) + { + SDL_RenderClear(this->renderer); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + + this->checkInputs(); + this->drawGrid(this->buffer); //todo unsure on if this is actually threadsafe or not + + + SDL_RenderPresent(renderer); + SDL_Delay(10); + } +} + +void VirtualScreen::drawGrid(UBYTE* buffer) +{ + BufferIterator* iterator = new BufferIterator(buffer, simulatedResolutionHeight*(simulatedResolutionWidth/8)); + + SDL_Rect *rect = new SDL_Rect(); + for (int y = 0; y < windowHeight; y=y+pixelScale) + { + for (int x = 0; x < windowWidth; x+=pixelScale) + { + SDL_Color color = boolToColor(iterator->next()); + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); + + rect->x = x; + rect->y = y; + rect->w = pixelScale; + rect->h = pixelScale; + SDL_RenderFillRect(this->renderer, rect); + } + } + + delete rect; + delete iterator; +} + +void VirtualScreen::checkInputs() +{ + SDL_Event event; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + exit(0); + } + } +} + +void VirtualScreen::updateWindowResolution() +{ + windowWidth = simulatedResolutionWidth * pixelScale; + windowHeight = simulatedResolutionHeight * pixelScale; +} + +SDL_Color VirtualScreen::boolToColor(bool value) +{ + const SDL_Color black = {0, 0, 0, 0}; + const SDL_Color white = {255, 255, 255, 255}; + return value ? white : black; +} + +void VirtualScreen::replaceBuffer(UBYTE* buffer) +{ + this->buffer = buffer; //todo does this need to delete the old data? +} + + +void VirtualScreen::cleanUp() +{ + +} + +void VirtualScreen::init() +{ + this->initSDL(); +} + +/* + * literally just use std::thread and make a + * std::thread thread_obj(method, params) + */ + +/* + UBYTE* data = (UBYTE*)calloc(sizeof(UBYTE), simulatedResolutionHeight*simulatedResolutionWidth/8); + for (int i=0; i<(simulatedResolutionHeight*simulatedResolutionWidth)/8; i++) + { + data[i] = 0b00001; + } + */ diff --git a/src/screens/VirtualScreen/VirtualScreen.h b/src/screens/VirtualScreen/VirtualScreen.h new file mode 100644 index 0000000..7ff3f8b --- /dev/null +++ b/src/screens/VirtualScreen/VirtualScreen.h @@ -0,0 +1,37 @@ +#include +#include "../Screen.h" +#include "BufferIterator.h" +#include +#include + +class VirtualScreen: Screen +{ + + +public: + void init(); + void display(); + void displayPartial(); + void cleanUp(); + +private: + int pixelScale = 10; + int simulatedResolutionWidth = 100; + int simulatedResolutionHeight = 70; + int windowWidth; + int windowHeight; + void updateWindowResolution(); + + SDL_Window* window; + SDL_Renderer* renderer; + + void initSDL(); + void runWindowAsync(); + void drawGrid(UBYTE* stream); + void checkInputs(); + SDL_Color boolToColor(bool value); + + std::atomic buffer; + void replaceBuffer(UBYTE* buffer); + std::thread windowThread; +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..520f827 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,17 @@ +Include(FetchContent) + +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.4.0 # or a later release +) + +FetchContent_MakeAvailable(Catch2) + +add_executable(tests test_VirtualScreen.cpp screens/VirtualScreens/test_BufferIterator.cpp) +target_link_libraries(tests PRIVATE Catch2::Catch2WithMain) +target_link_libraries(tests PRIVATE VirtualScreen) + +include(CTest) +include(Catch) +catch_discover_tests(tests) diff --git a/tests/screens/VirtualScreens/test_BufferIterator.cpp b/tests/screens/VirtualScreens/test_BufferIterator.cpp new file mode 100644 index 0000000..7675799 --- /dev/null +++ b/tests/screens/VirtualScreens/test_BufferIterator.cpp @@ -0,0 +1,32 @@ +#include +#include "../../../src/screens/VirtualScreen/BufferIterator.h" + +TEST_CASE("BufferIterator correctly steps through byte") { + UBYTE buffer = 0b00001111; + BufferIterator* iter = new BufferIterator(&buffer, 0); + + int results[8] = {0, 0, 0, 0, 1, 1, 1, 1}; + + for (int i=0; i<8; i++) + { + REQUIRE(iter->next() == results[i]); + } +} + +TEST_CASE("BufferIterator correctly steps up a byte") { + UBYTE buffer[2] = {0b00001111, 0b00001111}; + BufferIterator* iter = new BufferIterator(buffer,0 ); + + int results[8] = {0, 0, 0, 0, 1, 1, 1, 1}; + + for (int i=0; i<8; i++) + { + iter->next(); + } + + for (int i=0; i<8; i++) + { + REQUIRE(iter->next() == results[i]); + } + +} diff --git a/tests/test_VirtualScreen.cpp b/tests/test_VirtualScreen.cpp new file mode 100644 index 0000000..e69de29