From ac937989d747590ac4e65e05ac1db940485cccaa Mon Sep 17 00:00:00 2001 From: "Felix A. Epp" Date: Mon, 11 Jan 2021 12:11:55 +0200 Subject: [PATCH] Added a setter for beatlength with checking bounds --- src/ArduinoTapTempo.cpp | 19 +++++++++++++++++-- src/ArduinoTapTempo.h | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ArduinoTapTempo.cpp b/src/ArduinoTapTempo.cpp index 8b4c683..f61119e 100644 --- a/src/ArduinoTapTempo.cpp +++ b/src/ArduinoTapTempo.cpp @@ -43,9 +43,24 @@ float ArduinoTapTempo::getBPM() return 60000.0 / beatLengthMS; } -float ArduinoTapTempo::setBPM(float bpm) +void ArduinoTapTempo::setBPM(float bpm) { - beatLengthMS = 60000 / bpm; + setBeatLength(60000 / bpm); +} + +void ArduinoTapTempo::setBeatLength(unsigned long lengthMS) +{ + if (lengthMS > maxBeatLengthMS) + { + beatLengthMS = maxBeatLengthMS; + } + else if (lengthMS < minBeatLengthMS) { + beatLengthMS = minBeatLengthMS; + } + else + { + beatLengthMS = lengthMS; + } } bool ArduinoTapTempo::onBeat() diff --git a/src/ArduinoTapTempo.h b/src/ArduinoTapTempo.h index 55f54ad..d9c228a 100644 --- a/src/ArduinoTapTempo.h +++ b/src/ArduinoTapTempo.h @@ -38,12 +38,13 @@ class ArduinoTapTempo bool isChainActive(); // returns true if the current tap chain is still accepting new taps to fine tune the tempo bool isChainActive(unsigned long ms); // returns true if the current tap chain is still accepting new taps to fine tune the tempo float getBPM(); // returns the number of beats per minute - float setBPM(float bpm); // sets the number of beats per minute + void setBPM(float bpm); // sets the number of beats per minute float beatProgress(); // returns a float from 0.0 to 1.0 indicating the percent through the current beat void resetTapChain(); // resets the current chain of taps and sets the start of the bar to the current time void resetTapChain(unsigned long ms); // resets the current chain of taps and sets the start of the bar to the current time inline unsigned long getBeatLength() { return beatLengthMS; } // returns the length of the beat in milliseconds + void setBeatLength(unsigned long lengthMS); // sets the length of the beat in MS directly inline unsigned long getLastTapTime() { return lastTapMS; } // returns the time of the last tap in milliseconds since the program started void update(bool buttonDown); // call this each time you read your button state, accepts a boolean indicating if the button is down