Skip to content

Instantly share code, notes, and snippets.

@smcl
Created January 21, 2016 22:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save smcl/3a59a15c4dd09b5085d9 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
#define FROM_Z 128
#define TO_Z 32
#define CAM_F 60
#define OLED_DC 6
#define OLED_CS 7
#define OLED_RESET 8
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
typedef struct {
int x;
int y;
int z;
} point3D_type, *point3D;
bool invalidatedCache = true;
double cachedFromZ;
double cachedToZ;
void recalcCache() {
cachedFromZ = (CAM_F + FROM_Z) * CAM_F;
cachedToZ = (CAM_F + TO_Z) * CAM_F;
}
void drawProjectedLine(point3D_type from, point3D_type to) {
/* apply camera transform */
int x1_2D = 64 + (int)((double)from.x / (FROM_Z + CAM_F) * CAM_F);
int y1_2D = 32 + (int)((double)from.y / (FROM_Z + CAM_F) * CAM_F);
int x2_2D = 64 + (int)((double)to.x / (TO_Z + CAM_F) * CAM_F);
int y2_2D = 32 + (int)((double)to.y / (TO_Z + CAM_F) * CAM_F);
/* take projected positions and draw on screen */
display.drawLine(x1_2D, y1_2D, x2_2D, y2_2D, WHITE);
}
#define MAX_SWAY_X 16
#define MAX_SWAY_Y 16
int mod_x = 16;
int mod_y = 0;
int delta_mod_x = 1;
int delta_mod_y = 1;
void sway() {
if (mod_x > MAX_SWAY_X || mod_x < -MAX_SWAY_X) {
delta_mod_x *= -1;
}
if (mod_y > MAX_SWAY_Y || mod_y < -MAX_SWAY_Y) {
delta_mod_y *= -1;
}
mod_x += delta_mod_x;
mod_y += delta_mod_y;
}
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
}
void loop() {
display.clearDisplay();
point3D_type p0;
point3D_type p1;
p0 = { 0, 0, FROM_Z };
p1 = { 0, 0, TO_Z };
recalcCache();
for (int x = -70; x <= 70; x += 10) {
for (int y = -40; y <= 40; y += 10) {
p0.x = x + mod_x;
p0.y = y + mod_y;
p1.x = x + mod_x;
p1.y = y + mod_y;
drawProjectedLine(p0, p1);
}
}
sway();
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment