001/* 002 * Copyright (C) Cross The Road Electronics. All rights reserved. 003 * License information can be found in CTRE_LICENSE.txt 004 * For support and suggestions contact support@ctr-electronics.com or file 005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases 006 */ 007package com.ctre.phoenix6.wpiutils; 008 009import java.util.concurrent.locks.Lock; 010import java.util.concurrent.locks.ReentrantLock; 011 012import com.ctre.phoenix6.unmanaged.Unmanaged; 013 014import edu.wpi.first.wpilibj.DriverStation; 015import edu.wpi.first.wpilibj.Notifier; 016 017public class AutoFeedEnable implements AutoCloseable { 018 private static final AutoFeedEnable autoFeedEnable = new AutoFeedEnable(); 019 020 private final Notifier _enableNotifier; 021 private final Lock _lck = new ReentrantLock(); 022 private int _startCount = 0; 023 024 public static AutoFeedEnable getInstance() { 025 return autoFeedEnable; 026 } 027 028 private AutoFeedEnable() { 029 _enableNotifier = new Notifier(() -> { 030 if (DriverStation.isEnabled()) { 031 Unmanaged.feedEnable(100); 032 } 033 }); 034 } 035 036 /** 037 * Starts feeding the enable signal to CTRE actuators. 038 */ 039 public void start() { 040 _lck.lock(); 041 try { 042 if (_startCount < Integer.MAX_VALUE) { 043 if (_startCount++ == 0) { 044 /* start if we were previously at 0 */ 045 _enableNotifier.startPeriodic(0.02); 046 } 047 } 048 } finally { 049 _lck.unlock(); 050 } 051 } 052 053 /** 054 * Stops feeding the enable signal to CTRE actuators. The 055 * enable signal will only be stopped when all actuators 056 * have requested to stop the enable signal. 057 */ 058 public void stop() { 059 _lck.lock(); 060 try { 061 if (_startCount > 0) { 062 if (--_startCount == 0) { 063 /* stop if we are now at 0 */ 064 _enableNotifier.stop(); 065 } 066 } 067 } finally { 068 _lck.unlock(); 069 } 070 } 071 072 @Override 073 public void close() { 074 _enableNotifier.close(); 075 } 076}