Ram Gandikota / Mbed OS ABCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers metronome.hpp Source File

metronome.hpp

00001 #pragma once
00002 
00003 #include "mbed.h"
00004 
00005 class metronome
00006 {
00007 public:
00008     enum { beat_samples = 30 };
00009 
00010 public:
00011     metronome()
00012     : m_timing(false), m_beat_count(0) {}
00013     ~metronome() {}
00014 
00015 public:
00016     // Call when entering "learn" mode
00017     void start_timing();
00018     // Call when leaving "learn" mode
00019     void stop_timing();
00020 
00021     // Should only record the current time when timing
00022     // Insert the time at the next free position of m_beats
00023     void tap();
00024 
00025     bool is_timing() const { return m_timing; }
00026     // Calculate the BPM from the deltas between m_beats
00027     // Return 0 if there are not enough samples
00028     size_t get_bpm() const;
00029 
00030 private:
00031     bool m_timing;
00032     Timer m_timer;
00033 
00034     // Insert new samples at the end of the array, removing the oldest
00035     size_t m_beats[beat_samples];
00036     size_t m_beat_count;
00037 };