Better boot code

This commit is contained in:
Andrea Cavalli 2019-04-09 18:19:06 +02:00
parent ff7dd21788
commit 60f91fbc2e
8 changed files with 137 additions and 138 deletions

View File

@ -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>

View File

@ -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,15 +17,14 @@ 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;
@ -43,19 +44,18 @@ public class WarpPI {
* @throws InterruptedException
* @throws IOException
*/
public static void start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args)
throws InterruptedException, 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();
}
}

View File

@ -1,35 +1,32 @@
package it.cavallium.warppi.boot;
import java.util.Arrays;
import java.util.concurrent.Future;
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);
Future<Void> execution = WarpPI.start(
platform,
new LoadingScreen(),
new CalculatorHUD(),
Boot.parseStartupArguments(args),
Boot::loadCalculator);
execution.get();
}
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();
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) {

View File

@ -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();
}
}

View File

@ -0,0 +1,5 @@
package it.cavallium.warppi.util;
public interface RunnableWithException {
public void run() throws Exception;
}

View File

@ -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>

View File

@ -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>

View File

@ -1,6 +1,6 @@
package it.cavallium.warppi.util;
public class Error extends java.lang.Throwable {
public class Error extends java.lang.Exception {
/**
*