Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
metronome.hpp@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:dbad57390bd1 | 1 | #pragma once |
ram54288 | 0:dbad57390bd1 | 2 | |
ram54288 | 0:dbad57390bd1 | 3 | #include "mbed.h" |
ram54288 | 0:dbad57390bd1 | 4 | |
ram54288 | 0:dbad57390bd1 | 5 | class metronome |
ram54288 | 0:dbad57390bd1 | 6 | { |
ram54288 | 0:dbad57390bd1 | 7 | public: |
ram54288 | 0:dbad57390bd1 | 8 | enum { beat_samples = 30 }; |
ram54288 | 0:dbad57390bd1 | 9 | |
ram54288 | 0:dbad57390bd1 | 10 | public: |
ram54288 | 0:dbad57390bd1 | 11 | metronome() |
ram54288 | 0:dbad57390bd1 | 12 | : m_timing(false), m_beat_count(0) {} |
ram54288 | 0:dbad57390bd1 | 13 | ~metronome() {} |
ram54288 | 0:dbad57390bd1 | 14 | |
ram54288 | 0:dbad57390bd1 | 15 | public: |
ram54288 | 0:dbad57390bd1 | 16 | // Call when entering "learn" mode |
ram54288 | 0:dbad57390bd1 | 17 | void start_timing(); |
ram54288 | 0:dbad57390bd1 | 18 | // Call when leaving "learn" mode |
ram54288 | 0:dbad57390bd1 | 19 | void stop_timing(); |
ram54288 | 0:dbad57390bd1 | 20 | |
ram54288 | 0:dbad57390bd1 | 21 | // Should only record the current time when timing |
ram54288 | 0:dbad57390bd1 | 22 | // Insert the time at the next free position of m_beats |
ram54288 | 0:dbad57390bd1 | 23 | void tap(); |
ram54288 | 0:dbad57390bd1 | 24 | |
ram54288 | 0:dbad57390bd1 | 25 | bool is_timing() const { return m_timing; } |
ram54288 | 0:dbad57390bd1 | 26 | // Calculate the BPM from the deltas between m_beats |
ram54288 | 0:dbad57390bd1 | 27 | // Return 0 if there are not enough samples |
ram54288 | 0:dbad57390bd1 | 28 | size_t get_bpm() const; |
ram54288 | 0:dbad57390bd1 | 29 | |
ram54288 | 0:dbad57390bd1 | 30 | private: |
ram54288 | 0:dbad57390bd1 | 31 | bool m_timing; |
ram54288 | 0:dbad57390bd1 | 32 | Timer m_timer; |
ram54288 | 0:dbad57390bd1 | 33 | |
ram54288 | 0:dbad57390bd1 | 34 | // Insert new samples at the end of the array, removing the oldest |
ram54288 | 0:dbad57390bd1 | 35 | size_t m_beats[beat_samples]; |
ram54288 | 0:dbad57390bd1 | 36 | size_t m_beat_count; |
ram54288 | 0:dbad57390bd1 | 37 | }; |