Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.activelook.activelooksdk.types.GaugeInfo;
import com.activelook.activelooksdk.types.GlassesSettings;
import com.activelook.activelooksdk.types.GlassesVersion;
import com.activelook.activelooksdk.types.Image1bppData;
import com.activelook.activelooksdk.types.ImageData;
import com.activelook.activelooksdk.types.ImageInfo;
import com.activelook.activelooksdk.types.ImgStreamFormat;
Expand Down Expand Up @@ -147,7 +146,7 @@ default void unsubscribeToSensorInterfaceNotifications() {
*
* @param on Turn on or off
*/
void isWriteWithResponse(boolean on);
void setWriteWithResponse(boolean on);

/**
* Load a configuration into the glasses.
Expand Down Expand Up @@ -178,6 +177,8 @@ default void unsubscribeToSensorInterfaceNotifications() {
* Send demo command.
* {@link Deprecated}
*/

void grayscale(byte level);
void demo();
/**
* Send the firmware 4.0.0 demo command with parameter which was the test.
Expand Down Expand Up @@ -248,11 +249,20 @@ default void unsubscribeToSensorInterfaceNotifications() {
*/
void als(boolean enable);
/**
* Sets the grey level (0 to 15) used to draw the next graphical element.
*
* Sets the red-green color value (0 to 255) used to draw the next graphical element.
* Only available on color glasses (see {@link #isColorCapable()}).
* Sets the grey level (0 to 15) used to draw the next graphical element if not color glasses.
* @param value The selected color.
*/
void color(byte value);
/**
* Indicates whether the connected glasses have a color display (MDP08) rather than a
* grey-level one (MDP05), based on the Hardware Version String.
*/
default boolean isColorCapable() {
final String hw = this.getDeviceInformation().getHardwareVersion();
return hw != null && hw.contains("MDP08");
}
/**
* Set a pixel on at the corresponding coordinates.
*
Expand Down Expand Up @@ -381,6 +391,13 @@ default void unsubscribeToSensorInterfaceNotifications() {
* @param img The image to store in the configuration .
*/
void imgSave4bppHeatShrinkSaveComp(byte id, Bitmap img);
/**
* Save image data in the chosen format.
* @param id The image id in the configuration.
* @param imgData The pre-encoded image data to store in the configuration.
* @param format The image format.
*/
void imgSave(byte id, ImageData imgData, ImgSaveFormat format);
/**
* Display image id to the corresponding coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class SdkSingleton {

private static Sdk singleton;

static final synchronized Sdk init(Context applicationContext,
static synchronized Sdk init(Context applicationContext,
String token,
Consumer<GlassesUpdate> onUpdateStart,
Consumer<Pair<GlassesUpdate, Runnable>> onUpdateAvailableCallback,
Expand All @@ -51,7 +51,7 @@ static final synchronized Sdk init(Context applicationContext,
return SdkSingleton.singleton;
}

static final synchronized Sdk getInstance() {
static synchronized Sdk getInstance() {
return SdkSingleton.singleton;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ public abstract class AbstractGlasses implements Glasses {
static final byte ID_power = (byte) 0x00;
static final byte ID_clear = (byte) 0x01;
static final byte ID_grey = (byte) 0x02;
static final byte ID_grayscale = (byte) 0x30;
static final byte ID_demo = (byte) 0x03;
static final byte ID_test = (byte) 0x04;
static final byte ID_battery = (byte) 0x05;
static final byte ID_vers = (byte) 0x06;
static final byte ID_led = (byte) 0x08;
static final byte ID_shift = (byte) 0x09;
static final byte ID_settings = (byte) 0x0A;
static final byte ID_error = (byte) 0xE2;

/*
* Display luminance commands ids
*/
Expand All @@ -78,14 +81,15 @@ public abstract class AbstractGlasses implements Glasses {
/*
* Graphics commands ids
*/
static final byte ID_color = (byte) 0x30;
static final byte ID_color = (byte) 0x3D;
static final byte ID_point = (byte) 0x31;
static final byte ID_line = (byte) 0x32;
static final byte ID_rect = (byte) 0x33;
static final byte ID_rectf = (byte) 0x34;
static final byte ID_circ = (byte) 0x35;
static final byte ID_circf = (byte) 0x36;
static final byte ID_txt = (byte) 0x37;
static final byte ID_txtColor = (byte) 0x3E;
static final byte ID_polyline = (byte) 0x38;
static final byte ID_holdFlush = (byte) 0x39;
/*
Expand Down Expand Up @@ -175,6 +179,10 @@ public abstract class AbstractGlasses implements Glasses {
static final byte ID_shutdown = (byte) 0xE0;
private final HashMap<QueryId, Consumer<byte[]>> callbacks;
private QueryId currentQID;
private boolean useQueryId = false;
public void setUseQueryId(boolean useQueryId) {
this.useQueryId = useQueryId;
}

/*
Methods for children implementation
Expand All @@ -187,11 +195,18 @@ protected AbstractGlasses() {
protected void writeBytes(byte[] bytes) {
}

protected void writeBytes(byte[] bytes,boolean writeType) {
}

protected final void delegateToCallback(final Command command) {
final QueryId qid = command.getQueryId();
if (qid != null) {
final Consumer<byte[]> callback = this.callbacks.remove(qid);
if (callback != null) {
if (command.getCommandId() == ID_error) {
Log.e("AbstractGlasses", "Command failed: " + Command.bytesToStr(command.getData()));
return; // c'est un message d'erreur, pas la réponse attendue : on ne le donne pas au parseur
}
callback.accept(command.getData());
}
}
Expand All @@ -211,18 +226,39 @@ private QueryId nextQueryId() {
}

private void writeCommand(final Command command) {
final QueryId qid = this.nextQueryId();
command.setQueryId(qid);
if (this.useQueryId) {
final QueryId qid = this.nextQueryId();
command.setQueryId(qid);
}
this.writeBytes(command.toBytes());
}

private void writeCommand(final Command command, final Consumer<byte[]> callback) {
QueryId qid = this.nextQueryId();
command.setQueryId(qid);
this.registerCallback(qid, callback);
if (this.useQueryId) {
QueryId qid = this.nextQueryId();
command.setQueryId(qid);
this.registerCallback(qid, callback);
} else {
Log.w("AbstractGlasses", "Callback registered but useQueryId=false: it will never fire.");
}
this.writeBytes(command.toBytes());
}

// private void writeCommand(final Command command, boolean writeType) {
// final QueryId qid = this.nextQueryId();
// command.setQueryId(qid);
// this.writeBytes(command.toBytes(),writeType);
// }

// private void writeCommand(final Command command, final Consumer<byte[]> callback, boolean writeType) {
// QueryId qid = this.nextQueryId();
// command.setQueryId(qid);
// this.registerCallback(qid, callback);
// this.writeBytes(command.toBytes(),writeType);
// }



/*
Public defaults
*/
Expand Down Expand Up @@ -251,6 +287,14 @@ public void grey(final byte level) {
this.writeCommand(new Command(ID_grey, data));
}

@Override
public void grayscale(final byte level) {
final CommandData data = CommandData.fromGreyLevel(level);
this.writeCommand(new Command(ID_grayscale, data));
}



@Override
public void demo() {
this.writeCommand(new Command(ID_demo));
Expand Down Expand Up @@ -330,7 +374,7 @@ public void als(final boolean on) {

@Override
public void color(final byte value) {
final CommandData data = CommandData.fromGreyLevel(value);
final CommandData data = CommandData.fromColorLevel(value);
this.writeCommand(new Command(ID_color, data));
}

Expand Down Expand Up @@ -377,9 +421,10 @@ public void txt(final short x, final short y, final Rotation r, final byte f, fi
.add(CommandData.fromRotation(r))
.addUInt8(f, c)
.addNulTerminatedStrings(s);
this.writeCommand(new Command(ID_txt, data));
this.writeCommand(new Command((isColorCapable() ? ID_txtColor : ID_txt), data));
}


@Override
public void polyline(final short[] points) {
final CommandData data = new CommandData().addInt16(points);
Expand Down Expand Up @@ -439,11 +484,16 @@ public void imgSave(final byte id, final Bitmap image, final ImgSaveFormat forma
case MONO_4BPP_HEATSHRINK_SAVE_COMP:
this.imgSave4bppHeatShrinkSaveComp(id, image);
break;
case RG_COLOR_8BPP:
this.imgSaveRGColor8bpp(id, image);
break;

}
}


// TODO @Override
public void imgSave4bpp(final byte id, final int width, final int size, final byte[] bytes, final ImgSaveFormat format) {
public void imgSave(final byte id, final int width, final int size, final byte[] bytes, final ImgSaveFormat format) {
final CommandData data = new CommandData()
.addUInt8(id)
.addUInt32(size)
Expand All @@ -459,21 +509,27 @@ public void imgSave4bpp(final byte id, final int width, final int size, final by
public void imgSave4bpp(final byte id, final Bitmap image){
final ImgSaveFormat format = ImgSaveFormat.MONO_4BPP;
final ImageData imgData = ImageConverter.getImageData(image, format);
this.imgSave4bpp(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
this.imgSave(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}
public void imgSaveRGColor8bpp(final byte id, final Bitmap image){
final ImgSaveFormat format = ImgSaveFormat.RG_COLOR_8BPP;
final ImageData imgData = ImageConverter.getImageData(image, format);
this.imgSave(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}


@Override
public void imgSave4bppHeatShrink(final byte id, final Bitmap image){
final ImgSaveFormat format = ImgSaveFormat.MONO_4BPP_HEATSHRINK;
final ImageData imgData = ImageConverter.getImageData(image, format);
this.imgSave4bpp(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
this.imgSave(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}

@Override
public void imgSave4bppHeatShrinkSaveComp(final byte id, final Bitmap image){
final ImgSaveFormat format = ImgSaveFormat.MONO_4BPP_HEATSHRINK_SAVE_COMP;
final ImageData imgData = ImageConverter.getImageData(image, format);
this.imgSave4bpp(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
this.imgSave(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}

// TODO @Override
Expand Down Expand Up @@ -510,7 +566,10 @@ public void imgSave1bpp(final byte id, final Bitmap image){
final Image1bppData imgData = ImageConverter.getImage1bppData(image, format);
this.imgSave1bpp(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}

@Override
public void imgSave(final byte id, final ImageData imgData, final ImgSaveFormat format) {
this.imgSave(id, imgData.getWidth(), imgData.getSize(), imgData.getBytes(), format);
}
@Override
public void imgDisplay(final byte id, final short x, final short y) {
final CommandData data = new CommandData().addUInt8(id).addInt16(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public Command(final byte commandId, final CommandData data) {
this.commandId = commandId;
this.queryId = queryId;
}
public byte getCommandId() {
return this.commandId;
}

public Command(byte[] payload) {
super();
int fullLength = 5;
int fullLength = getFullLength(payload);
assert payload[0] == (byte) 0xFF;
this.commandId = payload[1];
int n = payload[2] & (byte) 0x0F;
int offset = 4;
if ((payload[2] & 0x10) == 0x10) {
fullLength = CommandData.UInt16.asInt(payload[3], payload[4]);
offset++;
} else {
fullLength = CommandData.UInt8.asShort(payload[3]);
}
int m = fullLength - (1 + n + offset);
if (n > 0) {
Expand All @@ -75,19 +75,25 @@ public Command(byte[] payload) {

public static final boolean isValidBuffer(byte[] payload) {
Log.d("Validating", Command.bytesToStr(payload));
int fullLength = 5;
assert payload[0] == (byte) 0xFF;
if (payload.length < 5) return false;
int fullLength;
if ((payload[2] & 0x10) == 0x10) {
if (payload.length < 5) return false;
fullLength = CommandData.UInt16.asInt(payload[3], payload[4]);
} else {
fullLength = CommandData.UInt8.asShort(payload[3]);
}
Log.d("Validating", String.format("payload.length %d == fullLength %d", payload.length, fullLength));
if (payload.length == fullLength) {
assert payload[fullLength - 1] == (byte) 0xAA;
return true;
return payload.length >= fullLength;
}

public static final int getFullLength(byte[] payload) {
assert payload.length >= 4 : "Buffer too short to read header";
assert payload[0] == (byte) 0xFF;
if ((payload[2] & 0x10) == 0x10) {
assert payload.length >= 5 : "Buffer too short to read extended length";
return CommandData.UInt16.asInt(payload[3], payload[4]);
} else {
return false;
return CommandData.UInt8.asShort(payload[3]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ public static CommandData fromGreyLevel(final byte level) {
return new CommandData(level);
}

public static CommandData fromColorLevel(final byte level) {
return new CommandData(level);
}

public static CommandData fromDemoPattern(final DemoPattern pattern) {
assert pattern != null : String.format(Locale.US, "Pattern cannot be null");
switch (pattern) {
Expand Down Expand Up @@ -285,6 +289,7 @@ public static CommandData fromImgSaveFormat(final ImgSaveFormat format) {
case MONO_1BPP: return new CommandData((byte) 0x01);
case MONO_4BPP_HEATSHRINK: return new CommandData((byte) 0x02);
case MONO_4BPP_HEATSHRINK_SAVE_COMP: return new CommandData((byte) 0x03);
case RG_COLOR_8BPP: return new CommandData((byte) 0x0A);
default: return new CommandData((byte) 0x04);
}
}
Expand Down
Loading