/*
  Boitarire software

  Plays a sound through an external MP3 module
  when triggered by external sensor : 
  - inclination switch
  - accelerometer module
  - touch sensors
  Configuration is selected through compilation switches.

  created 25 nov 2021
  by Florian Savard

  This example code is in the public domain.

  https://code.electrolab.fr/Flax/boitarire
*/

// Defines - Macros - Constants
#define SENSOR_INCLINATION
#define SENSOR_ACCELEROMETER
#define SENSOR_TOUCHSENSOR1
#define SENSOR_TOUCHSENSOR2
//#define MP3_RESET

// Other constants
#define PIN_SENSOR_POSITION 9     // Position sensor input
#define PIN_GROUND_SWITCH   7     // MP3 module ground switch command output
#define PLAY_BLANK_DELAY    1000  // Blanking when playing a sound (ms)
#define DELAY_MP3_RESET     100   // Time of ground cut of MP3 module for re-init (ms)
#define DEBOUNCE_DELAY      200   // Position sensor input debounce (ms)


// Typedefs
typedef enum 
{
  STM_INIT,
  STM_IDLE,
  STM_PLAY,
  STM_BLANK,
  STM_MP3_RESET,
} tStateMachine;

typedef enum
{
  STM_DEBOUNCE_IDLE_0,
  STM_DEBOUNCE_IDLE_1,
  STM_DEBOUNCE_DETECT_0,
  STM_DEBOUNCE_DETECT_1,
  STM_DEBOUNCE_DEBOUNCE_0,
  STM_DEBOUNCE_DEBOUNCE_1,
} tStateMachineDebounce;

// Local variables
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

unsigned long debounce_delay;
bool input_now;
unsigned long play_blank_delay, mp3_reset_delay;

// State machines states
tStateMachine stmState;
tStateMachineDebounce stmDebounceState;

// Local function prototypes
void ReadPlayState (void);
void WritePlay (void);

// Setup function
void setup() {
  // Initialize serial:
  Serial.begin(9600);
  // Reserve 200 bytes for the inputString:
  inputString.reserve(200);

  // Position sensor input pin - D9 - Input pull-up
  pinMode(PIN_SENSOR_POSITION, INPUT_PULLUP);

  // LED output for visualisation
  pinMode(LED_BUILTIN, OUTPUT);

  // MP3 module ground switch command - D7 - Output push-pull
  pinMode(PIN_GROUND_SWITCH, OUTPUT);

  play_blank_delay = 0;
  mp3_reset_delay = 0;
  stmState = STM_INIT;
  stmDebounceState = STM_DEBOUNCE_IDLE_0;
  debounce_delay = 0;
  input_now = 0;

  // Connect ground for MP3 module
  digitalWrite(PIN_GROUND_SWITCH, HIGH);
}

// Main loop
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);

    switch (inputString[0])
    {
      case 0x30:
        WritePlay();
        break;
      default:
        break;
    }
    
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
  
#ifdef SENSOR_INCLINATION
  // Read instantaneous value of position sensor
  input_now = digitalRead(PIN_SENSOR_POSITION);
#else
  input_now = 0;
#endif

  // Inclination debounce state machine
  //-----------------------------------
  switch (stmDebounceState)
  {
    case STM_DEBOUNCE_IDLE_0:
      if (input_now == 1)
      {
        stmDebounceState = STM_DEBOUNCE_DETECT_1;
        debounce_delay = millis();
      }
    break;

    case STM_DEBOUNCE_IDLE_1:
      if (input_now == 0)
      {
        stmDebounceState = STM_DEBOUNCE_DETECT_0;
        debounce_delay = millis();
      }
    break;

    case STM_DEBOUNCE_DETECT_0:
      if (input_now == 1)
      {
        stmDebounceState = STM_DEBOUNCE_IDLE_1;
      }
      else if ((millis() - debounce_delay) > DEBOUNCE_DELAY)
      {
        stmDebounceState = STM_DEBOUNCE_DEBOUNCE_0;
      }
    break;

    case STM_DEBOUNCE_DETECT_1:
      if (input_now == 0)
      {
        stmDebounceState = STM_DEBOUNCE_IDLE_0;
      }
      else if ((millis() - debounce_delay) > DEBOUNCE_DELAY)
      {
        stmDebounceState = STM_DEBOUNCE_DEBOUNCE_1;
      }
    break;

    case STM_DEBOUNCE_DEBOUNCE_0:
      stmDebounceState = STM_DEBOUNCE_IDLE_0;
      digitalWrite(LED_BUILTIN, LOW); // DEBUG
    break;

    case STM_DEBOUNCE_DEBOUNCE_1:
      stmDebounceState = STM_DEBOUNCE_IDLE_1;
      digitalWrite(LED_BUILTIN, HIGH); // DEBUG
    break;

    default:
    break;
  }
  
  // Main state machine
  //-------------------
  switch (stmState)
  {
    case STM_INIT:
      stmState = STM_IDLE;
    break;

    case STM_IDLE:
      if (stmDebounceState == STM_DEBOUNCE_DEBOUNCE_1)
      {
        stmState = STM_PLAY;
      }
    break;

    case STM_PLAY:
      WritePlay();
      play_blank_delay = millis();
      stmState = STM_BLANK;
    break;

    case STM_BLANK:
      if ((millis() - play_blank_delay) > PLAY_BLANK_DELAY)
      {
#ifdef MP3_RESET
        stmState = STM_MP3_RESET;
        mp3_reset_delay = millis();
        digitalWrite(PIN_GROUND_SWITCH, LOW);
#else
        stmState = STM_IDLE;
#endif
      }
    break;

    case STM_MP3_RESET:
      if ((millis() - mp3_reset_delay) > DELAY_MP3_RESET)
      {
        stmState = STM_IDLE;
        digitalWrite(PIN_GROUND_SWITCH, HIGH);
      }
    break;

    default:
      stmState = STM_INIT;
    break;
  }

}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void ReadPlayState (void)
{
  // Send Play State read request
  Serial.write(170); // 0xAA
  Serial.write(1);   // 0x01
  Serial.write(0);   // 0x00
  Serial.write(171); // 0xAB
}

void WritePlay (void)
{
  // Send Play request
  Serial.write(170); // 0xAA
  Serial.write(2);   // 0x02
  Serial.write(0);   // 0x00
  Serial.write(172); // 0xAC
}
