Fix small bug in toggle.
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / CameraFeeds.java
index 855d32a5bcbfe6935772af9a5ea5ce7f849ed6a9..15ea3695efe43ba65a1743d32568fb9898c5ebda 100755 (executable)
@@ -1,24 +1,25 @@
 package org.usfirst.frc.team3501.robot;
 
-import com.ni.vision.NIVision;
-import com.ni.vision.NIVision.Image;
-
 import edu.wpi.cscore.AxisCamera;
 import edu.wpi.cscore.UsbCamera;
+import edu.wpi.cscore.VideoSource;
 import edu.wpi.first.wpilibj.CameraServer;
 
 public class CameraFeeds {
   // private final int intakeCam;
   // private final int climberCam;
-  private Image frame;
-  private int curCam;
+  private VideoSource curCam;
   private CameraServer server;
   private static UsbCamera intakeCam;
   private static AxisCamera climberCam;
+  private static String usbCamName;
+  private static String axisCamName;
+  private static CameraFeeds cameraFeeds = null;
 
   @SuppressWarnings("deprecation")
   public CameraFeeds(/* Joystick Button */) {
-    // Get camera ids by supplying camera name ex 'cam0', found on roborio web
+    // Get camera id by supplying camera name example 'cam0', found on roborio
+    // web
     // interface
     /*
      * intakeCam =
@@ -26,62 +27,61 @@ public class CameraFeeds {
      * NIVision.IMAQdxCameraControlMode.CameraControlModeController); climberCam
      * = NIVision.IMAQdxOpenCamera(Constants.CameraFeeds.camNameRight,
      * NIVision.IMAQdxCameraControlMode.CameraControlModeController); curCam =
-     * intakeCam; // Img that will contain camera img frame =
+     * intakeCam; // Image that will contain camera image frame =
      * NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_RGB, 0); // Server that
-     * we'll give the img to server = CameraServer.getInstance();
+     * we'll give the image to server = CameraServer.getInstance();
      * server.setSize(Constants.CameraFeeds.imgQuality);
      */
-    intakeCam = CameraServer.getInstance().startAutomaticCapture();
-    CameraServer.getInstance().startAutomaticCapture();
+
     server = CameraServer.getInstance();
     climberCam = server.addAxisCamera("axisCamera", "10.35.1.11");
+    intakeCam = server.startAutomaticCapture();
+    curCam = intakeCam;
+    usbCamName = intakeCam.getName();
+    axisCamName = climberCam.getName();
 
     // server = CameraServer.getInstance();
     // axisCamera = cameraServer2.addAxisCamera("axisCamera", "10.35.1.11");
     // cameraFeeds = new CameraFeeds();
   }
 
-  public void init() {
-    changeCam(intakeCam);
+  public static CameraFeeds getCameraFeeds() {
+    if (cameraFeeds == null) {
+      cameraFeeds = new CameraFeeds();
+    }
+    return cameraFeeds;
+
   }
 
-  public void run()
-  {
-    if(/*add test for toggle*/)
-      changeCam(intakeCam);
+  public void init() {
+    changeCam(climberCam);
+  }
 
-    if(/*add test for toggle*/)
+  public void toggleCamera() {
+    if (curCam.equals(intakeCam)) {
       changeCam(climberCam);
+      curCam = climberCam;
+      System.out.println("Switching to climber camera.");
+      return;
+    }
 
-    updateCam();
-  }
-
-  /**
-   * Stop aka close camera stream
-   */
-  public void end() {
-    NIVision.IMAQdxStopAcquisition(curCam);
+    if (curCam.equals(climberCam)) {
+      changeCam(intakeCam);
+      curCam = intakeCam;
+      System.out.println("Switching to intake camera.");
+      return;
+    }
   }
 
   /**
    *
-   * Change the camera to get imgs from to a different one
+   * Change the camera to get image from to a different one
    *
-   * @param newId
-   *          for camera
-   */
-  public void changeCam(/* int newId */) {
-    // NIVision.IMAQdxStopAcquisition(curCam);
-    // NIVision.IMAQdxConfigureGrab(newId);
-    // NIVision.IMAQdxStartAcquisition(newId);
-    // curCam = newId;
-  }
-
-  /**
-   * Get the img from current camera and give it to the server
+   * newId for camera
    */
-  public void updateCam() {
-    NIVision.IMAQdxGrab(curCam, frame, 1);
-    server.setImage(frame);
+  public void changeCam(VideoSource cam) {
+    server.removeCamera(axisCamName);
+    server.removeCamera(usbCamName);
+    server.addCamera(cam);
   }
 }