// simple project using ATTINY85 on Digispark board and SSD1306 128x64pxIIC OLED Display to show turbo gauge indicator

// created by upir, 2023
// youtube channel: https://www.youtube.com/upir_upir

// YOUTUBE VIDEO: https://youtu.be/8IKBzZKTgD0
// Same project using Arduino UNO: https://youtu.be/JXmw1xOlBdk

// Links from the video:
// Attiny85 Digispark board: https://s.click.aliexpress.com/e/_DC6lBqT
// SSD1306xled library: https://github.com/tinusaur/ssd1306xled
// Digistump drivers: https://github.com/digistump/DigistumpArduino/releases
// Additional boards json file: http://digistump.com/package_digistump_index.json
// 128x64 SSD1306 OLED Display 1.54": https://s.click.aliexpress.com/e/_DCYdWXb
// Colorful breadboards: https://s.click.aliexpress.com/e/_DmQH8V9
// Potentiometer 10K: https://s.click.aliexpress.com/e/_DkU6R5D
// Colorful knobs: https://s.click.aliexpress.com/e/_DlOIaoj
// Image2cpp (convert array to image): https://javl.github.io/image2cpp/
// Photopea (online graphics editor like Photoshop): https://www.photopea.com/

// Related videos with Arduino UNO and 128x64 OLED screen:
// Arduino + OLED displays: https://www.youtube.com/playlist?list=PLjQRaMdk7pBZ1UV3IL5ol8Qc7R9k-kwXA

#include <Arduino.h>
//#include <font6x8.h> - fonts for the ssd1306xled library, but we are not drawing any strings
//#include <font8x16.h> - fonts for the ssd1306xled library, but we are not drawing any strings
#include <ssd1306xled.h>
#include "image.h" // fullscreen image for the turbo gauge, generated by image2cpp website

// default PIN configuration for OLED display and ATTINY85 is:
// VCC ---- vcc
// GND ---- gnd
// SCL ---- pb2
// SDA ---- pb0

// animated piece of the turbo icon (3 frames)
const unsigned char epd_bitmap_icon_anim_01 [] PROGMEM = {  0x42, 0xc3, 0x3c, 0x3c, 0x3c, 0x3c, 0xc3, 0x42};
const unsigned char epd_bitmap_icon_anim_02 [] PROGMEM = {  0x42, 0xa9, 0x1a, 0x7c, 0x3e, 0x58, 0x95, 0x42};
const unsigned char epd_bitmap_icon_anim_03 [] PROGMEM = {  0x42, 0x95, 0x58, 0x3e, 0x7c, 0x1a, 0xa9, 0x42};

const unsigned char* icon_anim_array[3] = {
  epd_bitmap_icon_anim_01,
  epd_bitmap_icon_anim_02,
  epd_bitmap_icon_anim_03
};

byte icon_anim_frame = 0; // current frame

const uint8_t gauge_fill [] PROGMEM = { B11111111 };   // image for fully filled gauge
const uint8_t gauge_empty [] PROGMEM = { B00000000 };  // image for fully empty gauge
const uint8_t gauge_red_a [] PROGMEM = { B01010101 };  // image for red area (checkerboard)
const uint8_t gauge_red_b [] PROGMEM = { B10101010 };  // image for red area (checkerboard)

int gauge_width = 30; // width of the gauge - size could be from 0-122px

void setup() {
  _delay_ms(40);
  SSD1306.ssd1306_init(); // display initialization

  SSD1306.ssd1306_draw_bmp(0, 0, 128, 8, turbo_gauge_128x64_image); // draw fullscreen 128x64 image, only once
  pinMode(A2, INPUT); // set pin A2 (pin PB4) as input to measure voltage - potentiometer value
}

void loop() {

  int pot_value = analogRead(A2); // read potentiometer value, 0-1023
  gauge_width = map(pot_value, 0, 1023, 0, 112); // remap the potentiometer value between values 0-122px (max gauge width)

  for (int i = 0; i < 112; i++) { // go over entire width of the gauge
    if (i < gauge_width) { // if the current position is smaller than gauge width
      SSD1306.ssd1306_draw_bmp(8 + i, 2, 8 + i + 1, 3, gauge_fill);  // draw filled gauge
    } else { // not the filled gauge - could be the empty gauge or checkerboard
      if (i > 88) { // draw checkerboard
        SSD1306.ssd1306_draw_bmp(8 + i, 2, 8 + i + 1, 3, i % 2 ? gauge_red_a : gauge_red_b); // draw checkerboard, toggle between two images based on the x position
      } else { // draw empty gauge
        SSD1306.ssd1306_draw_bmp(8 + i, 2, 8 + i + 1, 3, gauge_empty);  // draw empty gauge
      }
    }
  }

  SSD1306.ssd1306_draw_bmp(5 * 8, 5, 6 * 8, 6, icon_anim_array[icon_anim_frame]); // draw the animation part of the turbo gauge
  icon_anim_frame = (icon_anim_frame + 1) % 3; // increase the current frame, go over values 0-2


  //  gauge_width = (gauge_width + 1) % 112; // increase the gauge_width between 0-112px
}
ATTINY8520PU