Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple tone(), analogWrite(), and Servo #4640

Merged
merged 13 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 100000
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void tone(uint8_t _pin, double frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

// WMath prototypes
Expand Down
132 changes: 38 additions & 94 deletions cores/esp8266/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

A Tone Generator Library for the ESP8266

Copyright (c) 2016 Ben Pirt. All rights reserved.
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
Expand All @@ -22,115 +22,59 @@
*/

#include "Arduino.h"
#include "pins_arduino.h"
#include "core_esp8266_waveform.h"

#define AVAILABLE_TONE_PINS 1
const uint8_t tone_timers[] = { 1 };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255, };
static long toggle_counts[AVAILABLE_TONE_PINS] = { 0, };
#define T1INDEX 0
// Which pins have a tone running on them?
static uint32_t _toneMap = 0;

void t1IntHandler();

static int8_t toneBegin(uint8_t _pin) {
int8_t _index = -1;

// if we're already using the pin, reuse it.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
return i;
}
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration) {
if (_pin > 16) {
return;
}

// search for an unused timer.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == 255) {
tone_pins[i] = _pin;
_index = i;
break;
}
}
pinMode(_pin, OUTPUT);

return _index;
}
high = std::max(high, (uint32_t)100);
low = std::max(low, (uint32_t)100);

// frequency (in hertz) and duration (in milliseconds).
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
int8_t _index;

_index = toneBegin(_pin);

if (_index >= 0) {
// Set the pinMode as OUTPUT
pinMode(_pin, OUTPUT);

// Alternate handling of zero freqency to avoid divide by zero errors
if (frequency == 0)
{
noTone(_pin);
return;
}

// Calculate the toggle count
if (duration > 0) {
toggle_counts[_index] = 2 * frequency * duration / 1000;
} else {
toggle_counts[_index] = -1;
}

// set up the interrupt frequency
switch (tone_timers[_index]) {
case 0:
// Not currently supported
break;

case 1:
timer1_disable();
timer1_isr_init();
timer1_attachInterrupt(t1IntHandler);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency);
break;
}
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
_toneMap |= 1 << _pin;
}
}

void disableTimer(uint8_t _index) {
tone_pins[_index] = 255;

switch (tone_timers[_index]) {
case 0:
// Not currently supported
break;

case 1:
timer1_disable();
break;
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
if (frequency == 0) {
noTone(_pin);
} else {
uint32_t period = 1000000L / frequency;
uint32_t high = period / 2;
uint32_t low = period - high;
_startTone(_pin, high, low, duration);
}
}

void noTone(uint8_t _pin) {
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
tone_pins[i] = 255;
disableTimer(i);
break;
}

// Separate tone(float) to hopefully not pull in floating point libs unless
// it's called with a float.
void tone(uint8_t _pin, double frequency, unsigned long duration) {
if (frequency < 1.0) { // FP means no exact comparisons
noTone(_pin);
} else {
double period = 1000000.0 / frequency;
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
uint32_t low = (uint32_t)(period + 0.5) - high;
_startTone(_pin, high, low, duration);
}
digitalWrite(_pin, LOW);
}

ICACHE_RAM_ATTR void t1IntHandler() {
if (toggle_counts[T1INDEX] != 0){
// toggle the pin
digitalWrite(tone_pins[T1INDEX], toggle_counts[T1INDEX] % 2);
toggle_counts[T1INDEX]--;
// handle the case of indefinite duration
if (toggle_counts[T1INDEX] < -2){
toggle_counts[T1INDEX] = -1;
}
}else{
disableTimer(T1INDEX);
digitalWrite(tone_pins[T1INDEX], LOW);

void noTone(uint8_t _pin) {
if (_pin > 16) {
return;
}
stopWaveform(_pin);
_toneMap &= ~(1 << _pin);
digitalWrite(_pin, 0);
}
Empty file modified cores/esp8266/base64.cpp
100755 → 100644
Empty file.
Empty file modified cores/esp8266/base64.h
100755 → 100644
Empty file.
Loading