#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 64
#define LOGO_WIDTH 128
// 'logo128x64 copy1', 128x64px
const unsigned char epd_bitmap_logo128x64 [] 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,
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, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x01, 0xff, 0xff, 0xf8, 0x00, 0xff, 0xff, 0x81, 0xff, 0xff, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xfc, 0x07, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xf0, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xf8, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xfe, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0x80, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xc0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xe0, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xc0, 0x01, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0x80, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x03, 0xc0, 0x00, 0x00, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xf0, 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, 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
};
int potentiometer = 0; // value from the potentiometer
char buffer[20]; // helper buffer for converting values into C-style string (array of chars)
int string_width;
float pixel_x = 0; // x pos for pixel
float pixel_y = 0; // y pos for pixel
float line_x = 0; // x pos for line end
float line_y = 0; // y pos for line end
float text_x = 0; // x pos for text
float text_y = 0; // y pos for text
int center_x = 64; // x center of the knob
int center_y = 113; // y center of the knob (outside of the screen)
int radius_pixel = 92; // radius for pixel tickmarks
int radius_line = 87; // radius for line end
int radius_text = 75; // radius for text
int16_t x, y; // Coordinates of the bounding box's upper-left corner
uint16_t width, height; // Width and height of the bounding box
int angle; // angle for the individual tickmarks
int tick_value; // numeric value for the individual tickmarks
byte precalculated_x_radius_pixel[180]; // lookup table to prevent expensive sin/cos calculations
byte precalculated_y_radius_pixel[180]; // lookup table to prevent expensive sin/cos calculations
unsigned long millis_time; // fps
unsigned long millis_time_last; // fps
int fps;
void testdrawbitmap(void) {
display.clearDisplay();
display.drawBitmap(0, 0,
epd_bitmap_logo128x64, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
delay(1000);
}
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
for (int i = 0; i < 180; i++) { // pre-calculate x and y positions into the look-up tables
precalculated_x_radius_pixel[i] = sin(radians(i-90)) * radius_pixel + center_x;
precalculated_y_radius_pixel[i] = -cos(radians(i-90)) * radius_pixel + center_y;
}
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
}
int progress = 0;
void loop() {
// Serial.println(progress);
potentiometer = analogRead(12);
display.clearDisplay();
display.setCursor(50, 0);
display.drawRoundRect(45, 0, 40, 15, 30, SSD1306_WHITE);
display.drawTriangle(64 - 3, 15, 64 + 3, 15, 64, 19, SSD1306_WHITE);
// display.fillRect(45, 2, 30, 10, SSD1306_INVERSE);
display.setCursor(50, 4);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
// dtostrf(progress, 1, 1, buffer);
potentiometer = map(potentiometer, 0, 4092, 0, 1000);
dtostrf(potentiometer / 10.0, 1, 1, buffer);
progress = potentiometer;
sprintf(buffer, "%s%s", buffer, "%");
display.println(buffer);
for (int i = -48; i <= 48; i = i + 3) {
angle = i + ((progress * 3) / 10) % 3;
tick_value = round((progress / 10.0) + angle / 3.0);
// pixel_x = sin(radians(angle)) * radius_pixel + center_x;
// pixel_y = -cos(radians(angle)) * radius_pixel + center_y;
pixel_x = precalculated_x_radius_pixel[angle+90]; // get x value from lookup table
pixel_y = precalculated_y_radius_pixel[angle+90]; // get y value from lookup table
if (pixel_x > 0 && pixel_x < 128 && pixel_y > 0 && pixel_y < 64) {
if(tick_value >= 0 && tick_value <= 100) {
if (tick_value % 10 == 0) { //draw lines
line_x = sin(radians(angle)) * radius_line + center_x;
line_y = -cos(radians(angle)) * radius_line + center_y;
display.drawLine(pixel_x, pixel_y, line_x, line_y, SSD1306_WHITE);
text_x = sin(radians(angle)) * radius_text + center_x;
text_y = -cos(radians(angle)) * radius_text + center_y;
itoa(tick_value, buffer, 10);
display.getTextBounds(buffer, 0, 0, &x, &y, &width, &height);
display.setCursor(text_x - width / 2, text_y);
display.println(buffer);
}
else { //small tickmark pixel
display.drawPixel(pixel_x, pixel_y, SSD1306_WHITE);
}
}
}
}
// // u8g2.setFont(u8g2_font_ncenB10_tr);
// // u8g.drawStr(25, 50, "Progress Bar");
// display.setTextSize(1/2); //fps test
// itoa(fps, buffer, 10);
// display.setCursor(0, 0);
// display.print(String(buffer));
// display.drawRect(0, 10, 128, 15, SSD1306_WHITE);
// // u8g.drawFrame(0, 10, 128, 20);
// // u8g.drawBox(10, 15, progress, 10);
// display.fillRect(10, 15, progress, 5, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
// delay(1000);
// if (progress < 108)
// {
// progress++;
// }
// else
// {
// progress = 0;
// }
// millis_time_last = millis_time;
// millis_time = millis();
// fps = round(1000.0 / (millis_time * 1.0 - millis_time_last));
}