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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

@ -1,6 +1,8 @@
package it.cavallium.warppi; package it.cavallium.warppi;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import it.cavallium.warppi.Platform.ConsoleUtils; import it.cavallium.warppi.Platform.ConsoleUtils;
import it.cavallium.warppi.boot.StartupArguments; 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.flow.Observable;
import it.cavallium.warppi.gui.DisplayManager; import it.cavallium.warppi.gui.DisplayManager;
import it.cavallium.warppi.gui.HUD; import it.cavallium.warppi.gui.HUD;
import it.cavallium.warppi.gui.HardwareDisplay;
import it.cavallium.warppi.gui.screens.Screen; import it.cavallium.warppi.gui.screens.Screen;
import it.cavallium.warppi.util.ClassUtils; import it.cavallium.warppi.util.ClassUtils;
import it.cavallium.warppi.util.RunnableWithException;
public class WarpPI { public class WarpPI {
public static final WarpPI INSTANCE = new WarpPI(); public static final WarpPI INSTANCE = new WarpPI();
private static Platform platform; private static Platform platform;
private static boolean running = false; private static boolean running = false;
private static BehaviorSubject<LoadingStatus> loadPhase = BehaviorSubject.create();
private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false); private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false);
private Device device; private Device device;
private WarpPI() {} private WarpPI() {}
/** /**
* Start an instance of the calculator. * Start an instance of the calculator.
* *
* @param platform * @param platform
* Platform implementation * Platform implementation
* @param screen * @param screen
* Default screen to show at startup * Default screen to show at startup
* @param hud * @param hud
* Head-up display * Head-up display
* @param args * @param args
* Startup arguments * Startup arguments
* @throws InterruptedException * @throws InterruptedException
* @throws IOException * @throws IOException
*/ */
public static void start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args) public static Future<Void> start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args, final RunnableWithException onLoading) throws IOException {
throws InterruptedException, IOException {
if (WarpPI.running) { if (WarpPI.running) {
throw new RuntimeException("Already running!"); throw new RuntimeException("Already running!");
} else { } else {
WarpPI.running = true; 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, private Future<Void> startInstance(final Platform platform, final Screen screen,
final HUD hud, final StartupArguments args) final HUD hud, final StartupArguments args, final RunnableWithException onLoading)
throws InterruptedException, IOException { throws IOException {
WarpPI.platform = platform; WarpPI.platform = platform;
platform.getConsoleUtils().out().println("WarpPI Calculator"); platform.getConsoleUtils().out().println("WarpPI Calculator");
initializeEnvironment(args); initializeEnvironment(args);
@ -63,15 +63,23 @@ public class WarpPI {
final Thread currentThread = Thread.currentThread(); final Thread currentThread = Thread.currentThread();
currentThread.setPriority(Thread.MAX_PRIORITY); currentThread.setPriority(Thread.MAX_PRIORITY);
WarpPI.getPlatform().setThreadName(currentThread, "Main thread"); 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() { private void onShutdown() {
@ -117,10 +125,6 @@ public class WarpPI {
return loaded; return loaded;
} }
public Observable<LoadingStatus> getLoadPhase() {
return WarpPI.loadPhase;
}
public Device getHardwareDevice() { public Device getHardwareDevice() {
return device; return device;
} }
@ -129,19 +133,13 @@ public class WarpPI {
return WarpPI.platform; 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() { private void loadingFailed(Exception e) {
WarpPI.INSTANCE.loaded.onNext(true); e.printStackTrace();
WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
WarpPI.INSTANCE.onShutdown();
}
public void failed() {
WarpPI.INSTANCE.onShutdown();
}
} }
} }

View File

@ -1,82 +1,79 @@
package it.cavallium.warppi.boot; package it.cavallium.warppi.boot;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.Future;
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.WarpPI.LoadingStatus; import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.Platform; import it.cavallium.warppi.Platform;
import it.cavallium.warppi.device.PIHardwareDisplay; import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
import it.cavallium.warppi.device.input.PIHardwareTouchDevice; import it.cavallium.warppi.gui.CalculatorHUD;
import it.cavallium.warppi.gui.CalculatorHUD; import it.cavallium.warppi.gui.screens.LoadingScreen;
import it.cavallium.warppi.gui.screens.LoadingScreen; import it.cavallium.warppi.math.rules.RulesManager;
import it.cavallium.warppi.math.rules.RulesManager;
import it.cavallium.warppi.util.Error; public class Boot {
public class Boot { public static void boot(final Platform platform, final String[] args) throws Exception {
Future<Void> execution = WarpPI.start(
public static void boot(final Platform platform, final String[] args) throws Exception { platform,
WarpPI.start(platform, new LoadingScreen(), new CalculatorHUD(), Boot.parseStartupArguments(args)); new LoadingScreen(),
WarpPI.INSTANCE.getLoadPhase().subscribe(Boot::loadCalculator); new CalculatorHUD(),
} Boot.parseStartupArguments(args),
Boot::loadCalculator);
private static void loadCalculator(final LoadingStatus loading) { execution.get();
try { }
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
//TODO: plugins system: PluginsManager.initialize(); private static void loadCalculator() throws Exception {
RulesManager.initialize(); WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
RulesManager.warmUp(); //TODO: plugins system: PluginsManager.initialize();
loading.done(); RulesManager.initialize();
} catch (InterruptedException | Error e) { RulesManager.warmUp();
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));
public static StartupArguments parseStartupArguments(final String[] a) { return args;
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":
public static void parseArgument(final StartupArgumentsImpl args, final String arg) { args.setZoomed(true);
switch (arg) { break;
case "-zoomed": case "-verbose":
args.setZoomed(true); args.setVerboseLoggingEnabled(true);
break; break;
case "-verbose": case "-noraspi":
args.setVerboseLoggingEnabled(true); args.setRaspberryModeAllowed(false);
break; break;
case "-noraspi": case "nogui":
args.setRaspberryModeAllowed(false); args.setNoGUIEngineForced(true);
break; break;
case "nogui": case "ms-dos":
args.setNoGUIEngineForced(true); args.setMSDOSModeEnabled(true);
break; break;
case "ms-dos": case "html":
args.setMSDOSModeEnabled(true); args.setHTMLEngineForced(true);
break; break;
case "html": case "gpu":
args.setHTMLEngineForced(true); args.setGPUEngineForced(true);
break; break;
case "gpu": case "cpu":
args.setGPUEngineForced(true); args.setCPUEngineForced(true);
break; break;
case "cpu": case "framebuffer":
args.setCPUEngineForced(true); args.setFrameBufferEngineForced(true);
break; break;
case "framebuffer": case "-debug":
args.setFrameBufferEngineForced(true); args.setDebugEnabled(true);
break; break;
case "-debug": case "-uncached":
args.setDebugEnabled(true); args.setUncached(true);
break; break;
case "-uncached": default:
args.setUncached(true); // Not using ConsoleUtils because it isn't initialized at this point.
break; System.out.println("Unrecognized argument " + arg);
default: break;
// Not using ConsoleUtils because it isn't initialized at this point. }
System.out.println("Unrecognized argument " + arg); }
break; }
}
}
}

View File

@ -21,12 +21,8 @@ public class Device {
return inputManager; return inputManager;
} }
public void setup(final Runnable r) { public void setup() {
displayManager.initialize(); 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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

@ -8,13 +8,14 @@
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </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> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

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