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

Initial esp32 migration #224

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
67 changes: 67 additions & 0 deletions Firmware/AudioESP32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// ESP32 and DFPlayer Test

#include <Arduino.h>
#include <DFRobotDFPlayerMini.h>

DFRobotDFPlayerMini myDFPlayer;

#define Stop 34
#define Prev 33
#define Next 14

void setup() {
pinMode(Stop, INPUT);
pinMode(Prev, INPUT);
pinMode(Next, INPUT);

Serial2.begin(9600); //communicate with DFPlayer: RX = 16; TX = 17
Serial.begin(9600);

Serial.println();
Serial.println("DFRobot DFPlayer Mini Demo");
Serial.println("Inirializing DFPlayer ... (May take 3~5 seconds)");
delay(2000);

// Use hardwareSerial to communicate with DFPlayer
if (!myDFPlayer.begin(Serial2)) {
Serial.println("Unable to begin");
Serial.println("1. Please recheck the connection!");
Serial.println("2. Please insert the SD Card!");
while(true);
}

Serial.println("DFPlayer Mini online");
// Set serial communication time out 500ms
myDFPlayer.setTimeOut(500);

// Set volume value (0~30)
myDFPlayer.volume(20);

// ----Set different EQ
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);

// ----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);

// ----Mp3 Play Testing ----
myDFPlayer.randomAll();
}

void loop() {
if (!digitalRead(Stop)){
while (!digitalRead(Stop));
Serial.println("Stop");
myDFPlayer.stop();
}
if (!digitalRead(Prev)){

while (!digitalRead(Prev));
Serial.println("Previous");
myDFPlayer.previous();
}
if (!digitalRead(Next)) {
while (!digitalRead(Next));
Serial.println("Next");
myDFPlayer.next();
}
}
80 changes: 66 additions & 14 deletions Firmware/GPAD_API/GPAD_API.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "robot_api.h"
#include "gpad_utility.h"
#include "gpad_serial.h"
#include "dfplayer.h"

/* SPI_PERIPHERAL
From: https://circuitdigest.com/microcontroller-projects/arduino-spi-communication-tutorial
Expand Down Expand Up @@ -84,12 +85,21 @@
#define GPAD_VERSION1

#ifdef GPAD_VERSION1 //The Version 1 PCB.
//#define SS 7 // nCS aka /SS Input on GPAD Version 1 PCB.
#define LED_PIN PD3 // for GPAD LIGHT0
#define BUTTON_PIN PD2 //GPAD Button to GND, 10K Resistor to +5V.
//#define SS 7
// nCS aka /SS Input on GPAD Version 1 PCB.
//(Uno pin convention)
//#define LED_PIN PD3 // for GPAD LIGHT0

//(Uno pin convention)
//#define BUTTON_PIN PD2 //GPAD Button to GND, 10K Resistor to +5V.

#define LED_PIN 15
#define BUTTON_PIN 34
#else //The proof of concept wiring.


#define LED_PIN 7

#define BUTTON_PIN 2 //Button to GND, 10K Resistor to +5V.


Expand All @@ -106,6 +116,10 @@ volatile byte peripheralReceived ;
volatile bool procNewPacket = false;
volatile byte indx = 0;
volatile boolean process;
int progressIndicator=1;

int ledLevel;
const long AUDIO_WAIT_TIME=5000;

byte received_signal_raw_bytes[MAX_BUFFER_SIZE];

Expand All @@ -124,12 +138,19 @@ void setup_spi()
pinMode(MOSI, INPUT); //This works for Peripheral
pinMode(MISO, OUTPUT); //try this.
pinMode(SCK, INPUT); //Sets clock as input
SPCR |= _BV(SPE); //Turn on SPI in Peripheral Mode

//working on it separately for now
//SPCR |= _BV(SPE); //Turn on SPI in Peripheral Mode

// turn on interrupts
SPCR |= _BV(SPIE);

//working on it separately for now
//SPCR |= _BV(SPIE);

isReceived_SPI = false;
SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation


//SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation

}//end setup()

Expand All @@ -138,10 +159,12 @@ void setup_spi()
// I plan to add an index to this to handle the full message that we intend to receive.
// However, I think this also needs a timeout to handle the problem of getting out of synch.

ISR (SPI_STC_vect) //Inerrrput routine function
{
receive_byte(SPDR);
}//end ISR

//commenting out because attempting to compile for esp32
// ISR (SPI_STC_vect) //Inerrrput routine function
// {
// receive_byte(SPDR);
// }//end ISR


void receive_byte(byte c)
Expand Down Expand Up @@ -217,6 +240,7 @@ void updateWink(void) {


void setup() {

//Lets make the LED high near the start of setup for visual clue
pinMode(LED_BUILTIN, OUTPUT); // set the LED pin mode
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
Expand All @@ -229,7 +253,14 @@ void setup() {
Serial.println(VERSION);
robot_api_setup(&Serial);

setup_spi();

//uncomment later
//setup_spi();

//DF Player setup
dfPlayer_setup();



digitalWrite(LED_BUILTIN, LOW); // turn the LED off at end of setup

Expand All @@ -239,14 +270,35 @@ void setup() {
unsigned long last_ms = 0;
void loop() {

updateWink(); //The builtin LED
robot_api_loop();
//uncomment later
//updateWink(); //The builtin LED


//uncomment later
//robot_api_loop();

// This is causing a hang!

//uncomment later
processSerial(Serial);

// Now try to read from the SPI Port!
updateFromSPI();

//uncomment later
//updateFromSPI();
pinMode(2, OUTPUT);
pinMode(25, OUTPUT);
pinMode(24,OUTPUT);
pinMode(14, OUTPUT);
pinMode(26,OUTPUT);
pinMode(4, OUTPUT);



checkAlarmIteration(&Serial);




if (DEBUG > 1) {
unsigned long ms = millis();
Expand Down
2 changes: 1 addition & 1 deletion Firmware/GPAD_API/alarm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const char *AlarmNames[] = { "OK ","INFO.","PROB.","WARN ","CRIT.","PANIC" };
// assume the msg buffer will exist after this call.
// str must be null-terminated string!
int alarm_event(AlarmEvent& event,Stream &serialport) {
alarm(event.lvl,event.msg,serialport);
alarm((AlarmLevel)event.lvl,event.msg,serialport);
}
int alarm(AlarmLevel level,char *str,Stream &serialport) {
if (!(level >= 0 && level < NUM_LEVELS)) {
Expand Down
54 changes: 54 additions & 0 deletions Firmware/GPAD_API/dfplayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "dfplayer.h"
#include <DFRobotDFPlayerMini.h>

DFRobotDFPlayerMini myDFPlayer;

const char *SoundFileNames[] = { "A.wav","B.wav","C.wav","D.wav","E.wav","F.wav" };//audio files to output based on level
const int portbits[] = {2,26,25,14,15};
//const char *AlarmNames[] = { "OK ","INFO.","PROB.","WARN ","CRIT.","PANIC" };
void dfPlayer_setup(){


Serial2.begin(9600);

if (!myDFPlayer.begin(Serial2)) {
Serial.println("Unable to begin");
Serial.println("1. Please recheck the connection!");
Serial.println("2. Please insert the SD Card!");
while(true);
}

// Set serial communication time out 500ms
myDFPlayer.setTimeOut(500);

// Set volume value (0~30)
myDFPlayer.volume(30);

// ----Set different EQ
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);

// ----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);

// ----Mp3 Play Testing ----
// myDFPlayer.randomAll();


}


void playMessage1(AlarmLevel currentLevel){

//if the fileNumber matches the alarm level, play the track.

myDFPlayer.play((int)currentLevel + 1);


}




void muting(){
myDFPlayer.stop();
}
9 changes: 9 additions & 0 deletions Firmware/GPAD_API/dfplayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DF_PLAYER
#define DF_PLAYER 1
#include "alarm_api.h"

void playMessage1(AlarmLevel currentLevel);
void dfPlayer_setup();
void muting();

#endif
2 changes: 2 additions & 0 deletions Firmware/GPAD_API/gpad_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ void processSerial(Stream& serialport) {
serialport.println();
interpretBuffer(buf,rlen,serialport);
// Now "light and scream"appropriately...

//uncomment later
annunciateAlarmLevel();
printAlarmState(serialport);
}
Expand Down
4 changes: 2 additions & 2 deletions Firmware/GPAD_API/gpad_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <Stream.h>


#define PROG_NAME "******GPAD_API******" //Descriptive name of this software, 20 characters.
#define PROG_NAME "***GPAD_API_ESP32***" //Descriptive name of this software, 20 characters.

#define VERSION 0.07 //Version of this software
#define VERSION 0.08 //Version of this software

void printError(Stream &serialport);
void printInstructions(Stream &serialport);
Expand Down
Loading