Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created January 6, 2025 22:50
Show Gist options
  • Save devsnek/792c536762fec78367f6da87f48845b1 to your computer and use it in GitHub Desktop.
Save devsnek/792c536762fec78367f6da87f48845b1 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <thread>
#include <vector>
#include <Arduino.h>
#include <Assets/Models/OBJ/Background.h>
#include <Assets/Models/OBJ/DeltaDisplayBackground.h>
#include <Camera/Camera.h>
#include <Camera/CameraManager/CameraManager.h>
#include <Camera/Pixels/PixelGroup.h>
#include <Controller/Controller.h>
#include <Examples/Templates/Project.h>
#include <Utils/Math/Transform.h>
#include <Camera/Pixels/PixelGroups/P3HUB75.h> // TODO: replace
#include <Scene/Materials/Animated/RainbowSpiral.h>
#include <SDL.h>
const uint32_t WIDTH = 256;
const uint32_t HEIGHT = 128;
const uint32_t NPIXELS = WIDTH * HEIGHT;
const Vector2D *make_pixels() {
std::vector<Vector2D> *out = new std::vector<Vector2D>();
out->reserve(NPIXELS);
for (uint32_t y = 0; y < HEIGHT; y += 1) {
for (uint32_t x = 0; x < WIDTH; x += 1) {
out->emplace_back(x, y);
}
}
Serial.println("made pixels");
return out->data();
}
class OledCameraManager : public CameraManager {
private:
CameraLayout cameraLayout =
CameraLayout(CameraLayout::ZForward, CameraLayout::YUp);
Transform camTransform =
Transform(Vector3D(), Vector3D(0, 96.0f, -500.0f), Vector3D(1, 1, 1));
PixelGroup<NPIXELS> camPixels = PixelGroup<NPIXELS>(make_pixels());
Camera<NPIXELS> cam =
Camera<NPIXELS>(&camTransform, &cameraLayout, &camPixels);
public:
OledCameraManager() : CameraManager(new CameraBase *[1]{&cam}, 1) {}
};
class OledController : public Controller {
private:
SDL_Window *window;
SDL_Renderer *renderer;
public:
explicit OledController(CameraManager *cameras)
: Controller(cameras, 255, 255) {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer);
}
void Initialize() override {}
void Display() override {
IPixelGroup *camPixels = cameras->GetCameras()[0]->GetPixelGroup();
for (uint16_t y = 0; y < HEIGHT; y++) {
for (uint16_t x = 0; x < WIDTH; x++) {
uint16_t pixelNum = y * WIDTH + x;
auto r = camPixels->GetColor(pixelNum)->R;
auto g = camPixels->GetColor(pixelNum)->G;
auto b = camPixels->GetColor(pixelNum)->B;
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderDrawPoint(renderer, x, y);
}
}
SDL_RenderPresent(renderer);
}
void SetBrightness(uint8_t maxBrightness) override {}
void SetAccentBrightness(uint8_t maxAccentBrightness) override {}
};
class OledProject : public Project {
private:
OledCameraManager cameras;
OledController controller = OledController(&cameras);
Background background;
DeltaDisplayBackground deltaDisplayBackground;
RainbowSpiral material;
public:
OledProject() : Project(&cameras, &controller, 2) {
scene.AddObject(background.GetObject());
scene.AddObject(deltaDisplayBackground.GetObject());
background.GetObject()->SetMaterial(&material);
deltaDisplayBackground.GetObject()->SetMaterial(&material);
}
void Initialize() override { controller.Initialize(); }
void Update(float ratio) override { material.Update(ratio); }
};
int main() {
ard_init();
OledProject project;
project.Initialize();
while (true) {
float ratio = (float)(millis() % 5000) / 5000.0f;
project.Animate(ratio);
project.Render();
project.Display();
project.PrintStats();
SDL_Event e;
SDL_PollEvent(&e);
if (e.type == SDL_QUIT) {
exit(0);
}
/*
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(16ms);
}
*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment