telephone-jukebox/attiny402/telephone/telephone.ino
2023-01-14 00:09:05 +01:00

106 lines
2.8 KiB
C++

#include <DFMiniMp3.h>
/*
* CopyLeft 2022-2023 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
#define PIN_STOP PIN_PA1
#define PIN_TX PIN_PA6
#define PIN_RX PIN_PA7
#define PULSE_DELAY 100
unsigned long pulses_end = 0;
unsigned int pulses = 0;
bool pulse = false;
bool playing = false;
class Mp3Notify;
typedef DFMiniMp3<HardwareSerial, Mp3Notify> DfMp3;
DfMp3 dfmp3(Serial);
class Mp3Notify {
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
{
}
static void OnError([[maybe_unused]] DfMp3& mp3, uint16_t errorCode)
{
}
static void OnPlayFinished([[maybe_unused]] DfMp3& mp3, [[maybe_unused]] DfMp3_PlaySources source, uint16_t track) {
playing = false;
}
static void OnPlaySourceOnline([[maybe_unused]] DfMp3& mp3, DfMp3_PlaySources source) {}
static void OnPlaySourceInserted([[maybe_unused]] DfMp3& mp3, DfMp3_PlaySources source) {}
static void OnPlaySourceRemoved([[maybe_unused]] DfMp3& mp3, DfMp3_PlaySources source) {}
};
void wait_ms(uint16_t msWait) {
uint32_t start = millis();
while((millis() - start) < msWait) {
// if you have loops with delays, its important to
// call dfmp3.loop() periodically so it allows for notifications
// to be handled without interrupts
dfmp3.loop();
delay(1);
}
}
void setup() {
pinMode(PIN_STOP, INPUT_PULLUP);
//Serial.begin(9600, SERIAL_8N1);
dfmp3.begin();
dfmp3.setVolume(24);
//dfmp3.setPlaybackSource(DfMp3_PlaySource_Sd);
wait_ms(500);
dfmp3.playMp3FolderTrack(0);
wait_ms(1000);
}
void loop() {
// If phone is hang up
if(digitalRead(PIN_STOP)) {
// Stop tone and music
if(playing) {
dfmp3.stop();
//dfmp3.sleep();
playing = false;
}
if(!pulse) {
pulse = true;
pulses ++;
pulses_end = millis() + PULSE_DELAY;
}
else if(millis() > pulses_end) {
pulses = 0;
}
} else {
if(!playing) {
// Start tone
//dfmp3.awake();
//dfmp3.loopGlobalTrack(99);
dfmp3.playMp3FolderTrack(99);
playing = true;
}
if(pulse) {
pulse = false;
pulses_end = millis() + PULSE_DELAY;
} else if(pulses && millis() > pulses_end) {
dfmp3.playMp3FolderTrack(pulses % 10);
pulses = 0;
}
}
wait_ms(10);
}