#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
bool introSet= true;
int startTime;
int introTime = 3000; // time in ms
//cube
int points[8][2]; // eight 2D points for the cube, values will be calculated in the code
int orig_points [8][3] = { // eight 3D points - set values for 3D cube
{-1,-1, 1},
{1,-1,1},
{1,1,1},
{-1,1,1},
{-1,-1,-1},
{1,-1,-1},
{1,1,-1},
{-1,1,-1}
};
float rotated_3d_points [8][3]; // eight 3D points - rotated around Y axis
float angle_deg = 60.0; // rotation around the Y axis
float z_offset = -4.0; // offset on Z axis
float cube_size = 70.0; // cube size (multiplier)
float time_frame;
float shiftx = 0; // x shift
float shifty = -10;
//cube end
#define VOLTS_width 124
#define VOLTS_height 23
// 'VOLTS', 128x64px
const unsigned char VOLTS_bits[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x7f, 0x80, 0xff, 0xc0, 0xff, 0x1f, 0xf0, 0x0f, 0xe0, 0xff, 0xff, 0x01, 0xff, 0xff, 0x03,
0x80, 0x7f, 0xc0, 0x7f, 0xf0, 0xff, 0x3f, 0xf0, 0x0f, 0xe0, 0xff, 0xff, 0xc1, 0xff, 0xff, 0x03,
0xc0, 0x3f, 0xe0, 0x3f, 0xf8, 0xff, 0x7f, 0xf8, 0x07, 0xe0, 0xff, 0xff, 0xe1, 0xff, 0xff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 0x7f, 0x00, 0xfe, 0xff, 0x1f, 0x00,
0xfc, 0xff, 0x00, 0x80, 0xff, 0xff, 0x03, 0xff, 0xff, 0xc3, 0x3f, 0x00, 0xfe, 0xff, 0x0f, 0x00,
0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0xfc, 0xff, 0x81, 0x3f, 0x00, 0xfe, 0xff, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 1040)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
VOLTS_bits
};
void setup(void) {
u8g2.begin();
u8g2.setFont(u8g2_font_inb16_mr);
u8g2.setDrawColor(1);
u8g2.setBitmapMode(0);
int startTime = millis();
}
void loop(void) {
if(introSet){
//cube
time_frame++; // increase the time frame value by 1
// cube_size = 50 + sin(time_frame * 0.2)*20; // oscilate cube size between values 30 - 70
z_offset = -2.0; //
cube_size = 10.0; // uncomment those two lines for a "wide angle camera" -- bigger perspective distort
// increase the angle by 5° increments
if (angle_deg < 90-5) {
angle_deg = angle_deg + 5;
} else {
angle_deg = 0;
}
// calculate the points
for (int i=0; i<8; i++) {
// rotate 3d points around the Y axis (rotating X nad Z positions)
rotated_3d_points [i][0] = orig_points [i][0] * cos(radians(angle_deg)) - orig_points [i][2] * sin(radians(angle_deg));
rotated_3d_points [i][1] = orig_points [i][1];
rotated_3d_points [i][2] = orig_points [i][0] * sin(radians(angle_deg)) + orig_points [i][2] * cos(radians(angle_deg)) + z_offset;
// project 3d points into 2d space with perspective divide -- 2D x = x/z, 2D y = y/z
points[i][0] = round(64 + rotated_3d_points [i][0] / rotated_3d_points [i][2] * cube_size);
points[i][1] = round(32 + rotated_3d_points [i][1] / rotated_3d_points [i][2] * cube_size);
}
//emd cube
u8g2.clearBuffer();
int elapsedTime = millis()-startTime;
//cube start
// connect the lines between the individual points
u8g2.drawLine(points[ 0 ][ 0 ]+shiftx, points[ 0 ][ 1 ]+shifty, points[ 1 ][ 0 ] +shiftx, points[ 1 ][ 1 ] +shifty); // connect points 0-1
u8g2.drawLine(points[ 1 ][ 0 ]+shiftx, points[ 1 ][ 1 ]+shifty , points[ 2 ][ 0 ] +shiftx, points[ 2 ][ 1 ] +shifty); // connect points 1-2
u8g2.drawLine(points[ 2 ][ 0 ]+shiftx, points[ 2 ][ 1 ] +shifty, points[ 3 ][ 0 ] +shiftx, points[ 3 ][ 1 ] +shifty); // connect points 2-3
u8g2.drawLine(points[ 3 ][ 0 ]+shiftx, points[ 3 ][ 1 ] +shifty, points[ 0 ][ 0 ] +shiftx, points[ 0 ][ 1 ] +shifty); // connect points 3-0
u8g2.drawLine(points[ 4 ][ 0 ]+shiftx, points[ 4 ][ 1 ] +shifty, points[ 5 ][ 0 ] +shiftx, points[ 5 ][ 1 ] +shifty); // connect points 4-5
u8g2.drawLine(points[ 5 ][ 0 ]+shiftx, points[ 5 ][ 1 ] +shifty, points[ 6 ][ 0 ] +shiftx, points[ 6 ][ 1 ] +shifty); // connect points 5-6
u8g2.drawLine(points[ 6 ][ 0 ]+shiftx, points[ 6 ][ 1 ] +shifty, points[ 7 ][ 0 ] +shiftx, points[ 7 ][ 1 ]+shifty ); // connect points 6-7
u8g2.drawLine(points[ 7 ][ 0 ]+shiftx, points[ 7 ][ 1 ] +shifty, points[ 4 ][ 0 ] +shiftx, points[ 4 ][ 1 ] +shifty); // connect points 7-4
u8g2.drawLine(points[ 0 ][ 0 ]+shiftx, points[ 0 ][ 1 ] +shifty, points[ 4 ][ 0 ] +shiftx, points[ 4 ][ 1 ]+shifty ); // connect points 0-4
u8g2.drawLine(points[ 1 ][ 0 ]+shiftx, points[ 1 ][ 1 ] +shifty, points[ 5 ][ 0 ] +shiftx, points[ 5 ][ 1 ] +shifty); // connect points 1-5
u8g2.drawLine(points[ 2 ][ 0 ]+shiftx, points[ 2 ][ 1 ] +shifty, points[ 6 ][ 0 ] +shiftx, points[ 6 ][ 1 ] +shifty); // connect points 2-6
u8g2.drawLine(points[ 3 ][ 0 ]+shiftx, points[ 3 ][ 1 ] +shifty, points[ 7 ][ 0 ] +shiftx, points[ 7 ][ 1 ]+shifty ); // connect points 3-7
//cube end
//loading bar module
u8g2.drawFrame(10,48,108,15);
u8g2.setDrawColor(0);
u8g2.drawBox(9,47,108-(elapsedTime/(introTime/108)),18);
u8g2.setDrawColor(1);
u8g2.drawBox(12,50,(elapsedTime/(introTime/104)),11);
u8g2.setDrawColor(0);
for(int x{1};x<5;x++)
u8g2.drawBox(3+30*x,49,2,13);
u8g2.setDrawColor(1);
//end loading bar
u8g2.sendBuffer();
if(elapsedTime>introTime)
introSet = false;
}else{
u8g2.clearBuffer();
// clear the internal memory
// choose a suitable font
u8g2.setCursor(29,40);
u8g2.setFont(u8g2_font_7x13_tf);
u8g2.drawStr(100,40,"v");
u8g2.drawXBMP(0,0, VOLTS_width, VOLTS_height, VOLTS_bits);
float voltReading=analogRead(A7);
u8g2.setFont(u8g2_font_inb16_mr);
u8g2.print(voltReading/51);
//loading bar module
u8g2.drawFrame(10,48,108,15);
u8g2.drawBox(12,50,voltReading/8,11);
u8g2.setDrawColor(0);
for(int x{1};x<5;x++)
u8g2.drawBox(3+30*x,49,2,13);
u8g2.setDrawColor(1);
//end loading bar
// write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(100);
}//end if
}