|
3 | 3 |
|
4 | 4 | A Tone Generator Library for the ESP8266
|
5 | 5 |
|
6 |
| - Copyright (c) 2016 Ben Pirt. All rights reserved. |
| 6 | + Original Copyright (c) 2016 Ben Pirt. All rights reserved. |
7 | 7 | This file is part of the esp8266 core for Arduino environment.
|
8 | 8 |
|
9 | 9 | This library is free software; you can redistribute it and/or
|
|
22 | 22 | */
|
23 | 23 |
|
24 | 24 | #include "Arduino.h"
|
25 |
| -#include "pins_arduino.h" |
| 25 | +#include "core_esp8266_waveform.h" |
26 | 26 |
|
27 |
| -#define AVAILABLE_TONE_PINS 1 |
28 |
| -const uint8_t tone_timers[] = { 1 }; |
29 |
| -static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255, }; |
30 |
| -static long toggle_counts[AVAILABLE_TONE_PINS] = { 0, }; |
31 |
| -#define T1INDEX 0 |
| 27 | +// Which pins have a tone running on them? |
| 28 | +static uint32_t _toneMap = 0; |
32 | 29 |
|
33 |
| -void t1IntHandler(); |
34 |
| - |
35 |
| -static int8_t toneBegin(uint8_t _pin) { |
36 |
| - int8_t _index = -1; |
37 |
| - |
38 |
| - // if we're already using the pin, reuse it. |
39 |
| - for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { |
40 |
| - if (tone_pins[i] == _pin) { |
41 |
| - return i; |
42 |
| - } |
| 30 | +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { |
| 31 | + if (_pin > 16) { |
| 32 | + return; |
43 | 33 | }
|
44 | 34 |
|
45 |
| - // search for an unused timer. |
46 |
| - for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { |
47 |
| - if (tone_pins[i] == 255) { |
48 |
| - tone_pins[i] = _pin; |
49 |
| - _index = i; |
50 |
| - break; |
51 |
| - } |
| 35 | + if (frequency == 0) { |
| 36 | + noTone(_pin); |
| 37 | + return; |
52 | 38 | }
|
53 | 39 |
|
54 |
| - return _index; |
55 |
| -} |
56 |
| - |
57 |
| -// frequency (in hertz) and duration (in milliseconds). |
58 |
| -void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { |
59 |
| - int8_t _index; |
60 |
| - |
61 |
| - _index = toneBegin(_pin); |
62 |
| - |
63 |
| - if (_index >= 0) { |
64 |
| - // Set the pinMode as OUTPUT |
65 |
| - pinMode(_pin, OUTPUT); |
66 |
| - |
67 |
| - // Alternate handling of zero freqency to avoid divide by zero errors |
68 |
| - if (frequency == 0) |
69 |
| - { |
70 |
| - noTone(_pin); |
71 |
| - return; |
72 |
| - } |
73 |
| - |
74 |
| - // Calculate the toggle count |
75 |
| - if (duration > 0) { |
76 |
| - toggle_counts[_index] = 2 * frequency * duration / 1000; |
77 |
| - } else { |
78 |
| - toggle_counts[_index] = -1; |
79 |
| - } |
80 |
| - |
81 |
| - // set up the interrupt frequency |
82 |
| - switch (tone_timers[_index]) { |
83 |
| - case 0: |
84 |
| - // Not currently supported |
85 |
| - break; |
86 |
| - |
87 |
| - case 1: |
88 |
| - timer1_disable(); |
89 |
| - timer1_isr_init(); |
90 |
| - timer1_attachInterrupt(t1IntHandler); |
91 |
| - timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); |
92 |
| - timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency); |
93 |
| - break; |
94 |
| - } |
| 40 | + uint32_t halfCycle = 500000L / frequency; |
| 41 | + if (halfCycle < 100) { |
| 42 | + halfCycle = 100; |
95 | 43 | }
|
96 |
| -} |
97 |
| - |
98 |
| -void disableTimer(uint8_t _index) { |
99 |
| - tone_pins[_index] = 255; |
100 | 44 |
|
101 |
| - switch (tone_timers[_index]) { |
102 |
| - case 0: |
103 |
| - // Not currently supported |
104 |
| - break; |
105 |
| - |
106 |
| - case 1: |
107 |
| - timer1_disable(); |
108 |
| - break; |
| 45 | + if (startWaveform(_pin, halfCycle, halfCycle, (uint32_t) duration * 1000)) { |
| 46 | + _toneMap |= 1 << _pin; |
109 | 47 | }
|
110 | 48 | }
|
111 | 49 |
|
112 | 50 | void noTone(uint8_t _pin) {
|
113 |
| - for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { |
114 |
| - if (tone_pins[i] == _pin) { |
115 |
| - tone_pins[i] = 255; |
116 |
| - disableTimer(i); |
117 |
| - break; |
118 |
| - } |
119 |
| - } |
120 |
| - digitalWrite(_pin, LOW); |
121 |
| -} |
122 |
| - |
123 |
| -ICACHE_RAM_ATTR void t1IntHandler() { |
124 |
| - if (toggle_counts[T1INDEX] != 0){ |
125 |
| - // toggle the pin |
126 |
| - digitalWrite(tone_pins[T1INDEX], toggle_counts[T1INDEX] % 2); |
127 |
| - toggle_counts[T1INDEX]--; |
128 |
| - // handle the case of indefinite duration |
129 |
| - if (toggle_counts[T1INDEX] < -2){ |
130 |
| - toggle_counts[T1INDEX] = -1; |
131 |
| - } |
132 |
| - }else{ |
133 |
| - disableTimer(T1INDEX); |
134 |
| - digitalWrite(tone_pins[T1INDEX], LOW); |
| 51 | + if (_pin > 16) { |
| 52 | + return; |
135 | 53 | }
|
| 54 | + stopWaveform(_pin); |
| 55 | + _toneMap &= ~(1 << _pin); |
| 56 | + digitalWrite(_pin, 0); |
136 | 57 | }
|
0 commit comments