kopia lustrzana https://github.com/sh123/codec2_talkie
rodzic
83cffeaeea
commit
ad6e79044b
|
@ -9,6 +9,8 @@ public final class PreferenceKeys {
|
||||||
public static String PORTS_USB_PARITY = "ports_usb_parity";
|
public static String PORTS_USB_PARITY = "ports_usb_parity";
|
||||||
public static String PORTS_USB_DTR = "ports_usb_dtr";
|
public static String PORTS_USB_DTR = "ports_usb_dtr";
|
||||||
public static String PORTS_USB_RTS = "ports_usb_rts";
|
public static String PORTS_USB_RTS = "ports_usb_rts";
|
||||||
|
public static String PORTS_USB_IS_PREFIX_ENABLED = "ports_usb_is_prefix_enabled";
|
||||||
|
public static String PORTS_USB_PREFIX = "ports_usb_prefix";
|
||||||
|
|
||||||
public static String PORTS_BT_CLIENT_NAME = "ports_bt_client_name";
|
public static String PORTS_BT_CLIENT_NAME = "ports_bt_client_name";
|
||||||
|
|
||||||
|
|
|
@ -53,4 +53,14 @@ public class TextTools {
|
||||||
}
|
}
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] hexStringToByteArray(String s) {
|
||||||
|
int len = s.length();
|
||||||
|
byte[] data = new byte[len / 2];
|
||||||
|
for (int i = 0; i < len; i += 2) {
|
||||||
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
|
||||||
|
+ Character.digit(s.charAt(i+1), 16));
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class TransportFactory {
|
||||||
|
|
||||||
switch (transportType) {
|
switch (transportType) {
|
||||||
case USB:
|
case USB:
|
||||||
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName());
|
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName(), context);
|
||||||
case BLUETOOTH:
|
case BLUETOOTH:
|
||||||
return new Bluetooth(BluetoothSocketHandler.getSocket(), BluetoothSocketHandler.getName());
|
return new Bluetooth(BluetoothSocketHandler.getSocket(), BluetoothSocketHandler.getName());
|
||||||
case TCP_IP:
|
case TCP_IP:
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
package com.radio.codec2talkie.transport;
|
package com.radio.codec2talkie.transport;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.preference.PreferenceManager;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.SerialTimeoutException;
|
import com.hoho.android.usbserial.driver.SerialTimeoutException;
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||||
|
import com.radio.codec2talkie.settings.PreferenceKeys;
|
||||||
|
import com.radio.codec2talkie.tools.TextTools;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class UsbSerial implements Transport {
|
public class UsbSerial implements Transport {
|
||||||
|
|
||||||
|
@ -13,9 +21,18 @@ public class UsbSerial implements Transport {
|
||||||
private final UsbSerialPort _usbPort;
|
private final UsbSerialPort _usbPort;
|
||||||
private final String _name;
|
private final String _name;
|
||||||
|
|
||||||
public UsbSerial(UsbSerialPort usbPort, String name) {
|
private final boolean _isPrefixEnabled;
|
||||||
|
private final byte[] _bytePrefix;
|
||||||
|
|
||||||
|
protected SharedPreferences _sharedPreferences;
|
||||||
|
|
||||||
|
public UsbSerial(UsbSerialPort usbPort, String name, Context context) {
|
||||||
_usbPort = usbPort;
|
_usbPort = usbPort;
|
||||||
_name = name;
|
_name = name;
|
||||||
|
_sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
_isPrefixEnabled = _sharedPreferences.getBoolean(PreferenceKeys.PORTS_USB_IS_PREFIX_ENABLED, false);
|
||||||
|
String prefix = _sharedPreferences.getString(PreferenceKeys.PORTS_USB_PREFIX, "");
|
||||||
|
_bytePrefix = TextTools.hexStringToByteArray(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -31,7 +48,15 @@ public class UsbSerial implements Transport {
|
||||||
@Override
|
@Override
|
||||||
public int write(byte[] data) throws IOException {
|
public int write(byte[] data) throws IOException {
|
||||||
try {
|
try {
|
||||||
_usbPort.write(data, TX_TIMEOUT);
|
if (_isPrefixEnabled) {
|
||||||
|
byte[] pkt = ByteBuffer.allocate(_bytePrefix.length + data.length)
|
||||||
|
.put(_bytePrefix)
|
||||||
|
.put(data)
|
||||||
|
.array();
|
||||||
|
_usbPort.write(pkt, TX_TIMEOUT);
|
||||||
|
} else {
|
||||||
|
_usbPort.write(data, TX_TIMEOUT);
|
||||||
|
}
|
||||||
return data.length;
|
return data.length;
|
||||||
} catch (SerialTimeoutException e) {
|
} catch (SerialTimeoutException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -192,6 +192,10 @@
|
||||||
<string name="usb_stop_bits_title">Serial stop bits</string>
|
<string name="usb_stop_bits_title">Serial stop bits</string>
|
||||||
<string name="usb_serial_title">USB serial settings</string>
|
<string name="usb_serial_title">USB serial settings</string>
|
||||||
<string name="usb_serial_summary">Set USB serial settings, such as speed, bits, parity, etc.</string>
|
<string name="usb_serial_summary">Set USB serial settings, such as speed, bits, parity, etc.</string>
|
||||||
|
<string name="usb_is_prefix_enabled_title">Enable USB packet prefix</string>
|
||||||
|
<string name="usb_is_prefix_enabled_summary">Prefix USB data with the HEX string for LoRA UART modems</string>
|
||||||
|
<string name="usb_prefix_title">USB packet prefix value as HEX string</string>
|
||||||
|
<string name="usb_prefix_summary">Prefix sent content with a hex string (so called transmission target in UART modems), e.g. C0FFEE</string>
|
||||||
|
|
||||||
<string name="app_audio_output_speaker_title">Play audio through the speaker</string>
|
<string name="app_audio_output_speaker_title">Play audio through the speaker</string>
|
||||||
<string name="app_audio_output_speaker_summary">Output incoming audio through the speaker</string>
|
<string name="app_audio_output_speaker_summary">Output incoming audio through the speaker</string>
|
||||||
|
|
|
@ -54,5 +54,21 @@
|
||||||
app:summary="@string/usb_rts_summary"
|
app:summary="@string/usb_rts_summary"
|
||||||
app:defaultValue="false">
|
app:defaultValue="false">
|
||||||
</CheckBoxPreference>
|
</CheckBoxPreference>
|
||||||
|
|
||||||
|
<CheckBoxPreference
|
||||||
|
app:key="ports_usb_is_prefix_enabled"
|
||||||
|
app:title="@string/usb_is_prefix_enabled_title"
|
||||||
|
app:summary="@string/usb_is_prefix_enabled_summary"
|
||||||
|
app:defaultValue="false">
|
||||||
|
</CheckBoxPreference>
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
<EditTextPreference
|
||||||
|
app:key="ports_usb_prefix"
|
||||||
|
app:title="@string/usb_prefix_title"
|
||||||
|
app:summary = "@string/usb_prefix_summary"
|
||||||
|
app:dependency="ports_usb_is_prefix_enabled"
|
||||||
|
app:defaultValue="C0FFEE">
|
||||||
|
</EditTextPreference>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
Ładowanie…
Reference in New Issue