initial upload
This commit is contained in:
parent
3e1e8b073b
commit
a31d83cbc5
14 changed files with 312 additions and 0 deletions
11
CMakeLists.txt
Normal file
11
CMakeLists.txt
Normal file
|
@ -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)
|
||||
|
9
src/CMakeLists.txt
Normal file
9
src/CMakeLists.txt
Normal file
|
@ -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
|
1
src/Structs.h
Normal file
1
src/Structs.h
Normal file
|
@ -0,0 +1 @@
|
|||
typedef char UBYTE;
|
10
src/Utils.h
Normal file
10
src/Utils.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include "Structs.h"
|
||||
|
||||
class EInkBufferReader
|
||||
{
|
||||
private:
|
||||
UBYTE* buffer;
|
||||
|
||||
public:
|
||||
bool getPixelAtIndex(int index);
|
||||
};
|
20
src/main.cpp
Normal file
20
src/main.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "screens/VirtualScreen/VirtualScreen.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
|
||||
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;
|
||||
}
|
2
src/screens/CMakeLists.txt
Normal file
2
src/screens/CMakeLists.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
add_library(VirtualScreen VirtualScreen/VirtualScreen.cpp VirtualScreen/VirtualScreen.h VirtualScreen/BufferIterator.cpp VirtualScreen/BufferIterator.h)
|
||||
target_link_libraries(VirtualScreen SDL2)
|
14
src/screens/Screen.h
Normal file
14
src/screens/Screen.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "../Structs.h"
|
||||
|
||||
class Screen
|
||||
{
|
||||
protected:
|
||||
int width;
|
||||
int height;
|
||||
|
||||
public:
|
||||
void init();
|
||||
void display();
|
||||
void displayPartial();
|
||||
void cleanUp();
|
||||
};
|
29
src/screens/VirtualScreen/BufferIterator.cpp
Normal file
29
src/screens/VirtualScreen/BufferIterator.cpp
Normal file
|
@ -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;
|
||||
}
|
19
src/screens/VirtualScreen/BufferIterator.h
Normal file
19
src/screens/VirtualScreen/BufferIterator.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "../../Structs.h"
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
class BufferIterator
|
||||
{
|
||||
|
||||
|
||||
public:
|
||||
bool next();
|
||||
BufferIterator(UBYTE* buffer, int bufferLength);
|
||||
|
||||
private:
|
||||
int bytePointer;
|
||||
int interBytePointer;
|
||||
UBYTE* buffer;
|
||||
int bufferLength;
|
||||
|
||||
};
|
111
src/screens/VirtualScreen/VirtualScreen.cpp
Normal file
111
src/screens/VirtualScreen/VirtualScreen.cpp
Normal file
|
@ -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;
|
||||
}
|
||||
*/
|
37
src/screens/VirtualScreen/VirtualScreen.h
Normal file
37
src/screens/VirtualScreen/VirtualScreen.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include "../Screen.h"
|
||||
#include "BufferIterator.h"
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
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<UBYTE*> buffer;
|
||||
void replaceBuffer(UBYTE* buffer);
|
||||
std::thread windowThread;
|
||||
};
|
17
tests/CMakeLists.txt
Normal file
17
tests/CMakeLists.txt
Normal file
|
@ -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)
|
32
tests/screens/VirtualScreens/test_BufferIterator.cpp
Normal file
32
tests/screens/VirtualScreens/test_BufferIterator.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#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]);
|
||||
}
|
||||
|
||||
}
|
0
tests/test_VirtualScreen.cpp
Normal file
0
tests/test_VirtualScreen.cpp
Normal file
Loading…
Add table
Reference in a new issue