001/* 002 * Copyright (C) Photon Vision. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program. If not, see <https://www.gnu.org/licenses/>. 016 */ 017 018package org.photonvision.common.hardware; 019 020import java.io.IOException; 021import org.photonvision.common.util.ShellExec; 022 023public enum PiVersion { 024 PI_B("Pi Model B"), 025 COMPUTE_MODULE("Compute Module Rev"), 026 ZERO_W("Pi Zero W Rev 1.1"), 027 ZERO_2_W("Raspberry Pi Zero 2"), 028 PI_3("Pi 3"), 029 PI_4("Pi 4"), 030 PI_5("Pi 5"), 031 COMPUTE_MODULE_3("Compute Module 3"), 032 UNKNOWN("UNKNOWN"); 033 034 private final String identifier; 035 private static final ShellExec shell = new ShellExec(true, false); 036 private static final PiVersion currentPiVersion = calcPiVersion(); 037 038 PiVersion(String s) { 039 this.identifier = s.toLowerCase(); 040 } 041 042 public static PiVersion getPiVersion() { 043 return currentPiVersion; 044 } 045 046 private static PiVersion calcPiVersion() { 047 if (!Platform.isRaspberryPi()) return PiVersion.UNKNOWN; 048 String piString = getPiVersionString(); 049 for (PiVersion p : PiVersion.values()) { 050 if (piString.toLowerCase().contains(p.identifier)) return p; 051 } 052 return UNKNOWN; 053 } 054 055 // Query /proc/device-tree/model. This should return the model of the pi 056 // Versions here: 057 // https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/bcm2710-rpi-cm3.dts 058 private static String getPiVersionString() { 059 if (!Platform.isRaspberryPi()) return ""; 060 try { 061 shell.executeBashCommand("cat /proc/device-tree/model"); 062 } catch (IOException e) { 063 e.printStackTrace(); 064 } 065 if (shell.getExitCode() == 0) { 066 // We expect it to be in the format "raspberry pi X model X" 067 return shell.getOutput(); 068 } 069 070 return ""; 071 } 072}