This commit is contained in:
Tasuku Suzuki 2025-12-11 17:21:59 +02:00 committed by GitHub
commit e7ece76667
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 0 deletions

View file

@ -21,6 +21,7 @@ name = "home_automation_lib"
[dependencies]
slint = { path = "../../../api/rs/slint", features = ["backend-android-activity-06"] }
chrono = "0.4"
kira = "0.10"
[features]
sw-renderer = []

View file

@ -2,6 +2,11 @@
// SPDX-License-Identifier: MIT
use chrono::{Datelike, Local, Timelike};
use kira::{
AudioManager, AudioManagerSettings, DefaultBackend,
sound::static_sound::StaticSoundData,
};
use std::io::Cursor;
use slint::{Timer, TimerMode};
#[cfg(target_arch = "wasm32")]
@ -17,6 +22,10 @@ slint::slint! {
export { Api, AppWindow } from "../ui/demo.slint";
}
// https://sourceforge.net/projects/sox/
// $ sox -n dial-tick.wav synth 0.01 sine 1000 fade t 0 0.01 0.005 gain -1
const DIAL_TICK : &[u8] = include_bytes!("../ui/sounds/dial-tick.wav");
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
// This provides better error messages in debug mode.
@ -27,6 +36,18 @@ pub fn main() {
let app = AppWindow::new().expect("AppWindow::new() failed");
let app_weak = app.as_weak();
let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default()).unwrap();
let api = app.global::<Api>();
api.on_play_sound(move |sound| {
let sound = match sound {
SoundEffect::DialTick => DIAL_TICK,
};
let cursor = Cursor::new(sound);
let sound_data = StaticSoundData::from_cursor(cursor).unwrap();
let _sound_handle = manager.play(sound_data).unwrap();
});
let timer = Timer::default();
timer.start(TimerMode::Repeated, std::time::Duration::from_millis(1000), move || {
if let Some(app) = app_weak.upgrade() {

View file

@ -17,6 +17,10 @@ export struct WeatherData {
temperature: float,
}
export enum SoundEffect {
dial-tick,
}
export global Api {
in property <float> indoor-temperature: 22.0;
in property <float> outdoor-temperature: 24.0;
@ -95,4 +99,6 @@ export global Api {
temperature: 23.0,
},
];
callback play-sound(SoundEffect);
}

View file

@ -1,6 +1,7 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
import { Palette } from "../../common.slint";
import { Api, SoundEffect } from "../../api.slint";
export global DialState {
out property <int> totalLights: 60;
@ -19,6 +20,9 @@ export component Dial {
property <bool> moving: ta.firstTouch;
in-out property <angle> dialAngle: DialState.startAngle;
out property <int> volume: ((dialAngle - DialState.startAngle) / DialState.degreesFilledWithLights) * DialState.totalLights;
changed volume => {
Api.play-sound(SoundEffect.dial-tick);
}
width: 212px;
height: 213px;

Binary file not shown.