// https://forum.arduino.cc/t/problemi-con-la-condizione/1130482
// Dalla descrizione sembra si tratti di un montacarichi
// teorico, quindi niente parti mobili.
// Link articolo: https://programmersqtcpp.blogspot.com/2023/05/montacarichi-con-arduino.html
#include <SSD1306Ascii.h>
#include <SSD1306AsciiAvrI2c.h>
#include <JC_Button.h>
#include <LedControl.h>

#define CS_PIN  10
#define CLK_PIN 13
#define DIN_PIN 11
// font System5x7 
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
SSD1306AsciiAvrI2c oled;
#define MAX_N_PIANI     4
#define MAX_SEG MAX_N_PIANI

#define PIN_LEFT_BTN    2
#define PIN_RIGTH_BTN   3
#define PIN_UP_BTN      4
#define PIN_DWN_BTN     5
#define PIN_SET_BTN     6

Button leftBtn(PIN_LEFT_BTN);
Button rightBtn(PIN_RIGTH_BTN);
Button upBtn(PIN_UP_BTN);
Button downBtn(PIN_DWN_BTN);
Button setBtn(PIN_SET_BTN);

LedControl mat = LedControl(DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG);

const byte grow = 7;
const byte gcol = 1;

byte row = 0;
byte col = 0;

byte lastMatrix;
byte piano;
byte oldPiano = 255;
byte oldRow = 255;
byte oldCol = 255;
bool shiftKey;
uint32_t shiftTimer;


void setLight(byte matrix, byte row, byte col) {
    if (row != oldRow || col != oldCol || piano != oldPiano) {
        mat.clearDisplay(lastMatrix);
        lastMatrix = matrix;
        byte lrow = (grow - 1) - (row * 2);
        byte lcol = gcol + (col * 2);
        mat.setRow(matrix, lrow - 1, _BV(lcol) | _BV(lcol + 1));
        mat.setRow(matrix, lrow, _BV(lcol) | _BV(lcol + 1));
        oldRow = row;
        oldCol = col;
        oldPiano = piano;
    }
}

void setup() {

    Serial.begin(115200);
    leftBtn.begin();
    rightBtn.begin();
    upBtn.begin();
    downBtn.begin();
    setBtn.begin();

    mat.shutdown(0, false);
    mat.setIntensity(0, 7);
    mat.clearDisplay(0);
    
    oled.begin(&Adafruit128x64, I2C_ADDRESS);
    // prima di scrivere su oled seleziona un font
    oled.clear();
    oled.setFont(System5x7);
               
}

// visualizza lo stasto di capslock
void oledShift() {
    oled.setCursor(85, 6);
    if (!shiftKey)
        oled.print("     ");
    else    
        oled.print("shift");
}

void loop() {

    rightBtn.read();
    leftBtn.read();
    upBtn.read();
    downBtn.read();
    setBtn.read();
    
    setLight(piano, row, col);
    
    if (rightBtn.wasPressed()) {
        
        if (col < 2) {
            col++;
        }
    }

    if (leftBtn.wasPressed()) {
        
        if (col > 0) {
            col--;
        } 
    }

    if (upBtn.wasPressed()) {
        if (!shiftKey) {
            if (row < 2) {
                row++;
                //lastPin = select(row, col);
            }
        } else {
            // sali di piano
            if (piano < MAX_N_PIANI - 1) {
                piano++;
            }
        }
    }

    if (downBtn.wasPressed()) {
        if (!shiftKey) {
            if (row > 0) {
                row--;
            }
        } else {
            if (piano > 0) {
                piano--;
            }
        }
    }

    // abilita capslock per tasti doppia funzione
    if (setBtn.wasPressed()) {
        shiftKey = !shiftKey;
        oledShift();
        shiftTimer = millis();
    }
    // auto disabilita capslock dopo 2 secondi
    if (shiftKey) {
        if (millis() - shiftTimer > 2000) {
            shiftKey = false;
            oledShift();
        }
    }
    
}