#include "Arduino.h"
//#include "ledControl.h"
#include "waiting.h"
#include "tapeLED.h"
#include "lantan.h"
#include "list.h"

#define sinDeg(a) sin(a * PI / 180.0)
#define cosDeg(a) cos(a * PI / 180.0)
#define tanDeg(a) tan(a * PI / 180.0)
#define pow10(a) pow(10, (a))

#define ON  HIGH
#define OFF LOW

const word FREQ[] = {
  130, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
  262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523,
  554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109,
  1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217,
  2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 
};

void ledOn(int pin, int onoff) {
    noTone(pin);
    pinMode(pin, OUTPUT);
    digitalWrite(pin, onoff);
}

void led(float red, float green, float blue) {
    byte r = min(max(0, round(red)), 255);
    byte g = min(max(0, round(green)), 255);
    byte b = min(max(0, round(blue)), 255);
    analogWrite(9, r);
    analogWrite(6, g);
    analogWrite(10, b);
}

void ledControl(long color) {
  byte red = color >> 16 & 0xff;
  byte green = color >> 8 & 0xff;
  byte blue = color & 0xff;
  analogWrite(9, red);
  analogWrite(6, green);
  analogWrite(10, blue);
}

void buzzerControl(int pin, int index) {
  tone(pin, FREQ[index - 48]);
}

void buzzerOff(int pin) {
  noTone(pin);
}

int getDigital(int pin) {
  pinMode(pin, INPUT_PULLUP);
  return digitalRead(pin);
}

int getAnalog(int pin) {
  pinMode(pin, INPUT);
  return analogRead(pin);
}

float getTemperature(int pin) {
  pinMode(pin, INPUT);
  return (((analogRead(pin) / 1024.0) * 3.3) - 0.5) / 0.01;
}

void initUserApp() {
    //####  On-board features  ####
    pinMode(A0, INPUT_PULLUP);
    waiting(500);
}
// ---------------------------------------
// prototype declaration
// ---------------------------------------

// ---------------------------------------
// Global variable
// ---------------------------------------
TapeLED tapeLED_A2(A2);
// ---------------------------------------
// mainroutine
// ---------------------------------------
void startUserApp() {
    led(100,255,0);
    analogWrite(5, round(min(max(2.55 *100, 0), 255)));
    if (!waiting(1* 1000UL)) return;
    analogWrite(9, 0); analogWrite(6, 0); analogWrite(10, 0);
    analogWrite(5, 0);
    for (;;) { check(); }
}

