Skip to content

Instantly share code, notes, and snippets.

@lanrat
Created January 30, 2014 06:07
Show Gist options
  • Save lanrat/8703390 to your computer and use it in GitHub Desktop.
Save lanrat/8703390 to your computer and use it in GitHub Desktop.
Lightpack Android API
package com.prismadroid.colors;
import java.util.Random;
public class ColorUtils
{
private static Random random = new Random(System.currentTimeMillis());
public static Rgb24 randomColor()
{
return new Rgb24(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
public static Rgb24 randomColorHue()
{
return randomColorHue(1.0F, 1.0F);
}
public static Rgb24 randomColorHue(float paramFloat1, float paramFloat2)
{
return new Hsv(360.0F * random.nextFloat(), paramFloat1, paramFloat2).toRgb24();
}
}
package com.prismadroid.devices;
import com.prismadroid.colors.Rgb24;
import java.util.List;
public abstract interface Device
{
public abstract void close();
public abstract String getName();
public abstract String getSerial();
public abstract boolean isBusy();
public abstract void setColors(List<Rgb24> paramList);
public abstract void setGamma(double paramDouble);
public abstract void setSmoothness(int paramInt);
public abstract void setWbaB(double paramDouble);
public abstract void setWbaG(double paramDouble);
public abstract void setWbaR(double paramDouble);
}
package com.prismadroid.devices;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.prismadroid.PrismContext;
import com.prismadroid.ProfileManager;
import com.prismadroid.colors.Rgb24;
import com.prismadroid.colors.WhiteBalance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class DeviceManager
{
private static final String ACTION_USB_PERMISSION = "com.prismadroid.USB_PERMISSION";
private List<Rgb24> colors = null;
protected final Context context;
private List<Device> devices = new ArrayList();
private boolean devicesOpened = false;
private PendingIntent mPermissionIntent;
private UsbManager mUsbManager;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
{
public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
{
if ("com.prismadroid.USB_PERMISSION".equals(paramAnonymousIntent.getAction()))
try
{
UsbDevice localUsbDevice = (UsbDevice)paramAnonymousIntent.getParcelableExtra("device");
if (paramAnonymousIntent.getBooleanExtra("permission", false))
{
if (localUsbDevice != null)
{
LightpackDevice localLightpackDevice = new LightpackDevice(paramAnonymousContext.getApplicationContext(), localUsbDevice);
if (localLightpackDevice.open())
DeviceManager.this.addDevice(localLightpackDevice);
}
}
else
Log.d("usb", "permission denied for device " + localUsbDevice);
}
finally
{
}
}
};
protected PowerManager powerManager = null;
protected final PrismContext prismContext;
private int smoothness = 50;
private WhiteBalance whiteBalance = WhiteBalance.defaultWhiteBalance();
public DeviceManager(PrismContext paramPrismContext)
{
this.prismContext = paramPrismContext;
this.context = paramPrismContext.getApplicationContext();
}
/** @deprecated */
private void addDevice(Device paramDevice)
{
try
{
this.devices.add(paramDevice);
Collections.sort(this.devices, new Comparator()
{
public int compare(Device paramAnonymousDevice1, Device paramAnonymousDevice2)
{
String str1 = paramAnonymousDevice1.getSerial();
if (TextUtils.isEmpty(str1))
str1 = paramAnonymousDevice1.getName();
String str2 = paramAnonymousDevice2.getSerial();
if (TextUtils.isEmpty(str2))
str2 = paramAnonymousDevice2.getName();
int i;
if ((str1 == null) && (str2 == null))
i = 0;
while (true)
{
return i;
if (str1 == null)
i = -1;
else if (str2 == null)
i = 1;
else
i = str1.compareTo(str2);
}
}
});
paramDevice.setGamma(2.0D);
pushWhiteBalanceToDevice(paramDevice);
onDevicesChanged();
return;
}
finally
{
localObject = finally;
throw localObject;
}
}
private void pushSettingsToDevice()
{
Iterator localIterator = this.devices.iterator();
while (true)
{
if (!localIterator.hasNext())
return;
Device localDevice = (Device)localIterator.next();
localDevice.setSmoothness(getSmoothness());
pushWhiteBalanceToDevice(localDevice);
}
}
private void pushWhiteBalanceToDevice(Device paramDevice)
{
WhiteBalance localWhiteBalance = getWhiteBalance();
if (localWhiteBalance == null)
localWhiteBalance = WhiteBalance.defaultWhiteBalance();
float f1 = Math.max(localWhiteBalance.r, Math.max(localWhiteBalance.r, localWhiteBalance.b));
float f2;
float f3;
if (f1 == 0.0F)
{
f2 = 1.0F;
if (f1 != 0.0F)
break label134;
f3 = 1.0F;
label50: if (f1 != 0.0F)
break label145;
}
label134: label145: for (float f4 = 1.0F; ; f4 = localWhiteBalance.b / f1)
{
float f5 = 1.0F - 0.5F + f2 * 0.5F;
float f6 = 1.0F - 0.5F + f3 * 0.5F;
float f7 = 1.0F - 0.5F + f4 * 0.5F;
paramDevice.setWbaR(f5);
paramDevice.setWbaG(f6);
paramDevice.setWbaB(f7);
return;
f2 = localWhiteBalance.r / f1;
break;
f3 = localWhiteBalance.g / f1;
break label50;
}
}
public void changeColors(Rgb24 paramRgb24)
{
changeColors(colorListFromSingleColor(paramRgb24));
}
public void changeColors(List<Rgb24> paramList)
{
if (isScreenOn());
for (this.colors = paramList; ; this.colors = colorListFromSingleColor(new Rgb24(0, 0, 0)))
{
WriteToUsbTask localWriteToUsbTask = new WriteToUsbTask();
List[] arrayOfList = new List[1];
arrayOfList[0] = paramList;
localWriteToUsbTask.execute(arrayOfList);
return;
}
}
public void closeDevices()
{
if (!this.devicesOpened)
return;
this.devicesOpened = false;
resetColorsForce();
pushSettingsToDevice();
Iterator localIterator = this.devices.iterator();
while (true)
{
if (!localIterator.hasNext())
{
this.devices.clear();
onDevicesChanged();
this.context.unregisterReceiver(this.mUsbReceiver);
break;
}
((Device)localIterator.next()).close();
}
}
protected List<Rgb24> colorListFromSingleColor(Rgb24 paramRgb24)
{
int i = 10 * this.devices.size();
ArrayList localArrayList = new ArrayList(i);
for (int j = 0; ; j++)
{
if (j >= i)
return localArrayList;
localArrayList.add(paramRgb24);
}
}
public int getDeviceCount()
{
return this.devices.size();
}
public List<Device> getDevices()
{
return this.devices;
}
public int getSmoothness()
{
return this.smoothness;
}
public WhiteBalance getWhiteBalance()
{
return this.whiteBalance;
}
public boolean isDevicesOpened()
{
return this.devicesOpened;
}
protected boolean isScreenOn()
{
if (this.powerManager == null)
this.powerManager = ((PowerManager)this.prismContext.getApplicationContext().getSystemService("power"));
return this.powerManager.isScreenOn();
}
protected void onDevicesChanged()
{
this.prismContext.getProfileManager().checkForProfileChanging();
}
public void openDevices()
{
if (this.devicesOpened)
return;
this.devicesOpened = true;
this.mUsbManager = ((UsbManager)this.context.getSystemService("usb"));
this.mPermissionIntent = PendingIntent.getBroadcast(this.context, 0, new Intent("com.prismadroid.USB_PERMISSION"), 0);
IntentFilter localIntentFilter = new IntentFilter("com.prismadroid.USB_PERMISSION");
this.context.registerReceiver(this.mUsbReceiver, localIntentFilter);
Iterator localIterator = this.mUsbManager.getDeviceList().values().iterator();
while (true)
{
if (!localIterator.hasNext())
{
pushSettingsToDevice();
break;
}
UsbDevice localUsbDevice = (UsbDevice)localIterator.next();
Object[] arrayOfObject = new Object[2];
arrayOfObject[0] = Integer.valueOf(localUsbDevice.getVendorId());
arrayOfObject[1] = Integer.valueOf(localUsbDevice.getProductId());
Log.d("device", String.format("Device. vid: %d; pid: %d", arrayOfObject));
if (((localUsbDevice.getVendorId() == 7504) && (localUsbDevice.getProductId() == 24610)) || ((localUsbDevice.getVendorId() == 1003) && (localUsbDevice.getProductId() == 8271)))
{
Toast.makeText(this.context, "found Lightpack" + localUsbDevice.getDeviceName(), 0).show();
this.mUsbManager.requestPermission(localUsbDevice, this.mPermissionIntent);
}
}
}
public void resetColors()
{
changeColors(new Rgb24(0, 0, 0));
}
protected void resetColorsForce()
{
writeColorsToUsb(colorListFromSingleColor(new Rgb24(0, 0, 0)));
}
public void setSmoothness(int paramInt)
{
if (this.smoothness != paramInt)
{
this.smoothness = paramInt;
pushSettingsToDevice();
}
}
public void setWhiteBalance(WhiteBalance paramWhiteBalance)
{
this.whiteBalance = paramWhiteBalance;
pushSettingsToDevice();
}
protected void writeColorsToUsb(List<Rgb24> paramList)
{
int i = 0;
ArrayList localArrayList = new ArrayList(10);
Iterator localIterator = paramList.iterator();
while (true)
{
if (!localIterator.hasNext())
return;
localArrayList.add((Rgb24)localIterator.next());
if (((i + 1) % 10 == 0) || (i == -1 + paramList.size()))
{
if (this.devices.size() >= 1 + i / 10)
((Device)this.devices.get(i / 10)).setColors(localArrayList);
localArrayList.clear();
}
i++;
}
}
class WriteToUsbTask extends AsyncTask<List<Rgb24>, Void, Void>
{
WriteToUsbTask()
{
}
protected Void doInBackground(List<Rgb24>[] paramArrayOfList)
{
List<Rgb24> localList = paramArrayOfList[0];
DeviceManager.this.writeColorsToUsb(localList);
return null;
}
}
}
package com.prismadroid.colors;
import android.graphics.Color;
public class Hsv
{
public float h;
public float s;
public float v;
public Hsv(float paramFloat1, float paramFloat2, float paramFloat3)
{
this.h = paramFloat1;
this.s = paramFloat2;
this.v = paramFloat3;
}
public static Hsv fromAndroidColor(int paramInt)
{
float[] arrayOfFloat = new float[3];
Color.colorToHSV(paramInt, arrayOfFloat);
return new Hsv(arrayOfFloat[0], arrayOfFloat[1], arrayOfFloat[2]);
}
public static Hsv fromRgb24(Rgb24 paramRgb24)
{
return fromAndroidColor(paramRgb24.toAndroidColor());
}
public int toAndroidColor()
{
float[] arrayOfFloat = new float[3];
arrayOfFloat[0] = this.h;
arrayOfFloat[1] = this.s;
arrayOfFloat[2] = this.v;
return Color.HSVToColor(arrayOfFloat);
}
public Rgb24 toRgb24()
{
return Rgb24.fromAndroidColor(toAndroidColor());
}
}
package com.prismadroid.devices;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.util.Log;
import com.prismadroid.colors.Rgb24;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class LightpackDevice
implements Device
{
private final String TAG = getClass().getName();
private UsbDeviceConnection connection = null;
private UsbDevice dev = null;
private double gamma = 2.0D;
private UsbInterface intf = null;
private AtomicBoolean isBusy = new AtomicBoolean(false);
private TransferCompleteListener transferCompleteListener = null;
private UsbManager usbManager = null;
private double wbaB = 1.0D;
private double wbaG = 1.0D;
private double wbaR = 1.0D;
public LightpackDevice(Context paramContext, UsbDevice paramUsbDevice)
{
this.usbManager = ((UsbManager)paramContext.getSystemService("usb"));
this.dev = paramUsbDevice;
}
public void close()
{
this.connection.releaseInterface(this.intf);
this.connection.close();
this.connection = null;
}
public String getName()
{
if (this.dev == null);
for (String str = null; ; str = this.dev.getDeviceName())
return str;
}
public String getSerial()
{
if (this.connection == null);
for (String str = null; ; str = this.connection.getSerial())
return str;
}
public double getWbaB()
{
return this.wbaB;
}
public double getWbaG()
{
return this.wbaG;
}
public double getWbaR()
{
return this.wbaR;
}
public boolean isBusy()
{
return this.isBusy.get();
}
public boolean open()
{
boolean bool = false;
if (this.dev != null)
{
this.intf = this.dev.getInterface(0);
this.connection = this.usbManager.openDevice(this.dev);
if (this.connection != null)
break label45;
}
while (true)
{
return bool;
label45: bool = this.connection.claimInterface(this.intf, true);
}
}
public void setColors(List<Rgb24> paramList)
{
if (paramList.size() > 10)
Log.w(this.TAG, "colors.size > 10");
byte[] arrayOfByte;
int i;
Iterator localIterator;
if (!this.isBusy.get())
{
arrayOfByte = new byte[61];
arrayOfByte[0] = 1;
i = 1;
localIterator = paramList.iterator();
}
while (true)
{
if (!localIterator.hasNext())
{
SendTask localSendTask = new SendTask();
byte[][] arrayOfByte1 = new byte[1][];
arrayOfByte1[0] = arrayOfByte;
localSendTask.execute(arrayOfByte1);
return;
}
Rgb24 localRgb24 = (Rgb24)localIterator.next();
double d1 = localRgb24.r * this.wbaR;
if (d1 > 255.0D)
d1 = 255.0D;
double d2 = localRgb24.g * this.wbaG;
if (d2 > 255.0D)
d2 = 255.0D;
double d3 = localRgb24.b * this.wbaB;
if (d3 > 255.0D)
d3 = 255.0D;
int j = (short)(int)(255.0D * Math.pow(d1 / 255.0D, this.gamma));
int k = (short)(int)(255.0D * Math.pow(d2 / 255.0D, this.gamma));
int m = (short)(int)(255.0D * Math.pow(d3 / 255.0D, this.gamma));
int n = i + 1;
arrayOfByte[i] = ((byte)(j & 0xFF));
int i1 = n + 1;
arrayOfByte[n] = ((byte)(k & 0xFF));
int i2 = i1 + 1;
arrayOfByte[i1] = ((byte)(m & 0xFF));
int i3 = i2 + 1;
arrayOfByte[i2] = 0;
int i4 = i3 + 1;
arrayOfByte[i3] = 0;
i = i4 + 1;
arrayOfByte[i4] = 0;
}
}
public void setGamma(double paramDouble)
{
this.gamma = paramDouble;
}
public void setSmoothness(int paramInt)
{
if (!this.isBusy.get())
{
byte[] arrayOfByte = new byte[2];
arrayOfByte[0] = 5;
arrayOfByte[1] = ((byte)paramInt);
SendTask localSendTask = new SendTask();
byte[][] arrayOfByte1 = new byte[1][];
arrayOfByte1[0] = arrayOfByte;
localSendTask.execute(arrayOfByte1);
}
}
public void setTransferCompleteListener(TransferCompleteListener paramTransferCompleteListener)
{
this.transferCompleteListener = paramTransferCompleteListener;
}
public void setWbaB(double paramDouble)
{
this.wbaB = paramDouble;
}
public void setWbaG(double paramDouble)
{
this.wbaG = paramDouble;
}
public void setWbaR(double paramDouble)
{
this.wbaR = paramDouble;
}
class SendTask extends AsyncTask<byte[], Void, Void>
{
SendTask()
{
}
protected Void doInBackground(byte[][] paramArrayOfByte)
{
if (LightpackDevice.this.isBusy.compareAndSet(false, true));
try
{
if (LightpackDevice.this.connection != null)
{
int i = LightpackDevice.this.connection.controlTransfer(33, 9, 512, 0, paramArrayOfByte[0], paramArrayOfByte[0].length, 1000);
if (LightpackDevice.this.transferCompleteListener != null)
LightpackDevice.this.transferCompleteListener.onTransferComplete(paramArrayOfByte[0].length, i);
}
return null;
}
catch (Exception localException)
{
while (true)
{
Log.e(LightpackDevice.this.TAG, localException.getMessage(), localException);
localException.printStackTrace();
}
}
}
protected void onPostExecute(Void paramVoid)
{
super.onPostExecute(paramVoid);
LightpackDevice.this.isBusy.set(false);
}
}
public static abstract interface TransferCompleteListener
{
public abstract void onTransferComplete(int paramInt1, int paramInt2);
}
}
package com.prismadroid.colors;
import android.graphics.Color;
import java.io.Serializable;
public class Rgb24
implements Serializable
{
private static final long serialVersionUID = -8860050607675414854L;
public int b;
public int g;
public int r;
public Rgb24(int paramInt1, int paramInt2, int paramInt3)
{
this.r = paramInt1;
this.g = paramInt2;
this.b = paramInt3;
}
public static Rgb24 fromAndroidColor(int paramInt)
{
return new Rgb24(Color.red(paramInt), Color.green(paramInt), Color.blue(paramInt));
}
public static Rgb24 fromInt(int paramInt)
{
return new Rgb24(0xFF & paramInt >> 16, 0xFF & paramInt >> 8, paramInt & 0xFF);
}
public static Rgb24 mix2(Rgb24 paramRgb241, float paramFloat1, Rgb24 paramRgb242, float paramFloat2)
{
return new Rgb24(Math.round(paramFloat1 * paramRgb241.r + paramFloat2 * paramRgb242.r), Math.round(paramFloat1 * paramRgb241.g + paramFloat2 * paramRgb242.g), Math.round(paramFloat1 * paramRgb241.b + paramFloat2 * paramRgb242.b));
}
public static Rgb24 mix3(Rgb24 paramRgb241, float paramFloat1, Rgb24 paramRgb242, float paramFloat2, Rgb24 paramRgb243, float paramFloat3)
{
return new Rgb24(Math.round(paramFloat1 * paramRgb241.r + paramFloat2 * paramRgb242.r + paramFloat3 * paramRgb243.r), Math.round(paramFloat1 * paramRgb241.g + paramFloat2 * paramRgb242.g + paramFloat3 * paramRgb243.g), Math.round(paramFloat1 * paramRgb241.b + paramFloat2 * paramRgb242.b + paramFloat3 * paramRgb243.b));
}
public int toAndroidColor()
{
return Color.argb(255, this.r, this.g, this.b);
}
public int toInt()
{
return ((0xFF & this.r) << 16) + ((0xFF & this.g) << 8) + (0xFF & this.b);
}
}
package com.prismadroid.colors;
import java.io.Serializable;
import java.util.Locale;
public class WhiteBalance
implements Serializable
{
private static final long serialVersionUID = 6568496020292312857L;
public float b;
public float g;
public float r;
public WhiteBalance()
{
}
public WhiteBalance(float paramFloat1, float paramFloat2, float paramFloat3)
{
this.r = paramFloat1;
this.g = paramFloat2;
this.b = paramFloat3;
}
public static WhiteBalance defaultWhiteBalance()
{
return new WhiteBalance(0.5F, 0.5F, 0.5F);
}
public WhiteBalance clone()
{
return new WhiteBalance(this.r, this.g, this.b);
}
public String toString()
{
Locale localLocale = Locale.getDefault();
Object[] arrayOfObject = new Object[3];
arrayOfObject[0] = Float.valueOf(this.r);
arrayOfObject[1] = Float.valueOf(this.g);
arrayOfObject[2] = Float.valueOf(this.b);
return String.format(localLocale, "r: %f; g: %f; b: %f", arrayOfObject);
}
}
package com.prismadroid.colors;
public class Yuv
{
public float u;
public float v;
public float y;
public Yuv()
{
}
public Yuv(float paramFloat1, float paramFloat2, float paramFloat3)
{
this.y = paramFloat1;
this.u = paramFloat2;
this.v = paramFloat3;
}
public static Yuv fromRgb(Rgb24 paramRgb24)
{
return new Yuv(0.299F * paramRgb24.r + 0.587F * paramRgb24.g + 0.114F * paramRgb24.b, 128.0F + (-0.14713F * paramRgb24.r - 0.28886F * paramRgb24.g + 0.436F * paramRgb24.b), 128.0F + (0.615F * paramRgb24.r - 0.51499F * paramRgb24.g - 0.10001F * paramRgb24.b));
}
public Rgb24 toRgb()
{
float f1 = this.y + 1.13983F * (this.v - 128.0F);
float f2 = this.y - 0.39465F * (this.u - 128.0F) - 0.5806F * (this.v - 128.0F);
float f3 = this.y + 2.03211F * (this.u - 128.0F);
return new Rgb24(Math.round(f1), Math.round(f2), Math.round(f3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment