Fix mapping into Java enums.

The values are bitmask not contiguous, so we need to use a switch instead
of an array.
This commit is contained in:
Peter Johnson
2016-11-18 10:44:31 -08:00
parent 5ace9e4189
commit e1f4e3d2d7
5 changed files with 58 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ package edu.wpi.cscore;
/// Video event
public class VideoEvent {
public enum Kind {
kUnknown(0x0000),
kSourceCreated(0x0001),
kSourceDestroyed(0x0002),
kSourceConnected(0x0004),
@@ -35,18 +36,37 @@ public class VideoEvent {
return value;
}
}
private static final Kind[] m_kindValues = Kind.values();
public static Kind getKindFromInt(int kind) {
switch (kind) {
case 0x0001: return Kind.kSourceCreated;
case 0x0002: return Kind.kSourceDestroyed;
case 0x0004: return Kind.kSourceConnected;
case 0x0008: return Kind.kSourceDisconnected;
case 0x0010: return Kind.kSourceVideoModesUpdated;
case 0x0020: return Kind.kSourceVideoModeChanged;
case 0x0040: return Kind.kSourcePropertyCreated;
case 0x0080: return Kind.kSourcePropertyValueUpdated;
case 0x0100: return Kind.kSourcePropertyChoicesUpdated;
case 0x0200: return Kind.kSinkSourceChanged;
case 0x0400: return Kind.kSinkCreated;
case 0x0800: return Kind.kSinkDestroyed;
case 0x1000: return Kind.kSinkEnabled;
case 0x2000: return Kind.kSinkDisabled;
default: return Kind.kUnknown;
}
}
VideoEvent(int kind, int source, int sink, String name, int pixelFormat,
int width, int height, int fps, int property, int propertyKind,
int value, String valueStr) {
this.kind = m_kindValues[kind];
this.kind = getKindFromInt(kind);
this.sourceHandle = source;
this.sinkHandle = sink;
this.name = name;
this.mode = new VideoMode(pixelFormat, width, height, fps);
this.propertyHandle = property;
this.propertyKind = VideoProperty.m_kindValues[propertyKind];
this.propertyKind = VideoProperty.getKindFromInt(propertyKind);
this.value = value;
this.valueStr = valueStr;
}

View File

@@ -23,8 +23,12 @@ public class VideoMode {
}
private static final PixelFormat[] m_pixelFormatValues = PixelFormat.values();
public static PixelFormat getPixelFormatFromInt(int pixelFormat) {
return m_pixelFormatValues[pixelFormat];
}
public VideoMode(int pixelFormat, int width, int height, int fps) {
this.pixelFormat = m_pixelFormatValues[pixelFormat];
this.pixelFormat = getPixelFormatFromInt(pixelFormat);
this.width = width;
this.height = height;
this.fps = fps;

View File

@@ -20,7 +20,16 @@ public class VideoProperty {
return value;
}
}
static final Kind[] m_kindValues = Kind.values();
public static Kind getKindFromInt(int kind) {
switch (kind) {
case 1: return Kind.kBoolean;
case 2: return Kind.kInteger;
case 4: return Kind.kString;
case 8: return Kind.kEnum;
default: return Kind.kNone;
}
}
public String getName() {
return CameraServerJNI.getPropertyName(m_handle);
@@ -91,7 +100,7 @@ public class VideoProperty {
VideoProperty(int handle) {
m_handle = handle;
m_kind = m_kindValues[CameraServerJNI.getPropertyKind(handle)];
m_kind = getKindFromInt(CameraServerJNI.getPropertyKind(handle));
}
VideoProperty(int handle, Kind kind) {

View File

@@ -23,7 +23,14 @@ public class VideoSink {
return value;
}
}
static final Kind[] m_kindValues = Kind.values();
public static Kind getKindFromInt(int kind) {
switch (kind) {
case 2: return Kind.kMJPEG;
case 4: return Kind.kCv;
default: return Kind.kUnknown;
}
}
protected VideoSink(int handle) {
m_handle = handle;
@@ -58,7 +65,7 @@ public class VideoSink {
/// Get the kind of the sink.
public Kind getKind() {
return m_kindValues[CameraServerJNI.getSinkKind(m_handle)];
return getKindFromInt(CameraServerJNI.getSinkKind(m_handle));
}
/// Get the name of the sink. The name is an arbitrary identifier

View File

@@ -23,7 +23,15 @@ public class VideoSource {
return value;
}
}
static final Kind[] m_kindValues = Kind.values();
public static Kind getKindFromInt(int kind) {
switch (kind) {
case 1: return Kind.kUSB;
case 2: return Kind.kHTTP;
case 4: return Kind.kCv;
default: return Kind.kUnknown;
}
}
protected VideoSource(int handle) {
m_handle = handle;
@@ -58,7 +66,7 @@ public class VideoSource {
/// Get the kind of the source.
public Kind getKind() {
return m_kindValues[CameraServerJNI.getSourceKind(m_handle)];
return getKindFromInt(CameraServerJNI.getSourceKind(m_handle));
}
/// Get the name of the source. The name is an arbitrary identifier