diff --git a/libraries/HeartBeat/.arduino-ci.yml b/libraries/HeartBeat/.arduino-ci.yml index ff5659b9..cecf5850 100644 --- a/libraries/HeartBeat/.arduino-ci.yml +++ b/libraries/HeartBeat/.arduino-ci.yml @@ -2,6 +2,10 @@ compile: # Choosing to run compilation tests on 2 different Arduino platforms platforms: - uno - - leonardo - - due - - zero + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 diff --git a/libraries/HeartBeat/.github/workflows/arduino_test_runner.yml b/libraries/HeartBeat/.github/workflows/arduino_test_runner.yml index 476456bb..096b975b 100644 --- a/libraries/HeartBeat/.github/workflows/arduino_test_runner.yml +++ b/libraries/HeartBeat/.github/workflows/arduino_test_runner.yml @@ -4,10 +4,14 @@ name: Arduino CI on: [push, pull_request] jobs: - arduino_ci: + runTest: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: Arduino-CI/action@master - # Arduino-CI/action@v0.1.1 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + - run: | + gem install arduino_ci + arduino_ci.rb diff --git a/libraries/HeartBeat/HeartBeat.cpp b/libraries/HeartBeat/HeartBeat.cpp index c98c7f68..a6bfa3cd 100644 --- a/libraries/HeartBeat/HeartBeat.cpp +++ b/libraries/HeartBeat/HeartBeat.cpp @@ -1,7 +1,7 @@ // // FILE: HeartBeat.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.3 +// VERSION: 0.2.0 // PURPOSE: Arduino library for HeartBeat with frequency and dutyCycle // DATE: 2019-06-12 // URL: https://github.com/RobTillaart/HeartBeat @@ -12,6 +12,10 @@ // 0.1.2 2021-01-15 renamed all to HeartBeat // added dutyCycle // 0.1.3 2021-05-27 fix arduino-lint +// 0.2.0 2021-11-02 update Build-CI, add badges +// add getFrequency(), getDutyCycle(); +// add getState(). +// removed set() #include "HeartBeat.h" @@ -19,7 +23,6 @@ HeartBeat::HeartBeat() { - _dutyCycle = 50; } @@ -35,14 +38,15 @@ void HeartBeat::begin(const uint8_t pin, float frequency) void HeartBeat::setFrequency(float frequency) { - _frequency = abs(frequency); + _frequency = frequency; + if (_frequency < 0) _frequency = -frequency; + if (_frequency < 0.001) _frequency = 0.001; _setFreqDuty(); } void HeartBeat::setDutyCycle(float dutyCycle) { - _dutyCycle = constrain(dutyCycle, 0, 100); // percentage _setFreqDuty(); } @@ -50,29 +54,28 @@ void HeartBeat::setDutyCycle(float dutyCycle) void HeartBeat::beat() { - static uint8_t state = LOW; if (_running == false) { - state = LOW; + _state = LOW; } else { uint32_t now = micros(); - if ((state == LOW) && (now - _lastHeartBeat) < _dutyCycleLow) return; - if ((state == HIGH) && (now - _lastHeartBeat) < _dutyCycleHigh) return; + if ((_state == LOW) && (now - _lastHeartBeat) < _dutyCycleLow) return; + if ((_state == HIGH) && (now - _lastHeartBeat) < _dutyCycleHigh) return; _lastHeartBeat = now; - state = !state; + _state = !_state; } - digitalWrite(_pin, state); + digitalWrite(_pin, _state); } ///////////////////////////////// - - +// +// PRIVATE +// void HeartBeat::_setFreqDuty() { - _lastHeartBeat = 0; float time = 10000.0/_frequency; _dutyCycleHigh = round(_dutyCycle * time); _dutyCycleLow = round((100 - _dutyCycle) * time); @@ -80,3 +83,4 @@ void HeartBeat::_setFreqDuty() // -- END OF FILE -- + diff --git a/libraries/HeartBeat/HeartBeat.h b/libraries/HeartBeat/HeartBeat.h index 37762642..a3a7218b 100644 --- a/libraries/HeartBeat/HeartBeat.h +++ b/libraries/HeartBeat/HeartBeat.h @@ -2,7 +2,7 @@ // // FILE: HeartBeat.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.3 +// VERSION: 0.2.0 // PURPOSE: Arduino library for HeartBeat with frequency and dutyCycle // DATE: 2019-06-12 // URL: https://github.com/RobTillaart/HeartBeat @@ -10,34 +10,40 @@ #include "Arduino.h" -#define HEARTBEAT_LIB_VERSION (F("0.1.3")) +#define HEARTBEAT_LIB_VERSION (F("0.2.0")) + class HeartBeat { public: HeartBeat(); - void begin(const uint8_t pin, float frequency = 1.0); - void setFrequency(float frequency = 1.0); - void setDutyCycle(float dutyCycle = 50); + void begin(const uint8_t pin, float frequency = 1.0); + + void setFrequency(float frequency = 1.0); + void setDutyCycle(float dutyCycle = 50); + float getFrequency() { return _frequency; }; + float getDutyCycle() { return _dutyCycle; }; inline void enable() { _running = true; }; inline void disable() { _running = false; }; - void beat(); - - // OBSOLETE in next release - void set(float frequency = 1.0) { setFrequency(frequency); }; + void beat(); + uint8_t getState() { return _state; }; protected: void _setFreqDuty(); - float _frequency; - float _dutyCycle; - uint32_t _lastHeartBeat; - uint32_t _dutyCycleHigh; - uint32_t _dutyCycleLow; - bool _running; - uint8_t _pin; + + float _frequency = 1.0; + float _dutyCycle = 50; + + uint32_t _lastHeartBeat = 0; + uint32_t _dutyCycleHigh = 500; + uint32_t _dutyCycleLow = 500; + + bool _running = false; + uint8_t _pin = 255; + uint8_t _state = LOW; }; diff --git a/libraries/HeartBeat/README.md b/libraries/HeartBeat/README.md index 33dae985..497e9e40 100644 --- a/libraries/HeartBeat/README.md +++ b/libraries/HeartBeat/README.md @@ -1,18 +1,20 @@ [![Arduino CI](https://github.com/RobTillaart/HeartBeat/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![Arduino-lint](https://github.com/RobTillaart/HeartBeat/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/HeartBeat/actions/workflows/arduino-lint.yml) +[![JSON check](https://github.com/RobTillaart/HeartBeat/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/HeartBeat/actions/workflows/jsoncheck.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/HeartBeat/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/HeartBeat.svg?maxAge=3600)](https://github.com/RobTillaart/HeartBeat/releases) # HeartBeat -Arduino library for a HeartBeat with frequency and dutycycle +Arduino library for a HeartBeat with frequency and duty cycle. ## Description -The hearbeat library offers a simple HeartBeat by sending pulses to -a digital pin. Typical usage is to blink a (built in) LED as indicater +The heartbeat library offers a simple HeartBeat by sending pulses to +a digital pin. Typical usage is to blink a (built in) LED as indicator a program is still alive. The library uses no hardware timer and is based upon micros() so the user @@ -24,35 +26,39 @@ can be used as a first level debugging tool. Different frequencies can indicate a different state of the program or a different level of some sensor. No heart beat indicates the program is stuck or blocked. -The current version does not allow to differ the ON and OFF time, -this might be an option in a future version. - For more complex patterns, please check my pulsePattern library. ## Interface -The interface consists of a small set of +The interface consists of the following functions: - **HeartBeat()** constructor -- **begin(uint8_t pin, float freq)** to configure the HeartBeat. +- **void begin(uint8_t pin, float frequency)** to configure the HeartBeat. The default frequency is 1.0 -- **setFrequency(float freq)** change the frequency of the pulsing. -Will not enable or disable the HeartBeat. Must be > 0.0 -- **setDutyCycle(float dutyCycle = 50)** duty cycle in percentage time HIGH. -Must be between 0.00 and 100.0. -- **enable()** enable the pulsing -- **disable()** disable the pulsing; will switch of the LED. -- **beat()** the worker; this function checks if the HeartBeat is enabled +- **void setFrequency(float frequency)** change the frequency of the pulsing. +Will not enable or disable the HeartBeat. +Must be > 0.001 otherwise it will be constrained to 0.001. +On the upper side values beyond 10 Hz are hard for humans but are allowed. +- **void setDutyCycle(float dutyCycle = 50)** duty cycle in percentage time HIGH. +Must be between 0.00 and 100.0. A value of 0 will put the heartbeat effectively off. +- **float getFrequency()** returns set frequency (or constrained value). +- **float getDutyCycle()** returns set duty cycle (or constrained value). +- **void enable()** enable the pulsing. +- **void disable()** disable the pulsing; will switch of the LED. +- **void beat()** the worker; this function checks if the HeartBeat is enabled and the LED must be toggled. It must be called as often as possible to keep a steady pace, at least 4 times the given frequency. -Not calling **beat()** effectively stops the hearbeat. +Not calling **beat()** effectively stops the heartbeat. +- **uint8_t getState()** returns the state of the heartbeat. +Useful for debugging. #### Obsolete -- **set(float freq)** Will become obsolete, use **setFrequency()** instead. +- **set(float freq)** is replaced by **setFrequency()**. + ## Applications @@ -62,13 +68,22 @@ Applications include but are not limited to - 1 Hz = OK - 2 Hz = Warning - 5 Hz = Error -- indicate power usage by increasing HeartBeat e.g. round(amps) -- indicate sound volume by increasing HeartBeat -- indicate changing distant by hearbeat -- geiger counter style. + - no signal would indicate also an error. +- indicate power e.g. round(amps) - you might need to map the range! +- indicate the volume by increasing heartBeat +- indicate a changing distant - increasing or decreasing. +- Geiger counter style. ## Operation See examples + +## Future + +- differentiate between ON / OFF time +- improve documentation +- add examples +- + diff --git a/libraries/HeartBeat/examples/HearBeat_dutyCycle/HearBeat_dutyCycle.ino b/libraries/HeartBeat/examples/HearBeat_dutyCycle/HearBeat_dutyCycle.ino index 1b8c7ddc..70538f23 100644 --- a/libraries/HeartBeat/examples/HearBeat_dutyCycle/HearBeat_dutyCycle.ino +++ b/libraries/HeartBeat/examples/HearBeat_dutyCycle/HearBeat_dutyCycle.ino @@ -16,8 +16,13 @@ void setup() Serial.println(__FILE__); Serial.println(HEARTBEAT_LIB_VERSION); - HB.begin(13, 3); // PIN 13 with frequency 3 - HB.setDutyCycle(10.0); // 10% time high + HB.begin(13, 3); // PIN 13 with frequency 3 + HB.setDutyCycle(17.53); + + Serial.print("FR: "); + Serial.println(HB.getFrequency()); + Serial.print("DC: "); + Serial.println(HB.getDutyCycle()); } diff --git a/libraries/HeartBeat/examples/arrayHeartBeat/arrayHeartBeat.ino b/libraries/HeartBeat/examples/arrayHeartBeat/arrayHeartBeat.ino index 706631c7..c3401555 100644 --- a/libraries/HeartBeat/examples/arrayHeartBeat/arrayHeartBeat.ino +++ b/libraries/HeartBeat/examples/arrayHeartBeat/arrayHeartBeat.ino @@ -19,6 +19,9 @@ void setup() HB[0].begin(11, 1); HB[1].begin(12, 3); HB[2].begin(13, 5); + HB[0].setDutyCycle(20); + HB[1].setDutyCycle(30); + HB[2].setDutyCycle(40); } diff --git a/libraries/HeartBeat/examples/randomHeartBeat/randomHeartBeat.ino b/libraries/HeartBeat/examples/randomHeartBeat/randomHeartBeat.ino index 3052f38f..2aee9ee3 100644 --- a/libraries/HeartBeat/examples/randomHeartBeat/randomHeartBeat.ino +++ b/libraries/HeartBeat/examples/randomHeartBeat/randomHeartBeat.ino @@ -31,7 +31,7 @@ void loop() { lastUpdate = millis(); float f = 1.0 + 0.1 * random(50); - HB.set(f); + HB.setFrequency(f); } // do other stuff here diff --git a/libraries/HeartBeat/examples/sinusHeartBeat/sinusHeartBeat.ino b/libraries/HeartBeat/examples/sinusHeartBeat/sinusHeartBeat.ino index 48a229e5..ffaef152 100644 --- a/libraries/HeartBeat/examples/sinusHeartBeat/sinusHeartBeat.ino +++ b/libraries/HeartBeat/examples/sinusHeartBeat/sinusHeartBeat.ino @@ -9,8 +9,6 @@ HeartBeat HB; -int frequency = 1; - void setup() { @@ -18,7 +16,7 @@ void setup() Serial.println(__FILE__); Serial.println(HEARTBEAT_LIB_VERSION); - HB.begin(13, frequency); + HB.begin(13, 1.0); } @@ -26,9 +24,9 @@ void loop() { HB.beat(); - float f = 5 * (1 + sin(millis()/10000)); - HB.set(f); - + float frequency = 5 * (1 + sin(millis()/1000.0)); + HB.setFrequency(frequency); + // Serial.println(frequency, 6); // do other stuff here } diff --git a/libraries/HeartBeat/keywords.txt b/libraries/HeartBeat/keywords.txt index 5fc23a29..d7c9dea5 100644 --- a/libraries/HeartBeat/keywords.txt +++ b/libraries/HeartBeat/keywords.txt @@ -1,6 +1,6 @@ -# Syntax Coloring Map For HeartBeat +# Syntax Colouring Map For HeartBeat -# Datatypes (KEYWORD1) +# Data types (KEYWORD1) HeartBeat KEYWORD1 @@ -8,6 +8,8 @@ HeartBeat KEYWORD1 begin KEYWORD2 setFrequency KEYWORD2 setDutyCycle KEYWORD2 +getFrequency KEYWORD2 +getDutyCycle KEYWORD2 enable KEYWORD2 disable KEYWORD2 beat KEYWORD2 diff --git a/libraries/HeartBeat/library.json b/libraries/HeartBeat/library.json index ded2559b..030ca258 100644 --- a/libraries/HeartBeat/library.json +++ b/libraries/HeartBeat/library.json @@ -1,7 +1,7 @@ { "name": "HeartBeat", "keywords": "HeartBeat, blinking led, polling", - "description": "Arduino library for HeartBeat with frequency and dutyCycle.", + "description": "Arduino library for HeartBeat with frequency and duty cycle.", "authors": [ { @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/HeartBeat.git" }, - "version": "0.1.3", + "version": "0.2.0", "license": "MIT", "frameworks": "arduino", "platforms": "*" diff --git a/libraries/HeartBeat/library.properties b/libraries/HeartBeat/library.properties index 98905af1..b5ec7934 100644 --- a/libraries/HeartBeat/library.properties +++ b/libraries/HeartBeat/library.properties @@ -1,8 +1,8 @@ name=HeartBeat -version=0.1.3 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart -sentence=Arduino library for HeartBeat with frequency and dutyCycle. +sentence=Arduino library for HeartBeat with frequency and duty cycle. paragraph=blinking led by frequent polling. category=Other url=https://github.com/RobTillaart/HeartBeat diff --git a/libraries/HeartBeat/test/unit_test_001.cpp b/libraries/HeartBeat/test/unit_test_001.cpp index 6c489895..98313d2d 100644 --- a/libraries/HeartBeat/test/unit_test_001.cpp +++ b/libraries/HeartBeat/test/unit_test_001.cpp @@ -49,7 +49,7 @@ unittest_teardown() unittest(test_constructor) { - fprintf(stderr, "VERSION: %s\n", HEARTBEAT_LIB_VERSION ); + fprintf(stderr, "VERSION: %s\n", (char *) HEARTBEAT_LIB_VERSION ); GodmodeState* state = GODMODE(); state->reset(); @@ -79,6 +79,32 @@ unittest(test_constructor) } +unittest(test_frequency) +{ + HeartBeat HB; + HB.begin(13, 4); + + assertEqualFloat(4, HB.getFrequency(), 0.001); + for (int fr = 1; fr < 10; fr++) + { + HB.setFrequency(fr); + assertEqualFloat(fr, HB.getFrequency(), 0.001); + } +} + + +unittest(test_dutycycle) +{ + HeartBeat HB; + HB.begin(13, 1); + + for (int dc = 10; dc < 101; dc += 10) + { + HB.setDutyCycle(dc); + assertEqualFloat(dc, HB.getDutyCycle(), 0.001); + } +} + unittest_main() // --------