added speaker peripheral
This commit is contained in:
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user