Skip to content

Commit

Permalink
Sound beep and speak
Browse files Browse the repository at this point in the history
  • Loading branch information
hutch120 committed Mar 11, 2021
1 parent 92625bc commit 2a0aa1f
Show file tree
Hide file tree
Showing 9 changed files with 1,156 additions and 31 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# Metwork
# Metwork

## Expressif 32
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/

## TTGO
https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library
6 changes: 5 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ board = ttgo-t-watch
framework = arduino
build_flags =
-DCORE_DEBUG_LEVEL=3
lib_deps = TTGO TWatch Library
lib_deps =
TTGO TWatch Library
earlephilhower/ESP8266Audio@^1.8.1
gianbacchio/ESP8266Spiram@^1.0
earlephilhower/ESP8266SAM@^1.0
upload_speed = 2000000
monitor_speed = 115200
21 changes: 12 additions & 9 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#ifndef _CONFIG_H
#ifndef _METWORK_CONFIG_H

#define LILYGO_WATCH_2020_V1 //To use T-Watch2020, please uncomment this line
#define LILYGO_WATCH_LVGL //To use LVGL, you need to enable the macro LVGL
#define LILYGO_WATCH_2020_V1 // To use T-Watch2020, please uncomment this line
#define LILYGO_WATCH_LVGL // To use LVGL, you need to enable the macro LVGL
#define TWATCH_USE_PSRAM_ALLOC_LVGL

/*
* Enable non-latin languages support:
*/
#undef STANDARD_BACKPLANE
#define EXTERNAL_DAC_BACKPLANE

/**
* Enable non-latin languages support:
*/
#define USE_EXTENDED_CHARSET CHARSET_CYRILLIC

/*
* firmware version string
*/
/**
* firmware version string
*/
#define __FIRMWARE__ "2021012701"

#ifdef __cplusplus // Allows to include config.h from C code
Expand Down
13 changes: 7 additions & 6 deletions src/espnow.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "config.h"

#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <TTGO.h>
#include "esp_wifi_types.h" // RSSI signal strength
#include "power.h"
#include "sound.h"
#include "ui.h"

int tickReceived = 0;
Expand All @@ -15,15 +17,13 @@ uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//uint8_t broadcastAddress[] = {0xC4, 0x4F, 0x33, 0x7F, 0xCE, 0xF9};

const wifi_promiscuous_filter_t filt = {
.filter_mask=WIFI_PROMIS_FILTER_MASK_ALL
};
.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL};

String getStats()
{
char result[32];
snprintf(result, 32, "%d/%d", tickSent, tickReceived);
return String(result);

}

void formatMacAddress(const uint8_t *macAddr, char *buffer, int maxLength)
Expand Down Expand Up @@ -204,7 +204,7 @@ void espnow_setup()
peerInfo.channel = 1;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.encrypt = false;

if (!esp_now_is_peer_exist(broadcastAddress))
{
log_i("Adding peer 0xffffffffff");
Expand All @@ -231,9 +231,10 @@ void espnow_loop()
[network id] - Exterally created?? WIFI names?
*/

// esp_now_unregister_recv_cb();
// esp_now_register_recv_cb(onDataReceived);
// esp_now_unregister_recv_cb();
// esp_now_register_recv_cb(onDataReceived);

// log_i("WiFi.isConnected = %d", WiFi.isConnected());
broadcast();
sound_play_beep();
}
35 changes: 22 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <TTGO.h>
#include "espnow.h"
#include "power.h"
#include "sound.h"
#include "touch.h"
#include "ui.h"

Expand Down Expand Up @@ -36,10 +37,10 @@ void setup()
initializeUI();
power_setup();
espnow_setup();
sound_setup();
initializeTouch();

tick = 0;

}

void loop()
Expand All @@ -54,53 +55,59 @@ void loop()

// log_i("Touch loop...");
touch_loop();
sound_loop();

// check for sleep timeout
if (isTouched()) {
if (isTouched())
{

// touched
if (getTickTouch() == 1) {
if (getTickTouch() == 1)
{
drawStatsUI();
initializeTouchUI();
}

drawTouchUI();

if (tick % 5 == 0 || getTickTouch() == 1) {
if (tick % 5 == 0 || getTickTouch() == 1)
{
// every 1s
// log_i("Comms loop...");
espnow_loop();
}
tickSleepTimeout = 0;
}
else {
else
{

// not touched
tickSleepTimeout++;
if (tickSleepTimeout == 1) {
if (tickSleepTimeout == 1)
{
removeTouchUI();
drawStatsUI();
}

/// log_i("No touch ... tickSleepTimeout %d", tickSleepTimeout);

if (tickSleepTimeout > 500) {
if (tickSleepTimeout > 500)
{
// timeout after 5s
tickSleepTimeout = 0;
sleep();
}

if (tick % 10 == 0) {
if (tick % 10 == 0)
{
// every 1s
// log_i("Power loop...");
button_loop();
}

}
}

tick = tick % 300;
delay(100);

}
else
{
Expand All @@ -109,10 +116,12 @@ void loop()

// log_i("Touch loop...");
touch_loop();

if (isTouched()) {

if (isTouched())
{
wakeUp();
espnow_setup();
sound_setup();
drawWakeUpUI();
}

Expand Down
2 changes: 1 addition & 1 deletion src/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void sleep()
Serial.flush();
// esp_deep_sleep_start(); // See readme... requires other configuration.
ttgo->powerOff();

sleeping = true;
// esp_light_sleep_start();
}
Expand Down
51 changes: 51 additions & 0 deletions src/sound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "config.h"
#include <Arduino.h>
#include <TTGO.h>

#include <AudioFileSourcePROGMEM.h>
#include "AudioGeneratorWAV.h"
#include <AudioOutputI2S.h>
#include "ESP8266SAM.h"
#include "sound_beep.h"

TTGOClass *ttgo;
AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2S *out;
ESP8266SAM *sam;

void sound_speak(const char *str)
{
log_i("Speak text", str);
sam->Say(out, str);
out->stop();
}

void sound_play_beep()
{
log_i("Sound play beep");
file = new AudioFileSourcePROGMEM(beep_wav, beep_wav_len);
wav->begin(file, out);
}

void sound_setup()
{
TTGOClass *ttgo = TTGOClass::getWatch();
ttgo->enableLDO3(); // Turn on the audio power
sam = new ESP8266SAM();
sam->SetVoice(sam->VOICE_SAM);
wav = new AudioGeneratorWAV();
out = new AudioOutputI2S();
out->SetPinout(TWATCH_DAC_IIS_BCK, TWATCH_DAC_IIS_WS, TWATCH_DAC_IIS_DOUT);
out->SetGain(0.1);
sound_speak("Hi, how are you?");
}

void sound_loop()
{
if (wav->isRunning() && !wav->loop())
{
log_i("stop playing wav sound");
wav->stop();
}
}
19 changes: 19 additions & 0 deletions src/sound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Arduino.h>

#ifndef _SOUND_H
#define _SOUND_H

/*
* Play a sound
*/
void sound_setup();
void sound_loop();
/**
* @brief speak
*
* @param str the text to be spoken
*/
void sound_speak(const char *str);
void sound_play_beep();

#endif // _SOUND_H
Loading

0 comments on commit 2a0aa1f

Please sign in to comment.