big Peripheal refactor

This commit is contained in:
Djeeberjr 2023-06-04 20:52:24 +02:00
parent 2051f486d4
commit fd2b53167e
10 changed files with 86 additions and 72 deletions

View File

@ -9,7 +9,7 @@ using Lambda;
class Disk extends CLIAppBase { class Disk extends CLIAppBase {
public function new() { public function new() {
registerSyncSubcommand("ls", (args)->{ registerSyncSubcommand("ls", (args)->{
Peripheral.instance.getDrives().foreach(drive -> { Peripheral.instance.getAllDrives().foreach(drive -> {
var addr = drive.getAddr(); var addr = drive.getAddr();
var label = drive.getDiskLabel(); var label = drive.getDiskLabel();
var id = drive.getDiskID(); var id = drive.getDiskID();

View File

@ -19,7 +19,7 @@ class Net extends CLIAppBase {
}); });
registerSyncSubcommand("iface", (args)->{ registerSyncSubcommand("iface", (args)->{
var modems = Peripheral.instance.getModems(); var modems = Peripheral.instance.getAllModems();
for (modem in modems) { for (modem in modems) {
handle.writeLine(modem.name()); handle.writeLine(modem.name());

View File

@ -31,7 +31,7 @@ class Net {
@:allow(kernel.Init) @:allow(kernel.Init)
private function new() { private function new() {
this.interfaces = [for (e in Peripheral.instance.getModems()) e ]; // TODO: is this the way to do it? this.interfaces = [for (e in Peripheral.instance.getAllModems()) e ]; // TODO: is this the way to do it?
this.interfaces.push(Loopback.instance); this.interfaces.push(Loopback.instance);
for (interf in interfaces){ for (interf in interfaces){
@ -279,7 +279,7 @@ class Net {
data: null, data: null,
}; };
for (modem in Peripheral.instance.getModems()) { for (modem in Peripheral.instance.getAllModems()) {
if (!modem.isWireless()) continue; if (!modem.isWireless()) continue;
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
} }

View File

@ -64,7 +64,7 @@ class Routing {
private function brodcastRoutingTable() { private function brodcastRoutingTable() {
var pack = newRoutDiscoverPackage(); var pack = newRoutDiscoverPackage();
for (modem in Peripheral.instance.getModems()) { for (modem in Peripheral.instance.getAllModems()) {
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
} }
} }

View File

@ -1,8 +1,11 @@
package kernel.peripherals; package kernel.peripherals;
import cc.Peripheral;
using tink.CoreApi; using tink.CoreApi;
class Drive implements IPeripheral { class Drive implements IPeripheral {
public static inline final TYPE_NAME:String = "drive";
public final onDiskInsert:Signal<Noise>; public final onDiskInsert:Signal<Noise>;
public final onDiskEject:Signal<Noise>; public final onDiskEject:Signal<Noise>;
@ -12,9 +15,9 @@ class Drive implements IPeripheral {
private final onDiskEjectTrigger:SignalTrigger<Noise> = Signal.trigger(); private final onDiskEjectTrigger:SignalTrigger<Noise> = Signal.trigger();
@:allow(kernel.peripherals) @:allow(kernel.peripherals)
private function new(native: cc.periphs.Disk,addr: String) { private function new(addr: String) {
this.addr = addr; this.addr = addr;
this.native = native; this.native = Peripheral.wrap(addr);
this.onDiskInsert = this.onDiskInsertTrigger.asSignal(); this.onDiskInsert = this.onDiskInsertTrigger.asSignal();
this.onDiskEject = this.onDiskEjectTrigger.asSignal(); this.onDiskEject = this.onDiskEjectTrigger.asSignal();

View File

@ -1,5 +1,6 @@
package kernel.peripherals; package kernel.peripherals;
import cc.Peripheral;
import kernel.log.Log; import kernel.log.Log;
import kernel.net.Package; import kernel.net.Package;
import kernel.net.INetworkInterface; import kernel.net.INetworkInterface;
@ -7,6 +8,8 @@ import kernel.net.INetworkInterface;
using tink.CoreApi; using tink.CoreApi;
class Modem implements INetworkInterface implements IPeripheral { class Modem implements INetworkInterface implements IPeripheral {
public static inline final TYPE_NAME:String = "modem";
public final addr:String; public final addr:String;
public var onMessage(default, null):Signal<{pack:Package,dist:Null<Float>}>; public var onMessage(default, null):Signal<{pack:Package,dist:Null<Float>}>;
@ -14,9 +17,9 @@ class Modem implements INetworkInterface implements IPeripheral {
private final native:cc.periphs.Modem.Modem; private final native:cc.periphs.Modem.Modem;
@:allow(kernel.peripherals) @:allow(kernel.peripherals)
private function new(nativePeripherals:cc.periphs.Modem.Modem, addr:String) { private function new(addr:String) {
this.onMessage = onMessageTrigger.asSignal(); this.onMessage = onMessageTrigger.asSignal();
this.native = nativePeripherals; this.native = Peripheral.wrap(addr);
this.addr = addr; this.addr = addr;
KernelEvents.instance.onModemMessage.handle(params ->{ KernelEvents.instance.onModemMessage.handle(params ->{

View File

@ -17,93 +17,96 @@ class Peripheral {
**/ **/
public static var instance:Peripheral; public static var instance:Peripheral;
private var screens: Array<Screen> = [];
private var modes: Array<Modem> = [];
private var drives:Array<Drive> = [];
private var redstone:Array<Redstone> = [];
private var printers:Array<Printer> = [];
@:allow(kernel.Init) @:allow(kernel.Init)
private function new() { 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() { public function getAllAddresses(): Array<String> {
findScreens(); return cc.Peripheral.getNames().toArray();
findModems();
findDrives();
} }
/** public function isPresent(addr: String): Bool {
Get all connected screens. return cc.Peripheral.isPresent(addr);
**/
public function getScreens():ReadOnlyArray<Screen> {
return this.screens;
} }
private function findScreens():Void { public function getTypes(addr: String): Array<String> {
var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor"); if (!cc.Peripheral.isPresent(addr)) {
this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s)); return [];
}
return cc.Peripheral.getType(addr).toArray();
} }
public function getScreen(addr: String): Screen { private function findAddrByType(type: String): Array<String> {
return this.screens.find(item -> item.getAddr() == addr); return getAllAddresses().filter(addr -> getTypes(addr).contains(type));
}
private function safeGetAddr(addr: String, type: String): Null<String> {
if (!isPresent(addr)) {
return null;
}
var types = getTypes(addr);
if (!types.contains(type)) {
return null;
}
return addr;
} }
/** public function getScreen(addr: String): Null<Screen> {
Get all connected modems. var addr = safeGetAddr(addr, Screen.TYPE_NAME);
**/ if (addr == null) return null;
public function getModems():ReadOnlyArray<Modem> { return new Screen(addr);
return this.modes; }
public function getAllScreens(): Array<Screen> {
return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)];
} }
public function getModem(addr: String): Null<Modem> { public function getModem(addr: String): Null<Modem> {
return this.modes.find(item -> item.getAddr() == addr); var addr = safeGetAddr(addr, Modem.TYPE_NAME);
if (addr == null) return null;
return new Modem(addr);
} }
private function findModems():Void { public function getAllModems(): Array<Modem> {
var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem"); return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)];
this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s));
}
/**
Get all connected drives.
**/
public function getDrives(): ReadOnlyArray<Drive> {
return this.drives;
} }
public function getDrive(addr: String): Null<Drive> { public function getDrive(addr: String): Null<Drive> {
return this.drives.find(item -> item.getAddr() == addr); var addr = safeGetAddr(addr, Drive.TYPE_NAME);
if (addr == null) return null;
return new Drive(addr);
} }
private function findDrives() { public function getAllDrives(): Array<Drive> {
var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive"); return [ for (addr in findAddrByType(Drive.TYPE_NAME)) new Drive(addr)];
this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s));
} }
/**
Get redstone peripheral.
**/
public function getRedstone(side: String): Redstone { public function getRedstone(side: String): Redstone {
return this.redstone.find(item -> item.getAddr() == side); // TODO: maybe handle restone differently to not duplicate event listeners
return new Redstone(side);
} }
/** public function getPrinter(addr: String):Null<Printer> {
Get all connected printers. var addr = safeGetAddr(addr, Printer.TYPE_NAME);
**/ if (addr == null) return null;
return new Printer(addr);
public function getPrinters():ReadOnlyArray<Printer> {
return this.printers;
} }
private function findPrinters() { public function getAllPrinters(): Array<Printer> {
var allPrinters = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "printer"); return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)];
this.printers = allPrinters.map(s -> return new Printer((cc.Peripheral.wrap(s) : Dynamic), s));
} }
public function getEnergyStorage(addr: String): Null<EnergyStorage> {
var addr = safeGetAddr(addr, EnergyStorage.TYPE_NAME);
if (addr == null) return null;
return new EnergyStorage(addr);
}
public function getAllEnergyStorages(): Array<EnergyStorage> {
return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)];
}
} }

View File

@ -1,15 +1,17 @@
package kernel.peripherals; package kernel.peripherals;
import cc.Peripheral;
import lib.Rect; import lib.Rect;
import lib.Pos; import lib.Pos;
class Printer implements IPeripheral { class Printer implements IPeripheral {
public static inline final TYPE_NAME:String = "printer";
private final native:cc.periphs.Printer.Printer; private final native:cc.periphs.Printer.Printer;
private final addr:String; private final addr:String;
public function new(native: cc.periphs.Printer.Printer, addr: String) { public function new(addr: String) {
this.native = native; this.native = Peripheral.wrap(addr);
this.addr = addr; this.addr = addr;
} }

View File

@ -1,5 +1,6 @@
package kernel.peripherals; package kernel.peripherals;
import cc.Peripheral;
import lib.Pos; import lib.Pos;
import cc.Term.TerminalSize; import cc.Term.TerminalSize;
import kernel.ui.TermWriteable; import kernel.ui.TermWriteable;
@ -9,6 +10,8 @@ import lib.Color;
using tink.CoreApi; using tink.CoreApi;
class Screen implements TermWriteable implements IPeripheral { class Screen implements TermWriteable implements IPeripheral {
public static inline final TYPE_NAME:String = "monitor";
private final nativ:cc.periphs.Monitor.Monitor; private final nativ:cc.periphs.Monitor.Monitor;
private final addr:String; private final addr:String;
@ -17,10 +20,10 @@ class Screen implements TermWriteable implements IPeripheral {
public final onResize:Signal<Vec2<Int>>; public final onResize:Signal<Vec2<Int>>;
@:allow(kernel.peripherals) @:allow(kernel.peripherals)
private function new(nativePeripherals:cc.periphs.Monitor.Monitor, addr:String) { private function new(addr:String) {
this.onResize = onResizeTrigger.asSignal(); this.onResize = onResizeTrigger.asSignal();
this.nativ = nativePeripherals; this.nativ = Peripheral.wrap(addr);
this.addr = addr; this.addr = addr;
KernelEvents.instance.onMonitorResize.handle(addr -> { KernelEvents.instance.onMonitorResize.handle(addr -> {

View File

@ -134,7 +134,7 @@ class WindowManager {
} }
public function getOutputs():ReadOnlyArray<String> { public function getOutputs():ReadOnlyArray<String> {
var arr = Peripheral.instance.getScreens().map(screen -> return screen.getAddr()); var arr = Peripheral.instance.getAllScreens().map(screen -> return screen.getAddr());
arr.push("main"); arr.push("main");
return arr; return arr;
} }