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