ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
buzzer.cpp
Go to the documentation of this file.
1 /*
2  buzzer.cpp - ESP3D buzzer class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "../../include/esp3d_config.h"
22 
23 #ifdef BUZZER_DEVICE
24 #include <Ticker.h>
25 #include "buzzer.h"
26 #include "../../core/settings_esp3d.h"
27 #include "../../core/hal.h"
29 Ticker buzzer_tick;
30 #define BEEP_DURATION 200
31 #if defined(ARDUINO_ARCH_ESP8266)
32 extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration);
33 #endif //ARDUINO_ARCH_ESP8266
34 
35 
36 void process()
37 {
38  if (esp3d_buzzer.started()) {
39  tone_data * current = esp3d_buzzer.getNextTone();
40  if (current) {
41  Hal::toneESP(ESP3D_BUZZER_PIN,(unsigned int)current->frequency, (unsigned long) current->duration, false);
42  buzzer_tick.once_ms(current->duration, process);
43  }
44  }
45 }
46 
48 {
49  _head = nullptr;
50  _tail = nullptr;
51  _started = false;
52 }
53 
55 {
56  if(_started) {
57  end();
58  }
60  _started = true;
61  playsound(5000, 240);
62  playsound(3000, 120);
63  }
64  return _started;
65 }
66 void BuzzerDevice::end()
67 {
68  if(!_started) {
69  return;
70  }
71  purgeData();
72  _started = false;
73  no_tone();
74 }
75 
76 
78 {
79  //Nothing to do as handled by ticker
80 }
81 
82 void BuzzerDevice::beep(int count, int delay, int frequency)
83 {
84  if (_started) {
85  while (count > 0) {
86  playsound(frequency,BEEP_DURATION);
87  if (delay > 0 ) {
88  playsound(0,delay);
89  }
91  count--;
92  }
93  }
94 }
95 
96 void BuzzerDevice::no_tone()
97 {
98  Hal::no_tone(ESP3D_BUZZER_PIN);
99 }
100 
102 {
103  return !(_head == nullptr);
104 }
106 {
107  while (_head != nullptr) {
108  delay(10);
109  }
110 }
111 
112 bool BuzzerDevice::addToneToList(int frequency, int duration)
113 {
114  tone_data * tmp = (tone_data*)malloc(sizeof(tone_data));
115  bool startprocess = false;
116  if (tmp) {
117  tmp->_next = nullptr;
118  tmp->frequency = frequency;
119  tmp->duration = duration;
120  tmp->processing = false;
121  if (_tail) {
122  _tail->_next=tmp;
123  } else { //no tail means also no head
124  _head = tmp;
125  startprocess = true;//no ongoing list, so lets start it
126  }
127  _tail = tmp;
128  if(startprocess) {
129  process();
130  }
131  return true;
132  }
133  return false;
134 }
135 
137 {
138  if (_head) {
139  if (_head->processing == false) {
140  _head->processing = true;
141  } else {
142  tone_data * tmp = _head->_next;
143  free(_head);
144  _head = tmp;
145  if (!_head) {
146  _tail=_head;
147  no_tone();
148  }
149  }
150  }
151  return _head;
152 }
153 
154 void BuzzerDevice::purgeData()
155 {
156  while (_head) {
157  tone_data * tmp = _head->_next;
158  free(_head);
159  _head = tmp;
160  }
161  _tail = nullptr;
162 }
163 
165 {
166  end();
167 }
168 
169 void BuzzerDevice::playsound(int frequency, int duration)
170 {
171  if (_started) {
172  addToneToList(frequency, duration);
173  }
174 }
175 
176 #endif //BUZZER_DEVICE
tone_data
Definition: buzzer.h:24
BuzzerDevice::started
bool started()
Definition: buzzer.h:37
tone_data::processing
bool processing
Definition: buzzer.h:27
BuzzerDevice::handle
void handle()
BuzzerDevice::playsound
void playsound(int frequency, int duration)
buzzer.h
BuzzerDevice::waitWhilePlaying
void waitWhilePlaying()
tone_data::frequency
int frequency
Definition: buzzer.h:25
BuzzerDevice
Definition: buzzer.h:31
BuzzerDevice::beep
void beep(int count=1, int delay=0, int frequency=BEEP_FREQUENCY)
BuzzerDevice::end
void end()
ESP_BUZZER
#define ESP_BUZZER
Definition: settings_esp3d.h:50
BuzzerDevice::~BuzzerDevice
~BuzzerDevice()
tone_data::duration
int duration
Definition: buzzer.h:26
BuzzerDevice::begin
bool begin()
Settings_ESP3D::read_byte
static uint8_t read_byte(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:715
BuzzerDevice::BuzzerDevice
BuzzerDevice()
Hal::toneESP
static void toneESP(uint8_t pin, unsigned int frequency, unsigned int duration, bool sync=true)
Definition: hal.cpp:80
BuzzerDevice::isPlaying
bool isPlaying()
BuzzerDevice::getNextTone
tone_data * getNextTone()
Hal::no_tone
static void no_tone(uint8_t pin)
Definition: hal.cpp:99
esp3d_buzzer
BuzzerDevice esp3d_buzzer
tone_data::_next
tone_data * _next
Definition: buzzer.h:28