diff --git a/README.md b/README.md index f68076dc..8f381026 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ the box, without any further configuration: #include #include +KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport); + void setup (void) { Serial.begin(9600); - Kaleidoscope.use (&CycleTimeReport); - Kaleidoscope.setup (); } ``` diff --git a/examples/CycleTimeReport/CycleTimeReport.ino b/examples/CycleTimeReport/CycleTimeReport.ino index e35c9821..28b3f849 100644 --- a/examples/CycleTimeReport/CycleTimeReport.ino +++ b/examples/CycleTimeReport/CycleTimeReport.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-CycleTimeReport -- Scan cycle time reporting - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * 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 @@ -39,10 +39,10 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_skip), }; +KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport); + void setup() { Serial.begin(9600); - Kaleidoscope.use(&CycleTimeReport); - Kaleidoscope.setup(); } diff --git a/src/kaleidoscope/CycleTimeReport.cpp b/src/kaleidoscope/CycleTimeReport.cpp index 936d0aa8..82748b52 100644 --- a/src/kaleidoscope/CycleTimeReport.cpp +++ b/src/kaleidoscope/CycleTimeReport.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-CycleTimeReport -- Scan cycle time reporting - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * 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 @@ -23,34 +23,48 @@ uint32_t CycleTimeReport::next_report_time_; uint32_t CycleTimeReport::loop_start_time_; uint32_t CycleTimeReport::average_loop_time; -void CycleTimeReport::begin(void) { - Kaleidoscope.useLoopHook(loopHook); +EventHandlerResult CycleTimeReport::onSetup() { next_report_time_ = millis() + 1000; + return EventHandlerResult::OK; } -void CycleTimeReport::loopHook(bool post_clear) { - if (!post_clear) - return; +EventHandlerResult CycleTimeReport::beforeEachCycle() { + loop_start_time_ = micros(); + return EventHandlerResult::OK; +} - if (loop_start_time_) { - uint32_t loop_time = micros() - loop_start_time_; +EventHandlerResult CycleTimeReport::afterEachCycle() { + uint32_t loop_time = micros() - loop_start_time_; - if (average_loop_time) - average_loop_time = (average_loop_time + loop_time) / 2; - else - average_loop_time = loop_time; + if (average_loop_time) + average_loop_time = (average_loop_time + loop_time) / 2; + else + average_loop_time = loop_time; - if (millis() >= next_report_time_) { - cycleTimeReport(); + if (millis() >= next_report_time_) { + cycleTimeReport(); - average_loop_time = 0; - next_report_time_ = millis() + 1000; - } - loop_start_time_ = 0; - } else { - loop_start_time_ = micros(); + average_loop_time = 0; + next_report_time_ = millis() + 1000; } + + return EventHandlerResult::OK; +} + +// Deprecated V1 APIs +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void CycleTimeReport::begin() { + Kaleidoscope.useLoopHook(legacyLoopHook); } + +void CycleTimeReport::legacyLoopHook(bool is_post_clear) { + if (is_post_clear) + ::CycleTimeReport.afterEachCycle(); + else + ::CycleTimeReport.beforeEachCycle(); +} +#endif + } __attribute__((weak)) void cycleTimeReport(void) { diff --git a/src/kaleidoscope/CycleTimeReport.h b/src/kaleidoscope/CycleTimeReport.h index 3a62538a..ec3aa0ad 100644 --- a/src/kaleidoscope/CycleTimeReport.h +++ b/src/kaleidoscope/CycleTimeReport.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-CycleTimeReport -- Scan cycle time reporting - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * 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 @@ -21,18 +21,25 @@ #include namespace kaleidoscope { -class CycleTimeReport : public KaleidoscopePlugin { +class CycleTimeReport : public kaleidoscope::Plugin { public: CycleTimeReport() {} - void begin(void) final; + EventHandlerResult onSetup(); + EventHandlerResult beforeEachCycle(); + EventHandlerResult afterEachCycle(); static uint32_t average_loop_time; + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + protected: + void begin(); + static void legacyLoopHook(bool is_post_clear); +#endif + private: static uint32_t next_report_time_; static uint32_t loop_start_time_; - - static void loopHook(bool post_clear); }; }