Better boot code
This commit is contained in:
parent
ff7dd21788
commit
60f91fbc2e
@ -18,8 +18,9 @@
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
|
@ -1,6 +1,8 @@
|
||||
package it.cavallium.warppi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import it.cavallium.warppi.Platform.ConsoleUtils;
|
||||
import it.cavallium.warppi.boot.StartupArguments;
|
||||
@ -15,47 +17,45 @@ import it.cavallium.warppi.flow.BehaviorSubject;
|
||||
import it.cavallium.warppi.flow.Observable;
|
||||
import it.cavallium.warppi.gui.DisplayManager;
|
||||
import it.cavallium.warppi.gui.HUD;
|
||||
import it.cavallium.warppi.gui.HardwareDisplay;
|
||||
import it.cavallium.warppi.gui.screens.Screen;
|
||||
import it.cavallium.warppi.util.ClassUtils;
|
||||
import it.cavallium.warppi.util.RunnableWithException;
|
||||
|
||||
public class WarpPI {
|
||||
public static final WarpPI INSTANCE = new WarpPI();
|
||||
private static Platform platform;
|
||||
private static boolean running = false;
|
||||
private static BehaviorSubject<LoadingStatus> loadPhase = BehaviorSubject.create();
|
||||
private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false);
|
||||
private Device device;
|
||||
|
||||
private WarpPI() {}
|
||||
|
||||
/**
|
||||
* Start an instance of the calculator.
|
||||
*
|
||||
* @param platform
|
||||
* Platform implementation
|
||||
* @param screen
|
||||
* Default screen to show at startup
|
||||
* @param hud
|
||||
* Head-up display
|
||||
* @param args
|
||||
* Startup arguments
|
||||
* @throws InterruptedException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args)
|
||||
throws InterruptedException, IOException {
|
||||
/**
|
||||
* Start an instance of the calculator.
|
||||
*
|
||||
* @param platform
|
||||
* Platform implementation
|
||||
* @param screen
|
||||
* Default screen to show at startup
|
||||
* @param hud
|
||||
* Head-up display
|
||||
* @param args
|
||||
* Startup arguments
|
||||
* @throws InterruptedException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Future<Void> start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args, final RunnableWithException onLoading) throws IOException {
|
||||
if (WarpPI.running) {
|
||||
throw new RuntimeException("Already running!");
|
||||
} else {
|
||||
WarpPI.running = true;
|
||||
WarpPI.INSTANCE.startEngine(platform, screen, hud, args);
|
||||
return WarpPI.INSTANCE.startInstance(platform, screen, hud, args, onLoading);
|
||||
}
|
||||
}
|
||||
|
||||
private void startEngine(final Platform platform, final Screen screen,
|
||||
final HUD hud, final StartupArguments args)
|
||||
throws InterruptedException, IOException {
|
||||
private Future<Void> startInstance(final Platform platform, final Screen screen,
|
||||
final HUD hud, final StartupArguments args, final RunnableWithException onLoading)
|
||||
throws IOException {
|
||||
WarpPI.platform = platform;
|
||||
platform.getConsoleUtils().out().println("WarpPI Calculator");
|
||||
initializeEnvironment(args);
|
||||
@ -63,15 +63,23 @@ public class WarpPI {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
currentThread.setPriority(Thread.MAX_PRIORITY);
|
||||
WarpPI.getPlatform().setThreadName(currentThread, "Main thread");
|
||||
final DisplayOutputDevice display = platform.getDisplayOutputDevice();
|
||||
final BacklightOutputDevice backlight = platform.getBacklightOutputDevice();
|
||||
final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
|
||||
final KeyboardInputDevice keyboard = platform.getKeyboardInputDevice();
|
||||
final TouchInputDevice touchscreen = platform.getTouchInputDevice();
|
||||
final InputManager im = new InputManager(keyboard, touchscreen);
|
||||
device = new Device(dm, im);
|
||||
|
||||
device.setup(() -> WarpPI.loadPhase.onNext(new LoadingStatus()));
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
final DisplayOutputDevice display = platform.getDisplayOutputDevice();
|
||||
final BacklightOutputDevice backlight = platform.getBacklightOutputDevice();
|
||||
final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
|
||||
final KeyboardInputDevice keyboard = platform.getKeyboardInputDevice();
|
||||
final TouchInputDevice touchscreen = platform.getTouchInputDevice();
|
||||
final InputManager im = new InputManager(keyboard, touchscreen);
|
||||
device = new Device(dm, im);
|
||||
device.setup();
|
||||
onLoading.run();
|
||||
this.loadingCompleted();
|
||||
} catch (Exception ex) {
|
||||
this.loadingFailed(ex);
|
||||
}
|
||||
}).thenRun(this::onShutdown);
|
||||
}
|
||||
|
||||
private void onShutdown() {
|
||||
@ -117,10 +125,6 @@ public class WarpPI {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public Observable<LoadingStatus> getLoadPhase() {
|
||||
return WarpPI.loadPhase;
|
||||
}
|
||||
|
||||
public Device getHardwareDevice() {
|
||||
return device;
|
||||
}
|
||||
@ -129,19 +133,13 @@ public class WarpPI {
|
||||
return WarpPI.platform;
|
||||
}
|
||||
|
||||
public static class LoadingStatus {
|
||||
protected LoadingStatus() {
|
||||
|
||||
}
|
||||
private void loadingCompleted() {
|
||||
WarpPI.INSTANCE.loaded.onNext(true);
|
||||
WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
|
||||
}
|
||||
|
||||
public void done() {
|
||||
WarpPI.INSTANCE.loaded.onNext(true);
|
||||
WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
|
||||
WarpPI.INSTANCE.onShutdown();
|
||||
}
|
||||
|
||||
public void failed() {
|
||||
WarpPI.INSTANCE.onShutdown();
|
||||
}
|
||||
private void loadingFailed(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -1,82 +1,79 @@
|
||||
package it.cavallium.warppi.boot;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import it.cavallium.warppi.WarpPI;
|
||||
import it.cavallium.warppi.WarpPI.LoadingStatus;
|
||||
import it.cavallium.warppi.Platform;
|
||||
import it.cavallium.warppi.device.PIHardwareDisplay;
|
||||
import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
|
||||
import it.cavallium.warppi.gui.CalculatorHUD;
|
||||
import it.cavallium.warppi.gui.screens.LoadingScreen;
|
||||
import it.cavallium.warppi.math.rules.RulesManager;
|
||||
import it.cavallium.warppi.util.Error;
|
||||
|
||||
public class Boot {
|
||||
|
||||
public static void boot(final Platform platform, final String[] args) throws Exception {
|
||||
WarpPI.start(platform, new LoadingScreen(), new CalculatorHUD(), Boot.parseStartupArguments(args));
|
||||
WarpPI.INSTANCE.getLoadPhase().subscribe(Boot::loadCalculator);
|
||||
}
|
||||
|
||||
private static void loadCalculator(final LoadingStatus loading) {
|
||||
try {
|
||||
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
|
||||
//TODO: plugins system: PluginsManager.initialize();
|
||||
RulesManager.initialize();
|
||||
RulesManager.warmUp();
|
||||
loading.done();
|
||||
} catch (InterruptedException | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
loading.failed();
|
||||
}
|
||||
|
||||
public static StartupArguments parseStartupArguments(final String[] a) {
|
||||
final StartupArgumentsImpl args = new StartupArgumentsImpl();
|
||||
Arrays.asList(a).stream().parallel().filter((x) -> x != null).map(String::toLowerCase).forEach(arg -> Boot.parseArgument(args, arg));
|
||||
return args;
|
||||
}
|
||||
|
||||
public static void parseArgument(final StartupArgumentsImpl args, final String arg) {
|
||||
switch (arg) {
|
||||
case "-zoomed":
|
||||
args.setZoomed(true);
|
||||
break;
|
||||
case "-verbose":
|
||||
args.setVerboseLoggingEnabled(true);
|
||||
break;
|
||||
case "-noraspi":
|
||||
args.setRaspberryModeAllowed(false);
|
||||
break;
|
||||
case "nogui":
|
||||
args.setNoGUIEngineForced(true);
|
||||
break;
|
||||
case "ms-dos":
|
||||
args.setMSDOSModeEnabled(true);
|
||||
break;
|
||||
case "html":
|
||||
args.setHTMLEngineForced(true);
|
||||
break;
|
||||
case "gpu":
|
||||
args.setGPUEngineForced(true);
|
||||
break;
|
||||
case "cpu":
|
||||
args.setCPUEngineForced(true);
|
||||
break;
|
||||
case "framebuffer":
|
||||
args.setFrameBufferEngineForced(true);
|
||||
break;
|
||||
case "-debug":
|
||||
args.setDebugEnabled(true);
|
||||
break;
|
||||
case "-uncached":
|
||||
args.setUncached(true);
|
||||
break;
|
||||
default:
|
||||
// Not using ConsoleUtils because it isn't initialized at this point.
|
||||
System.out.println("Unrecognized argument " + arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
package it.cavallium.warppi.boot;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import it.cavallium.warppi.WarpPI;
|
||||
import it.cavallium.warppi.Platform;
|
||||
import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
|
||||
import it.cavallium.warppi.gui.CalculatorHUD;
|
||||
import it.cavallium.warppi.gui.screens.LoadingScreen;
|
||||
import it.cavallium.warppi.math.rules.RulesManager;
|
||||
|
||||
public class Boot {
|
||||
|
||||
public static void boot(final Platform platform, final String[] args) throws Exception {
|
||||
Future<Void> execution = WarpPI.start(
|
||||
platform,
|
||||
new LoadingScreen(),
|
||||
new CalculatorHUD(),
|
||||
Boot.parseStartupArguments(args),
|
||||
Boot::loadCalculator);
|
||||
execution.get();
|
||||
}
|
||||
|
||||
private static void loadCalculator() throws Exception {
|
||||
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
|
||||
//TODO: plugins system: PluginsManager.initialize();
|
||||
RulesManager.initialize();
|
||||
RulesManager.warmUp();
|
||||
}
|
||||
|
||||
public static StartupArguments parseStartupArguments(final String[] a) {
|
||||
final StartupArgumentsImpl args = new StartupArgumentsImpl();
|
||||
Arrays.asList(a).stream().parallel().filter((x) -> x != null).map(String::toLowerCase).forEach(arg -> Boot.parseArgument(args, arg));
|
||||
return args;
|
||||
}
|
||||
|
||||
public static void parseArgument(final StartupArgumentsImpl args, final String arg) {
|
||||
switch (arg) {
|
||||
case "-zoomed":
|
||||
args.setZoomed(true);
|
||||
break;
|
||||
case "-verbose":
|
||||
args.setVerboseLoggingEnabled(true);
|
||||
break;
|
||||
case "-noraspi":
|
||||
args.setRaspberryModeAllowed(false);
|
||||
break;
|
||||
case "nogui":
|
||||
args.setNoGUIEngineForced(true);
|
||||
break;
|
||||
case "ms-dos":
|
||||
args.setMSDOSModeEnabled(true);
|
||||
break;
|
||||
case "html":
|
||||
args.setHTMLEngineForced(true);
|
||||
break;
|
||||
case "gpu":
|
||||
args.setGPUEngineForced(true);
|
||||
break;
|
||||
case "cpu":
|
||||
args.setCPUEngineForced(true);
|
||||
break;
|
||||
case "framebuffer":
|
||||
args.setFrameBufferEngineForced(true);
|
||||
break;
|
||||
case "-debug":
|
||||
args.setDebugEnabled(true);
|
||||
break;
|
||||
case "-uncached":
|
||||
args.setUncached(true);
|
||||
break;
|
||||
default:
|
||||
// Not using ConsoleUtils because it isn't initialized at this point.
|
||||
System.out.println("Unrecognized argument " + arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,12 +21,8 @@ public class Device {
|
||||
return inputManager;
|
||||
}
|
||||
|
||||
public void setup(final Runnable r) {
|
||||
public void setup() {
|
||||
displayManager.initialize();
|
||||
final Thread t = new Thread(r);
|
||||
WarpPI.getPlatform().setThreadDaemon(t, false);
|
||||
WarpPI.getPlatform().setThreadName(t, "Main thread (after setup)");
|
||||
t.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
package it.cavallium.warppi.util;
|
||||
|
||||
public interface RunnableWithException {
|
||||
public void run() throws Exception;
|
||||
}
|
@ -23,8 +23,9 @@
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
|
@ -8,13 +8,14 @@
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
|
@ -1,6 +1,6 @@
|
||||
package it.cavallium.warppi.util;
|
||||
|
||||
public class Error extends java.lang.Throwable {
|
||||
public class Error extends java.lang.Exception {
|
||||
|
||||
/**
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user