Pico-Arduino
PicoTone.h
1 #pragma once
2 
3 #include "PicoTimer.h"
4 #include "Tools/Map.h"
5 
6 namespace pico_arduino {
7 
8 // forward declaratioins
9 extern int64_t stop_tone_callback(alarm_id_t id, void *user_data);
10 
11 
17 class PicoTone {
18  public:
19  PicoTone(){
20  }
21 
22  ~PicoTone(){
23  noTone();
24  }
25 
26  static bool generate_sound_callback(repeating_timer_t *rt){
27  PicoTone *pt = (PicoTone*)rt->user_data;
28  pt->state = !pt->state; // toggle state
29  digitalWrite(pt->pin, pt->state);
30  return true;
31  }
32 
33  void tone(uint8_t pinNumber, unsigned int frequency) {
34  pin = pinNumber;
35  int delay = 1000 / frequency / 2;
36  alarm.start(generate_sound_callback, delay, MS, this);
37  }
38 
39  void noTone() {
40  alarm.stop();
41  }
42 
43  bool operator==(const PicoTone& other){
44  return other.pin == this->pin;
45  }
46 
47  bool operator!=(const PicoTone& other){
48  return other.pin != this->pin;
49  }
50 
51  int pin;
52  bool state;
53 
54  protected:
55  TimerAlarmRepeating alarm;
56 };
57 
58 
64  public:
65  static int64_t stop_tone_callback(alarm_id_t id, void *user_data){
66  // ugliy hack: we receive the pin number in user_data (not a pointer!)
67  PicoTone* ptr = (PicoTone*) user_data;
68  noTone(ptr->pin);
69  return 0;
70  }
71 
73  static void tone(uint8_t pinNumber, unsigned int frequency, int duration) {
74  Logger.debug("ArduinoPicoTone::tone");
75  PicoTone ptone = pinMap().get(pinNumber);
76  if (ptone==empyTone()){
77  // add entry
78  pinMap().put(pinNumber, ptone);
79  }
80  ptone.tone(pinNumber, frequency);
81  add_alarm_in_ms(duration, stop_tone_callback, &ptone , false);
82  }
83 
85  static void noTone(uint8_t pinNumber) {
86  Logger.debug("ArduinoPicoTone::noTone");
87  // find the actual PicoTone with the pin number
88  PicoTone ptone = pinMap().get(pinNumber);
89  if (ptone!=empyTone()){
90  ptone.noTone();
91  }
92  }
93 
94  static const PicoTone empyTone() {
95  static const PicoTone EMPTY_TONE;
96  return EMPTY_TONE;
97  }
98 
101  static Map<int, PicoTone> ArduinoPicoTonePinMap(empyTone());
102  return ArduinoPicoTonePinMap;
103  }
104 };
105 
106 }
ArduinoPicoTone provides static methods for PicoTone.
Definition: PicoTone.h:63
static void noTone(uint8_t pinNumber)
static interface which supports multipe pins
Definition: PicoTone.h:85
static void tone(uint8_t pinNumber, unsigned int frequency, int duration)
static interface which supports multipe pins concurrently
Definition: PicoTone.h:73
static Map< int, PicoTone > pinMap()
for static interface
Definition: PicoTone.h:100
A simple key value map collection.
Definition: Map.h:14
virtual void debug(const char *str, const char *str1=nullptr, const char *str2=nullptr)
writes an debug message
Definition: PicoLogger.h:60
We use the TimerAlarmRepeating to generate tones.
Definition: PicoTone.h:17
Repeating Timer functions for simple scheduling of repeated execution.
Definition: PicoTimer.h:58
bool stop()
stops the execution
Definition: PicoTimer.h:84
bool start(repeating_timer_callback_t callback, uint64_t time, TimeUnit unit=MS, void *user_data=nullptr)
starts the repeated exection of the callback methods in the indicate period
Definition: PicoTimer.h:70
Pico Arduino Framework.
Definition: Arduino.cpp:26