diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index 074abe3..ec3ce38 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -20,12 +20,15 @@ class Peripheral { private var screens: Array = []; private var modes: Array = []; private var drives:Array = []; + private var redstone:Array = []; @:allow(kernel.Init) private function new() { KernelEvents.instance.onPeripheral.handle(this.updatePeripherals); KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals); updatePeripherals(); + + redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ]; } private function updatePeripherals() { @@ -81,4 +84,8 @@ class Peripheral { var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive"); this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s)); } + + public function getRedstone(side: String): Redstone { + return this.redstone.find(item -> item.getAddr() == side); + } } diff --git a/src/kernel/peripherals/Redstone.hx b/src/kernel/peripherals/Redstone.hx new file mode 100644 index 0000000..cfe3409 --- /dev/null +++ b/src/kernel/peripherals/Redstone.hx @@ -0,0 +1,60 @@ +package kernel.peripherals; + +import util.Color; + +typedef BundleMask = Color; + +class Redstone implements IPeripheral { + + private final addr:Side; + + @:allow(kernel.peripherals) + private function new(side: Side) { + this.addr = side; + } + + public function getAddr():String { + return this.addr; + } + + public inline function setOutput(on:Bool):Void { + cc.Redstone.setOutput(this.addr,on); + } + + public inline function getOutput(): Bool { + return cc.Redstone.getOutput(this.addr); + } + + public inline function getInput(): Bool { + return cc.Redstone.getInput(this.addr); + } + + public inline function setAnalogOutput(strength:Int): Void { + cc.Redstone.setAnalogOutput(this.addr,strength); + } + + public inline function getAnalogOutput(): Int { + return cc.Redstone.getAnalogOutput(this.addr); + } + + public inline function getAnalogInput(): Int { + return cc.Redstone.getAnalogInput(this.addr); + } + + public inline function setBundledOutput(output: BundleMask) { + cc.Redstone.setBundledOutput(this.addr,output); + } + + public inline function getBundledOutput():BundleMask { + return cc.Redstone.getBundledOutput(this.addr); + } + + public inline function getBundledInput():BundleMask { + return cc.Redstone.getBundledInput(this.addr); + } + + public inline function testBundledInput(mask: Color): Bool { + return cc.Redstone.testBundledInput(this.addr,mask); + } + +} diff --git a/src/kernel/peripherals/Side.hx b/src/kernel/peripherals/Side.hx new file mode 100644 index 0000000..bc4c6ee --- /dev/null +++ b/src/kernel/peripherals/Side.hx @@ -0,0 +1,10 @@ +package kernel.peripherals; + +enum abstract Side(String) to String { + var Top = "top"; + var Bottom = "bottom"; + var Left = "left"; + var Right = "right"; + var Front = "front"; + var Back = "back"; +} diff --git a/src/util/Color.hx b/src/util/Color.hx index 929826c..b1445fb 100644 --- a/src/util/Color.hx +++ b/src/util/Color.hx @@ -1,5 +1,7 @@ package util; +import kernel.peripherals.Redstone.BundleMask; + enum abstract Color(Int) from cc.Colors.Color to cc.Colors.Color { var White = 0x1; var Orange = 0x2; @@ -17,4 +19,9 @@ enum abstract Color(Int) from cc.Colors.Color to cc.Colors.Color { var Green = 0x2000; var Red = 0x4000; var Black = 0x8000; + + @:op(A + B) + public inline function combine(rhs: Color):BundleMask { + return this + rhs; + } }