conform device protocol to LMC (w/ hacks)

pull/48/head
Theia Vogel 11 months ago
parent 22761d0344
commit 968aa854c0

@ -70,6 +70,7 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
} }
void InitI2SSpeakerOrMic(int mode) { void InitI2SSpeakerOrMic(int mode) {
Serial.printf("InitI2sSpeakerOrMic %d\n", mode);
esp_err_t err = ESP_OK; esp_err_t err = ESP_OK;
i2s_driver_uninstall(SPEAKER_I2S_NUMBER); i2s_driver_uninstall(SPEAKER_I2S_NUMBER);
@ -136,17 +137,19 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
break; break;
case WStype_TEXT: case WStype_TEXT:
Serial.printf("[WSc] get text: %s\n", payload); Serial.printf("[WSc] get text: %s\n", payload);
if ((char)payload[0] == 's'){ {
Serial.println("start"); std::string str(payload, payload + length);
bool isAudio = str.find("\"audio\"") != std::string::npos;
if (isAudio && str.find("\"start\"") != std::string::npos) {
Serial.println("start playback");
speaker_offset = 0; speaker_offset = 0;
InitI2SSpeakerOrMic(MODE_SPK); InitI2SSpeakerOrMic(MODE_SPK);
} } else if (isAudio && str.find("\"end\"") != std::string::npos) {
if ((char)payload[0] == 'e'){ Serial.println("end playback");
Serial.println("end");
// speaker_play(speakerdata0, speaker_offset); // speaker_play(speakerdata0, speaker_offset);
// speaker_offset = 0; // speaker_offset = 0;
} }
}
// send message to server // send message to server
// webSocket.sendTXT("message here"); // webSocket.sendTXT("message here");
break; break;
@ -180,12 +183,12 @@ void websocket_setup() {
Serial.println("connecting to WiFi"); Serial.println("connecting to WiFi");
} }
Serial.println("connected to WiFi"); Serial.println("connected to WiFi");
webSocket.begin(COMPUTER_IP, 9001, "/"); webSocket.begin(COMPUTER_IP, 8000, "/");
webSocket.onEvent(webSocketEvent); webSocket.onEvent(webSocketEvent);
// webSocket.setAuthorization("user", "Password"); // webSocket.setAuthorization("user", "Password");
webSocket.setReconnectInterval(5000); webSocket.setReconnectInterval(5000);
} }
void setup() { void setup() {
M5.begin(true, false, true); M5.begin(true, false, true);
M5.dis.drawpix(0, CRGB(128, 128, 0)); M5.dis.drawpix(0, CRGB(128, 128, 0));
@ -208,17 +211,19 @@ void loop() {
button.loop(); button.loop();
if (button.justPressed()) { if (button.justPressed()) {
Serial.println("Recording..."); Serial.println("Recording...");
webSocket.sendTXT("s"); webSocket.sendTXT("{\"role\": \"user\", \"type\": \"audio\", \"format\": \"bytes.raw\", \"start\": true}");
InitI2SSpeakerOrMic(MODE_MIC); InitI2SSpeakerOrMic(MODE_MIC);
recording = true; recording = true;
data_offset = 0;
Serial.println("Recording ready.");
} else if (button.justReleased()) { } else if (button.justReleased()) {
Serial.println("Stopped recording."); Serial.println("Stopped recording.");
webSocket.sendTXT("e"); webSocket.sendTXT("{\"role\": \"user\", \"type\": \"audio\", \"format\": \"bytes.raw\", \"end\": true}");
flush_microphone(); flush_microphone();
recording = false; recording = false;
} data_offset = 0;
} else if (recording) {
if (recording) { Serial.printf("Reading chunk at %d...\n", data_offset);
size_t bytes_read; size_t bytes_read;
i2s_read( i2s_read(
SPEAKER_I2S_NUMBER, SPEAKER_I2S_NUMBER,
@ -226,13 +231,13 @@ void loop() {
DATA_SIZE, &bytes_read, (100 / portTICK_RATE_MS) DATA_SIZE, &bytes_read, (100 / portTICK_RATE_MS)
); );
data_offset += bytes_read; data_offset += bytes_read;
Serial.printf("Read %d bytes in chunk.\n", bytes_read);
if (data_offset > 1024*10) { if (data_offset > 1024*9) {
flush_microphone(); flush_microphone();
} }
} }
M5.update(); M5.update();
webSocket.loop(); webSocket.loop();
} }

Loading…
Cancel
Save