#define EN_PIN 17   //  D1
#define DIR_PIN 3   //  D2
#define STEP_PIN 1  //  D3
#define CS_PIN 16   // D8
#define MOSI_PIN 23 //  D7
#define MISO_PIN 19 //   D6
#define SCK_PIN 18  //   D5

// bool dir = true;
// #include <TMC2130Stepper.h>
// TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);

// void setup() {
//   Serial.begin(115200);
//   while(!Serial);
//   Serial.println("Start...");
//   driver.begin();       // Initiate pins and registeries
//   driver.rms_current(1200);  // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
//   driver.stealthChop(1);  // Enable extremely quiet stepping
//   driver.microsteps(0);
//   Serial.println(driver.microsteps()); // shows microsteps setting
//   digitalWrite(EN_PIN, LOW);

//   Serial.print("DRV_STATUS=0b");
//   Serial.println(driver.DRV_STATUS(), BIN);
// }

// void loop() {
//   digitalWrite(STEP_PIN, HIGH);
//   //delayMicroseconds(10);
//   delay(50);
//   digitalWrite(STEP_PIN, LOW);
//   //delayMicroseconds(10);
//   delay(50);
//   uint32_t ms = millis();
//   static uint32_t last_time = 0;
//   if ((ms - last_time) > 3000) {
//     if (dir) {
//       Serial.println("Dir -> 0");
//       driver.shaft_dir(0);
//     } else {
//       Serial.println("Dir -> 1");
//       driver.shaft_dir(1);
//     }
//     dir = !dir;
//     last_time = ms;
//   }
// }

bool dir = true;
#define TMC2130DEBUG
#include <TMC2130Stepper.h>
// Soft SPI
TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);
// Hard SPI
//TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN);
void setup()
{
    Serial.begin(9600);
    while (!Serial)
        ;
    Serial.println("Start...");
    driver.begin();           // Initiate pins and registeries
    driver.rms_current(1200); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
    driver.microsteps(16);   
    driver.stealthChop(1); // Enable extremely quiet stepping

    digitalWrite(EN_PIN, LOW);

    Serial.print("DRV_STATUS=0b");
    Serial.println(driver.DRV_STATUS(), BIN);
}
bool isKilled = false;



void loop()
{
    if (driver.checkOT())
    {
        if (!isKilled)
        {   
            Serial.print("Overheat flag triggered: ");
            Serial.println(driver.getOTPW(), DEC);
            driver.TPOWERDOWN();
            isKilled = true;
            Serial.println("Program stopped"); 
        }
        else
        {
            delay(1000); 
        }
    }
    else
    {
        digitalWrite(STEP_PIN, HIGH);
        delayMicroseconds(10);
        digitalWrite(STEP_PIN, LOW);
        delayMicroseconds(10);
 
        uint32_t ms = millis();
        static uint32_t last_time = 0;
        if ((ms - last_time) > 500) 
        {
            if (dir)
            {
                Serial.println("Dir -> 0");
                driver.shaft_dir(0);
            }
            else 
            {
                Serial.println("Dir -> 1");
                driver.shaft_dir(1);
            }
            Serial.print("OverTemperature: ");
            Serial.print(driver.getOTPW(), DEC);
            Serial.print(" MSTEP: ");
            Serial.print(driver.microsteps(), DEC);
            Serial.print(" DIR: ");
            Serial.println(driver.dir(), DEC);
            dir = !dir;
            last_time = ms;
        }
    }
}