// WIP, not fully adapted from the prototype yet, but the
// PEEP-poop-poop-poop-PEEP-poop-poop-poop is running :-) (if wobbly)

// #include <TM1637TinyDisplay.h>        //  Include 7-seg display driver
#include <EdgieD.h>                   //  Include Edge Detector Library
#include <LiquidCrystal_I2C.h>

#define SETBTN   2                     //  define set/func button
#define BUZZER   7                     //  define buzzer output
#define STTSTP  13                     //  define start/stop button
#define SETTING A7                     //  define settings/time sig pot
#define TEMPO   A6                     //  define tempo pot

int pitch  = 880;                     //  declare blip pitch for first beat
int tempo  = 285;                     //  declare start tempo
int beats  =   4;                     //  declare beats
int barLen =   4;                     //  declare bar length in beats
int count  =   1;                     //  declare beat count variable

uint32_t backThen = 0;                //  placeholder for previous time reading in main loop
uint32_t period = 60000000 / tempo;   //  calculate beat period      uS
uint32_t blipPrd = 21053;             //  calculate tone pulse width uS

bool state = false;                   //  declare state variable (tone off)
bool run   = false;                   //  declare run/stop variable (stopped)

//  instantiate classes
//  LCD_I2C will be here
Edge functBtn;
Edge ststpBtn;

void setup() {
  //  Fast analog read - speed up to 6.5 uS
  ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); //  clear prescaler bits
  ADCSRA |= bit (ADPS0) | bit (ADPS1);                  //  prescale by 8
  pinMode(SETBTN,INPUT_PULLUP);
  pinMode(STTSTP,INPUT_PULLUP);
  pinMode(BUZZER,OUTPUT);
  //  start display here
  //  welcome screen here
}

void loop() {
  //  check start/stop button
  if(ststpBtn.detect(digitalRead(STTSTP))){
    if(run) noTone(BUZZER);           //  if run == true, we're stopping all sound
    run = !run;                       //  if run, then not run, else not run becomes run
    count = 1;                        //  reset beat count
  }
  //  if run, we might make "bleep, blip, blip, blip"
  if(run){
    uint32_t rightNow = micros();     //  measure time this instant
    if(!state && rightNow - backThen >= period){    //  if !state and a beat has passed
      state = true;                   //  we make another bleep or blip"
      if(count > 1){
        pitch = 440;                  //  for 2, 3 or 4, etc we blip
      } else {
        pitch = 880;                  //  on the 1, we bleep
      }
      tone(BUZZER,pitch);             //  and this makes the bleeps and blips
      if(count >= 4){                 //  plus, for each bleep and blip we count beats
        count = 1;                    //  if it's beat 4, we reset count to 1
      } else {
        count++;                      //  otherwise we increment the count
      }
      backThen = rightNow;
    }
    //  if the current blip is long enough, we stop it
    if(state && micros() - backThen >= blipPrd){
      state = false;
      noTone(BUZZER);
    }
  }

  // read the tempo pot and set the tempo between 30 and 285 (255 steps)
  int pot = (analogRead(TEMPO) + 1) / 4 - 1 + 30;  // read pot value & calculate tempo
  if(pot != tempo) tempo = pot;       //  set new tempo,
  if(tempo > 285) tempo = 285;        //  keep tempo below 285 and
  if(tempo < 30) tempo = 30;          //  keep tempo above 30
  period = 60000000 / tempo;          //  calculate beat period      uS
  blipPrd = 21053;                    //  calculate tone pulse width uS
}