added speaker peripheral
This commit is contained in:
parent
69ca3f3282
commit
4836cae3fa
95
src/bin/Speaker.hx
Normal file
95
src/bin/Speaker.hx
Normal file
@ -0,0 +1,95 @@
|
||||
package bin;
|
||||
|
||||
import kernel.peripherals.Peripherals.Peripheral;
|
||||
import lib.CLIAppBase;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
@:build(macros.Binstore.includeBin("Speaker", ["speaker", "sp"]))
|
||||
class Speaker extends CLIAppBase {
|
||||
public function new() {
|
||||
registerSyncSubcommand("note", (args) -> {
|
||||
if (args.length < 2) {
|
||||
handle.writeLine("Not enough args");
|
||||
return false;
|
||||
}
|
||||
|
||||
var sp = Peripheral.getSpeaker(args[0]);
|
||||
|
||||
var note = args[1];
|
||||
var r;
|
||||
|
||||
if (args.length > 3) {
|
||||
var volume = Std.parseFloat(args[2]);
|
||||
if (volume == null) {
|
||||
handle.writeLine("Failed to parse volume");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length > 4) {
|
||||
var pitch = Std.parseInt(args[3]);
|
||||
if (pitch == null) {
|
||||
handle.writeLine("Failed to parse pitch");
|
||||
return false;
|
||||
}
|
||||
|
||||
r = sp.playNote(note, volume, pitch);
|
||||
} else {
|
||||
r = sp.playNote(note, volume);
|
||||
}
|
||||
} else {
|
||||
r = sp.playNote(note);
|
||||
}
|
||||
|
||||
switch r {
|
||||
case Failure(failure):
|
||||
handle.writeLine(failure);
|
||||
return false;
|
||||
case Success(_):
|
||||
return true;
|
||||
}
|
||||
}, "<addr> <note> [pitch] [volume]");
|
||||
|
||||
registerSyncSubcommand("sound", (args) -> {
|
||||
if (args.length < 2) {
|
||||
handle.writeLine("Not enough args");
|
||||
return false;
|
||||
}
|
||||
|
||||
var sp = Peripheral.getSpeaker(args[0]);
|
||||
|
||||
var sound = args[1];
|
||||
var r;
|
||||
|
||||
if (args.length > 3) {
|
||||
var volume = Std.parseFloat(args[2]);
|
||||
if (volume == null) {
|
||||
handle.writeLine("Failed to parse volume");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length > 4) {
|
||||
var pitch = Std.parseFloat(args[3]);
|
||||
if (pitch == null) {
|
||||
handle.writeLine("Failed to parse pitch");
|
||||
return false;
|
||||
}
|
||||
|
||||
r = sp.playSound(sound, volume, pitch);
|
||||
} else {
|
||||
r = sp.playSound(sound, volume);
|
||||
}
|
||||
} else {
|
||||
r = sp.playSound(sound);
|
||||
}
|
||||
|
||||
switch r {
|
||||
case Failure(failure):
|
||||
handle.writeLine(failure);
|
||||
return false;
|
||||
case Success(_):
|
||||
return true;
|
||||
}
|
||||
}, "<addr> <sound> [volume] [pitch]");
|
||||
}
|
||||
}
|
@ -81,6 +81,8 @@ class Peripheral {
|
||||
return getModem(addr);
|
||||
case Printer.TYPE_NAME:
|
||||
return getPrinter(addr);
|
||||
case Speaker.TYPE_NAME:
|
||||
return getSpeaker(addr);
|
||||
case "redstone":
|
||||
return getRedstone(addr);
|
||||
}
|
||||
@ -158,4 +160,15 @@ class Peripheral {
|
||||
public static function getAllComputers():Array<Computer> {
|
||||
return [for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)];
|
||||
}
|
||||
|
||||
public static function getSpeaker(addr:String):Null<Speaker> {
|
||||
var addr = safeGetAddr(addr, Speaker.TYPE_NAME);
|
||||
if (addr == null)
|
||||
return null;
|
||||
return new Speaker(addr);
|
||||
}
|
||||
|
||||
public static function getAllSpeakers():Array<Speaker> {
|
||||
return [for (addr in findAddrByType(Speaker.TYPE_NAME)) new Speaker(addr)];
|
||||
}
|
||||
}
|
||||
|
61
src/kernel/peripherals/Speaker.hx
Normal file
61
src/kernel/peripherals/Speaker.hx
Normal file
@ -0,0 +1,61 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class Speaker implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "speaker";
|
||||
|
||||
private final addr:String;
|
||||
|
||||
public function new(addr:String) {
|
||||
this.addr = addr;
|
||||
}
|
||||
|
||||
public function getType():String {
|
||||
return Speaker.TYPE_NAME;
|
||||
}
|
||||
|
||||
public function getAddr():String {
|
||||
return this.addr;
|
||||
}
|
||||
|
||||
/**
|
||||
Plays a note block note through the speaker.
|
||||
The pitch argument uses semitones as the unit. This directly maps to the number of clicks on a note block.
|
||||
For reference, 0, 12, and 24 map to F#, and 6 and 18 map to C.
|
||||
A maximum of 8 notes can be played in a single tick. If this limit is hit, this function will return an error.
|
||||
**/
|
||||
public function playNote(instrument:String, ?volume:Float = 1.0, ?pitch:Int = 12):Outcome<Noise, String> {
|
||||
if (Peripheral.call(addr, "playNote", instrument, volume, pitch)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("maximum reached");
|
||||
}
|
||||
}
|
||||
|
||||
public function playSound(sound:String, ?volume:Float = 1.0, ?pitch:Float = 1.0):Outcome<Noise, String> {
|
||||
try {
|
||||
if (Peripheral.call(addr, "playSound", sound, volume, pitch)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("Sound still playing");
|
||||
}
|
||||
} catch (e) {
|
||||
return Failure("Sound does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
public function playAudio(buffer:Array<Int>, ?volume:Float = 1.0):Outcome<Noise, String> {
|
||||
try {
|
||||
if (Peripheral.call(addr, "playAudio", buffer, volume)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("Buffer full");
|
||||
}
|
||||
} catch (e) {
|
||||
return Failure("Buffer malformed");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user