diff --git a/photon-core/src/main/java/org/photonvision/common/hardware/PiVersion.java b/photon-core/src/main/java/org/photonvision/common/hardware/PiVersion.java new file mode 100644 index 000000000..3e4b457a6 --- /dev/null +++ b/photon-core/src/main/java/org/photonvision/common/hardware/PiVersion.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.photonvision.common.hardware; + +public enum PiVersion { + PI_B("Pi Model B"), + COMPUTE_MODULE("Compute Module Rev"), + ZERO_W("Pi Zero W Rev 1.1"), + PI_3("Pi 3"), + PI_4("Pi 4"), + COMPUTE_MODULE_3("Compute Module 3"), + UNKNOWN("UNKNOWN"); + + private final String identifier; + + PiVersion(String s) { + this.identifier = s.toLowerCase(); + } + + public static PiVersion getPiVersion() { + if (!Platform.isRaspberryPi()) return PiVersion.UNKNOWN; + String piString = Platform.currentPiVersionStr; + for (PiVersion p : PiVersion.values()) { + if (piString.toLowerCase().contains(p.identifier)) return p; + } + return UNKNOWN; + } +} diff --git a/photon-core/src/main/java/org/photonvision/common/hardware/Platform.java b/photon-core/src/main/java/org/photonvision/common/hardware/Platform.java index 55436115e..5f4416787 100644 --- a/photon-core/src/main/java/org/photonvision/common/hardware/Platform.java +++ b/photon-core/src/main/java/org/photonvision/common/hardware/Platform.java @@ -45,7 +45,11 @@ public enum Platform { private static final String OS_NAME = System.getProperty("os.name"); private static final String OS_ARCH = System.getProperty("os.arch"); - public static final Platform CurrentPlatform = getCurrentPlatform(); + + // These are querried on init and should never change after + public static final Platform currentPlatform = getCurrentPlatform(); + protected static final String currentPiVersionStr = getPiVersionString(); + public static final PiVersion currentPiVersion = PiVersion.getPiVersion(); private static String UnknownPlatformString = String.format("Unknown Platform. OS: %s, Architecture: %s", OS_NAME, OS_ARCH); @@ -61,7 +65,7 @@ public enum Platform { } public static boolean isRaspberryPi() { - return CurrentPlatform.equals(LINUX_RASPBIAN); + return currentPlatform.equals(LINUX_RASPBIAN); } @SuppressWarnings("StatementWithEmptyBody") @@ -114,4 +118,22 @@ public enum Platform { return this.value; } } + + // Querry /proc/device-tree/model. This should return the model of the pi + // Versions here: + // https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/bcm2710-rpi-cm3.dts + private static String getPiVersionString() { + if (!isRaspberryPi()) return ""; + try { + shell.executeBashCommand("cat /proc/device-tree/model"); + } catch (IOException e) { + e.printStackTrace(); + } + if (shell.getExitCode() == 0) { + // We expect it to be in the format "raspberry pi X model X" + return shell.getOutput(); + } + + return ""; + } } diff --git a/photon-core/src/main/java/org/photonvision/common/scripting/ScriptManager.java b/photon-core/src/main/java/org/photonvision/common/scripting/ScriptManager.java index ff8172dae..916e9b02e 100644 --- a/photon-core/src/main/java/org/photonvision/common/scripting/ScriptManager.java +++ b/photon-core/src/main/java/org/photonvision/common/scripting/ScriptManager.java @@ -128,7 +128,7 @@ public class ScriptManager { } public static void queueEvent(ScriptEventType eventType) { - if (!Platform.CurrentPlatform.isWindows()) { + if (!Platform.currentPlatform.isWindows()) { try { queuedEvents.putLast(eventType); logger.info("Queued event: " + eventType.name()); diff --git a/photon-core/src/main/java/org/photonvision/common/util/file/FileUtils.java b/photon-core/src/main/java/org/photonvision/common/util/file/FileUtils.java index 40ce70c83..538a7e7e1 100644 --- a/photon-core/src/main/java/org/photonvision/common/util/file/FileUtils.java +++ b/photon-core/src/main/java/org/photonvision/common/util/file/FileUtils.java @@ -76,7 +76,7 @@ public class FileUtils { } public static void setFilePerms(Path path) throws IOException { - if (!Platform.CurrentPlatform.isWindows()) { + if (!Platform.currentPlatform.isWindows()) { File thisFile = path.toFile(); Set perms = Files.readAttributes(path, PosixFileAttributes.class).permissions(); @@ -94,7 +94,7 @@ public class FileUtils { } public static void setAllPerms(Path path) { - if (!Platform.CurrentPlatform.isWindows()) { + if (!Platform.currentPlatform.isWindows()) { String command = String.format("chmod 777 -R %s", path.toString()); try { Process p = Runtime.getRuntime().exec(command); diff --git a/photon-core/src/main/java/org/photonvision/raspi/PicamJNI.java b/photon-core/src/main/java/org/photonvision/raspi/PicamJNI.java index c58e70189..aa6c3180f 100644 --- a/photon-core/src/main/java/org/photonvision/raspi/PicamJNI.java +++ b/photon-core/src/main/java/org/photonvision/raspi/PicamJNI.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import org.photonvision.common.hardware.PiVersion; import org.photonvision.common.hardware.Platform; import org.photonvision.common.logging.LogGroup; import org.photonvision.common.logging.Logger; @@ -83,7 +84,12 @@ public class PicamJNI { } public static boolean isSupported() { - return libraryLoaded && !isVCSMSupported() && getSensorModel() != SensorModel.Disconnected; + return libraryLoaded + && !isVCSMSupported() + && getSensorModel() != SensorModel.Disconnected + && Platform.isRaspberryPi() + && (Platform.currentPiVersion == PiVersion.PI_3 + || Platform.currentPiVersion == PiVersion.COMPUTE_MODULE_3); } public static SensorModel getSensorModel() { diff --git a/photon-core/src/test/java/org/photonvision/hardware/HardwareTest.java b/photon-core/src/test/java/org/photonvision/hardware/HardwareTest.java index fd6610d52..eb4f5bd34 100644 --- a/photon-core/src/test/java/org/photonvision/hardware/HardwareTest.java +++ b/photon-core/src/test/java/org/photonvision/hardware/HardwareTest.java @@ -36,7 +36,7 @@ public class HardwareTest { if (!Platform.isRaspberryPi()) return; - System.out.println("Testing on platform: " + Platform.CurrentPlatform); + System.out.println("Testing on platform: " + Platform.currentPlatform); System.out.println("Printing CPU Info:"); System.out.println("Memory: " + cpuMetrics.getMemory() + "MB"); diff --git a/photon-server/src/main/java/org/photonvision/Main.java b/photon-server/src/main/java/org/photonvision/Main.java index 99000b068..ea2ba7478 100644 --- a/photon-server/src/main/java/org/photonvision/Main.java +++ b/photon-server/src/main/java/org/photonvision/Main.java @@ -169,7 +169,8 @@ public class Main { "Starting PhotonVision version " + PhotonVersion.versionString + " on " - + Platform.CurrentPlatform.toString()); + + Platform.currentPlatform.toString() + + (Platform.isRaspberryPi() ? (" (Pi " + Platform.currentPiVersion.name() + ")") : "")); try { CameraServerCvJNI.forceLoad();