#include <Arduino.h>

#define X_DIR_IN 22
#define X_STP_IN 23
#define X_ENA_IN 24

#define Z_DIR_IN 25
#define Z_STP_IN 26
#define Z_ENA_IN 27

#define Y_DIR_IN 28
#define Y_STP_IN 29
#define Y_ENA_IN 30

#define A_DIR_IN 31
#define A_STP_IN 32
#define A_ENA_IN 33

// #define STEPPER_ALARM_IN 13
// #define Laser_IN 14
// #define SPINDLE_ENABLE_IN 15
// #define SPINDLE_DIR_IN 16

#define ACORN_Y_DIR_OUT 34
#define ACORN_Y_STP_OUT 35
#define ACORN_Y_ENA_OUT 36

#define ACORN_A_DIR_OUT 37
#define ACORN_A_STP_OUT 38
#define ACORN_A_ENA_OUT 39

#define GRBL_Y_DIR_OUT 40
#define GRBL_Y_STP_OUT 41
#define GRBL_Y_ENA_OUT 42

#define ACORN_X_DIR_OUT 43
#define ACORN_X_STP_OUT 44
#define ACORN_X_ENA_OUT 45


#define ACORN_Z_DIR_OUT 46
#define ACORN_Z_STP_OUT 47
#define ACORN_Z_ENA_OUT 48

#define GRBL_X_DIR_OUT 49
#define GRBL_X_STP_OUT 50
#define GRBL_X_ENA_OUT 51

#define GRBL_Z_DIR_OUT 2
#define GRBL_Z_STP_OUT 3
#define GRBL_Z_ENA_OUT 4


#define GRBL_A_DIR_OUT 5
#define GRBL_A_STP_OUT 6
#define GRBL_A_ENA_OUT 7



#define RE_COUNT 12

bool grblState = false;

struct pinRedirection
{
    uint8_t INPIN;
    uint8_t ACORN_OUT;
    uint8_t GRBL_OUT;
};

const pinRedirection r[RE_COUNT] = {
    
    { X_DIR_IN, ACORN_X_DIR_OUT, GRBL_X_DIR_OUT},
    { X_STP_IN, ACORN_X_STP_OUT, GRBL_X_STP_OUT},
    { X_ENA_IN, ACORN_X_ENA_OUT, GRBL_X_ENA_OUT},

    { Z_DIR_IN, ACORN_Z_DIR_OUT, GRBL_Z_DIR_OUT},
    { Z_STP_IN, ACORN_Z_STP_OUT, GRBL_Z_STP_OUT},
    { Z_ENA_IN, ACORN_Z_ENA_OUT, GRBL_Z_ENA_OUT},

    { Y_DIR_IN, ACORN_Y_DIR_OUT, GRBL_Y_DIR_OUT},
    { Y_STP_IN, ACORN_Y_STP_OUT, GRBL_Y_STP_OUT},
    { Y_ENA_IN, ACORN_Y_ENA_OUT, GRBL_Y_ENA_OUT},

    {A_DIR_IN, ACORN_A_DIR_OUT, GRBL_A_DIR_OUT},
    {A_STP_IN, ACORN_A_STP_OUT, GRBL_A_STP_OUT},
    {A_ENA_IN, ACORN_A_ENA_OUT, GRBL_A_ENA_OUT}
};

void setup(){
    Serial.begin(115200);
    for (size_t i = 0; i < RE_COUNT; i++)
    {
        pinMode(r[i].INPIN, INPUT);
        pinMode(r[i].ACORN_OUT, OUTPUT);
        if (r[i].GRBL_OUT > 0)
            pinMode(r[i].GRBL_OUT, OUTPUT);
    }
}

void loop(){
    if (Serial.available()){
        int x = Serial.read();
        grblState = (x == 7);
    }


    for (size_t i = 0; i < RE_COUNT; i++)
    {
       // uint8_t z = digitalRead(r[i].INPIN);
        uint8_t v = grblState ? r[i].GRBL_OUT : r[i].ACORN_OUT;
        if (v > 0)
            digitalWrite(v,  digitalRead(r[i].INPIN));
    }
}
A4988
A4988
A4988
A4988