Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
melody.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 18.11.2013 23:06:31 CET
3 // File: melody.hpp
4 
5 #ifndef __MELODY_HEADER
6 #define __MELODY_HEADER
7 
8 #include <ustd.hpp>
9 #include <notes.hpp>
10 #include <tool/clock.hpp>
11 #include <tool/pin_concept.hpp>
12 
13 namespace tool {
14  template<typename pin_concept>
15  class melody_class {
16  public:
17  melody_class(): t_start_(0), current_note_(1), rep_(false), unit_dur_(250), cutoff_(10) {
18  }
20  ASSERT(notes.size() == dur.size())
21 
22  notes_.clear();
23  dur_.clear();
24 
25  for(auto n : notes)
26  notes_.push_back(n);
27  for(auto d : dur)
28  dur_.push_back(d);
29 
30  current_note_ = notes.size() + 1;
31  }
32  void set_unit_dur(uint16_t const & unit_dur) {
33  unit_dur_ = unit_dur;
34  }
35  void set_cutoff(uint16_t const & cutoff) {
36  cutoff_ = cutoff;
37  }
38  void play_blocking() {
39  for(uint8_t i = 0; i < notes_.size(); ++i) {
40  pin_.tone(notes_[i], dur_[i] * unit_dur_ - cutoff_);
41  delay(dur_[i] * unit_dur_);
42  }
43  pin_.no_tone();
44  }
45  void repeat(bool const & rep) {
46  rep_ = rep;
47  }
48  void play() {
49  if(notes_.size() == 0)
50  return;
51 
52  current_note_ = 0;
53 
54  t_start_ = tool::clock.millis();
55  pin_.tone(notes_[current_note_], dur_[current_note_] * unit_dur_ - cutoff_);
56  }
57  void update() {
58  if(current_note_ > notes_.size())
59  return;
60  if(current_note_ == notes_.size()) {
61  if(rep_)
62  play();
63  else
64  ++current_note_;
65  return;
66  }
67 
68  if(tool::clock.millis() - t_start_ >= dur_[current_note_] * unit_dur_) {
69  ++current_note_;
70  if(current_note_ == notes_.size())
71  return;
72  t_start_ = tool::clock.millis();
73  pin_.tone(notes_[current_note_], dur_[current_note_] * unit_dur_ - cutoff_);
74  }
75  }
76  private:
77  pin_concept pin_;
78  double t_start_;
79  uint8_t current_note_;
80  bool rep_;
81 
84  uint16_t unit_dur_;
85  uint16_t cutoff_;
86  };
87 
88 }//end namespace tool
89 
90 #endif //__MELODY_HEADER
constexpr size_type size() const noexcept
Definition: initializer_list.hpp:36
void play()
Definition: melody.hpp:48
void set_cutoff(uint16_t const &cutoff)
Definition: melody.hpp:35
class tool::clock_class clock
Definition: melody.hpp:15
size_type const & size() const
Definition: vector.hpp:110
void push_back(T const &in)
Definition: vector.hpp:50
void update()
Definition: melody.hpp:57
uint32_t millis() const
Definition: clock.hpp:35
void set_melody(std::initializer_list< note::note_enum > const &notes, std::initializer_list< uint8_t > const &dur)
Definition: melody.hpp:19
initializer_list
Definition: initializer_list.hpp:12
void set_unit_dur(uint16_t const &unit_dur)
Definition: melody.hpp:32
void clear()
Definition: vector.hpp:68
melody_class()
Definition: melody.hpp:17
void play_blocking()
Definition: melody.hpp:38
void repeat(bool const &rep)
Definition: melody.hpp:45
#define ASSERT(exp)
Definition: ard_assert.hpp:33