#include <SPI.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
//
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//
#define OLED_RESET 4
int count = 0;
// OLED SCL, SDA // D3, D2
SoftwareSerial BTSerial(8, 9); // RX, TX
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String inputString = String(500);
String btString = String(500);
class Point
{
public:
Point(int x, int y)
{
this->x = x;
this->y = y;
}
int x;
int y;
};
class Branch
{
public:
Point* start;
Point* fin;
int alpha;
int leng;
//Branch* r;
//Branch* l;
Branch(Point* str, Point* fn)
{
this->start = str;
this->fin = fn;
this->alpha = calculateAngle(start, fin);
this->leng = calculateLength(start, fin);
}
int calculateLength(Point* s, Point* e)
{
int dx = abs(s->x - e->x);
int dy = abs(s->y - e->y);
return (int)round(sqrt(pow(dx, 2) + pow(dy, 2)));
}
int calculateAngle(Point* s, Point* e)
{
int dx = abs(s->x - e->x);
int dy = abs(s->y - e->y);
int ang = round(angle(atan(dy / (double)dx)));
if (s->x - e->x >= 0)
{
//II - III
if (s->y - e->y >= 0)
{
//II region
return 180 - ang;
}
else
{
//III region
return 180 + ang;
}
}
else
{
//I - IV
if (s->y - e->y >= 0)
{
//I region
return ang;
}
else
{
//IV region
return 360 - ang;
}
}
}
Point* endPoint(int x, int y, int angle, int len)
{
x += (len * cos(angleToRadian(angle)));
y -= (len * sin(angleToRadian(angle)));
return new Point(x, y);
}
double angleToRadian(int angle)
{
return (3.1415 / 180.0) * angle;
}
int angle(double radian)
{
return (int)(radian * (180.0 / 3.1415));
}
Branch* generateR(int angle)
{
return new Branch(this->fin, endPoint(this->fin->x, this->fin->y, alpha - (90 - angle), (int)leng * 0.7));
}
Branch* generateL(int angle)
{
return new Branch(this->fin, endPoint(this->fin->x, this->fin->y, alpha + (90 - angle), (int)leng * 0.7));
}
void draw()
{
display.drawLine(this->start->x, this->start->y, this->fin->x, this->fin->y, SSD1306_WHITE);
}
};
void setup()
{
BTSerial.begin(9600);
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
display.clearDisplay();
display.drawPixel(64, 32, SSD1306_WHITE); //test dot
display.display();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
delay(1000);
display.setTextWrap(false);
display.setTextSize(1);
display.clearDisplay();
drawFrame();
int i;
for (i = 0; i < 7; i++)
{
display.setCursor(1, 1 + i * 9);
display.println("012345678901234567890");
}
display.display();
delay(100);
}
int jelly = 45;
void loop() {
if (Serial.available() > 0)
{
inputString = Serial.readString();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println(inputString);
display.display();
// display.setTextWrap(false);
/*
display.setTextSize(2);
display.println(inputString);
display.display();
display.startscrollright(0x00, 0x0F);
delay(5000);
display.stopscroll();
delay(1000);
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);*/
}
if (BTSerial.available())
{
btString = BTSerial.readString();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextWrap(true);
display.setTextSize(2);
display.println(btString);
display.display();
}
/*
Point* n = endPoint(64, 32, count, 32);
Serial.print("x: ");
Serial.print(n->x);
Serial.print(", y: ");
Serial.println(n->y);
count ++;
display.drawLine(64, 32, n->x, n->y, SSD1306_WHITE);
*/
display.clearDisplay();
Branch* branches[14];
Branch* root = new Branch(new Point(64, 64), new Point(64, 40));
branches[0] = root;
branches[1] = branches[0]->generateR(45);
branches[2] = branches[0]->generateL(45);
branches[3] = branches[1]->generateR(45);
branches[4] = branches[1]->generateL(45);
branches[5] = branches[2]->generateR(45);
branches[6] = branches[2]->generateL(45);
branches[7] = branches[3]->generateR(45);
branches[8] = branches[3]->generateL(45);
branches[10] = branches[4]->generateR(45);
branches[11] = branches[4]->generateL(45);
branches[12] = branches[5]->generateR(45);
branches[13] = branches[5]->generateL(45);
int i;
for (i = 0; i < 14; i++)
{
branches[i]->draw();
}
display.display();
delay(10000);
}
void drawFrame()
{
display.clearDisplay();
display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
display.display();
}