Browse Source

Working Sounds

master
Englebert 3 years ago
parent
commit
ffdb494cf6
  1. 33
      src/CustomWiFi.cpp
  2. 24
      src/CustomWiFi.h
  3. 4
      src/Screen.cpp
  4. 7612
      src/Speak.cpp
  5. 12
      src/Speak.h
  6. 1
      src/Storage.cpp
  7. 153
      src/Web.cpp
  8. 23
      src/Web.h
  9. 33
      src/main.cpp
  10. 3
      src/main.h

33
src/CustomWiFi.cpp

@ -0,0 +1,33 @@
#include "CustomWiFi.h"
CustomWiFi::CustomWiFi(void) {
// pinMode(CustomWiFi_INDICATOR, OUTPUT);
last_wifi_check = 0;
wifi_check_duration = 10;
}
void CustomWiFi::set_credential(char *ssid, char *passphrase) {
strcpy(CustomWiFi::_ssid, ssid);
strcpy(CustomWiFi::_passphrase, passphrase);
// Initial value
this->wifi_check_duration = 5000;
}
void CustomWiFi::begin(void) {
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WiFi.softAP(WIFI_SSID, WIFI_PASSPHRASE);
delay(100);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
IPAddress IP = WiFi.softAPIP();
// Serial.print(F("AP IP address: "); Serial.prinln(IP);
}
CustomWiFi customwifi;

24
src/CustomWiFi.h

@ -0,0 +1,24 @@
#ifndef CUSTOMWIFI_H_
#define CUSTOMWIFI_H_
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define WIFI_SSID "M5CoreTX"
#define WIFI_PASSPHRASE "LetMeIn123"
// #define CUSTOMWIFI_INDICATOR 2
class CustomWiFi {
public:
CustomWiFi(void);
void set_credential(char *ssid, char *passphrase);
void begin(void);
char _ssid[32], _passphrase[32];
private:
uint32_t last_wifi_check, wifi_check_duration;
};
extern CustomWiFi customwifi;
#endif

4
src/Screen.cpp

@ -6,6 +6,7 @@
TFT_eSprite mainscreen_buffer = TFT_eSprite(&M5.Lcd);
Screen::Screen(void) {
}
@ -257,3 +258,6 @@ void Screen::menu_poweroff(void) {
updated = true;
}
}
Screen screen;

7612
src/Speak.cpp
File diff suppressed because it is too large
View File

12
src/Speak.h

@ -2,23 +2,29 @@
#define _SPEAK_H
#include <Arduino.h>
#include <driver/i2s.h>
#include <M5Core2.h>
#include <driver/i2s.h>
#include <LITTLEFS.h>
#define MODE_SPEAKER 1
#define MODE_MICROPHONE 0
#define NUM_FRAMES_TO_SEND 256
#define MODE_SPEAKER 1
#define MODE_MICROPHONE 0
#define SPEAK_I2S_NUMBER I2S_NUM_0
#define CONFIG_I2S_BCK_PIN 12 //定义I2S相关端口
#define CONFIG_I2S_LRCK_PIN 0
#define CONFIG_I2S_DATA_PIN 2
#define CONFIG_I2S_DATA_IN_PIN 34
#define OUTPUT_GAIN 10
class Speak {
public:
Speak(void);
void begin(void);
void dingdong(void);
void welcome(void);
virtual int16_t process_sample(int16_t sample) { return sample; }
// virtual int8_t process_sample(int8_t sample) { return sample; }
private:
};

1
src/Storage.cpp

@ -223,3 +223,4 @@ void Storage::read_block(void * data, const char * path, uint32_t len){
file.close();
}
Storage storage;

153
src/Web.cpp

@ -0,0 +1,153 @@
#include "Web.h"
WebServer server(PORT_HTTP);
bool update_status = false;
bool opened = false;
File root;
WEB::WEB(void){
}
void WEB::begin(void) {
// Allow CORS
server.enableCORS();
// Index
server.on("/", HTTP_GET, []() {
// File html_handler = LITTLEFS.open("/index.html.gz", FILE_READ);
// server.streamFile(html_handler, "text/html");
// html_handler.close();
server.send(200, "text/html", "MSCoreTX Version 0.1");
server.sendHeader("Connection", "close");
});
// Update
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK. Device is rebooting");
if(Update.hasError()) {
update_status = false;
// web.set_update_status(false);
} else {
delay(1000);
ESP.restart();
}
}, []() {
HTTPUpload& upload = server.upload();
if(!update_status)
update_status = true;
if(upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
// Start with maximum available size
if(!Update.begin(UPDATE_SIZE_UNKNOWN))
Update.printError(Serial);
} else if(upload.status == UPLOAD_FILE_WRITE) {
// Flashing firmware to ESP
if(
Update.write(upload.buf, upload.currentSize) != upload.currentSize
)
Update.printError(Serial);
} else if(upload.status == UPLOAD_FILE_END) {
if(Update.end(true))
// True to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
else
Update.printError(Serial);
}
});
// Reset
server.on("/reset", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", "1");
storage.deleteFile(LITTLEFS, "/MSCoreTX");
delay(1000);
ESP.restart();
});
// Reboot
server.on("/reboot", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", "1");
delay(1000);
ESP.restart();
});
// Remove files
server.on("/erase", HTTP_GET, []() {
String filename;
uint8_t detected = 0;
for(uint8_t i = 0; i < server.args(); i++) {
String value = server.arg(i);
if(server.argName(i) == "filename") {
filename = value;
detected++;
}
}
server.sendHeader("Connection", "close");
if(detected) {
LITTLEFS.remove("/" + filename);
server.send(200, "text/plain", "REMOVED - [" + filename + "]");
} else {
server.send(200, "text/plain", "Nothing remove.");
}
});
// TODO: create a way to upload files to the ESP32 and also handling it
// REF: https://techtutorialsx.com/2019/03/04/esp32-arduino-serving-bootstrap/
// SPIFFS File upload
server.on("/webupload", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", "webupload Done");
}, []() {
HTTPUpload& upload = server.upload();
if(opened == false) {
opened = true;
root = LITTLEFS.open((String("/") + upload.filename).c_str(), FILE_WRITE);
if(!root) {
Serial.printf("Failed to open file for writing. [%s]\n", upload.filename.c_str());
return;
} else {
Serial.println("Webupload file start.");
}
}
if(upload.status == UPLOAD_FILE_START) {
Serial.printf("webupload: %s\n", upload.filename.c_str());
} else if(upload.status == UPLOAD_FILE_WRITE) {
// Writting to SPIFFS
if(root.write(upload.buf, upload.currentSize) != upload.currentSize) {
Serial.printf("Failed to write. [%s]\n", upload.filename.c_str());
return;
}
} else if(upload.status == UPLOAD_FILE_END) {
root.close();
opened = false;
Serial.printf("webupload done. [%s]\n", upload.filename.c_str());
#ifdef DEBUG_MODE
Serial.printf("SPIFFS Files:\n");
// list_files();
storage.listDir(LITTLEFS, "/", 0);
#endif
}
});
server.begin();
}
void WEB::handler(void) {
server.handleClient();
}
WEB web;

23
src/Web.h

@ -0,0 +1,23 @@
#ifndef WEB_H_
#define WEB_H_
#include <Arduino.h>
#include <LITTLEFS.h>
#include <WebServer.h>
#include <Update.h>
#include "Storage.h"
#define PORT_HTTP 80
class WEB {
public:
WEB(void);
void begin(void);
void handler(void);
private:
};
extern WEB web;
#endif

33
src/main.cpp

@ -1,9 +1,10 @@
#include <Arduino.h>
#include "main.h"
Screen screen;
Speak speak;
Storage storage;
// Screen screen;
// Speak speak;
// Storage storage;
// CustomWiFi customwifi;
void setup() {
Serial.begin(115200);
@ -18,7 +19,11 @@ void setup() {
screen.begin();
speak.begin();
// speak.dingdong();
storage.begin();
customwifi.begin();
web.begin();
// Task Creation
xTaskCreatePinnedToCore(
@ -31,6 +36,16 @@ void setup() {
CPU_1
);
xTaskCreatePinnedToCore(
taskWeb,
"TaskWeb", // Name of the process
4096, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
4, // Priority
NULL,
CPU_0
);
/***
xTaskCreatePinnedToCore(
taskSpeak,
@ -43,7 +58,7 @@ void setup() {
);
***/
/// speak.dingdong();
speak.welcome();
}
@ -70,3 +85,13 @@ void taskSpeak(void *pvParameters) {
vTaskDelay(100);
}
}
void taskWeb(void *pvParameters) {
(void) pvParameters;
for(;;) {
web.handler();
vTaskDelay(100);
}
}

3
src/main.h

@ -9,9 +9,12 @@
#include "Screen.h"
#include "Speak.h"
#include "Storage.h"
#include "CustomWiFi.h"
#include "Web.h"
// Task Handler
void taskScreen(void *pvParameters);
void taskSpeak(void *pvParameters);
void taskWeb(void *pvParameters);
#endif
Loading…
Cancel
Save