// File : Cascade165.ino
//
// Version 1, 5 August 2021, by Koepel
//     Initial version.
// Version 2, 5 August 2021, by Koepel
//     Layout of the wiring made better.
// Version 3, 13 August 2021, by Koepel
//     Changed 'SCK' to 'clockPin'.
// Version 4, 14 Sep 2023, by Gabriel Balbuena
//     Changed Arduino to ESP32
//
// Cascade of four 74HC165 shift-in registers.
// Only three pins are used on the ESP32 board, to read 32 switches.
//
// Using the 74HC165 is safe, because a pulse to the Latch pin 
// ('PL' on the 74HC165) will make a new start every time. 
// In case of an error or a wrong clock pulse by noise, 
// it synchronizes the data when inputs are read the next time.
//
// Based on:
//   (1)
//     Demo sketch to read from a 74HC165 input shift register
//     by Nick Gammon, https://www.gammon.com.au/forum/?id=11979
//   (2)
//     74HC165 Shift register input example
//     by Uri Shaked, https://wokwi.com/arduino/projects/306031380875182657

// Feature Toggles
const bool buzzerEnabled = true;

constexpr byte buzzer   = 23; // D23
constexpr byte latchPin = 14; // D14 latch the inputs into the registers
constexpr byte clockPin = 13; // D13
constexpr byte dataPin  = 12; // D12

// uint32_t oldOptionSwitch = 0;   // previous state of all the inputs
uint64_t optionSwitch1 = 0;
uint64_t optionSwitch2 = 0;
uint64_t oldOptionSwitch1 = 0;   
uint64_t oldOptionSwitch2 = 0;  


const int pulseWidth = 10;      // pulse width in microseconds

void setup () {
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  pinMode(buzzer, OUTPUT);

  Serial.println("Turn on and off the switches");
  Serial.println("Top row is switch 0 (right) to switch 7 (left)");
  Serial.println("Second row is 8 to 15, and so on");

  pinMode(clockPin, OUTPUT); // clock signal, idle LOW
  pinMode(latchPin, OUTPUT); // latch (copy input into registers), idle HIGH
  digitalWrite(latchPin, HIGH);
}

void loop () {
  // Give a pulse to the parallel load latch of all 74HC165
  digitalWrite(latchPin, LOW);    
  delayMicroseconds(pulseWidth);
  digitalWrite(latchPin, HIGH);

  // Reading one 74HC165 at a time and combining them into a 32 bit variable
  // The last 74HC165 is at the bottom, but the switches start numbering
  // at the top. So the first byte has to be shifted into the highest place.
  // for( int i=32; i>=0; i-=8) { // uint32_t -> uint64 (5轮循环!没错读取8位 )
  //   optionSwitch |= ((uint64_t) ReadOne165()) << i;
  // }

  optionSwitch1 = 0;
  optionSwitch2 = 0;
  // Read data from the shift registers
  for (int i = 0; i < 72; i += 8) {
    uint8_t byteValue = ReadOne165();
    if (i < 64) {
      optionSwitch1 |= ((uint64_t)byteValue << (56 - i));
    } else {
      optionSwitch2 |= ((uint64_t)byteValue << (72 - i - 8));
    }
  }



  // for( int i = 0; i<40; i++) { // 32 -> 40
  //   if( bitRead( optionSwitch, i) != bitRead( oldOptionSwitch,i)) {
  //     Serial.print( "Switch ");
  //     if ( i < 10) {
  //       Serial.print( " ");
  //     }

  //     Serial.print(i);
  //     Serial.print(" is now ");
  //     // 为了简单的测试起见 直接只输出按下的状态
  //     if(bitRead( optionSwitch, i) == 0) {
  //       Serial.println("down ↓");
  //     }
  //     // Serial.println(bitRead( optionSwitch, i) == 0 ? "down ↓" : "up   ↑");

  //     if (buzzerEnabled) {
  //       if (bitRead(optionSwitch, i) != 0) {
  //         play(i);
  //       } else {
  //         noTone(buzzer);
  //       } 
  //     }
  //   }
  // }
  
 // Process changes in switch states
  for (int i = 0; i < 72; i++) {
    bool current = bitRead((i < 64 ? optionSwitch1 : optionSwitch2), (i % 64));
    bool previous = bitRead((i < 64 ? oldOptionSwitch1 : oldOptionSwitch2), (i % 64));

    if (current != previous) {
      Serial.print("Switch ");
      if (i < 10) {
        Serial.print(" ");
      }

      Serial.print(i);
      Serial.print(" is now ");
      if(current == 0) {  // assuming 0 means 'down'
        Serial.println("down ↓");
      } else {
        Serial.println("up ↑");
      }
    }
  }


  oldOptionSwitch1 = optionSwitch1;
  oldOptionSwitch2 = optionSwitch2;
  delay(25); // slow down the sketch to avoid switch bounce
}

// The ReadOne165() function reads only 8 bits,
// because of the similar functions shiftIn() and SPI.transfer() 
// which both use 8 bits.
//
// The shiftIn() can not be used here, because the clock is set idle low
// and the shiftIn() makes the clock high to read a bit.
// The 74HC165 require to read the bit first and then give a clock pulse.
//
byte ReadOne165() {
  byte ret = 0x00;

  // The first one that is read is the highest bit (input D7 of the 74HC165).
  for( int i=7; i>=0; i--) {
    if( digitalRead( dataPin) == HIGH) {
      bitSet( ret, i);
    }

    digitalWrite(clockPin, HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(clockPin, LOW);
  }

  return( ret);
}

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define NOTE_E8  5274
#define NOTE_F8  5588
#define NOTE_FS8 5920
#define NOTE_G8  6272
#define NOTE_GS8 6645
#define NOTE_A8  7040
#define NOTE_AS8 7459
#define NOTE_B8  7902

// Assuming a 31-button accordion
// #define BUTTON_COUNT 31
#define BUTTON_COUNT 72 // 改31 -> 40


typedef struct {
    int pushTone;
    int pullTone;
    int buttonId; // Optionally, if you want to store button ID in the struct
} Button;

// Create an array of Button objects to represent the accordion's layout
Button FINGER_LAYOUT[BUTTON_COUNT] = {
    // G Row (10 buttons)
    { NOTE_G3, NOTE_B3, 1 },
    { NOTE_A3, NOTE_C4, 2 },
    { NOTE_B3, NOTE_D4, 3 },
    { NOTE_C4, NOTE_E4, 4 },
    { NOTE_D4, NOTE_F4, 5 },
    { NOTE_E4, NOTE_G4, 6 },
    { NOTE_F4, NOTE_A4, 7 },
    { NOTE_G4, NOTE_B4, 8 },
    { NOTE_A4, NOTE_C5, 9 },
    { NOTE_B4, NOTE_D5, 10 },
    
    // C Row (11 buttons)
    { NOTE_C4, NOTE_E4, 11 },
    { NOTE_D4, NOTE_F4, 12 },
    { NOTE_E4, NOTE_G4, 13 },
    { NOTE_F4, NOTE_A4, 14 },
    { NOTE_G4, NOTE_B4, 15 },
    { NOTE_A4, NOTE_C5, 16 },
    { NOTE_B4, NOTE_D5, 17 },
    { NOTE_C5, NOTE_E5, 18 },
    { NOTE_D5, NOTE_F5, 19 },
    { NOTE_E5, NOTE_G5, 20 },
    { NOTE_F5, NOTE_A5, 21 },

    // D Row (10 buttons)
    { NOTE_D4, NOTE_F4, 22 },
    { NOTE_E4, NOTE_G4, 23 },
    { NOTE_F4, NOTE_A4, 24 },
    { NOTE_G4, NOTE_B4, 25 },
    { NOTE_A4, NOTE_C5, 26 },
    { NOTE_B4, NOTE_D5, 27 },
    { NOTE_C5, NOTE_E5, 28 },
    { NOTE_D5, NOTE_F5, 29 },
    { NOTE_E5, NOTE_G5, 30 },
    { NOTE_F5, NOTE_A5, 31 },
    
    // D1 Row (10 buttons)
    { NOTE_D4, NOTE_F4, 32 },
    { NOTE_E4, NOTE_G4, 33 },
    { NOTE_F4, NOTE_A4, 34 },
    { NOTE_G4, NOTE_B4, 35 },
    { NOTE_A4, NOTE_C5, 36 },
    { NOTE_B4, NOTE_D5, 37 },
    { NOTE_C5, NOTE_E5, 38 },
    { NOTE_D5, NOTE_F5, 39 },
    { NOTE_E5, NOTE_G5, 40 },

    { NOTE_D4, NOTE_F4, 41 },
    { NOTE_D4, NOTE_F4, 42 },
    { NOTE_E4, NOTE_G4, 43 },
    { NOTE_F4, NOTE_A4, 44 },
    { NOTE_G4, NOTE_B4, 45 },
    { NOTE_A4, NOTE_C5, 46 },
    { NOTE_B4, NOTE_D5, 47 },
    { NOTE_C5, NOTE_E5, 48 },
    { NOTE_D5, NOTE_F5, 49 },
    
    { NOTE_E5, NOTE_G5, 50 },
    { NOTE_E5, NOTE_G5, 51 },
    { NOTE_D4, NOTE_F4, 52 },
    { NOTE_E4, NOTE_G4, 53 },
    { NOTE_F4, NOTE_A4, 54 },
    { NOTE_G4, NOTE_B4, 55 },
    { NOTE_A4, NOTE_C5, 56 },
    { NOTE_B4, NOTE_D5, 57 },

    { NOTE_C5, NOTE_E5, 58 },
    { NOTE_D5, NOTE_F5, 59 },
    { NOTE_E5, NOTE_G5, 60 },
    { NOTE_G4, NOTE_B4, 61 },
    { NOTE_A4, NOTE_C5, 61 },
    { NOTE_B4, NOTE_D5, 63 },
    { NOTE_C5, NOTE_E5, 64 },
    { NOTE_D5, NOTE_F5, 65 },

    { NOTE_C4, NOTE_E4, 67 },
    { NOTE_D4, NOTE_F4, 68 },
    { NOTE_E4, NOTE_G4, 69 },
    { NOTE_F4, NOTE_A4, 70 },
    { NOTE_G4, NOTE_B4, 71 },
    { NOTE_A4, NOTE_C5, 72 }
};

void play(int16_t btnId) {
  int freq = FINGER_LAYOUT[btnId].pullTone;
  tone(buzzer, freq);
}
74HC165
74HC165
74HC165
74HC165
74HC165
74HC165
74HC165
74HC165
74HC165