|
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 | 30 |
|
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 |
| - } |
| 31 | +static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration) { |
| 32 | + if (_pin > 16) { |
| 33 | + return; |
43 | 34 | }
|
44 | 35 |
|
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 |
| - } |
52 |
| - } |
| 36 | + pinMode(_pin, OUTPUT); |
53 | 37 |
|
54 |
| - return _index; |
55 |
| -} |
| 38 | + high = std::max(high, (uint32_t)100); |
| 39 | + low = std::max(low, (uint32_t)100); |
56 | 40 |
|
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 |
| - } |
| 41 | + if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) { |
| 42 | + _toneMap |= 1 << _pin; |
95 | 43 | }
|
96 | 44 | }
|
97 | 45 |
|
98 |
| -void disableTimer(uint8_t _index) { |
99 |
| - tone_pins[_index] = 255; |
100 |
| - |
101 |
| - switch (tone_timers[_index]) { |
102 |
| - case 0: |
103 |
| - // Not currently supported |
104 |
| - break; |
105 | 46 |
|
106 |
| - case 1: |
107 |
| - timer1_disable(); |
108 |
| - break; |
| 47 | +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { |
| 48 | + if (frequency == 0) { |
| 49 | + noTone(_pin); |
| 50 | + } else { |
| 51 | + uint32_t period = 1000000L / frequency; |
| 52 | + uint32_t high = period / 2; |
| 53 | + uint32_t low = period - high; |
| 54 | + _startTone(_pin, high, low, duration); |
109 | 55 | }
|
110 | 56 | }
|
111 | 57 |
|
112 |
| -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 |
| - } |
| 58 | + |
| 59 | +// Separate tone(float) to hopefully not pull in floating point libs unless |
| 60 | +// it's called with a float. |
| 61 | +void tone(uint8_t _pin, double frequency, unsigned long duration) { |
| 62 | + if (frequency < 1.0) { // FP means no exact comparisons |
| 63 | + noTone(_pin); |
| 64 | + } else { |
| 65 | + double period = 1000000.0 / frequency; |
| 66 | + uint32_t high = (uint32_t)((period / 2.0) + 0.5); |
| 67 | + uint32_t low = (uint32_t)(period + 0.5) - high; |
| 68 | + _startTone(_pin, high, low, duration); |
119 | 69 | }
|
120 |
| - digitalWrite(_pin, LOW); |
121 | 70 | }
|
122 | 71 |
|
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); |
| 72 | + |
| 73 | +void noTone(uint8_t _pin) { |
| 74 | + if (_pin > 16) { |
| 75 | + return; |
135 | 76 | }
|
| 77 | + stopWaveform(_pin); |
| 78 | + _toneMap &= ~(1 << _pin); |
| 79 | + digitalWrite(_pin, 0); |
136 | 80 | }
|
0 commit comments