2021-12-20 00:55:30 +00:00
|
|
|
package kernel.peripherals;
|
|
|
|
|
|
|
|
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-03-05 02:01:26 +00:00
|
|
|
private var drives:Array<Drive> = [];
|
2022-02-24 18:47:27 +00:00
|
|
|
|
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();
|
2022-03-05 02:06:41 +00:00
|
|
|
findDrives();
|
2022-02-24 18:47:27 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-03-05 02:01:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
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));
|
|
|
|
}
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|