187 lines
5 KiB
Arduino
187 lines
5 KiB
Arduino
|
/*
|
||
|
* CopyLeft 2022 Pascal Engélibert
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License.
|
||
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
||
|
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
|
||
|
*/
|
||
|
|
||
|
// Telephone Jukebox
|
||
|
//
|
||
|
// Three types of output may be used, by configuring the code below.
|
||
|
//
|
||
|
// 1: Digital I2S - Normally used with the audio shield:
|
||
|
// http://www.pjrc.com/store/teensy3_audio.html
|
||
|
//
|
||
|
// 2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
|
||
|
// https://www.oshpark.com/shared_projects/KcDBKHta
|
||
|
//
|
||
|
// 3: Analog DAC - Connect the DAC pin to an amplified speaker
|
||
|
// http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
|
||
|
//
|
||
|
// To configure the output type, first uncomment one of the three
|
||
|
// output objects. If not using the audio shield, comment out
|
||
|
// the sgtl5000_1 lines in setup(), so it does not wait forever
|
||
|
// trying to configure the SGTL5000 codec chip.
|
||
|
//
|
||
|
// The SD card may connect to different pins, depending on the
|
||
|
// hardware you are using. Uncomment or configure the SD card
|
||
|
// pins to match your hardware.
|
||
|
//
|
||
|
// Two pins can be used (one for hang up detection and one for counting), but it also works well with only one pin for both functions.
|
||
|
|
||
|
#include <Audio.h>
|
||
|
#include <Wire.h>
|
||
|
#include <SPI.h>
|
||
|
#include <SD.h>
|
||
|
#include <SerialFlash.h>
|
||
|
|
||
|
//#define PIN_PULSE 4
|
||
|
#define PIN_STOP 2
|
||
|
|
||
|
#define PULSE_DELAY 100
|
||
|
|
||
|
unsigned long pulses_end = 0;
|
||
|
unsigned int pulses = 0;
|
||
|
bool pulse = false;
|
||
|
|
||
|
AudioMixer4 mix2;
|
||
|
AudioMixer4 mix1;
|
||
|
AudioSynthWaveform waveform1;
|
||
|
AudioPlaySdWav wav1;
|
||
|
// Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
|
||
|
AudioOutputI2S phone1;
|
||
|
//AudioOutputSPDIF phone1;
|
||
|
//AudioOutputAnalog phone1;
|
||
|
//On Teensy LC, use this for the Teensy Audio Shield:
|
||
|
//AudioOutputI2Sslave phone1;
|
||
|
|
||
|
AudioConnection c1(waveform1, 0, mix1, 0);
|
||
|
AudioConnection c2(waveform1, 0, mix2, 0);
|
||
|
AudioConnection c3(wav1, 0, mix1, 1);
|
||
|
AudioConnection c4(wav1, 1, mix2, 1);
|
||
|
AudioConnection c5(mix1, 0, phone1, 0);
|
||
|
AudioConnection c6(mix2, 0, phone1, 1);
|
||
|
|
||
|
AudioControlSGTL5000 sgtl5000_1;
|
||
|
|
||
|
// Use these with the Teensy Audio Shield
|
||
|
#define SDCARD_CS_PIN 10
|
||
|
#define SDCARD_MOSI_PIN 7
|
||
|
#define SDCARD_SCK_PIN 14
|
||
|
|
||
|
// Use these with the Teensy 3.5 & 3.6 SD card
|
||
|
//#define SDCARD_CS_PIN BUILTIN_SDCARD
|
||
|
//#define SDCARD_MOSI_PIN 11 // not actually used
|
||
|
//#define SDCARD_SCK_PIN 13 // not actually used
|
||
|
|
||
|
// Use these for the SD+Wiz820 or other adaptors
|
||
|
//#define SDCARD_CS_PIN 4
|
||
|
//#define SDCARD_MOSI_PIN 11
|
||
|
//#define SDCARD_SCK_PIN 13
|
||
|
|
||
|
void setup() {
|
||
|
pinMode(PIN_STOP, INPUT_PULLUP);
|
||
|
//pinMode(PIN_PULSE, INPUT_PULLUP);
|
||
|
|
||
|
AudioMemory(8);
|
||
|
|
||
|
// Comment these out if not using the audio adaptor board.
|
||
|
// This may wait forever if the SDA & SCL pins lack
|
||
|
// pullup resistors
|
||
|
sgtl5000_1.enable();
|
||
|
sgtl5000_1.volume(0.5);
|
||
|
|
||
|
waveform1.begin(WAVEFORM_SINE);
|
||
|
waveform1.frequency(440);
|
||
|
|
||
|
SPI.setMOSI(SDCARD_MOSI_PIN);
|
||
|
SPI.setSCK(SDCARD_SCK_PIN);
|
||
|
if(!SD.begin(SDCARD_CS_PIN)) {
|
||
|
// Error: Cannot read SD card
|
||
|
while(true) {
|
||
|
if(!digitalRead(PIN_STOP)) {
|
||
|
waveform1.amplitude(0.5);
|
||
|
delay(250);
|
||
|
waveform1.amplitude(0.0);
|
||
|
delay(1750);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void play_track(unsigned int track) {
|
||
|
char filename[16];
|
||
|
sprintf(filename, "%d.WAV", track);
|
||
|
wav1.play(filename);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
/*if(digitalRead(PIN_STOP)) {
|
||
|
waveform1.amplitude(0.0);
|
||
|
if(wav1.isPlaying()) {
|
||
|
wav1.stop();
|
||
|
}
|
||
|
} else {
|
||
|
if(wav1.isPlaying()) {
|
||
|
waveform1.amplitude(0.0);
|
||
|
} else {
|
||
|
waveform1.amplitude(0.5);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(digitalRead(PIN_PULSE)) {
|
||
|
if(!pulse) {
|
||
|
pulses ++;
|
||
|
pulse = true;
|
||
|
}
|
||
|
}
|
||
|
else if(pulse) {
|
||
|
pulse = false;
|
||
|
pulses_end = millis() + PULSE_DELAY;
|
||
|
}
|
||
|
else if(pulses) {
|
||
|
if(millis() >= pulses_end) {
|
||
|
if(!digitalRead(PIN_STOP))
|
||
|
play_track(pulses);
|
||
|
pulses = 0;
|
||
|
}
|
||
|
}*/
|
||
|
|
||
|
// If phone is hang up
|
||
|
if(digitalRead(PIN_STOP)) {
|
||
|
// Stop tone and music
|
||
|
waveform1.amplitude(0.0);
|
||
|
if(wav1.isPlaying()) {
|
||
|
wav1.stop();
|
||
|
}
|
||
|
|
||
|
if(!pulse) {
|
||
|
pulse = true;
|
||
|
pulses ++;
|
||
|
pulses_end = millis() + PULSE_DELAY;
|
||
|
}
|
||
|
else if(millis() > pulses_end) {
|
||
|
pulses = 0;
|
||
|
}
|
||
|
} else {
|
||
|
if(wav1.isPlaying()) {
|
||
|
// Stop tone
|
||
|
waveform1.amplitude(0.0);
|
||
|
} else {
|
||
|
// Start tone
|
||
|
waveform1.amplitude(0.5);
|
||
|
}
|
||
|
|
||
|
if(pulse) {
|
||
|
pulse = false;
|
||
|
pulses_end = millis() + PULSE_DELAY;
|
||
|
} else if(pulses && millis() > pulses_end) {
|
||
|
play_track(pulses % 10);
|
||
|
pulses = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delay(10);
|
||
|
}
|