/*
* This ESP32 code is created by sabri elenani
*
* This ESP32 code is released in github
*
* For more detail i did not finish the code (i work on code until i finish the project )
*/
// 1---- drivers and librarys
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
#include <U8g2lib.h>
// definitions
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Set the coordinates for the center of the display
int centerX = 128 / 2;
int centerY = 64 / 2;
// Define the length of the base of the isosceles triangle
int baseLength = 15;
// Define the height of the isosceles triangle (3 times the base)
int height = baseLength * 2;
// Calculate the coordinates of the three vertices of the isosceles triangle
int vertex1X = centerX;
int vertex1Y = centerY - height / 2;
int vertex2X = centerX - baseLength / 2;
int vertex2Y = centerY + height / 2;
int vertex3X = centerX + baseLength / 2;
int vertex3Y = centerY + height / 2;
void rotatePoint(float x, float y, float px, float py, float theta, float& newX, float& newY) {
// Translate the point so that the pivot is at the origin
float translatedX = x - px;
float translatedY = y - py;
// Perform the rotation
float cosTheta = cos(theta);
float sinTheta = sin(theta);
newX = translatedX * cosTheta - translatedY * sinTheta + px;
newY = translatedX * sinTheta + translatedY * cosTheta + py;
}
void draw_Isosceles (int _theta){
// Convert rotation angle from degrees to radians
float thetaDegrees = _theta;
float thetaRadians = radians(thetaDegrees);
// Rotate each vertex around the center of the isosceles triangle
float rotatedVertex1X, rotatedVertex1Y;
rotatePoint(vertex1X, vertex1Y, centerX, centerY, thetaRadians, rotatedVertex1X, rotatedVertex1Y);
float rotatedVertex2X, rotatedVertex2Y;
rotatePoint(vertex2X, vertex2Y, centerX, centerY, thetaRadians, rotatedVertex2X, rotatedVertex2Y);
float rotatedVertex3X, rotatedVertex3Y;
rotatePoint(vertex3X, vertex3Y, centerX, centerY, thetaRadians, rotatedVertex3X, rotatedVertex3Y);
// Draw the rotated isosceles triangle
oled.fillTriangle(rotatedVertex1X, rotatedVertex1Y, rotatedVertex2X, rotatedVertex2Y, rotatedVertex3X, rotatedVertex3Y,WHITE);
}
void setup() {
Serial.begin(115200);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(500); // wait one seconds for initializing
oled.clearDisplay(); // clear display
}
void loop() {
//clear
oled.clearDisplay(); // clear every loop (:
// random (testing)
int theta = random(0 , 180); // random numbers generation
// main usage : the function the we will use : put something in (rand)
draw_Isosceles(theta);
oled.display(); // display what chip has done on OLED
delay(100);
}