Merge branch 'dev' into dashboard_port_from_enviromnent_var

This commit is contained in:
Dinghan Zhu 2024-08-15 11:25:30 -07:00 committed by GitHub
commit 06eb0858eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 173 additions and 51 deletions

View file

@ -397,7 +397,7 @@ jobs:
file: ${{ fromJson(needs.list-components.outputs.components) }}
steps:
- name: Install dependencies
run: sudo apt-get install libsodium-dev libsdl2-dev
run: sudo apt-get install libsdl2-dev
- name: Check out code from GitHub
uses: actions/checkout@v4.1.7
@ -451,7 +451,7 @@ jobs:
run: echo ${{ matrix.components }}
- name: Install dependencies
run: sudo apt-get install libsodium-dev libsdl2-dev
run: sudo apt-get install libsdl2-dev
- name: Check out code from GitHub
uses: actions/checkout@v4.1.7

View file

@ -155,7 +155,7 @@ async def to_code(config):
decoded = base64.b64decode(encryption_config[CONF_KEY])
cg.add(var.set_noise_psk(list(decoded)))
cg.add_define("USE_API_NOISE")
cg.add_library("esphome/noise-c", "0.1.4")
cg.add_library("esphome/noise-c", "0.1.6")
else:
cg.add_define("USE_API_PLAINTEXT")

View file

@ -1,15 +1,14 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (
CONF_MAC_ADDRESS,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
PLATFORM_HOST,
CONF_MAC_ADDRESS,
)
from esphome.core import CORE
from esphome.helpers import IS_MACOS
import esphome.config_validation as cv
import esphome.codegen as cg
from .const import KEY_HOST
@ -42,8 +41,5 @@ async def to_code(config):
cg.add_build_flag("-DUSE_HOST")
cg.add_define("USE_ESPHOME_HOST_MAC_ADDRESS", config[CONF_MAC_ADDRESS].parts)
cg.add_build_flag("-std=c++17")
cg.add_build_flag("-lsodium")
if IS_MACOS:
cg.add_build_flag("-L/opt/homebrew/lib")
cg.add_define("ESPHOME_BOARD", "host")
cg.add_platformio_option("platform", "platformio/native")

View file

@ -1,5 +1,5 @@
#include "improv_serial_component.h"
#ifdef USE_WIFI
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
@ -313,3 +313,4 @@ ImprovSerialComponent *global_improv_serial_component = // NOLINT(cppcoreguidel
} // namespace improv_serial
} // namespace esphome
#endif

View file

@ -5,7 +5,7 @@
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#ifdef USE_WIFI
#include <improv.h>
#include <vector>
@ -78,3 +78,4 @@ extern ImprovSerialComponent
} // namespace improv_serial
} // namespace esphome
#endif

View file

@ -29,6 +29,13 @@ inline double deg2rad(double degrees) {
void Rtttl::dump_config() { ESP_LOGCONFIG(TAG, "Rtttl"); }
void Rtttl::play(std::string rtttl) {
if (this->state_ != State::STATE_STOPPED && this->state_ != State::STATE_STOPPING) {
int pos = this->rtttl_.find(':');
auto name = this->rtttl_.substr(0, pos);
ESP_LOGW(TAG, "RTTL Component is already playing: %s", name.c_str());
return;
}
this->rtttl_ = std::move(rtttl);
this->default_duration_ = 4;
@ -98,13 +105,20 @@ void Rtttl::play(std::string rtttl) {
this->note_duration_ = 1;
#ifdef USE_SPEAKER
this->samples_sent_ = 0;
this->samples_count_ = 0;
if (this->speaker_ != nullptr) {
this->set_state_(State::STATE_INIT);
this->samples_sent_ = 0;
this->samples_count_ = 0;
}
#endif
#ifdef USE_OUTPUT
if (this->output_ != nullptr) {
this->set_state_(State::STATE_RUNNING);
}
#endif
}
void Rtttl::stop() {
this->note_duration_ = 0;
#ifdef USE_OUTPUT
if (this->output_ != nullptr) {
this->output_->set_level(0.0);
@ -117,16 +131,35 @@ void Rtttl::stop() {
}
}
#endif
this->note_duration_ = 0;
this->set_state_(STATE_STOPPING);
}
void Rtttl::loop() {
if (this->note_duration_ == 0)
if (this->note_duration_ == 0 || this->state_ == State::STATE_STOPPED)
return;
#ifdef USE_SPEAKER
if (this->speaker_ != nullptr) {
if (this->state_ == State::STATE_STOPPING) {
if (this->speaker_->is_stopped()) {
this->set_state_(State::STATE_STOPPED);
}
} else if (this->state_ == State::STATE_INIT) {
if (this->speaker_->is_stopped()) {
this->speaker_->start();
this->set_state_(State::STATE_STARTING);
}
} else if (this->state_ == State::STATE_STARTING) {
if (this->speaker_->is_running()) {
this->set_state_(State::STATE_RUNNING);
}
}
if (!this->speaker_->is_running()) {
return;
}
if (this->samples_sent_ != this->samples_count_) {
SpeakerSample sample[SAMPLE_BUFFER_SIZE + 1];
SpeakerSample sample[SAMPLE_BUFFER_SIZE + 2];
int x = 0;
double rem = 0.0;
@ -136,7 +169,7 @@ void Rtttl::loop() {
if (this->samples_per_wave_ != 0 && this->samples_sent_ >= this->samples_gap_) { // Play note//
rem = ((this->samples_sent_ << 10) % this->samples_per_wave_) * (360.0 / this->samples_per_wave_);
int16_t val = (49152 * this->gain_) * sin(deg2rad(rem));
int16_t val = (127 * this->gain_) * sin(deg2rad(rem)); // 16bit = 49152
sample[x].left = val;
sample[x].right = val;
@ -153,9 +186,9 @@ void Rtttl::loop() {
x++;
}
if (x > 0) {
int send = this->speaker_->play((uint8_t *) (&sample), x * 4);
int send = this->speaker_->play((uint8_t *) (&sample), x * 2);
if (send != x * 4) {
this->samples_sent_ -= (x - (send / 4));
this->samples_sent_ -= (x - (send / 2));
}
return;
}
@ -167,14 +200,7 @@ void Rtttl::loop() {
return;
#endif
if (!this->rtttl_[position_]) {
this->note_duration_ = 0;
#ifdef USE_OUTPUT
if (this->output_ != nullptr) {
this->output_->set_level(0.0);
}
#endif
ESP_LOGD(TAG, "Playback finished");
this->on_finished_playback_callback_.call();
this->finish_();
return;
}
@ -213,6 +239,7 @@ void Rtttl::loop() {
case 'a':
note = 10;
break;
case 'h':
case 'b':
note = 12;
break;
@ -238,14 +265,21 @@ void Rtttl::loop() {
uint8_t scale = get_integer_();
if (scale == 0)
scale = this->default_octave_;
if (scale < 4 || scale > 7) {
ESP_LOGE(TAG, "Octave out of valid range. Should be between 4 and 7. (Octave: %d)", scale);
this->finish_();
return;
}
bool need_note_gap = false;
// Now play the note
if (note) {
auto note_index = (scale - 4) * 12 + note;
if (note_index < 0 || note_index >= (int) sizeof(NOTES)) {
ESP_LOGE(TAG, "Note out of valid range");
this->note_duration_ = 0;
ESP_LOGE(TAG, "Note out of valid range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
(int) sizeof(NOTES));
this->finish_();
return;
}
auto freq = NOTES[note_index];
@ -285,14 +319,17 @@ void Rtttl::loop() {
this->samples_gap_ = (this->sample_rate_ * DOUBLE_NOTE_GAP_MS) / 1600; //(ms);
}
if (this->output_freq_ != 0) {
// make sure there is enough samples to add a full last sinus.
uint16_t samples_wish = this->samples_count_;
this->samples_per_wave_ = (this->sample_rate_ << 10) / this->output_freq_;
// make sure there is enough samples to add a full last sinus.
uint16_t division = ((this->samples_count_ << 10) / this->samples_per_wave_) + 1;
uint16_t x = this->samples_count_;
this->samples_count_ = (division * this->samples_per_wave_);
ESP_LOGD(TAG, "play time old: %d div: %d new: %d %d", x, division, this->samples_count_, this->samples_per_wave_);
this->samples_count_ = this->samples_count_ >> 10;
ESP_LOGVV(TAG, "- Calc play time: wish: %d gets: %d (div: %d spw: %d)", samples_wish, this->samples_count_,
division, this->samples_per_wave_);
}
// Convert from frequency in Hz to high and low samples in fixed point
}
@ -301,5 +338,53 @@ void Rtttl::loop() {
this->last_note_ = millis();
}
void Rtttl::finish_() {
#ifdef USE_OUTPUT
if (this->output_ != nullptr) {
this->output_->set_level(0.0);
}
#endif
#ifdef USE_SPEAKER
if (this->speaker_ != nullptr) {
SpeakerSample sample[2];
sample[0].left = 0;
sample[0].right = 0;
sample[1].left = 0;
sample[1].right = 0;
this->speaker_->play((uint8_t *) (&sample), 8);
this->speaker_->finish();
}
#endif
this->set_state_(State::STATE_STOPPING);
this->note_duration_ = 0;
this->on_finished_playback_callback_.call();
ESP_LOGD(TAG, "Playback finished");
}
static const LogString *state_to_string(State state) {
switch (state) {
case STATE_STOPPED:
return LOG_STR("STATE_STOPPED");
case STATE_STARTING:
return LOG_STR("STATE_STARTING");
case STATE_RUNNING:
return LOG_STR("STATE_RUNNING");
case STATE_STOPPING:
return LOG_STR("STATE_STOPPING");
case STATE_INIT:
return LOG_STR("STATE_INIT");
default:
return LOG_STR("UNKNOWN");
}
};
void Rtttl::set_state_(State state) {
State old_state = this->state_;
this->state_ = state;
ESP_LOGD(TAG, "State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
LOG_STR_ARG(state_to_string(state)));
}
} // namespace rtttl
} // namespace esphome

View file

@ -14,12 +14,20 @@
namespace esphome {
namespace rtttl {
enum State : uint8_t {
STATE_STOPPED = 0,
STATE_INIT,
STATE_STARTING,
STATE_RUNNING,
STATE_STOPPING,
};
#ifdef USE_SPEAKER
static const size_t SAMPLE_BUFFER_SIZE = 512;
static const size_t SAMPLE_BUFFER_SIZE = 2048;
struct SpeakerSample {
int16_t left{0};
int16_t right{0};
int8_t left{0};
int8_t right{0};
};
#endif
@ -42,7 +50,7 @@ class Rtttl : public Component {
void stop();
void dump_config() override;
bool is_playing() { return this->note_duration_ != 0; }
bool is_playing() { return this->state_ != State::STATE_STOPPED; }
void loop() override;
void add_on_finished_playback_callback(std::function<void()> callback) {
@ -57,6 +65,8 @@ class Rtttl : public Component {
}
return ret;
}
void finish_();
void set_state_(State state);
std::string rtttl_{""};
size_t position_{0};
@ -68,13 +78,12 @@ class Rtttl : public Component {
uint32_t output_freq_;
float gain_{0.6f};
State state_{State::STATE_STOPPED};
#ifdef USE_OUTPUT
output::FloatOutput *output_;
#endif
void play_output_();
#ifdef USE_SPEAKER
speaker::Speaker *speaker_{nullptr};
int sample_rate_{16000};

View file

@ -1,19 +1,20 @@
import re
import ipaddress
import re
from esphome import automation
import esphome.codegen as cg
from esphome.components import time
from esphome.components.esp32 import CORE, add_idf_sdkconfig_option
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
CONF_TIME_ID,
CONF_ADDRESS,
CONF_ID,
CONF_REBOOT_TIMEOUT,
CONF_TIME_ID,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
)
from esphome.components.esp32 import CORE, add_idf_sdkconfig_option
from esphome.components import time
from esphome.core import TimePeriod
from esphome import automation
CONF_NETMASK = "netmask"
CONF_PRIVATE_KEY = "private_key"
@ -91,6 +92,8 @@ CONFIG_SCHEMA = cv.Schema(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add_define("USE_WIREGUARD")
cg.add(var.set_address(str(config[CONF_ADDRESS])))
cg.add(var.set_netmask(str(config[CONF_NETMASK])))
cg.add(var.set_private_key(config[CONF_PRIVATE_KEY]))

View file

@ -1,5 +1,5 @@
#include "wireguard.h"
#ifdef USE_WIREGUARD
#include <cinttypes>
#include <ctime>
#include <functional>
@ -289,3 +289,4 @@ std::string mask_key(const std::string &key) { return (key.substr(0, 5) + "[...]
} // namespace wireguard
} // namespace esphome
#endif

View file

@ -1,5 +1,6 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_WIREGUARD
#include <ctime>
#include <vector>
#include <tuple>
@ -170,3 +171,4 @@ template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, pu
} // namespace wireguard
} // namespace esphome
#endif

View file

@ -75,6 +75,7 @@
#define USE_VALVE
#define USE_WIFI
#define USE_WIFI_AP
#define USE_WIREGUARD
// Arduino-specific feature flags
#ifdef USE_ARDUINO

View file

@ -11,10 +11,6 @@ esphome:
message: Button was pressed
- homeassistant.tag_scanned: pulse
wifi:
ssid: MySSID
password: password1
api:
port: 8000
password: pwd

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1

View file

@ -0,0 +1,3 @@
<<: !include common.yaml
network:

View file

@ -1 +1,5 @@
<<: !include common.yaml
wifi:
ssid: MySSID
password: password1