Implement listener interfaces.

This commit is contained in:
Peter Johnson
2016-11-05 13:13:09 -07:00
parent 8d2efb2838
commit 29d8d1d74c
13 changed files with 616 additions and 13 deletions

View File

@@ -121,9 +121,6 @@ public class CameraServerJNI {
public static native void setSourceConnected(int source, boolean connected);
public static native void setSourceDescription(int source, String description);
public static native int createSourceProperty(int source, String name, int type, int minimum, int maximum, int step, int defaultValue, int value);
//public static native int createSourcePropertyCallback(int source, String name,
// int type, int minimum, int maximum, int step, int defaultValue, int value,
// void (*onChange)(int property));
public static native void setSourceEnumPropertyChoices(int source, int property, String[] choices);
//
@@ -156,8 +153,8 @@ public class CameraServerJNI {
//
// Listener Functions
//
//public static native int addListener(void (*callback)(VideoEvent event),
// int eventMask);
public static native int addListener(VideoListenerFunction listener,
int eventMask, boolean immediateNotify);
public static native void removeListener(int handle);

View File

@@ -0,0 +1,83 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.cameraserver;
/// Video event
public class VideoEvent {
public enum Type {
kSourceCreated(0x0001),
kSourceDestroyed(0x0002),
kSourceConnected(0x0004),
kSourceDisconnected(0x0008),
kSourceVideoModesUpdated(0x0010),
kSourceVideoModeChanged(0x0020),
kSinkCreated(0x0100),
kSinkDestroyed(0x0200),
kSinkEnabled(0x0400),
kSinkDisabled(0x0800),
kSourcePropertyCreated(0x1000),
kSourcePropertyValueUpdated(0x2000),
kSourcePropertyChoicesUpdated(0x4000);
private int value;
private Type(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
private static final Type[] m_typeValues = Type.values();
VideoEvent(int type, int source, int sink, String name, int pixelFormat,
int width, int height, int fps, int property, int propertyType,
int value, String valueStr) {
this.type = m_typeValues[type];
this.sourceHandle = source;
this.sinkHandle = sink;
this.name = name;
this.mode = new VideoMode(pixelFormat, width, height, fps);
this.propertyHandle = property;
this.propertyType = VideoProperty.m_typeValues[propertyType];
this.value = value;
this.valueStr = valueStr;
}
public Type type;
// Valid for kSource* and kSink* respectively
private int sourceHandle;
private int sinkHandle;
// Source/sink name
public String name;
// Fields for kSourceVideoModeChanged event
public VideoMode mode;
// Fields for kSourceProperty* events
private int propertyHandle;
public VideoProperty.Type propertyType;
public int value;
public String valueStr;
public VideoSource getSource() {
return new VideoSource(CameraServerJNI.copySource(sourceHandle));
}
public VideoSink getSink() {
return new VideoSink(CameraServerJNI.copySink(sinkHandle));
}
public VideoProperty getProperty() {
return new VideoProperty(propertyHandle, propertyType);
}
}

View File

@@ -0,0 +1,35 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.cameraserver;
/// An event listener. This calls back to a desigated callback function when
/// an event matching the specified mask is generated by the library.
public class VideoListener {
/// Create an event listener.
/// @param listener Listener function
/// @param eventMask Bitmask of VideoEvent.Type values
/// @param immediateNotify Whether callback should be immediately called with
/// a representative set of events for the current library state.
public VideoListener(VideoListenerFunction listener, int eventMask,
boolean immediateNotify) {
m_handle = CameraServerJNI.addListener(listener, eventMask, immediateNotify);
}
public synchronized void free() {
if (m_handle != 0) {
CameraServerJNI.removeListener(m_handle);
}
m_handle = 0;
}
public boolean isValid() {
return m_handle != 0;
}
private int m_handle;
}

View File

@@ -0,0 +1,12 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.cameraserver;
public interface VideoListenerFunction {
void apply(VideoEvent event);
}

View File

@@ -20,7 +20,7 @@ public class VideoProperty {
return value;
}
}
private static final Type[] m_typeValues = Type.values();
static final Type[] m_typeValues = Type.values();
public String getName() {
return CameraServerJNI.getPropertyName(m_handle);
@@ -94,6 +94,11 @@ public class VideoProperty {
m_type = m_typeValues[CameraServerJNI.getPropertyType(handle)];
}
VideoProperty(int handle, Type type) {
m_handle = handle;
m_type = type;
}
int m_handle;
private Type m_type;
}