Skip to content

Commit

Permalink
Added brightness flag to color command, working on absolute dim command.
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Apr 16, 2014
1 parent a4d086a commit f126738
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/main/java/jlifx/boblightd/BoblightProtocolHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void handleSetLightCenterMessage(String message) throws IOException {
float g = Float.parseFloat(tokenizer.nextToken());
float b = Float.parseFloat(tokenizer.nextToken());
for (IBulb bulb : bulbs) {
bulb.colorize(new Color(r, g, b), 0);
bulb.colorize(new Color(r, g, b), 0, 1.0f);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/jlifx/bulb/Bulb.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void switchOff() throws IOException {
PacketService.sendPowerManagementPacket(this, false);
}

public void colorize(Color color, int fadetime) throws IOException {
PacketService.sendColorManagementPacket(this, color, fadetime);
public void colorize(Color color, int fadetime, float brightness) throws IOException {
PacketService.sendColorManagementPacket(this, color, fadetime, brightness);
}

public StatusResponsePacket getStatus() {
Expand Down Expand Up @@ -78,6 +78,10 @@ public int getDim() {
return getStatus().getDim();
}

public void setDim(float brightness) throws IOException {
PacketService.sendSetDimAbsolutePacket(this, brightness);
}

public int getPower() {
return getStatus().getPower();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/jlifx/bulb/IBulb.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IBulb {

void switchOff() throws IOException;

void colorize(Color color, int fadetime) throws IOException;
void colorize(Color color, int fadetime, float brightness) throws IOException;

StatusResponsePacket getStatus();

Expand All @@ -34,5 +34,7 @@ public interface IBulb {

int getDim();

void setDim(float brightness) throws IOException;

int getPower();
}
8 changes: 8 additions & 0 deletions src/main/java/jlifx/commandline/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class Main {
commands.put("status", new StatusCommand());
commands.put("switch", new SwitchCommand());
commands.put("color", new ColorCommand());
// commands.put("dim", new DimCommand());
commands.put("blink", new BlinkCommand());
commands.put("rainbow", new RainbowCommand());
}
Expand All @@ -38,12 +39,19 @@ private static void printUsage() {
OUT.println(" status <mac-address|gateway|all>");
OUT.println(" switch <mac-address|gateway|all> <on|off>");
OUT.println(" color <mac-address|gateway|all> <color-name|rgb-hex-value>");
OUT.println(" [brightness (0.0 - 1.0)]");
// OUT.println(" dim <mac-address|gateway|all> <0.0 - 1.0>");
OUT.println(" blink <mac-address|gateway|all> [times]");
OUT.println(" rainbow <mac-address|gateway|all>");
OUT.println("");
printExamples();
}

private static void printExamples() {
OUT.println("Examples:");
OUT.println(" java -jar jlifx.jar switch all off");
OUT.println(" java -jar jlifx.jar color all red");
// OUT.println(" java -jar jlifx.jar dim all 0.5");
OUT.println(" java -jar jlifx.jar blink gateway");
OUT.println(" java -jar jlifx.jar blink AA:BB:CC:DD:EE:FF 3");
OUT.println(" java -jar jlifx.jar rainbow all");
Expand Down
121 changes: 64 additions & 57 deletions src/main/java/jlifx/commandline/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,71 @@

public final class Utils {

private Utils() {
}
private Utils() {}

public static byte[] parseMacAddress(String macAddress) {
try {
byte[] result = new byte[6];
result[0] = (byte) (Integer
.parseInt(macAddress.substring(0, 2), 16));
result[1] = (byte) (Integer
.parseInt(macAddress.substring(3, 5), 16));
result[2] = (byte) (Integer
.parseInt(macAddress.substring(6, 8), 16));
result[3] = (byte) (Integer.parseInt(macAddress.substring(9, 11),
16));
result[4] = (byte) (Integer.parseInt(macAddress.substring(12, 14),
16));
result[5] = (byte) (Integer.parseInt(macAddress.substring(15, 17),
16));
return result;
} catch (NumberFormatException e) {
return null;
}
}
public static byte[] parseMacAddress(String macAddress) {
try {
byte[] result = new byte[6];
result[0] = (byte)(Integer.parseInt(macAddress.substring(0, 2), 16));
result[1] = (byte)(Integer.parseInt(macAddress.substring(3, 5), 16));
result[2] = (byte)(Integer.parseInt(macAddress.substring(6, 8), 16));
result[3] = (byte)(Integer.parseInt(macAddress.substring(9, 11), 16));
result[4] = (byte)(Integer.parseInt(macAddress.substring(12, 14), 16));
result[5] = (byte)(Integer.parseInt(macAddress.substring(15, 17), 16));
return result;
} catch (NumberFormatException e) {
return null;
}
}

private static final Map<String, Color> COLORS = new HashMap<String, Color>();
static {
COLORS.put("black", Color.BLACK);
COLORS.put("blue", Color.BLUE);
COLORS.put("cyan", Color.CYAN);
COLORS.put("darkgray", Color.DARK_GRAY);
COLORS.put("gray", Color.GRAY);
COLORS.put("green", Color.GREEN);
COLORS.put("lightgrey", Color.LIGHT_GRAY);
COLORS.put("magenta", Color.MAGENTA);
COLORS.put("orange", Color.ORANGE);
COLORS.put("pink", Color.PINK);
COLORS.put("red", Color.RED);
COLORS.put("white", Color.WHITE);
COLORS.put("yellow", Color.YELLOW);
}
private static final Map<String, Color> COLORS = new HashMap<String, Color>();
static {
COLORS.put("black", Color.BLACK);
COLORS.put("blue", Color.BLUE);
COLORS.put("cyan", Color.CYAN);
COLORS.put("darkgray", Color.DARK_GRAY);
COLORS.put("gray", Color.GRAY);
COLORS.put("green", Color.GREEN);
COLORS.put("lightgrey", Color.LIGHT_GRAY);
COLORS.put("magenta", Color.MAGENTA);
COLORS.put("orange", Color.ORANGE);
COLORS.put("pink", Color.PINK);
COLORS.put("red", Color.RED);
COLORS.put("white", Color.WHITE);
COLORS.put("yellow", Color.YELLOW);
}

public static Color stringToColor(String string) {
Color color = COLORS.get(string);
if (color != null) {
return color;
} else {
try {
return Color.decode("#" + string);
} catch (NumberFormatException e) {
return null;
}
}
}
public static Color stringToColor(String string) {
Color color = COLORS.get(string);
if (color != null) {
return color;
} else {
try {
return Color.decode("#" + string);
} catch (NumberFormatException e) {
return null;
}
}
}

/**
* Returns a string containing the hex value of a word.
*/
public static String wordToHexString(int w) {
return "$" + Integer.toHexString(w & 0xFFFF).toUpperCase();
}
}
/**
* Returns a string containing the hex value of a word.
*/
public static String wordToHexString(int w) {
return "$" + Integer.toHexString(w & 0xFFFF).toUpperCase();
}

/**
* Returns true if the given string is a valid float value, false otherwise.
*/
public static boolean isFloatValue(String s) {
boolean result = true;
try {
Float.parseFloat(s);
} catch (NumberFormatException e) {
result = false;
}
return result;
}

}
12 changes: 8 additions & 4 deletions src/main/java/jlifx/commandline/command/ColorCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ public class ColorCommand extends AbstractBulbCommand {

@Override
public boolean execute(Collection<IBulb> bulbs, String[] commandArgs, PrintStream out) throws Exception {
if (commandArgs.length != 2) {
if (commandArgs.length < 2) {
return false;
} else {
Color color = Utils.stringToColor(commandArgs[1]);
if (color == null) {
return false;
} else {
colorizeBulbs(bulbs, color);
if (commandArgs.length >= 3 && Utils.isFloatValue(commandArgs[2])) {
colorizeBulbs(bulbs, color, Float.parseFloat(commandArgs[2]));
} else {
colorizeBulbs(bulbs, color, 1.0f);
}
}
}
return true;
}

private void colorizeBulbs(Collection<IBulb> bulbs, Color color) throws IOException {
private void colorizeBulbs(Collection<IBulb> bulbs, Color color, float brightness) throws IOException {
for (IBulb bulb : bulbs) {
bulb.colorize(color, 3);
bulb.colorize(color, 3, brightness);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/jlifx/commandline/command/DimCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package jlifx.commandline.command;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;

import jlifx.bulb.IBulb;
import jlifx.commandline.AbstractBulbCommand;
import jlifx.commandline.Utils;

public class DimCommand extends AbstractBulbCommand {

@Override
public boolean execute(Collection<IBulb> bulbs, String[] commandArgs, PrintStream out) throws Exception {
if (commandArgs.length != 2 || !Utils.isFloatValue(commandArgs[1])) {
return false;
} else {
dimBulbs(bulbs, Float.parseFloat(commandArgs[1]));
return true;
}
}

private void dimBulbs(Collection<IBulb> bulbs, float brightness) throws IOException {
for (IBulb bulb : bulbs) {
bulb.setDim(brightness);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public boolean execute(Collection<IBulb> bulbs, String[] commandArgs, PrintStrea
}
for (IBulb bulb : bulbs) {
Color color = spectrumColors.get(i);
bulb.colorize(color, 3);
bulb.colorize(color, 3, 1.0f);
}
Thread.sleep(3000);
i++;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jlifx/packet/ColorManagementPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

public class ColorManagementPacket extends Packet {

public ColorManagementPacket(byte[] targetMacAddress, Color color, int fadetime) {
public ColorManagementPacket(byte[] targetMacAddress, Color color, int fadetime, float brightness) {
float[] hsbValues = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
setType((byte)0x66);
setTargetMac(targetMacAddress);
byte streaming = 0x00;
byte[] hueBytes = floatToBytes(hsbValues[0]);
byte[] saturationBytes = floatToBytes(hsbValues[1]);
byte[] brightnessBytes = floatToBytes(hsbValues[2]);
byte[] brightnessBytes = floatToBytes(brightness);
byte[] kelvin = new byte[] {0x0a, (byte)0xf0};
byte[] payload = new byte[] {streaming, //
hueBytes[1], hueBytes[0], //
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/jlifx/packet/PacketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.util.ArrayList;
import java.util.List;

import jlifx.bulb.Bulb;
import jlifx.bulb.DiscoveryService;
import jlifx.bulb.GatewayBulb;
import jlifx.bulb.IBulb;

public final class PacketService {
private static Socket socket;
Expand All @@ -21,7 +21,7 @@ public final class PacketService {

private PacketService() {}

public static void sendPowerManagementPacket(Bulb bulb, boolean on) throws IOException {
public static void sendPowerManagementPacket(IBulb bulb, boolean on) throws IOException {
Packet packet = new PowerManagementPacket(bulb.getMacAddress(), on);
sendPacket(bulb.getGatewayBulb(), packet);
}
Expand All @@ -36,8 +36,14 @@ public static List<Packet> sendWifiInfoRequestPacket(GatewayBulb bulb) throws IO
return sendPacketAndWaitForResponse(bulb, packet);
}

public static void sendColorManagementPacket(Bulb bulb, Color color, int fadetime) throws IOException {
Packet packet = new ColorManagementPacket(bulb.getMacAddress(), color, fadetime);
public static void sendColorManagementPacket(IBulb bulb, Color color, int fadetime, float brightness)
throws IOException {
Packet packet = new ColorManagementPacket(bulb.getMacAddress(), color, fadetime, brightness);
sendPacket(bulb.getGatewayBulb(), packet);
}

public static void sendSetDimAbsolutePacket(IBulb bulb, float brightness) throws IOException {
Packet packet = new SetDimAbsolutePacket(brightness);
sendPacket(bulb.getGatewayBulb(), packet);
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/jlifx/packet/SetDimAbsolutePacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jlifx.packet;

public class SetDimAbsolutePacket extends Packet {

public SetDimAbsolutePacket(float brightness) {
setType((byte)0x68);
int value;
if (Math.floor(brightness) >= 1) {
value = 0xFFFF;
} else {
value = (int)(0xFFFF * brightness);
}
byte[] payload = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
payload[0] = (byte)(value >> 8);
payload[1] = (byte)value;
payload[2] = 0x10;
setPayload(payload);
}

}
2 changes: 1 addition & 1 deletion src/main/java/jlifx/packet/StatusResponsePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public int getKelvin() {
}

public int getDim() {
return ((packet.getPayload()[8] << 8) | packet.getPayload()[9]) & 0xFFFF;
return ((packet.getPayload()[9] << 8) | packet.getPayload()[8]) & 0xFFFF;
}

public int getPower() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/jlifx/packet/StatusResponsePacketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private StatusResponsePacket makeTestPacket() {
0x43, 0x21, // saturation (LE)
0x12, 0x34, // brightness (LE)
0x12, 0x34, // kelvin (LE)
0x01, 0x02, // dim
0x02, 0x01, // dim (LE)
(byte)0xFF, (byte)0xFF, // power
'h', 'e', 'l', 'l', 'o', 0x00 // label
};
Expand Down

0 comments on commit f126738

Please sign in to comment.