/* 
  Millis() clock with timed event intervals.

  Set all values in seconds.
  - Set initial event start
  - Set event duration time, max is 999
  - Set event repeat time, max is 9999, 3600 = 1 hour

  Replace led with DC motor. Have motor run a small animation.
  Make a 'peanut' man and have him sawing wood.

*/


#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <TinyDebug.h>

#define led PB3

uint8_t width = 128;
uint8_t height = 64;

// Time variables
uint8_t second, minute, hour;

int eventCountdown = 0; // Time to next event countdown
int eventDurationCount = 0; // Event duration countdown
int firstInterval = 10; // First event starts at 15 sec
int eventDuration = 30; // Event lasts 10 sec
int eventRepeatInterval = 60; // Event repeats every 60 sec


void setup() {
  Debug.begin();
  pinMode(led, OUTPUT);
  oled.begin(width, height, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
  // Two rotations are supported,
  // The begin() method sets the rotation to 1.
  //oled.setRotation(0);

  // Some newer devices do not contain an external current reference.
  // Older devices may also support using the internal curret reference,
  // which provides more consistent brightness across devices.
  // The internal current reference can be configured as either low current, or high current.
  // Using true as the parameter value choses the high current internal current reference,
  // resulting in a brighter display, and a more effective contrast setting.
  oled.setInternalIref(true);

  // Two fonts are supplied with this library, FONT8X16 and FONT6X8
  // Other fonts are available from the TinyOLED-Fonts library
  oled.setFont(FONT8X16);

  oled.clear(); // Clear the memory before turning on the display
  oled.on(); // Turn on the display

  digitalWrite(led, LOW);
}


void loop() {
  // Get the current time in milliseconds and calculate seconds
  unsigned long currentMillis = millis();
  unsigned long elapsedSeconds = currentMillis / 1000;

  // Convert current time to hours, minutes, and seconds
  hour = (elapsedSeconds / 3600) % 12;  // 12-hour format
  minute = (elapsedSeconds / 60) % 60;
  second = elapsedSeconds % 60;

  // Calculate time to the next event
  eventCountdown = (eventRepeatInterval + firstInterval - (elapsedSeconds % eventRepeatInterval)) % eventRepeatInterval;


  Debug.print("firstInterval: "); Debug.println(firstInterval);
  Debug.print("eventCountdown: "); Debug.println(eventCountdown);
  Debug.print("eventRepeatInterval: "); Debug.println(eventRepeatInterval);
  Debug.print("eventDuration: "); Debug.println(eventDuration);
  Debug.print("eventDurationCount: "); Debug.println(eventDurationCount);


  // Check if the current second is within the event duration
  if ((elapsedSeconds % eventRepeatInterval) >= firstInterval &&
      (elapsedSeconds % eventRepeatInterval) < (firstInterval + eventDuration)) {
    eventDurationCount = firstInterval + eventDuration - (elapsedSeconds % eventRepeatInterval);
    digitalWrite(led, HIGH);
    Debug.println("LED ON");
  } else {
    eventDurationCount = 0;
    digitalWrite(led, LOW);
    Debug.println("LED OFF");
  }


  // Display the time
  oled.setCursor(0, 0);
  oled.print("Time");
  oled.setCursor(63, 0);
  if (hour < 10) oled.print("0");
  oled.print(hour); oled.print(":");
  if (minute < 10) oled.print("0");
  oled.print(minute); oled.print(":");
  if (second < 10) oled.print("0");
  oled.print(second);

  // Display time to the next event
  oled.setCursor(0, 2);
  oled.print("Next Event");
  oled.setCursor(95, 2);
  if (eventCountdown < 10) oled.print("0");
  if (eventCountdown < 100) oled.print("0");
  if (eventCountdown < 1000) oled.print("0");
  oled.print(eventCountdown);

  // Display remaining time for the current event
  oled.setCursor(0, 4);
  oled.print("Event On");
  oled.setCursor(103, 4);
  if (eventDurationCount < 10) oled.print("00");
  else if (eventDurationCount < 100) oled.print("0");
  oled.print(eventDurationCount);


  delay(10);

}
ATTINY8520PU