2009-06-25 07:16:22 -07:00
|
|
|
|
/*
|
|
|
|
|
* VsqNrpn.cs
|
|
|
|
|
* Copyright (c) 2008-2009 kbinani
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Boare.Lib.Vsq.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.Vsq is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the BSD License.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.Vsq is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
*/
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
package org.kbinani.vsq;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
#else
|
|
|
|
|
using System;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
using bocoree;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
using bocoree.java.util;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
|
2009-06-25 07:16:22 -07:00
|
|
|
|
namespace Boare.Lib.Vsq {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
using boolean = System.Boolean;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|
2009-09-07 03:44:18 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
public class VsqNrpn implements Comparable<VsqNrpn> {
|
|
|
|
|
#else
|
2009-07-29 10:03:20 -07:00
|
|
|
|
public struct VsqNrpn : IComparable<VsqNrpn> {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|
2009-06-25 07:16:22 -07:00
|
|
|
|
public int Clock;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public int Nrpn;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
public byte DataMsb;
|
|
|
|
|
public byte DataLsb;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public boolean DataLsbSpecified;
|
|
|
|
|
public boolean msbOmitRequired;
|
|
|
|
|
private Vector<VsqNrpn> m_list;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public VsqNrpn( int clock, int nrpn, byte data_msb ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
Clock = clock;
|
|
|
|
|
Nrpn = nrpn;
|
|
|
|
|
DataMsb = data_msb;
|
2009-07-29 10:03:20 -07:00
|
|
|
|
DataLsb = 0x0;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
DataLsbSpecified = false;
|
2009-07-30 08:02:59 -07:00
|
|
|
|
msbOmitRequired = false;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list = new Vector<VsqNrpn>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public VsqNrpn( int clock, int nrpn, byte data_msb, byte data_lsb ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
Clock = clock;
|
|
|
|
|
Nrpn = nrpn;
|
|
|
|
|
DataMsb = data_msb;
|
|
|
|
|
DataLsb = data_lsb;
|
|
|
|
|
DataLsbSpecified = true;
|
2009-07-30 08:02:59 -07:00
|
|
|
|
msbOmitRequired = false;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list = new Vector<VsqNrpn>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public VsqNrpn[] expand() {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
Vector<VsqNrpn> ret = new Vector<VsqNrpn>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
if ( DataLsbSpecified ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
VsqNrpn v = new VsqNrpn( Clock, Nrpn, DataMsb, DataLsb );
|
|
|
|
|
v.msbOmitRequired = msbOmitRequired;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
ret.add( v );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
} else {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
VsqNrpn v = new VsqNrpn( Clock, Nrpn, DataMsb );
|
|
|
|
|
v.msbOmitRequired = msbOmitRequired;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
ret.add( v );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2009-09-07 03:44:18 -07:00
|
|
|
|
for ( int i = 0; i < m_list.size(); i++ ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.addAll( Arrays.asList( m_list.get( i ).expand() ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return ret.toArray( new VsqNrpn[] { } );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public static Vector<VsqNrpn> sort( Vector<VsqNrpn> list ) {
|
|
|
|
|
Vector<VsqNrpn> ret = new Vector<VsqNrpn>();
|
|
|
|
|
Collections.sort( list );
|
|
|
|
|
if ( list.size() >= 2 ) {
|
|
|
|
|
Vector<VsqNrpn> work = new Vector<VsqNrpn>(); //workには、clockが同じNRPNだけが入る
|
|
|
|
|
int last_clock = list.get( 0 ).Clock;
|
|
|
|
|
work.add( list.get( 0 ) );
|
|
|
|
|
for ( int i = 1; i < list.size(); i++ ) {
|
|
|
|
|
if ( list.get( i ).Clock == last_clock ) {
|
|
|
|
|
work.add( list.get( i ) );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
} else {
|
|
|
|
|
// まずworkを並べ替え
|
2009-09-07 03:44:18 -07:00
|
|
|
|
last_clock = list.get( i ).Clock;
|
|
|
|
|
boolean changed = true;
|
2009-07-29 10:03:20 -07:00
|
|
|
|
while ( changed ) {
|
|
|
|
|
changed = false;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
for ( int j = 0; j < work.size() - 1; j++ ) {
|
|
|
|
|
byte nrpn_msb0 = (byte)((work.get( j ).Nrpn >> 8) & 0xff);
|
|
|
|
|
byte nrpn_msb1 = (byte)((work.get( j + 1 ).Nrpn >> 8) & 0xff);
|
2009-07-29 10:03:20 -07:00
|
|
|
|
if ( nrpn_msb1 > nrpn_msb0 ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
VsqNrpn buf = work.get( j );
|
|
|
|
|
work.set( j, work.get( j + 1 ) );
|
|
|
|
|
work.set( j + 1, buf );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-07 03:44:18 -07:00
|
|
|
|
for ( int j = 0; j < work.size(); j++ ) {
|
|
|
|
|
ret.add( work.get( j ) );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
2009-09-07 03:44:18 -07:00
|
|
|
|
work.clear();
|
|
|
|
|
work.add( list.get( i ) );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-07 03:44:18 -07:00
|
|
|
|
for ( int j = 0; j < work.size(); j++ ) {
|
|
|
|
|
ret.add( work.get( j ) );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
for ( int i = 0; i < list.size(); i++ ) {
|
|
|
|
|
ret.add( list.get( i ) );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-25 07:16:22 -07:00
|
|
|
|
public static VsqNrpn[] merge( VsqNrpn[] src1, VsqNrpn[] src2 ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
Vector<VsqNrpn> ret = new Vector<VsqNrpn>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
for ( int i = 0; i < src1.Length; i++ ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
ret.add( src1[i] );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
for ( int i = 0; i < src2.Length; i++ ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
ret.add( src2[i] );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2009-09-07 03:44:18 -07:00
|
|
|
|
Collections.sort( ret );
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return ret.toArray( new VsqNrpn[] { } );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static NrpnData[] convert( VsqNrpn[] source ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
int nrpn = source[0].Nrpn;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
byte msb = (byte)(nrpn >> 8);
|
|
|
|
|
byte lsb = (byte)(nrpn - (nrpn << 8));
|
2009-09-07 03:44:18 -07:00
|
|
|
|
Vector<NrpnData> ret = new Vector<NrpnData>();
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( source[0].Clock, (byte)0x63, msb ) );
|
|
|
|
|
ret.add( new NrpnData( source[0].Clock, (byte)0x62, lsb ) );
|
|
|
|
|
ret.add( new NrpnData( source[0].Clock, (byte)0x06, source[0].DataMsb ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
if ( source[0].DataLsbSpecified ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( source[0].Clock, (byte)0x26, source[0].DataLsb ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
for ( int i = 1; i < source.Length; i++ ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
VsqNrpn item = source[i];
|
2010-03-16 20:14:08 -07:00
|
|
|
|
int tnrpn = item.Nrpn;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
msb = (byte)(tnrpn >> 8);
|
|
|
|
|
lsb = (byte)(tnrpn - (tnrpn << 8));
|
2009-07-30 08:02:59 -07:00
|
|
|
|
if ( item.msbOmitRequired ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x62, lsb ) );
|
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x06, item.DataMsb ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
if ( item.DataLsbSpecified ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x26, item.DataLsb ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x63, msb ) );
|
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x62, lsb ) );
|
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x06, item.DataMsb ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
if ( item.DataLsbSpecified ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ret.add( new NrpnData( item.Clock, (byte)0x26, item.DataLsb ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return ret.toArray( new NrpnData[] { } );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public int compareTo( VsqNrpn item ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
return Clock - item.Clock;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if !JAVA
|
|
|
|
|
public int CompareTo( VsqNrpn item ) {
|
|
|
|
|
return compareTo( item );
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
public void append( int nrpn, byte data_msb ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list.add( new VsqNrpn( Clock, nrpn, data_msb ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public void append( int nrpn, byte data_msb, byte data_lsb ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list.add( new VsqNrpn( Clock, nrpn, data_msb, data_lsb ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2009-07-30 08:02:59 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public void append( int nrpn, byte data_msb, boolean msb_omit_required ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
VsqNrpn v = new VsqNrpn( Clock, nrpn, data_msb );
|
|
|
|
|
v.msbOmitRequired = msb_omit_required;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list.add( v );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public void append( int nrpn, byte data_msb, byte data_lsb, boolean msb_omit_required ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
VsqNrpn v = new VsqNrpn( Clock, nrpn, data_msb, data_lsb );
|
|
|
|
|
v.msbOmitRequired = msb_omit_required;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_list.add( v );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if !JAVA
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|