/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */

/*
 * ----------------------------------------------------------------------------------
 * The code in this file is copied (with a slight modifications) from:
 *     Schedule.h - allow to trigger from SYS stack a registered user code to be
 *                  then executed in user stack (CONT stack) without the common
 *                  restrictions from system context
 * ----------------------------------------------------------------------------------
 * Copyright (C) 2020 esp8266/Arduino
 *
 * This file is part of the esp8266 core for Arduino environment.
 *
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 * ----------------------------------------------------------------------------------
 */


#include <eCxx/eCxx_Internal.h>


#ifndef ECXX_PLATFORM_NODEMCU_SCHEDULE_H
#define ECXX_PLATFORM_NODEMCU_SCHEDULE_H


namespace eCxx {

namespace NodeMCU {


#if 0

    // NOTE : Using eCxx::Ext::function<T> here would produce a larger
    //        and most likely less efficient executable/binary

    #define __ECXX_NODEMCU_SCHEDULE_USE_ECXX_FUNCTION

    using ScheduledFunctionalVoid = eCxx::Ext::function<void(void)>;
    using ScheduledFunctionalBool = eCxx::Ext::function<bool(void)>;

#else

    #define __ECXX_NODEMCU_SCHEDULE_USE_STD_FUNCTION

    using ScheduledFunctionalVoid = std::function<void(void)>;
    using ScheduledFunctionalBool = std::function<bool(void)>;

#endif


extern bool ICACHE_RAM_ATTR scheduleFunction(const ScheduledFunctionalVoid& fn);
extern bool ICACHE_RAM_ATTR scheduleRecurrentFunction(const ScheduledFunctionalBool& fn, uint32_t repeatUS, const ScheduledFunctionalBool& alarm = nullptr);

extern void runScheduledFunctions();
extern void runScheduledRecurrentFunctions();


} // namespace NodeMCU

} // namespace eCxx


#endif
