2018-09-02 13:36:49 +02:00
|
|
|
package it.cavallium.warppi;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2018-09-12 22:16:33 +02:00
|
|
|
import it.cavallium.warppi.Platform.ConsoleUtils;
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.boot.StartupArguments;
|
|
|
|
import it.cavallium.warppi.device.HardwareDevice;
|
|
|
|
import it.cavallium.warppi.device.HardwareTouchDevice;
|
|
|
|
import it.cavallium.warppi.device.InputManager;
|
|
|
|
import it.cavallium.warppi.device.Keyboard;
|
|
|
|
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;
|
2018-09-12 22:16:33 +02:00
|
|
|
import it.cavallium.warppi.util.ClassUtils;
|
2018-09-02 13:36:49 +02:00
|
|
|
|
|
|
|
public class Engine {
|
|
|
|
public static final Engine INSTANCE = new Engine();
|
2018-09-04 12:12:41 +02:00
|
|
|
private static Platform platform;
|
2018-09-02 13:36:49 +02:00
|
|
|
private static boolean running = false;
|
|
|
|
private static BehaviorSubject<LoadingStatus> loadPhase = BehaviorSubject.create();
|
2018-09-22 11:17:30 +02:00
|
|
|
private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false);
|
2018-09-11 19:19:24 +02:00
|
|
|
private HardwareDevice hardwareDevice;
|
2018-09-02 13:36:49 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private Engine() {}
|
2018-09-02 13:36:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start an instance of the calculator.
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2018-09-04 12:12:41 +02:00
|
|
|
* @param platform
|
|
|
|
* Platform implementation
|
2018-09-02 13:36:49 +02:00
|
|
|
* @param screen
|
|
|
|
* Default screen to show at startup
|
|
|
|
* @param disp
|
|
|
|
* Hardware display
|
|
|
|
* @param hud
|
|
|
|
* Head-up display
|
|
|
|
* @param args
|
|
|
|
* Startup arguments
|
|
|
|
* @throws InterruptedException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public static void start(final Platform platform, final Screen screen, final HardwareDisplay disp,
|
|
|
|
final HardwareTouchDevice touchdevice, final HUD hud, final StartupArguments args)
|
|
|
|
throws InterruptedException, IOException {
|
2018-09-28 11:39:28 +02:00
|
|
|
if (Engine.running) {
|
2018-09-02 14:21:08 +02:00
|
|
|
throw new RuntimeException("Already running!");
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2018-09-22 11:17:30 +02:00
|
|
|
Engine.running = true;
|
|
|
|
Engine.INSTANCE.startInstance(platform, screen, disp, touchdevice, hud, args);
|
2018-09-02 13:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private void startInstance(final Platform platform, final Screen screen, final HardwareDisplay disp,
|
|
|
|
final HardwareTouchDevice touchdevice, final HUD hud, final StartupArguments args)
|
|
|
|
throws InterruptedException, IOException {
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.platform = platform;
|
|
|
|
platform.getConsoleUtils().out().println("WarpPI Calculator");
|
2018-09-02 13:36:49 +02:00
|
|
|
initializeEnvironment(args);
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
final Thread currentThread = Thread.currentThread();
|
2018-09-02 13:36:49 +02:00
|
|
|
currentThread.setPriority(Thread.MAX_PRIORITY);
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.getPlatform().setThreadName(currentThread, "Main thread");
|
2018-09-02 13:36:49 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
final DisplayManager dm = new DisplayManager(disp, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
|
|
|
|
final Keyboard k = new Keyboard();
|
|
|
|
final InputManager im = new InputManager(k, touchdevice);
|
2018-09-11 19:19:24 +02:00
|
|
|
hardwareDevice = new HardwareDevice(dm, im);
|
2018-09-22 11:17:30 +02:00
|
|
|
|
|
|
|
hardwareDevice.setup(() -> Engine.loadPhase.onNext(new LoadingStatus()));
|
2018-09-02 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onShutdown() {
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.platform.getConsoleUtils().out().println(1, "Shutdown...");
|
2018-09-02 13:36:49 +02:00
|
|
|
beforeShutdown();
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.platform.getConsoleUtils().out().println(1, "");
|
|
|
|
Engine.platform.getConsoleUtils().out().println(1, "Closed.");
|
|
|
|
Engine.getPlatform().exit(0);
|
2018-09-02 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private void initializeEnvironment(final StartupArguments args) throws IOException {
|
2018-09-02 13:36:49 +02:00
|
|
|
ClassUtils.classLoader = this.getClass();
|
|
|
|
StaticVars.startupArguments = args;
|
|
|
|
StaticVars.debugWindow2x = args.isZoomed();
|
2018-09-28 11:39:28 +02:00
|
|
|
if (args.isVerboseLoggingEnabled() || args.isDebugEnabled()) {
|
2018-09-02 13:36:49 +02:00
|
|
|
StaticVars.outputLevel = ConsoleUtils.OUTPUTLEVEL_DEBUG_VERBOSE;
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.platform.getConsoleUtils().out().println(ConsoleUtils.OUTPUTLEVEL_DEBUG_MIN, args);
|
2018-09-02 13:36:49 +02:00
|
|
|
checkDeviceType();
|
2018-10-06 16:37:44 +02:00
|
|
|
if (args.isRaspberryModeAllowed() == false) {
|
|
|
|
Engine.getPlatform().setRunningOnRaspberry(false);
|
|
|
|
}
|
|
|
|
if (Engine.getPlatform().isRunningOnRaspberry()) {
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.getPlatform().getGpio().wiringPiSetupPhys();
|
|
|
|
Engine.getPlatform().getGpio().pinMode(12, Engine.getPlatform().getGpio().valuePwmOutput());
|
2018-09-02 13:36:49 +02:00
|
|
|
} else {
|
|
|
|
StaticVars.screenPos = new int[] { 0, 0 };
|
2018-10-06 16:37:44 +02:00
|
|
|
if (Engine.getPlatform().isJavascript() == false) {
|
|
|
|
Engine.getPlatform().getSettings().setDebugEnabled(true);
|
|
|
|
}
|
2018-09-02 13:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDeviceType() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void beforeShutdown() {
|
|
|
|
Keyboard.stopKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Observable<Boolean> isLoaded() {
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Observable<LoadingStatus> getLoadPhase() {
|
2018-09-22 11:17:30 +02:00
|
|
|
return Engine.loadPhase;
|
2018-09-02 13:36:49 +02:00
|
|
|
}
|
2018-09-22 11:17:30 +02:00
|
|
|
|
2018-09-11 19:19:24 +02:00
|
|
|
public HardwareDevice getHardwareDevice() {
|
|
|
|
return hardwareDevice;
|
|
|
|
}
|
2018-09-22 11:17:30 +02:00
|
|
|
|
2018-09-04 12:12:41 +02:00
|
|
|
public static Platform getPlatform() {
|
2018-09-22 11:17:30 +02:00
|
|
|
return Engine.platform;
|
2018-09-04 12:12:41 +02:00
|
|
|
}
|
2018-09-02 13:36:49 +02:00
|
|
|
|
|
|
|
public static class LoadingStatus {
|
|
|
|
protected LoadingStatus() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void done() {
|
|
|
|
Engine.INSTANCE.loaded.onNext(true);
|
2018-09-11 19:19:24 +02:00
|
|
|
Engine.INSTANCE.hardwareDevice.getDisplayManager().waitForExit();
|
2018-09-02 13:36:49 +02:00
|
|
|
Engine.INSTANCE.onShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void failed() {
|
|
|
|
Engine.INSTANCE.onShutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|