Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,37 @@ public class HardwareManager {
private final ShellExec shellExec = new ShellExec(true, false);
private final Logger logger = new Logger(HardwareManager.class, LogGroup.General);

private final HardwareConfig hardwareConfig;
private final HardwareSettings hardwareSettings;
private HardwareConfig hardwareConfig;
private HardwareSettings hardwareSettings;

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final StatusLED statusLED;
private StatusLED statusLED;

@SuppressWarnings("FieldCanBeLocal")
private final IntegerSubscriber ledModeRequest;

private final IntegerPublisher ledModeState;

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final NTDataChangeListener ledModeListener;
private NTDataChangeListener ledModeListener;

public final VisionLED visionLED; // May be null if no LED is specified
private VisionLED visionLED; // May be null if no LED is specified

public VisionLED getVisionLED() {
return visionLED;
}

public static HardwareManager getInstance() {
if (instance == null) {
var conf = ConfigManager.getInstance().getConfig();
instance = new HardwareManager(conf.getHardwareConfig(), conf.getHardwareSettings());
instance = new HardwareManager();
}
return instance;
}

private HardwareManager(HardwareConfig hardwareConfig, HardwareSettings hardwareSettings) {
public void setConfig(HardwareConfig hardwareConfig, HardwareSettings hardwareSettings) {
this.hardwareConfig = hardwareConfig;
this.hardwareSettings = hardwareSettings;

ledModeRequest =
NetworkTablesManager.getInstance()
.kRootTable
.getIntegerTopic("ledModeRequest")
.subscribe(-1);
ledModeState =
NetworkTablesManager.getInstance().kRootTable.getIntegerTopic("ledModeState").publish();
ledModeState.set(VisionLEDMode.kDefault.value);

// Device factory is lazy to prevent creating one if it will go unused.
Supplier<NativeDeviceFactoryInterface> lazyDeviceFactory =
new Supplier<NativeDeviceFactoryInterface>() {
Expand Down Expand Up @@ -130,12 +124,23 @@ public NativeDeviceFactoryInterface get() {
ledModeRequest,
visionLED::onLedModeChange);

Runtime.getRuntime().addShutdownHook(new Thread(this::onJvmExit));

if (visionLED != null) {
visionLED.setBrightness(hardwareSettings.ledBrightnessPercentage);
visionLED.blink(85, 4); // bootup blink
}
}

private HardwareManager() {
ledModeRequest =
NetworkTablesManager.getInstance()
.kRootTable
.getIntegerTopic("ledModeRequest")
.subscribe(-1);
ledModeState =
NetworkTablesManager.getInstance().kRootTable.getIntegerTopic("ledModeState").publish();
ledModeState.set(VisionLEDMode.kDefault.value);

Runtime.getRuntime().addShutdownHook(new Thread(this::onJvmExit));

// Start hardware metrics thread (Disabled until implemented)
// if (Platform.isLinux()) MetricsPublisher.getInstance().startTask();
Expand Down Expand Up @@ -169,6 +174,10 @@ public static NativeDeviceFactoryInterface configureCustomGPIO(HardwareConfig ha
}

public void setBrightnessPercent(int percent) {
if (hardwareSettings == null) {
logger.error("Could not set led brightness! No hardware settings found");
return;
}
if (percent != hardwareSettings.ledBrightnessPercentage) {
hardwareSettings.ledBrightnessPercentage = percent;
if (visionLED != null) visionLED.setBrightness(percent);
Expand All @@ -194,6 +203,10 @@ public boolean restartDevice() {
}
}
try {
if (hardwareConfig == null) {
logger.error("Could not restart device! No hardware configuration found");
return false;
}
return shellExec.executeBashCommand(hardwareConfig.restartHardwareCommand) == 0;
} catch (IOException e) {
logger.error("Could not restart device!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public VisionModule(PipelineManager pipelineManager, VisionSource visionSource)
}

// Configure LED's if supported by the underlying hardware
if (HardwareManager.getInstance().visionLED != null && this.camShouldControlLEDs()) {
if (HardwareManager.getInstance().getVisionLED() != null && this.camShouldControlLEDs()) {
HardwareManager.getInstance()
.visionLED
.getVisionLED()
.setPipelineModeSupplier(() -> pipelineManager.getCurrentPipelineSettings().ledMode);
setVisionLEDs(pipelineManager.getCurrentPipelineSettings().ledMode);
}
Expand Down Expand Up @@ -513,8 +513,8 @@ private boolean camShouldControlLEDs() {
}

private void setVisionLEDs(boolean on) {
if (camShouldControlLEDs() && HardwareManager.getInstance().visionLED != null)
HardwareManager.getInstance().visionLED.setState(on);
if (camShouldControlLEDs() && HardwareManager.getInstance().getVisionLED() != null)
HardwareManager.getInstance().getVisionLED().setState(on);
}

public void saveModule() {
Expand Down
3 changes: 2 additions & 1 deletion photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ public static void main(String[] args) {

logger.debug("Loading HardwareManager...");
// Force load the hardware manager
HardwareManager.getInstance();
var conf = ConfigManager.getInstance().getConfig();
HardwareManager.getInstance().setConfig(conf.getHardwareConfig(), conf.getHardwareSettings());

if (isSmoketest) {
logger.info("PhotonVision base functionality loaded -- smoketest complete");
Expand Down
Loading