/*
   parking-access-s01 is demo application of parking access.
   https://programmersqtcpp.blogspot.com/2022/03/applicazione-accesso-parcheggio-entry.html
   
   Copyright (C) 2023 Maurilio Pizzurro 

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

/* 
    Pulsante verde per simulare la barriera IR
    Pulsante giallo per simulare il contatto chiave
*/

//#include <IRremote.h>
//#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>


#include <Servo.h>
#include <JC_Button.h>
// Funzioni come s_start(), s_inOpening() sono dichiarate in "states.h"
#include "states.h"
// Macro come S_START, S_INOPENING sono definite in "macros.h"
#include "macros.h"
// Funzioni come lcdPrintPos(), lcdPrintState ecc sono dichiarate in "helperfunc.h"
#include "helperfunc.h"

#include "fsm.h"
#include "safety.h"

#define PIN_RECEIVER 2   // Signal Pin of IR receiver

//IRrecv receiver(PIN_RECEIVER);
Servo RCservo;
Servo RCservo1;
//                RS, E,  D4, D5, D6, D7
//LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
LiquidCrystal_I2C lcd(0x27, 16, 2);

/* PIN_IR_BARRIER contatto pulito relay IR RX */
const byte PIN_IR_BARRIER   = 4;
const byte PIN_EXT_IR_BARRIER   = 7;


/* PIN_KEY contatto pulito comando chiave */
const byte PIN_KEY          = 6;


uint8_t     g_servoPos    = SERVO_SX_CLOSE;
uint8_t     g_servoPos1   = SERVO_DX_CLOSE;

uint8_t     g_saveCommand;

Button irBarrier(PIN_IR_BARRIER, 55, true, true); // 55ms debounce
//Button irBarrier(PIN_IR_BARRIER, 55, true, true); // 55ms debounce
Button keyButton(PIN_KEY, 55); // 55ms debounce
Button extIrBarrier(PIN_EXT_IR_BARRIER, 55, true, true);
//Button extIrBarrier(PIN_EXT_IR_BARRIER, 55, true, true);
FsmData mainFsm;

void setup() {
    
    //irBarrier.begin();
    keyButton.begin();
    //extIrBarrier.begin();

    RCservo.write(g_servoPos);
    RCservo1.write(g_servoPos1);  
        
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(A0, OUTPUT);

    //receiver.enableIRIn(); // Start the receiver

    
    lcd.init();        // initialize the lcd 
    lcd.backlight();

    Serial.begin(115200);
    Serial.print("Press play or\nyellow button\n");
    Safety::init();
    /*Safety::init();
    while(Safety::power(Safety::ON));
    Serial.println("foto on");*/
           
    RCservo.attach(5);
    RCservo1.attach(A1);
    
    lcd.setCursor(0, 1);
    lcd.print((char)223);
    lcd.setCursor(6,1);
    lcd.print("Tms: ");
    lcdPrintPos(g_servoPos);
    // inizializza la fsm con S_START stato iniziale 
    fsmInit(mainFsm, S_START);
           
}


void loop() {
    //irBarrier.read();
    keyButton.read();
    //extIrBarrier.read();
    // blocchiamo il lampeggio (vedi case S_ISBLOCK)
    uint8_t localCstate = fsmGetCurrentState(mainFsm);
    if (localCstate != S_ISBLOCK) {
        flashingLight(localCstate);
    }
    uint8_t command = 0;

    /*if (receiver.decode()) {
        command = receiver.decodedIRData.command;
        receiver.resume();  // Receive the next value
        //Serial.println(command);
    } else*/ if (keyButton.wasPressed()) {
        command = IRCMD_PLAY;
    } 

    if(fsmIsChangedState(mainFsm)) {
        lcdPrintState(localCstate);
    }

        
    switch (localCstate) {
        
        case S_START:

            //s_start(command);
            testCustomFtc(command);
               
            break;

        case S_INOPENING:

            //s_inOpening(command);
            
            break;

        case S_ISOPEN:

            //s_isOpen(command);
                        
            break;

        case S_INCLOSING:

            //s_inClosing(command);
            
            break;

        case S_ISBLOCK:

            //s_isBlock(command);

            break;

    } // end switch
    
} // end loop()

ftcrxBreakout
ftcrxBreakout