From e786dc2da131c884465bb9e8eb73032558ff9f80 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 25 Jun 2019 11:07:41 +0200 Subject: [PATCH 01/14] Branch had become mangled with lots of unrelated changes - reduced to core changeset. --- cores/esp8266/core_esp8266_main.cpp | 10 +++------- libraries/Schedule/library.properties | 10 ++++++++++ .../Schedule/src}/Schedule.cpp | 17 ++++++++++++++--- .../Schedule/src}/Schedule.h | 0 tests/host/Makefile | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 libraries/Schedule/library.properties rename {cores/esp8266 => libraries/Schedule/src}/Schedule.cpp (93%) rename {cores/esp8266 => libraries/Schedule/src}/Schedule.h (100%) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 61c07da909..f5077e1dc6 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -23,7 +23,6 @@ //This may be used to change user task stack size: //#define CONT_STACKSIZE 4096 #include -#include "Schedule.h" extern "C" { #include "ets_sys.h" #include "os_type.h" @@ -90,13 +89,12 @@ void preloop_update_frequency() { #endif } - -static inline void esp_yield_within_cont() __attribute__((always_inline)); -static void esp_yield_within_cont() { +extern "C" void __esp_yield_within_cont() { cont_yield(g_pcont); - run_scheduled_recurrent_functions(); } +extern "C" void esp_yield_within_cont() __attribute__ ((weak, alias("__esp_yield_within_cont"))); + extern "C" void esp_yield() { if (cont_can_yield(g_pcont)) { esp_yield_within_cont(); @@ -159,8 +157,6 @@ extern "C" bool IRAM_ATTR ets_post(uint8 prio, ETSSignal sig, ETSParam par) { extern "C" void __loop_end (void) { - run_scheduled_functions(); - run_scheduled_recurrent_functions(); } extern "C" void loop_end (void) __attribute__ ((weak, alias("__loop_end"))); diff --git a/libraries/Schedule/library.properties b/libraries/Schedule/library.properties new file mode 100644 index 0000000000..0d31af9fb5 --- /dev/null +++ b/libraries/Schedule/library.properties @@ -0,0 +1,10 @@ +name=Schedule +version=1.0 +author=Earle F. Philhower, III +maintainer=Earle F. Philhower, III +sentence= +paragraph= +category=Other +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/cores/esp8266/Schedule.cpp b/libraries/Schedule/src/Schedule.cpp similarity index 93% rename from cores/esp8266/Schedule.cpp rename to libraries/Schedule/src/Schedule.cpp index ed39736e55..b3db5ecc45 100644 --- a/cores/esp8266/Schedule.cpp +++ b/libraries/Schedule/src/Schedule.cpp @@ -2,9 +2,20 @@ #include #include "Schedule.h" -#include "PolledTimeout.h" -#include "interrupts.h" -#include "coredecls.h" +#include +#include +#include + +extern "C" void loop_end() +{ + run_scheduled_functions(); + run_scheduled_recurrent_functions();} + +extern "C" void esp_yield_within_cont() +{ + cont_yield(g_pcont); + run_scheduled_recurrent_functions(); +} typedef std::function mSchedFuncT; struct scheduled_fn_t diff --git a/cores/esp8266/Schedule.h b/libraries/Schedule/src/Schedule.h similarity index 100% rename from cores/esp8266/Schedule.h rename to libraries/Schedule/src/Schedule.h diff --git a/tests/host/Makefile b/tests/host/Makefile index aaf8a88d91..1159d7d0b6 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -67,7 +67,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\ spiffs/spiffs_nucleus.cpp \ libb64/cencode.cpp \ libb64/cdecode.cpp \ - Schedule.cpp \ + ../../libraries/Schedule/src/Schedule.cpp \ HardwareSerial.cpp \ crc32.cpp \ ) \ From 76a79b5203e79c6e68474d82b389a5fb461f39e8 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 25 Jun 2019 11:30:56 +0200 Subject: [PATCH 02/14] Core can't reference to libraries, need to move FunctionalInterrupt to libraries, too. --- .../examples/Functional/Functional.ino | 84 +++++++++++++++++++ libraries/FunctionalInterrupt/keywords.txt | 13 +++ .../FunctionalInterrupt/library.properties | 10 +++ .../src}/FunctionalInterrupt.cpp | 0 .../src}/FunctionalInterrupt.h | 0 5 files changed, 107 insertions(+) create mode 100644 libraries/FunctionalInterrupt/examples/Functional/Functional.ino create mode 100644 libraries/FunctionalInterrupt/keywords.txt create mode 100644 libraries/FunctionalInterrupt/library.properties rename {cores/esp8266 => libraries/FunctionalInterrupt/src}/FunctionalInterrupt.cpp (100%) rename {cores/esp8266 => libraries/FunctionalInterrupt/src}/FunctionalInterrupt.h (100%) diff --git a/libraries/FunctionalInterrupt/examples/Functional/Functional.ino b/libraries/FunctionalInterrupt/examples/Functional/Functional.ino new file mode 100644 index 0000000000..5abac705de --- /dev/null +++ b/libraries/FunctionalInterrupt/examples/Functional/Functional.ino @@ -0,0 +1,84 @@ +#include +#include + +#if defined(ESP32) +#define BUTTON1 16 +#define BUTTON2 17 +#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) +#define BUTTON1 D4 +#define BUTTON2 D3 +#else +#define BUTTON1 2 +#define BUTTON2 0 +#endif + +class Button { + public: + Button(uint8_t reqPin) : PIN(reqPin) { + pinMode(PIN, INPUT_PULLUP); + // Arduino C API: + //attachInterruptArg(PIN, [](void* self) { + // static_cast(self)->isr(); + //}, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM" + //attachInterruptArg(PIN, reinterpret_cast(&isr_static), this, FALLING); // works on ESP32; works on ESP8266 + // FunctionalInterrupts API: + attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { + Serial.print("Pin "); + Serial.println(ii.pin); + isr(); + }, FALLING); // works on ESP32; works on ESP8266 + }; + ~Button() { + detachInterrupt(PIN); + } + +#if defined(ESP8266) + void ICACHE_RAM_ATTR isr() +#elif defined(ESP32) + void IRAM_ATTR isr() +#endif + { + numberKeyPresses += 1; + pressed = true; + } + +#if defined(ESP8266) + static void ICACHE_RAM_ATTR isr_static(Button* const self) +#elif defined(ESP32) + static void IRAM_ATTR isr_static(Button* const self) +#endif + { + self->isr(); + } + + void checkPressed() { + if (pressed) { + Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); + pressed = false; + } + } + + private: + const uint8_t PIN; + volatile uint32_t numberKeyPresses = 0; + volatile bool pressed = false; +}; + +Button* button1; +Button* button2; + + +void setup() { + Serial.begin(115200); + Serial.println("FunctionalInterrupt test/example"); + + button1 = new Button(BUTTON1); + button2 = new Button(BUTTON2); + + Serial.println("setup() complete"); +} + +void loop() { + button1->checkPressed(); + button2->checkPressed(); +} diff --git a/libraries/FunctionalInterrupt/keywords.txt b/libraries/FunctionalInterrupt/keywords.txt new file mode 100644 index 0000000000..b8c0fc83f1 --- /dev/null +++ b/libraries/FunctionalInterrupt/keywords.txt @@ -0,0 +1,13 @@ +####################################### +# Datatypes (KEYWORD1) +####################################### + +InterruptInfo KEYWORD1 +ArgStructure KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +attachInterrupt KEYWORD2 +attachScheduledInterrupt KEYWORD2 diff --git a/libraries/FunctionalInterrupt/library.properties b/libraries/FunctionalInterrupt/library.properties new file mode 100644 index 0000000000..9738f1e971 --- /dev/null +++ b/libraries/FunctionalInterrupt/library.properties @@ -0,0 +1,10 @@ +name=FunctionalInterrupt +version=1.0 +author=hreintke +maintainer=hreintke +sentence=C++ functional and scheduled interrupt handling +paragraph= +category=Other +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/cores/esp8266/FunctionalInterrupt.cpp b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp similarity index 100% rename from cores/esp8266/FunctionalInterrupt.cpp rename to libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp diff --git a/cores/esp8266/FunctionalInterrupt.h b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h similarity index 100% rename from cores/esp8266/FunctionalInterrupt.h rename to libraries/FunctionalInterrupt/src/FunctionalInterrupt.h From 0039f23bc3e601a3ddfc893719cc772cac6547a4 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 25 Jun 2019 22:29:21 +0200 Subject: [PATCH 03/14] Fix crazy dependencies. --- cores/esp8266/core_esp8266_wiring_digital.cpp | 19 +++++++++++++++---- .../src/FunctionalInterrupt.cpp | 11 ----------- .../src/FunctionalInterrupt.h | 3 +-- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cores/esp8266/core_esp8266_wiring_digital.cpp b/cores/esp8266/core_esp8266_wiring_digital.cpp index 5ed0a40ab7..be831f348a 100644 --- a/cores/esp8266/core_esp8266_wiring_digital.cpp +++ b/cores/esp8266/core_esp8266_wiring_digital.cpp @@ -115,16 +115,21 @@ typedef struct { bool functional; } interrupt_handler_t; -//duplicate from functionalInterrupt.h keep in sync +//duplicates from functionalInterrupt.h keep in sync typedef struct InterruptInfo { uint8_t pin; uint8_t value; uint32_t micro; } InterruptInfo; -typedef struct { +typedef struct FunctionInfo { + std::function reqFunction; + std::function reqScheduledFunction; +} FunctionInfo; + +typedef struct ArgStructure { InterruptInfo* interruptInfo; - void* functionInfo; + FunctionInfo* functionInfo; } ArgStructure; static interrupt_handler_t interrupt_handlers[16] = { {0, 0, 0, 0}, }; @@ -172,7 +177,13 @@ void ICACHE_RAM_ATTR interrupt_handler(void*) ETS_GPIO_INTR_ENABLE(); } -extern void cleanupFunctional(void* arg); +static void cleanupFunctional(void* arg) +{ + ArgStructure* localArg = (ArgStructure*)arg; + delete (FunctionInfo*)localArg->functionInfo; + delete (InterruptInfo*)localArg->interruptInfo; + delete localArg; +} static void set_interrupt_handlers(uint8_t pin, voidFuncPtr userFunc, void* arg, uint8_t mode, bool functional) { diff --git a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp index 665d8043b3..40282547a9 100644 --- a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp +++ b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp @@ -23,17 +23,6 @@ void ICACHE_RAM_ATTR interruptFunctional(void* arg) } } -extern "C" -{ - void cleanupFunctional(void* arg) - { - ArgStructure* localArg = (ArgStructure*)arg; - delete (FunctionInfo*)localArg->functionInfo; - delete (InterruptInfo*)localArg->interruptInfo; - delete localArg; - } -} - void attachInterrupt(uint8_t pin, std::function intRoutine, int mode) { // use the local interrupt routine which takes the ArgStructure as argument diff --git a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h index 968793e499..e1f1ea2f3e 100644 --- a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h +++ b/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h @@ -31,5 +31,4 @@ struct ArgStructure { void attachInterrupt(uint8_t pin, std::function intRoutine, int mode); void attachScheduledInterrupt(uint8_t pin, std::function scheduledIntRoutine, int mode); - -#endif //INTERRUPTS_H +#endif //FUNCTIONALINTERRUPT_H From ae9a054ceb918ffd5f183255034851a1318c5da3 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 26 Jun 2019 08:43:50 +0200 Subject: [PATCH 04/14] Updated examples --- .../examples/Functional/Functional.ino | 41 ++++------ .../ScheduledFunctional.ino | 75 +++++++++++++++++++ 2 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino diff --git a/libraries/FunctionalInterrupt/examples/Functional/Functional.ino b/libraries/FunctionalInterrupt/examples/Functional/Functional.ino index 5abac705de..3d45019ed7 100644 --- a/libraries/FunctionalInterrupt/examples/Functional/Functional.ino +++ b/libraries/FunctionalInterrupt/examples/Functional/Functional.ino @@ -1,5 +1,8 @@ #include -#include + +#ifndef IRAM_ATTR +#define IRAM_ATTR ICACHE_RAM_ATTR +#endif #if defined(ESP32) #define BUTTON1 16 @@ -16,46 +19,27 @@ class Button { public: Button(uint8_t reqPin) : PIN(reqPin) { pinMode(PIN, INPUT_PULLUP); - // Arduino C API: - //attachInterruptArg(PIN, [](void* self) { - // static_cast(self)->isr(); - //}, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM" - //attachInterruptArg(PIN, reinterpret_cast(&isr_static), this, FALLING); // works on ESP32; works on ESP8266 - // FunctionalInterrupts API: - attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { - Serial.print("Pin "); - Serial.println(ii.pin); - isr(); - }, FALLING); // works on ESP32; works on ESP8266 + attachInterrupt(PIN, std::bind(&Button::buttonIsr, this), FALLING); }; ~Button() { detachInterrupt(PIN); } -#if defined(ESP8266) - void ICACHE_RAM_ATTR isr() -#elif defined(ESP32) - void IRAM_ATTR isr() -#endif - { + void IRAM_ATTR buttonIsr() { numberKeyPresses += 1; pressed = true; } -#if defined(ESP8266) - static void ICACHE_RAM_ATTR isr_static(Button* const self) -#elif defined(ESP32) - static void IRAM_ATTR isr_static(Button* const self) -#endif - { - self->isr(); + static void IRAM_ATTR buttonIsr_static(Button* const self) { + self->buttonIsr(); } - void checkPressed() { + uint32_t checkPressed() { if (pressed) { Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); pressed = false; } + return numberKeyPresses; } private: @@ -80,5 +64,8 @@ void setup() { void loop() { button1->checkPressed(); - button2->checkPressed(); + if (nullptr != button2 && 10 < button2->checkPressed()) { + delete button2; + button2 = nullptr; + } } diff --git a/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino b/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino new file mode 100644 index 0000000000..dc0e0fc318 --- /dev/null +++ b/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino @@ -0,0 +1,75 @@ +#include + +#ifndef IRAM_ATTR +#define IRAM_ATTR ICACHE_RAM_ATTR +#endif + +#if defined(ESP32) +#define BUTTON1 16 +#define BUTTON2 17 +#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) +#define BUTTON1 D4 +#define BUTTON2 D3 +#else +#define BUTTON1 2 +#define BUTTON2 0 +#endif + +class Button { + public: + Button(uint8_t reqPin) : PIN(reqPin) { + pinMode(PIN, INPUT_PULLUP); + attachScheduledInterrupt(PIN, [this](const InterruptInfo & ii) { + Serial.print("Pin "); + Serial.println(ii.pin); + buttonIsr(); + }, FALLING); // works on ESP8266 + }; + ~Button() { + detachInterrupt(PIN); + } + + void IRAM_ATTR buttonIsr() { + numberKeyPresses += 1; + pressed = true; + } + + static void IRAM_ATTR buttonIsr_static(Button* const self) { + self->buttonIsr(); + } + + uint32_t checkPressed() { + if (pressed) { + Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); + pressed = false; + } + return numberKeyPresses; + } + + private: + const uint8_t PIN; + volatile uint32_t numberKeyPresses = 0; + volatile bool pressed = false; +}; + +Button* button1; +Button* button2; + + +void setup() { + Serial.begin(115200); + Serial.println("FunctionalInterrupt test/example"); + + button1 = new Button(BUTTON1); + button2 = new Button(BUTTON2); + + Serial.println("setup() complete"); +} + +void loop() { + button1->checkPressed(); + if (nullptr != button2 && 10 < button2->checkPressed()) { + delete button2; + button2 = nullptr; + } +} From ec8cee0ba356c71db0f6100895d53237243278a1 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sat, 29 Jun 2019 00:10:27 +0200 Subject: [PATCH 05/14] Remove redundant C-style casts --- cores/esp8266/core_esp8266_wiring_digital.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_wiring_digital.cpp b/cores/esp8266/core_esp8266_wiring_digital.cpp index be831f348a..21568879e0 100644 --- a/cores/esp8266/core_esp8266_wiring_digital.cpp +++ b/cores/esp8266/core_esp8266_wiring_digital.cpp @@ -180,8 +180,8 @@ void ICACHE_RAM_ATTR interrupt_handler(void*) static void cleanupFunctional(void* arg) { ArgStructure* localArg = (ArgStructure*)arg; - delete (FunctionInfo*)localArg->functionInfo; - delete (InterruptInfo*)localArg->interruptInfo; + delete localArg->interruptInfo; + delete localArg->functionInfo; delete localArg; } From 826a319d36c0b5d67beb6073b3966b65135026dd Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 4 Jul 2019 08:15:38 +0200 Subject: [PATCH 06/14] Updated credits --- libraries/Schedule/library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Schedule/library.properties b/libraries/Schedule/library.properties index 0d31af9fb5..433e275659 100644 --- a/libraries/Schedule/library.properties +++ b/libraries/Schedule/library.properties @@ -1,7 +1,7 @@ name=Schedule version=1.0 -author=Earle F. Philhower, III -maintainer=Earle F. Philhower, III +author=David Gauchard +maintainer=David Gauchard sentence= paragraph= category=Other From 1e260349e4348f8cb5929e656dfd30781a89ee9f Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 5 Jul 2019 14:18:36 +0200 Subject: [PATCH 07/14] Corrected library author and maintainer --- libraries/Schedule/library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Schedule/library.properties b/libraries/Schedule/library.properties index 433e275659..533268880f 100644 --- a/libraries/Schedule/library.properties +++ b/libraries/Schedule/library.properties @@ -1,7 +1,7 @@ name=Schedule version=1.0 -author=David Gauchard -maintainer=David Gauchard +author=Ivan Grokhotkov +maintainer=Ivan Grokhotkov sentence= paragraph= category=Other From 1006766a979611bad5562e4e0283764dacfefd8a Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 19 Jul 2019 11:08:40 +0200 Subject: [PATCH 08/14] Update Ticker library --- libraries/Ticker/examples/Blinker/Blinker.ino | 41 +++++++++++++++++++ .../TickerFunctional/TickerFunctional.ino | 4 +- libraries/Ticker/keywords.txt | 20 +++------ libraries/Ticker/{ => src}/Ticker.cpp | 0 libraries/Ticker/{ => src}/Ticker.h | 0 5 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 libraries/Ticker/examples/Blinker/Blinker.ino rename libraries/Ticker/{ => src}/Ticker.cpp (100%) rename libraries/Ticker/{ => src}/Ticker.h (100%) diff --git a/libraries/Ticker/examples/Blinker/Blinker.ino b/libraries/Ticker/examples/Blinker/Blinker.ino new file mode 100644 index 0000000000..ce5a18a316 --- /dev/null +++ b/libraries/Ticker/examples/Blinker/Blinker.ino @@ -0,0 +1,41 @@ +#include +#include + +// attach a LED to pPIO 21 +#define LED_PIN 21 + +Ticker blinker; +Ticker toggler; +Ticker changer; +float blinkerPace = 0.1; //seconds +const float togglePeriod = 5; //seconds + +void change() { + blinkerPace = 0.5; +} + +void blink() { + digitalWrite(LED_PIN, !digitalRead(LED_PIN)); +} + +void toggle() { + static bool isBlinking = false; + if (isBlinking) { + blinker.detach(); + isBlinking = false; + } else { + blinker.attach(blinkerPace, blink); + isBlinking = true; + } + digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON) +} + +void setup() { + pinMode(LED_PIN, OUTPUT); + toggler.attach(togglePeriod, toggle); + changer.once(30, change); +} + +void loop() { + +} diff --git a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino index 33c9435982..387e6d6bcb 100644 --- a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino +++ b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino @@ -1,5 +1,5 @@ -#include "Arduino.h" -#include "Ticker.h" +#include +#include #define LED1 2 #define LED2 4 diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt index 1ecd8d0eda..b1020c4e59 100644 --- a/libraries/Ticker/keywords.txt +++ b/libraries/Ticker/keywords.txt @@ -1,29 +1,21 @@ -####################################### -# Syntax Coloring Map For Wire -####################################### - ####################################### # Datatypes (KEYWORD1) ####################################### +Ticker KEYWORD1 + ####################################### # Methods and Functions (KEYWORD2) ####################################### +attach_scheduled KEYWORD2 attach KEYWORD2 +attach_ms_scheduled KEYWORD2 attach_ms KEYWORD2 +once_scheduled KEYWORD2 once KEYWORD2 +once_ms_scheduled KEYWORD2 once_ms KEYWORD2 detach KEYWORD2 active KEYWORD2 -####################################### -# Instances (KEYWORD2) -####################################### - -Ticker KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp similarity index 100% rename from libraries/Ticker/Ticker.cpp rename to libraries/Ticker/src/Ticker.cpp diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/src/Ticker.h similarity index 100% rename from libraries/Ticker/Ticker.h rename to libraries/Ticker/src/Ticker.h From 4d8df2d512bee0899b60cd9aeb461cb74367fb19 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 24 Jul 2019 10:13:02 +0200 Subject: [PATCH 09/14] More efficient loop extension --- cores/esp8266/core_esp8266_main.cpp | 11 ++++------- libraries/Schedule/src/Schedule.cpp | 3 ++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index f5077e1dc6..0d8ebd601b 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -39,7 +39,6 @@ extern "C" { #define OPTIMISTIC_YIELD_TIME_US 16000 extern "C" void call_user_start(); -extern void loop(); extern void setup(); extern void (*__init_array_start)(void); extern void (*__init_array_end)(void); @@ -155,12 +154,11 @@ extern "C" bool IRAM_ATTR ets_post(uint8 prio, ETSSignal sig, ETSParam par) { return rc; } -extern "C" void __loop_end (void) -{ +extern "C" void esp_loop(void) __attribute__((weak)); +extern "C" void esp_loop(void) { + loop(); } -extern "C" void loop_end (void) __attribute__ ((weak, alias("__loop_end"))); - static void loop_wrapper() { static bool setup_done = false; preloop_update_frequency(); @@ -168,8 +166,7 @@ static void loop_wrapper() { setup(); setup_done = true; } - loop(); - loop_end(); + esp_loop(); esp_schedule(); } diff --git a/libraries/Schedule/src/Schedule.cpp b/libraries/Schedule/src/Schedule.cpp index b3db5ecc45..61faaeab62 100644 --- a/libraries/Schedule/src/Schedule.cpp +++ b/libraries/Schedule/src/Schedule.cpp @@ -6,8 +6,9 @@ #include #include -extern "C" void loop_end() +extern "C" void esp_loop() { + loop(); run_scheduled_functions(); run_scheduled_recurrent_functions();} From 44714a9ab9faad3e38c91ceb46b7184baa34ec9f Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 24 Jul 2019 10:35:10 +0200 Subject: [PATCH 10/14] yield() plugin code more straightforward, same efficiency --- cores/esp8266/core_esp8266_main.cpp | 11 +++-------- libraries/Schedule/src/Schedule.cpp | 6 ++++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 0d8ebd601b..04506b6ec3 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -88,15 +88,10 @@ void preloop_update_frequency() { #endif } -extern "C" void __esp_yield_within_cont() { - cont_yield(g_pcont); -} - -extern "C" void esp_yield_within_cont() __attribute__ ((weak, alias("__esp_yield_within_cont"))); - +extern "C" void esp_yield() __attribute__((weak)); extern "C" void esp_yield() { if (cont_can_yield(g_pcont)) { - esp_yield_within_cont(); + cont_yield(g_pcont); } } @@ -108,7 +103,7 @@ extern "C" void esp_schedule() { extern "C" void __yield() { if (cont_can_yield(g_pcont)) { esp_schedule(); - esp_yield_within_cont(); + esp_yield(); } else { panic(); diff --git a/libraries/Schedule/src/Schedule.cpp b/libraries/Schedule/src/Schedule.cpp index 61faaeab62..98540570b8 100644 --- a/libraries/Schedule/src/Schedule.cpp +++ b/libraries/Schedule/src/Schedule.cpp @@ -12,9 +12,11 @@ extern "C" void esp_loop() run_scheduled_functions(); run_scheduled_recurrent_functions();} -extern "C" void esp_yield_within_cont() +extern "C" void esp_yield() { - cont_yield(g_pcont); + if (cont_can_yield(g_pcont)) { + cont_yield(g_pcont); + } run_scheduled_recurrent_functions(); } From cb44d521b6b550ba5c423df11b40b2e61a8fcfeb Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 26 Jul 2019 10:29:21 +0200 Subject: [PATCH 11/14] Two-level hook system for esp_yield to accommodate host test environment --- cores/esp8266/core_esp8266_main.cpp | 6 ++++-- libraries/Schedule/src/Schedule.cpp | 22 +++++++++++++--------- tests/host/common/Arduino.cpp | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 04506b6ec3..23afc9c483 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -88,13 +88,15 @@ void preloop_update_frequency() { #endif } -extern "C" void esp_yield() __attribute__((weak)); -extern "C" void esp_yield() { +extern "C" void __esp_yield() __attribute__((weak)); +extern "C" void __esp_yield() { if (cont_can_yield(g_pcont)) { cont_yield(g_pcont); } } +extern "C" void esp_yield(void) __attribute__ ((weak, alias("__esp_yield"))); + extern "C" void esp_schedule() { // always on CONT stack here ets_post(LOOP_TASK_PRIORITY, 0, 0); diff --git a/libraries/Schedule/src/Schedule.cpp b/libraries/Schedule/src/Schedule.cpp index 98540570b8..6be96d4816 100644 --- a/libraries/Schedule/src/Schedule.cpp +++ b/libraries/Schedule/src/Schedule.cpp @@ -6,18 +6,22 @@ #include #include -extern "C" void esp_loop() +extern "C" { - loop(); - run_scheduled_functions(); - run_scheduled_recurrent_functions();} + void esp_loop() + { + loop(); + run_scheduled_functions(); + run_scheduled_recurrent_functions(); + } -extern "C" void esp_yield() -{ - if (cont_can_yield(g_pcont)) { - cont_yield(g_pcont); + void __esp_yield(); + + extern "C" void esp_yield() + { + __esp_yield(); + run_scheduled_recurrent_functions(); } - run_scheduled_recurrent_functions(); } typedef std::function mSchedFuncT; diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 5192028e73..93aa923264 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -42,7 +42,7 @@ extern "C" void optimistic_yield (uint32_t interval_us) usleep(interval_us); } -extern "C" void esp_yield() +extern "C" void __esp_yield() { } From 92900d2b9ba76ac436e857c4166695421d4baba9 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 26 Jul 2019 10:32:16 +0200 Subject: [PATCH 12/14] Fix igrr's email address --- libraries/Schedule/library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Schedule/library.properties b/libraries/Schedule/library.properties index 533268880f..b9153597be 100644 --- a/libraries/Schedule/library.properties +++ b/libraries/Schedule/library.properties @@ -1,7 +1,7 @@ name=Schedule version=1.0 -author=Ivan Grokhotkov -maintainer=Ivan Grokhotkov +author=Ivan Grokhotkov (ivan@esp8266.com) +maintainer=Ivan Grokhotkov (ivan@esp8266.com) sentence= paragraph= category=Other From 579940e416df358f12e79c074dc05451ef79649a Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 26 Jul 2019 11:42:28 +0200 Subject: [PATCH 13/14] Rename FunctionalInterrupt to ScheduledInterrupts. This was a review result in another PR. --- .../examples/Functional/Functional.ino | 71 ------------------ .../ScheduledFunctional.ino | 75 ------------------- .../examples/Functional/Functional.ino | 69 +++++++++++++++++ .../ScheduledFunctional.ino | 74 ++++++++++++++++++ .../keywords.txt | 0 .../library.properties | 4 +- .../src/ScheduledInterrupts.cpp} | 4 +- .../src/ScheduledInterrupts.h} | 6 +- 8 files changed, 150 insertions(+), 153 deletions(-) delete mode 100644 libraries/FunctionalInterrupt/examples/Functional/Functional.ino delete mode 100644 libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino create mode 100644 libraries/ScheduledInterrupts/examples/Functional/Functional.ino create mode 100644 libraries/ScheduledInterrupts/examples/ScheduledFunctional/ScheduledFunctional.ino rename libraries/{FunctionalInterrupt => ScheduledInterrupts}/keywords.txt (100%) rename libraries/{FunctionalInterrupt => ScheduledInterrupts}/library.properties (70%) rename libraries/{FunctionalInterrupt/src/FunctionalInterrupt.cpp => ScheduledInterrupts/src/ScheduledInterrupts.cpp} (96%) rename libraries/{FunctionalInterrupt/src/FunctionalInterrupt.h => ScheduledInterrupts/src/ScheduledInterrupts.h} (88%) diff --git a/libraries/FunctionalInterrupt/examples/Functional/Functional.ino b/libraries/FunctionalInterrupt/examples/Functional/Functional.ino deleted file mode 100644 index 3d45019ed7..0000000000 --- a/libraries/FunctionalInterrupt/examples/Functional/Functional.ino +++ /dev/null @@ -1,71 +0,0 @@ -#include - -#ifndef IRAM_ATTR -#define IRAM_ATTR ICACHE_RAM_ATTR -#endif - -#if defined(ESP32) -#define BUTTON1 16 -#define BUTTON2 17 -#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) -#define BUTTON1 D4 -#define BUTTON2 D3 -#else -#define BUTTON1 2 -#define BUTTON2 0 -#endif - -class Button { - public: - Button(uint8_t reqPin) : PIN(reqPin) { - pinMode(PIN, INPUT_PULLUP); - attachInterrupt(PIN, std::bind(&Button::buttonIsr, this), FALLING); - }; - ~Button() { - detachInterrupt(PIN); - } - - void IRAM_ATTR buttonIsr() { - numberKeyPresses += 1; - pressed = true; - } - - static void IRAM_ATTR buttonIsr_static(Button* const self) { - self->buttonIsr(); - } - - uint32_t checkPressed() { - if (pressed) { - Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); - pressed = false; - } - return numberKeyPresses; - } - - private: - const uint8_t PIN; - volatile uint32_t numberKeyPresses = 0; - volatile bool pressed = false; -}; - -Button* button1; -Button* button2; - - -void setup() { - Serial.begin(115200); - Serial.println("FunctionalInterrupt test/example"); - - button1 = new Button(BUTTON1); - button2 = new Button(BUTTON2); - - Serial.println("setup() complete"); -} - -void loop() { - button1->checkPressed(); - if (nullptr != button2 && 10 < button2->checkPressed()) { - delete button2; - button2 = nullptr; - } -} diff --git a/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino b/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino deleted file mode 100644 index dc0e0fc318..0000000000 --- a/libraries/FunctionalInterrupt/examples/ScheduledFunctional/ScheduledFunctional.ino +++ /dev/null @@ -1,75 +0,0 @@ -#include - -#ifndef IRAM_ATTR -#define IRAM_ATTR ICACHE_RAM_ATTR -#endif - -#if defined(ESP32) -#define BUTTON1 16 -#define BUTTON2 17 -#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) -#define BUTTON1 D4 -#define BUTTON2 D3 -#else -#define BUTTON1 2 -#define BUTTON2 0 -#endif - -class Button { - public: - Button(uint8_t reqPin) : PIN(reqPin) { - pinMode(PIN, INPUT_PULLUP); - attachScheduledInterrupt(PIN, [this](const InterruptInfo & ii) { - Serial.print("Pin "); - Serial.println(ii.pin); - buttonIsr(); - }, FALLING); // works on ESP8266 - }; - ~Button() { - detachInterrupt(PIN); - } - - void IRAM_ATTR buttonIsr() { - numberKeyPresses += 1; - pressed = true; - } - - static void IRAM_ATTR buttonIsr_static(Button* const self) { - self->buttonIsr(); - } - - uint32_t checkPressed() { - if (pressed) { - Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); - pressed = false; - } - return numberKeyPresses; - } - - private: - const uint8_t PIN; - volatile uint32_t numberKeyPresses = 0; - volatile bool pressed = false; -}; - -Button* button1; -Button* button2; - - -void setup() { - Serial.begin(115200); - Serial.println("FunctionalInterrupt test/example"); - - button1 = new Button(BUTTON1); - button2 = new Button(BUTTON2); - - Serial.println("setup() complete"); -} - -void loop() { - button1->checkPressed(); - if (nullptr != button2 && 10 < button2->checkPressed()) { - delete button2; - button2 = nullptr; - } -} diff --git a/libraries/ScheduledInterrupts/examples/Functional/Functional.ino b/libraries/ScheduledInterrupts/examples/Functional/Functional.ino new file mode 100644 index 0000000000..0461ef2879 --- /dev/null +++ b/libraries/ScheduledInterrupts/examples/Functional/Functional.ino @@ -0,0 +1,69 @@ +#include + +#ifndef IRAM_ATTR +#define IRAM_ATTR ICACHE_RAM_ATTR +#endif + +#if defined(ESP32) +#define BUTTON1 16 +#define BUTTON2 17 +#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) +#define BUTTON1 D4 +#define BUTTON2 D3 +#else +#define BUTTON1 2 +#define BUTTON2 0 +#endif + +class Button { + public: + Button(const uint8_t reqPin) : _PIN(reqPin) { + pinMode(_PIN, INPUT_PULLUP); + attachInterrupt(_PIN, std::bind(&Button::buttonIsr, this), FALLING); + }; + ~Button() { + detachInterrupt(_PIN); + } + + void IRAM_ATTR buttonIsr() { + _numberKeyPresses += 1; + _pressed = true; + } + + uint32_t testResetPressed() { + if (_pressed) { + Serial.printf("Button on pin %u has been pressed %u times\n", _PIN, _numberKeyPresses); + _pressed = false; + } + return _numberKeyPresses; + } + + private: + const uint8_t _PIN; + volatile uint32_t _numberKeyPresses = 0; + volatile bool _pressed = false; +}; + +// Pointers and "new" in setup() are used in this example to simply test +// and demonstrate how an ISR object can be constructed and destructed at runtime, +// including the detach of the ISR from the GPIO. +Button* button1 = nullptr; +Button* button2 = nullptr; + +void setup() { + Serial.begin(115200); + Serial.println("ScheduledInterrupts test/example"); + + button1 = new Button(BUTTON1); + button2 = new Button(BUTTON2); + + Serial.println("setup() complete"); +} + +void loop() { + button1->testResetPressed(); + if (nullptr != button2 && 10 < button2->testResetPressed()) { + delete button2; + button2 = nullptr; + } +} diff --git a/libraries/ScheduledInterrupts/examples/ScheduledFunctional/ScheduledFunctional.ino b/libraries/ScheduledInterrupts/examples/ScheduledFunctional/ScheduledFunctional.ino new file mode 100644 index 0000000000..6d21a01c1e --- /dev/null +++ b/libraries/ScheduledInterrupts/examples/ScheduledFunctional/ScheduledFunctional.ino @@ -0,0 +1,74 @@ +#include + +#ifndef IRAM_ATTR +#define IRAM_ATTR ICACHE_RAM_ATTR +#endif + +#if defined(ESP32) +#define BUTTON1 16 +#define BUTTON2 17 +#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) +#define BUTTON1 D4 +#define BUTTON2 D3 +#else +#define BUTTON1 2 +#define BUTTON2 0 +#endif + +class Button { + public: + Button(const uint8_t reqPin) : _PIN(reqPin) { + pinMode(_PIN, INPUT_PULLUP); + attachScheduledInterrupt(_PIN, [this](const InterruptInfo & ii) { + Serial.print("Pin "); + Serial.println(ii.pin); + buttonIsr(); + }, FALLING); // works on ESP8266 + }; + ~Button() { + detachInterrupt(_PIN); + } + + void IRAM_ATTR buttonIsr() { + _numberKeyPresses += 1; + _pressed = true; + } + + uint32_t testResetPressed() { + if (_pressed) { + Serial.printf("Button on pin %u has been pressed %u times\n", _PIN, _numberKeyPresses); + _pressed = false; + } + return _numberKeyPresses; + } + + private: + const uint8_t _PIN; + volatile uint32_t _numberKeyPresses = 0; + volatile bool _pressed = false; +}; + +// Pointers and "new" in setup() are used in this example to simply test +// and demonstrate how an ISR object can be constructed and destructed at runtime, +// including the detach of the ISR from the GPIO. +Button* button1; +Button* button2; + + +void setup() { + Serial.begin(115200); + Serial.println("ScheduledInterrupts test/example"); + + button1 = new Button(BUTTON1); + button2 = new Button(BUTTON2); + + Serial.println("setup() complete"); +} + +void loop() { + button1->testResetPressed(); + if (nullptr != button2 && 10 < button2->testResetPressed()) { + delete button2; + button2 = nullptr; + } +} diff --git a/libraries/FunctionalInterrupt/keywords.txt b/libraries/ScheduledInterrupts/keywords.txt similarity index 100% rename from libraries/FunctionalInterrupt/keywords.txt rename to libraries/ScheduledInterrupts/keywords.txt diff --git a/libraries/FunctionalInterrupt/library.properties b/libraries/ScheduledInterrupts/library.properties similarity index 70% rename from libraries/FunctionalInterrupt/library.properties rename to libraries/ScheduledInterrupts/library.properties index 9738f1e971..e47c0faa4a 100644 --- a/libraries/FunctionalInterrupt/library.properties +++ b/libraries/ScheduledInterrupts/library.properties @@ -1,8 +1,8 @@ -name=FunctionalInterrupt +name=ScheduledInterrupts version=1.0 author=hreintke maintainer=hreintke -sentence=C++ functional and scheduled interrupt handling +sentence=C++ functional, scheduled interrupt handling paragraph= category=Other url=https://github.com/esp8266/Arduino diff --git a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp b/libraries/ScheduledInterrupts/src/ScheduledInterrupts.cpp similarity index 96% rename from libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp rename to libraries/ScheduledInterrupts/src/ScheduledInterrupts.cpp index 40282547a9..d583b48be0 100644 --- a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.cpp +++ b/libraries/ScheduledInterrupts/src/ScheduledInterrupts.cpp @@ -1,6 +1,6 @@ -#include +#include "ScheduledInterrupts.h" #include -#include "Arduino.h" +#include // Duplicate typedefs from core_esp8266_wiring_digital_c typedef void (*voidFuncPtr)(void); diff --git a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h b/libraries/ScheduledInterrupts/src/ScheduledInterrupts.h similarity index 88% rename from libraries/FunctionalInterrupt/src/FunctionalInterrupt.h rename to libraries/ScheduledInterrupts/src/ScheduledInterrupts.h index e1f1ea2f3e..c192fd212e 100644 --- a/libraries/FunctionalInterrupt/src/FunctionalInterrupt.h +++ b/libraries/ScheduledInterrupts/src/ScheduledInterrupts.h @@ -1,5 +1,5 @@ -#ifndef FUNCTIONALINTERRUPT_H -#define FUNCTIONALINTERRUPT_H +#ifndef SCHEDULEDINTERRUPTS_H +#define SCHEDULEDINTERRUPTS_H #include #include @@ -31,4 +31,4 @@ struct ArgStructure { void attachInterrupt(uint8_t pin, std::function intRoutine, int mode); void attachScheduledInterrupt(uint8_t pin, std::function scheduledIntRoutine, int mode); -#endif //FUNCTIONALINTERRUPT_H +#endif // SCHEDULEDINTERRUPTS_H From 789662ecbc4e98dc983ecd2b6569be06b8db05ac Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 16 Sep 2019 10:01:21 +0200 Subject: [PATCH 14/14] Comment --- libraries/Schedule/src/Schedule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Schedule/src/Schedule.cpp b/libraries/Schedule/src/Schedule.cpp index 6be96d4816..3112ecb485 100644 --- a/libraries/Schedule/src/Schedule.cpp +++ b/libraries/Schedule/src/Schedule.cpp @@ -138,7 +138,7 @@ void run_scheduled_functions () if (yieldNow) { - // because scheduled function are allowed to last: + // because scheduled functions are allowed to last: // this is yield() in cont stack: esp_schedule(); cont_yield(g_pcont);