Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Helper scripts and elevator music #5

Merged
merged 10 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions elevatorMusic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module elevatorMusic

require elevatorDriver v0.0.0

replace elevatorDriver => ../elevatorDriver

go 1.20
66 changes: 66 additions & 0 deletions elevatorMusic/musicplayer/musicplayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package musicplayer

import (
"log"
"os"
"os/exec"
"syscall"
)

var MusicPid *os.Process
var dingPid *os.Process
var pairPid *os.Process

func PlayMusic(path string, pid *os.Process, loop_music bool) {
if path == "" {
if pid != nil {
pid.Signal(syscall.SIGCONT)
}
return
}

var cmd *exec.Cmd

if loop_music {
cmd = exec.Command("ffplay", path, "-autoexit", "-nodisp", "-hide_banner", "-loglevel", "warning", "-loop", "0")
} else {
cmd = exec.Command("ffplay", path, "-autoexit", "-nodisp", "-hide_banner", "-loglevel", "warning")
}

if err := cmd.Start(); err != nil {
log.Printf("Failed to start music: %v", err)
return
}

pid = cmd.Process
log.Println("Playing elevator", path)
}

func PauseMusic(pid *os.Process) {
if pid != nil {
pid.Signal(syscall.SIGSTOP)
log.Println("Elevator music paused")
}
}

func ResumeMusic(pid *os.Process) {
if pid != nil {
pid.Signal(syscall.SIGCONT)
log.Println("Elevator music resumed")
}
}

func StopMusic(pid *os.Process) {
if pid != nil {
pid.Signal(syscall.SIGINT)
log.Println("Elevator music stopped")
}
}

func PlayDing() {
PlayMusic("media/ding.opus", dingPid, false)
}

func PlayPairing() {
PlayMusic("media/pairing_sound.opus", pairPid, false)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ require elevatorAlgorithm v0.0.0

replace elevatorAlgorithm => ./elevatorAlgorithm

require elevatorMusic v0.0.0

replace elevatorMusic => ./elevatorMusic

go 1.20
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {

log.Println("Elevator starting 🛗")
elevio.Init("localhost:15657", elevator.N_FLOORS)

if elevio.GetFloor() == -1 {
fsm.OnInitBetweenFloors()
}
Expand Down
Binary file added media/ding.opus
Binary file not shown.
Binary file added media/elevator.m4a
Binary file not shown.
Binary file added media/logistics_lounge.opus
Binary file not shown.
Binary file added media/pairing_sound.opus
Binary file not shown.
Binary file added media/pairing_successful.opus
Binary file not shown.
Binary file added media/portal.opus
Binary file not shown.
42 changes: 42 additions & 0 deletions scripts/launch_elevator_w_sim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Check if the number of elevators was passed as an argument
if [ -z "$1" ]; then
echo "Please specify the number of elevators."
exit 1
fi

N=$1 # Number of elevators
default_udp_port=4200
default_id=0

simulator_path=$(pwd)/Simulator-v2
go_program_path=$(pwd)
go_path_macos=/opt/homebrew/bin/go

# Loop to start N elevators and simulators
for ((i=0; i<N; i++)); do
udp_port=$((default_udp_port + i)) # Calculate UDP port for main.go
sim_udp_port=$udp_port
id=$((default_id + i)) # Calculate ID

# Check operating system
if [ "$(uname)" == "Darwin" ]; then
# macOS specific commands to open new terminal windows
# Run simulator in a new terminal window
osascript -e "tell app \"Terminal\" to do script \"echo Starting simulator with UDP port: $sim_udp_port; $simulator_path/sim_server --port $sim_udp_port\""
# Run main.go in a new terminal window
sleep 1
osascript -e "tell app \"Terminal\" to do script \"cd $go_program_path && echo Using UDP port: $udp_port; echo Using ID: $id; $go_path_macos run main.go --port $udp_port --id $id\""
elif [ "$(uname)" == "Linux" ]; then
# Linux specific commands to open new terminal windows
# Run simulator in a new terminal window
gnome-terminal -- bash -c "echo Starting simulator with UDP port: $sim_udp_port; $simulator_path/SimElevatorServer --port $sim_udp_port; exec bash"
# Run main.go in a new terminal window
sleep 1
gnome-terminal -- bash -c "cd $go_program_path && echo Using UDP port: $udp_port; echo Using ID: $id; go run main.go --port $udp_port --id $id; exec bash"
else
echo "Unknown operating system."
exit 1
fi
done
Loading