mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-06-05 21:49:48 +02:00
Roidmi: Slight code cleanup
This commit is contained in:
@@ -17,5 +17,9 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
|
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
|
||||||
|
|
||||||
public class GBDeviceEventFmFrequency extends GBDeviceEvent {
|
public class GBDeviceEventFmFrequency extends GBDeviceEvent {
|
||||||
public float frequency;
|
public final float frequency;
|
||||||
|
|
||||||
|
public GBDeviceEventFmFrequency(final float frequency) {
|
||||||
|
this.frequency = frequency;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,5 +17,9 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
|
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
|
||||||
|
|
||||||
public class GBDeviceEventLEDColor extends GBDeviceEvent {
|
public class GBDeviceEventLEDColor extends GBDeviceEvent {
|
||||||
public int color;
|
public final int color;
|
||||||
|
|
||||||
|
public GBDeviceEventLEDColor(final int color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -31,15 +31,15 @@ public class Roidmi1Coordinator extends RoidmiCoordinator {
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public DeviceType getSupportedType(GBDeviceCandidate candidate) {
|
public DeviceType getSupportedType(final GBDeviceCandidate candidate) {
|
||||||
try {
|
try {
|
||||||
BluetoothDevice device = candidate.getDevice();
|
final BluetoothDevice device = candidate.getDevice();
|
||||||
String name = device.getName();
|
final String name = device.getName();
|
||||||
|
|
||||||
if (name != null && name.contains("睿米车载蓝牙播放器")) {
|
if (name != null && name.contains("睿米车载蓝牙播放器")) {
|
||||||
return DeviceType.ROIDMI;
|
return DeviceType.ROIDMI;
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (final Exception ex) {
|
||||||
LOG.error("unable to check device support", ex);
|
LOG.error("unable to check device support", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -31,9 +31,9 @@ public class Roidmi3Coordinator extends RoidmiCoordinator {
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public DeviceType getSupportedType(GBDeviceCandidate candidate) {
|
public DeviceType getSupportedType(final GBDeviceCandidate candidate) {
|
||||||
try {
|
try {
|
||||||
BluetoothDevice device = candidate.getDevice();
|
final BluetoothDevice device = candidate.getDevice();
|
||||||
final String name = device.getName();
|
final String name = device.getName();
|
||||||
|
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
|
@@ -56,7 +56,7 @@ public class Roidmi1Protocol extends RoidmiProtocol {
|
|||||||
private static final byte[] COMMAND_PERIODIC = new byte[]{(byte) 0xaa, 0x55, 0x02, 0x01, (byte) 0x85, (byte) 0x88, (byte) 0xc3, 0x3c};
|
private static final byte[] COMMAND_PERIODIC = new byte[]{(byte) 0xaa, 0x55, 0x02, 0x01, (byte) 0x85, (byte) 0x88, (byte) 0xc3, 0x3c};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GBDeviceEvent[] decodeResponse(byte[] responseData) {
|
public GBDeviceEvent[] decodeResponse(final byte[] responseData) {
|
||||||
if (responseData.length <= PACKET_MIN_LENGTH) {
|
if (responseData.length <= PACKET_MIN_LENGTH) {
|
||||||
LOG.info("Response too small");
|
LOG.info("Response too small");
|
||||||
return null;
|
return null;
|
||||||
@@ -83,17 +83,15 @@ public class Roidmi1Protocol extends RoidmiProtocol {
|
|||||||
|
|
||||||
switch (responseData[3]) {
|
switch (responseData[3]) {
|
||||||
case COMMAND_GET_COLOR:
|
case COMMAND_GET_COLOR:
|
||||||
int color = responseData[5];
|
final int color = responseData[5];
|
||||||
LOG.debug("Got color: " + color);
|
LOG.debug("Got color: " + color);
|
||||||
GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor();
|
final GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(RoidmiConst.COLOR_PRESETS[color - 1]);
|
||||||
evColor.color = RoidmiConst.COLOR_PRESETS[color - 1];
|
|
||||||
return new GBDeviceEvent[]{evColor};
|
return new GBDeviceEvent[]{evColor};
|
||||||
case COMMAND_GET_FREQUENCY:
|
case COMMAND_GET_FREQUENCY:
|
||||||
String frequencyHex = GB.hexdump(responseData, 4, 2);
|
final String frequencyHex = GB.hexdump(responseData, 4, 2);
|
||||||
float frequency = Float.valueOf(frequencyHex) / 10.0f;
|
final float frequency = Float.parseFloat(frequencyHex) / 10.0f;
|
||||||
LOG.debug("Got frequency: " + frequency);
|
LOG.debug("Got frequency: " + frequency);
|
||||||
GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency();
|
final GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(frequency);
|
||||||
evFrequency.frequency = frequency;
|
|
||||||
return new GBDeviceEvent[]{evFrequency};
|
return new GBDeviceEvent[]{evFrequency};
|
||||||
default:
|
default:
|
||||||
LOG.error("Unrecognized response type 0x" + GB.hexdump(responseData, packetHeader().length, 1));
|
LOG.error("Unrecognized response type 0x" + GB.hexdump(responseData, packetHeader().length, 1));
|
||||||
@@ -102,8 +100,8 @@ public class Roidmi1Protocol extends RoidmiProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] encodeLedColor(int color) {
|
public byte[] encodeLedColor(final int color) {
|
||||||
int[] presets = RoidmiConst.COLOR_PRESETS;
|
final int[] presets = RoidmiConst.COLOR_PRESETS;
|
||||||
int color_id = -1;
|
int color_id = -1;
|
||||||
for (int i = 0; i < presets.length; i++) {
|
for (int i = 0; i < presets.length; i++) {
|
||||||
if (presets[i] == color) {
|
if (presets[i] == color) {
|
||||||
@@ -119,11 +117,11 @@ public class Roidmi1Protocol extends RoidmiProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] encodeFmFrequency(float frequency) {
|
public byte[] encodeFmFrequency(final float frequency) {
|
||||||
if (frequency < 87.5 || frequency > 108.0)
|
if (frequency < 87.5 || frequency > 108.0)
|
||||||
throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0");
|
throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0");
|
||||||
|
|
||||||
byte[] freq = frequencyToBytes(frequency);
|
final byte[] freq = frequencyToBytes(frequency);
|
||||||
|
|
||||||
return encodeCommand(COMMAND_SET_FREQUENCY, freq[0], freq[1]);
|
return encodeCommand(COMMAND_SET_FREQUENCY, freq[0], freq[1]);
|
||||||
}
|
}
|
||||||
|
@@ -73,37 +73,37 @@ public class Roidmi3Protocol extends RoidmiProtocol {
|
|||||||
LOG.warn("Potentially unsupported response: " + GB.hexdump(res, 0, res.length));
|
LOG.warn("Potentially unsupported response: " + GB.hexdump(res, 0, res.length));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res[1] == RESPONSE_VOLTAGE) {
|
switch(res[1]) {
|
||||||
String voltageHex = GB.hexdump(res, 3, 2);
|
case RESPONSE_VOLTAGE:
|
||||||
float voltage = Float.valueOf(voltageHex) / 100.0f;
|
final String voltageHex = GB.hexdump(res, 3, 2);
|
||||||
LOG.debug("Got voltage: " + voltage);
|
final float voltage = Float.parseFloat(voltageHex) / 100.0f;
|
||||||
GBDeviceEventBatteryInfo evBattery = new GBDeviceEventBatteryInfo();
|
LOG.debug("Got voltage: " + voltage);
|
||||||
evBattery.state = BatteryState.NO_BATTERY;
|
GBDeviceEventBatteryInfo evBattery = new GBDeviceEventBatteryInfo();
|
||||||
evBattery.level = GBDevice.BATTERY_UNKNOWN;
|
evBattery.state = BatteryState.NO_BATTERY;
|
||||||
evBattery.voltage = voltage;
|
evBattery.level = GBDevice.BATTERY_UNKNOWN;
|
||||||
return new GBDeviceEvent[]{evBattery};
|
evBattery.voltage = voltage;
|
||||||
} else if (res[1] == RESPONSE_COLOR) {
|
return new GBDeviceEvent[]{evBattery};
|
||||||
LOG.debug("Got color: #" + GB.hexdump(res, 3, 3));
|
case RESPONSE_COLOR:
|
||||||
int color = 0xFF000000 | ((res[3] << 16) & 0xFF0000) | ((res[4] << 8) & 0xFF00) | (res[5] & 0xFF);
|
LOG.debug("Got color: #" + GB.hexdump(res, 3, 3));
|
||||||
GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor();
|
final int color = 0xFF000000 | ((res[3] << 16) & 0xFF0000) | ((res[4] << 8) & 0xFF00) | (res[5] & 0xFF);
|
||||||
evColor.color = color;
|
final GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(color);
|
||||||
return new GBDeviceEvent[]{evColor};
|
return new GBDeviceEvent[]{evColor};
|
||||||
} else if (res[1] == RESPONSE_FREQUENCY) {
|
case RESPONSE_FREQUENCY:
|
||||||
String frequencyHex = GB.hexdump(res, 3, 2);
|
final String frequencyHex = GB.hexdump(res, 3, 2);
|
||||||
float frequency = Float.valueOf(frequencyHex) / 10.0f;
|
final float frequency = Float.parseFloat(frequencyHex) / 10.0f;
|
||||||
LOG.debug("Got frequency: " + frequency);
|
LOG.debug("Got frequency: " + frequency);
|
||||||
GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency();
|
final GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(frequency);
|
||||||
evFrequency.frequency = frequency;
|
return new GBDeviceEvent[]{evFrequency};
|
||||||
return new GBDeviceEvent[]{evFrequency};
|
default:
|
||||||
} else {
|
LOG.error("Unrecognized response: " + GB.hexdump(res, 0, res.length));
|
||||||
LOG.error("Unrecognized response: " + GB.hexdump(res, 0, res.length));
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] encodeLedColor(int color) {
|
public byte[] encodeLedColor(final int color) {
|
||||||
byte[] cmd = COMMAND_SET_COLOR.clone();
|
final byte[] cmd = COMMAND_SET_COLOR.clone();
|
||||||
|
|
||||||
cmd[2] = (byte) (color >> 16);
|
cmd[2] = (byte) (color >> 16);
|
||||||
cmd[3] = (byte) (color >> 8);
|
cmd[3] = (byte) (color >> 8);
|
||||||
@@ -113,12 +113,12 @@ public class Roidmi3Protocol extends RoidmiProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] encodeFmFrequency(float frequency) {
|
public byte[] encodeFmFrequency(final float frequency) {
|
||||||
if (frequency < 87.5 || frequency > 108.0)
|
if (frequency < 87.5 || frequency > 108.0)
|
||||||
throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0");
|
throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0");
|
||||||
|
|
||||||
byte[] cmd = COMMAND_SET_FREQUENCY.clone();
|
final byte[] cmd = COMMAND_SET_FREQUENCY.clone();
|
||||||
byte[] freq = frequencyToBytes(frequency);
|
final byte[] freq = frequencyToBytes(frequency);
|
||||||
cmd[2] = freq[0];
|
cmd[2] = freq[0];
|
||||||
cmd[3] = freq[1];
|
cmd[3] = freq[1];
|
||||||
|
|
||||||
@@ -149,8 +149,8 @@ public class Roidmi3Protocol extends RoidmiProtocol {
|
|||||||
return encodeCommand(COMMAND_GET_VOLTAGE);
|
return encodeCommand(COMMAND_GET_VOLTAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] encodeDenoise(boolean enabled) {
|
public byte[] encodeDenoise(final boolean enabled) {
|
||||||
byte[] cmd = enabled ? COMMAND_DENOISE_ON : COMMAND_DENOISE_OFF;
|
final byte[] cmd = enabled ? COMMAND_DENOISE_ON : COMMAND_DENOISE_OFF;
|
||||||
return encodeCommand(cmd);
|
return encodeCommand(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -45,16 +45,16 @@ public class RoidmiIoThread extends BtClassicIoThread {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected byte[] parseIncoming(InputStream inputStream) throws IOException {
|
protected byte[] parseIncoming(InputStream inputStream) throws IOException {
|
||||||
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
|
final ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
boolean finished = false;
|
boolean finished = false;
|
||||||
byte[] incoming = new byte[1];
|
final byte[] incoming = new byte[1];
|
||||||
|
|
||||||
while (!finished) {
|
while (!finished) {
|
||||||
inputStream.read(incoming);
|
inputStream.read(incoming);
|
||||||
msgStream.write(incoming);
|
msgStream.write(incoming);
|
||||||
|
|
||||||
byte[] arr = msgStream.toByteArray();
|
final byte[] arr = msgStream.toByteArray();
|
||||||
if (arr.length > HEADER.length) {
|
if (arr.length > HEADER.length) {
|
||||||
int expectedLength = HEADER.length + TRAILER.length + arr[HEADER.length] + 2;
|
int expectedLength = HEADER.length + TRAILER.length + arr[HEADER.length] + 2;
|
||||||
if (arr.length == expectedLength) {
|
if (arr.length == expectedLength) {
|
||||||
@@ -63,7 +63,7 @@ public class RoidmiIoThread extends BtClassicIoThread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] msgArray = msgStream.toByteArray();
|
final byte[] msgArray = msgStream.toByteArray();
|
||||||
LOG.debug("Packet: " + GB.hexdump(msgArray, 0, msgArray.length));
|
LOG.debug("Packet: " + GB.hexdump(msgArray, 0, msgArray.length));
|
||||||
return msgArray;
|
return msgArray;
|
||||||
}
|
}
|
||||||
|
@@ -54,7 +54,7 @@ public abstract class RoidmiProtocol extends GBDeviceProtocol {
|
|||||||
public abstract byte[] packetTrailer();
|
public abstract byte[] packetTrailer();
|
||||||
|
|
||||||
public byte[] encodeCommand(byte... params) {
|
public byte[] encodeCommand(byte... params) {
|
||||||
byte[] cmd = new byte[packetHeader().length + packetTrailer().length + params.length + 2];
|
final byte[] cmd = new byte[packetHeader().length + packetTrailer().length + params.length + 2];
|
||||||
|
|
||||||
for (int i = 0; i < packetHeader().length; i++)
|
for (int i = 0; i < packetHeader().length; i++)
|
||||||
cmd[i] = packetHeader()[i];
|
cmd[i] = packetHeader()[i];
|
||||||
@@ -79,13 +79,13 @@ public abstract class RoidmiProtocol extends GBDeviceProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] frequencyToBytes(float frequency) {
|
public byte[] frequencyToBytes(float frequency) {
|
||||||
byte[] res = new byte[2];
|
final byte[] res = new byte[2];
|
||||||
String format = String.format(Locale.getDefault(), "%04d", (int) (10.0f * frequency));
|
final String format = String.format(Locale.ROOT, "%04d", (int) (10.0f * frequency));
|
||||||
try {
|
try {
|
||||||
res[0] = (byte) (Integer.parseInt(format.substring(0, 2), 16) & 255);
|
res[0] = (byte) (Integer.parseInt(format.substring(0, 2), 16) & 255);
|
||||||
res[1] = (byte) (Integer.parseInt(format.substring(2), 16) & 255);
|
res[1] = (byte) (Integer.parseInt(format.substring(2), 16) & 255);
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error(e.getMessage());
|
LOG.error("Failed to format frequency {}", frequency, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
@@ -70,7 +70,7 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport {
|
|||||||
LOG.error("Failed to get Roidmi infos after 6 tries");
|
LOG.error("Failed to get Roidmi infos after 6 tries");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Failed to get Roidmi infos", e);
|
LOG.error("Failed to get Roidmi infos", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,13 +91,17 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GBDeviceProtocol createDeviceProtocol() {
|
protected GBDeviceProtocol createDeviceProtocol() {
|
||||||
if (getDevice().getType() == DeviceType.ROIDMI) {
|
final DeviceType deviceType = getDevice().getType();
|
||||||
return new Roidmi1Protocol(getDevice());
|
|
||||||
} else if (getDevice().getType() == DeviceType.ROIDMI3) {
|
switch(deviceType) {
|
||||||
return new Roidmi3Protocol(getDevice());
|
case ROIDMI:
|
||||||
|
return new Roidmi1Protocol(getDevice());
|
||||||
|
case ROIDMI3:
|
||||||
|
return new Roidmi3Protocol(getDevice());
|
||||||
|
default:
|
||||||
|
LOG.error("Unsupported device type {} with key = {}", deviceType, deviceType.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.error("Unsupported device type with key = " + getDevice().getType().getKey());
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,8 +109,8 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport {
|
|||||||
public void onSendConfiguration(final String config) {
|
public void onSendConfiguration(final String config) {
|
||||||
LOG.debug("onSendConfiguration " + config);
|
LOG.debug("onSendConfiguration " + config);
|
||||||
|
|
||||||
RoidmiIoThread roidmiIoThread = getDeviceIOThread();
|
final RoidmiIoThread roidmiIoThread = getDeviceIOThread();
|
||||||
RoidmiProtocol roidmiProtocol = (RoidmiProtocol) getDeviceProtocol();
|
final RoidmiProtocol roidmiProtocol = (RoidmiProtocol) getDeviceProtocol();
|
||||||
|
|
||||||
switch (config) {
|
switch (config) {
|
||||||
case RoidmiConst.ACTION_GET_LED_COLOR:
|
case RoidmiConst.ACTION_GET_LED_COLOR:
|
||||||
|
Reference in New Issue
Block a user