package kernel.peripherals; import kernel.peripherals.Modem; import kernel.peripherals.Screen; using lua.Table; using Lambda; using tink.CoreApi; /** Class responseable for retrieving peripherals. **/ class Peripheral { /** Depends on: KernelEvents **/ public static var instance:Peripheral; private var screens: Array = []; private var modes: Array = []; private var drives:Array = []; @:allow(kernel.Init) private function new() { KernelEvents.instance.onPeripheral.handle(this.updatePeripherals); KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals); updatePeripherals(); } private function updatePeripherals() { findScreens(); findModems(); findDrives(); } /** Get all connected screens. **/ public function getScreens():Array { return this.screens; } private function findScreens():Void { var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor"); this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s)); } public function getScreen(addr: String): Screen { return this.screens.find(item -> item.getAddr() == addr); } /** Get all connected modems. **/ public function getModems():Array { return this.modes; } public function getModem(addr: String): Null { return this.modes.find(item -> item.getAddr() == addr); } private function findModems():Void { var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem"); this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s)); } /** Get all connected drives. **/ public function getDrives(): Array { return this.drives; } public function getDrive(addr: String): Null { return this.drives.find(item -> item.getAddr() == addr); } private function findDrives() { 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)); } }