cc-haxe/src/kernel/peripherals/Peripherals.hx

67 lines
1.6 KiB
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
package kernel.peripherals;
2022-02-24 18:47:27 +00:00
import kernel.net.INetworkInterface;
2021-12-20 00:55:30 +00:00
import kernel.peripherals.Modem;
import kernel.peripherals.Screen;
using lua.Table;
using Lambda;
2022-03-05 01:41:30 +00:00
using tink.CoreApi;
2021-12-20 00:55:30 +00:00
/**
Class responseable for retrieving peripherals.
**/
class Peripheral {
2022-02-24 18:47:27 +00:00
/**
Depends on: KernelEvents
**/
2022-02-21 00:50:19 +00:00
public static var instance:Peripheral;
2022-02-24 18:47:27 +00:00
private var screens: Array<Screen> = [];
private var modes: Array<Modem> = [];
2022-02-21 00:50:19 +00:00
@:allow(kernel.Init)
2022-02-24 18:47:27 +00:00
private function new() {
KernelEvents.instance.onPeripheral.handle(this.updatePeripherals);
KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals);
updatePeripherals();
}
private function updatePeripherals() {
findScreens();
findModems();
}
2021-12-20 00:55:30 +00:00
/**
Get all connected screens.
**/
2022-02-21 14:35:37 +00:00
public function getScreens():Array<Screen> {
2022-02-24 18:47:27 +00:00
return this.screens;
2021-12-20 00:55:30 +00:00
}
2022-02-24 18:47:27 +00:00
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));
}
2021-12-20 00:55:30 +00:00
2022-02-24 18:47:27 +00:00
public function getScreen(addr: String): Screen {
return this.screens.find(item -> item.getAddr() == addr);
2021-12-20 00:55:30 +00:00
}
/**
Get all connected modems.
**/
2022-02-21 14:35:37 +00:00
public function getModems():Array<Modem> {
2022-02-24 18:47:27 +00:00
return this.modes;
2021-12-20 00:55:30 +00:00
}
2022-03-05 01:41:30 +00:00
public function getModem(addr: String): Null<Modem> {
return this.modes.find(item -> item.getAddr() == addr);
}
2022-02-24 18:47:27 +00:00
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));
2021-12-20 00:55:30 +00:00
}
}