cc-haxe/src/kernel/peripherals/Peripherals.hx
2022-03-05 03:06:41 +01:00

84 lines
2.1 KiB
Haxe

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<Screen> = [];
private var modes: Array<Modem> = [];
private var drives:Array<Drive> = [];
@: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<Screen> {
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<Modem> {
return this.modes;
}
public function getModem(addr: String): Null<Modem> {
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<Drive> {
return this.drives;
}
public function getDrive(addr: String): Null<Drive> {
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));
}
}