#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "MD_RobotEyes.h"

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 2

#define CLK_PIN   13  // or SCK
#define DATA_PIN  11  // or MOSI
#define CS_PIN    10  // or SS

MD_MAX72XX M = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MD_RobotEyes E;

typedef struct
{
  char name[7];
  MD_RobotEyes::emotion_t e;
  uint16_t timePause;  // in milliseconds
  const char *englishName;  // English translation
} sampleItem_t;

const sampleItem_t eSeq[] =
{
  { "Nutral", MD_RobotEyes::E_NEUTRAL, 8000, "Neutral" },
  { "Blink" , MD_RobotEyes::E_BLINK, 1000, "Blink" },
  { "Wink"  , MD_RobotEyes::E_WINK, 1000, "Wink" },
  { "Left"  , MD_RobotEyes::E_LOOK_L, 1000, "Left" },
  { "Right" , MD_RobotEyes::E_LOOK_R, 1000, "Right" },
  { "Up"    , MD_RobotEyes::E_LOOK_U, 1000, "Up" },
  { "Down"  , MD_RobotEyes::E_LOOK_D, 1000, "Down" },
  { "Angry" , MD_RobotEyes::E_ANGRY, 1000, "Angry" },
  { "Sad"   , MD_RobotEyes::E_SAD, 1000, "Sad" },
  { "Evil"  , MD_RobotEyes::E_EVIL, 1000, "Evil" },
  { "Evil2" , MD_RobotEyes::E_EVIL2, 1000, "Evil2" },
  { "Squint", MD_RobotEyes::E_SQUINT, 1000, "Squint" },
  { "Dead"  , MD_RobotEyes::E_DEAD, 1000, "Dead" },
  { "ScanV" , MD_RobotEyes::E_SCAN_UD, 1000, "ScanV" },
  { "ScanH" , MD_RobotEyes::E_SCAN_LR, 1000, "ScanH" },
};

// Initialize the LCD with the I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD I2C address

void setup()
{
  lcd.init();  // Initialize the LCD
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("RobotEyes");

  M.begin();
  E.begin(&M);
  do { } while (!E.runAnimation()); // wait for the text to finish
}

void loop()
{
  static uint32_t timeStartDelay;
  static uint8_t index = ARRAY_SIZE(eSeq);
  static enum { S_IDLE, S_TEXT, S_ANIM, S_PAUSE } state = S_IDLE;

  bool b = E.runAnimation();    // always run the animation

  switch (state)
  {
  case S_IDLE:
    index++;
    if (index >= ARRAY_SIZE(eSeq)) 
      index = 0;
    E.setText(eSeq[index].name);
    
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("Action: ");  // Action:
    lcd.print(eSeq[index].englishName);  // English translation
    lcd.setCursor(0, 2);
    lcd.print(" by Kai_29 alonwolf "); 
    lcd.setCursor(4, 3);
    lcd.print(" :ROBOT EYE "); 
    state = S_TEXT;
    break;

  case S_TEXT: // wait for the text to finish
    if (b)  // text animation is finished
    {
      E.setAnimation(eSeq[index].e, true);
      state = S_ANIM;
    }
    break;

  case S_ANIM:  // checking animation is completed
    if (b)  // animation is finished
    {
      timeStartDelay = millis();
      state = S_PAUSE;
    }
    break;
  
  case S_PAUSE: // non blocking waiting for a period between animations
    if (millis() - timeStartDelay >= eSeq[index].timePause)
      state = S_IDLE;
    break;

  default:
    state = S_IDLE;
    break;
  }
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
matrix1:V+
matrix1:GND
matrix1:DIN
matrix1:CS
matrix1:CLK
matrix1:V+.2
matrix1:GND.2
matrix1:DOUT
matrix1:CS.2
matrix1:CLK.2
matrix2:V+
matrix2:GND
matrix2:DIN
matrix2:CS
matrix2:CLK
matrix2:V+.2
matrix2:GND.2
matrix2:DOUT
matrix2:CS.2
matrix2:CLK.2
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL