-
Notifications
You must be signed in to change notification settings - Fork 0
/
macPlayMusic.ino
56 lines (42 loc) · 1.65 KB
/
macPlayMusic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* macPlayMusic.ino
*
* arduinoCLI sketch to play a specific song using the Music.app
* application on the mac
*
*/
#include <SoftwareSerial.h>
#define RX_PIN 7
#define TX_PIN 8
// Software Serial object to send the
// commands to the Python Agent
SoftwareSerial command_serial(RX_PIN, TX_PIN); // RX, TX
void create_applescript_file(char const *song) {
char buff[512] = "";
command_serial.println(F("!echo 'tell application \"Music\"' > play_and_stop.scpt"));
sprintf(buff, "!echo ' play track \"%s\"' >> play_and_stop.scpt", song);
command_serial.println(buff);
command_serial.println(F("!echo \" repeat until player state is stopped\" >> play_and_stop.scpt"));
command_serial.println(F("!echo \" delay 1\" >> play_and_stop.scpt"));
command_serial.println(F("!echo \" end repeat\" >> play_and_stop.scpt"));
command_serial.println(F("!echo \" stop\" >> play_and_stop.scpt"));
command_serial.println(F("!echo \"end tell\" >> play_and_stop.scpt"));
}
void play_song(char const *song) {
// create an applescript file to play the song
create_applescript_file(song);
// Play the song!
command_serial.println("!osascript play_and_stop.scpt");
// quit the Music.app if the user stopped it themselves
command_serial.println("!osascript -e 'tell application \"Music\" to quit'");
// remove the applescript file
command_serial.println("!rm play_and_stop.scpt");
}
void setup() {
Serial.begin(115200);
command_serial.begin(9600);
// name the song to play (Moby's "Porcelain" in this case)
char const *song = "Porcelain";
play_song(song);
}
void loop() { }