Skip to content

Commit

Permalink
Hotfix for NEP on SpiFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
kiryuxxu committed Jan 15, 2016
1 parent ab86f4c commit 9284fe2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 62 deletions.
150 changes: 88 additions & 62 deletions app/src/main/java/com/uxxu/konashi/inspector/android/SpiFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Fragment;
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -100,41 +101,47 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mSpiDataSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.LOW)
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.spiWrite(mSpiDataEditText.getText().toString().getBytes());
}
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.HIGH);
}
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.spiRead();
}
})
.then(new DoneCallback<BluetoothGattCharacteristic>() {
@Override
public void onDone(BluetoothGattCharacteristic result) {
byte data[] = new byte[result.getValue().length];
for (int i = 0; i < result.getValue().length; i++) {
data[i] = (byte) ((result.getValue()[i] & 0xff));

try {
mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.LOW)
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.spiWrite(mSpiDataEditText.getText().toString().getBytes());
}
mSpiResultEditText.setText(Arrays.toString(data));
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
}
});
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.HIGH);
}
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.spiRead();
}
})
.then(new DoneCallback<BluetoothGattCharacteristic>() {
@Override
public void onDone(BluetoothGattCharacteristic result) {
byte data[] = new byte[result.getValue().length];
for (int i = 0; i < result.getValue().length; i++) {
data[i] = (byte) ((result.getValue()[i] & 0xff));
}
mSpiResultEditText.setText(Arrays.toString(data));
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} catch (NullPointerException e) {
// TODO: hotfix for https://github.com/YUKAI/konashi-android-sdk/issues/170
noticeForNoSpiDevices();
}
}
});

Expand All @@ -150,6 +157,15 @@ public void onClick(View view) {
setEnableSpiViews(false);
}

private void noticeForNoSpiDevices() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), getString(R.string.message_spi_notSupported), Toast.LENGTH_SHORT).show();
}
});
}

private void resetSpi() {
if (!mKonashiManager.isReady()) {
return;
Expand All @@ -159,35 +175,45 @@ private void resetSpi() {
String[] labels = getResources().getStringArray(R.array.spiSpeedLabels);
String label = labels[i];
int speed = Utils.spiLabelToValue(getActivity(), label);
mKonashiManager.spiConfig(Konashi.SPI_MODE_ENABLE_CPOL0_CPHA0,
Konashi.SPI_BIT_ORDER_LITTLE_ENDIAN, speed)
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.pinMode(Konashi.PIO2, Konashi.OUTPUT);
}
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
setEnableSpiViews(true);
return mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.HIGH);
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
}
});
try {
mKonashiManager.spiConfig(Konashi.SPI_MODE_ENABLE_CPOL0_CPHA0,
Konashi.SPI_BIT_ORDER_LITTLE_ENDIAN, speed)
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
return mKonashiManager.pinMode(Konashi.PIO2, Konashi.OUTPUT);
}
})
.then(new DonePipe<BluetoothGattCharacteristic, BluetoothGattCharacteristic, BletiaException, Void>() {
@Override
public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(BluetoothGattCharacteristic result) {
setEnableSpiViews(true);
return mKonashiManager.digitalWrite(Konashi.PIO2, Konashi.HIGH);
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} catch (NullPointerException e) {
// TODO: hotfix for https://github.com/YUKAI/konashi-android-sdk/issues/170
noticeForNoSpiDevices();
}
} else {
mKonashiManager.spiConfig(Konashi.SPI_MODE_DISABLE, Konashi.SPI_BIT_ORDER_LITTLE_ENDIAN, Konashi.SPI_SPEED_1M)
.then(new DoneCallback<BluetoothGattCharacteristic>() {
@Override
public void onDone(BluetoothGattCharacteristic result) {
setEnableSpiViews(false);
}
});
try {
mKonashiManager.spiConfig(Konashi.SPI_MODE_DISABLE, Konashi.SPI_BIT_ORDER_LITTLE_ENDIAN, Konashi.SPI_SPEED_1M)
.then(new DoneCallback<BluetoothGattCharacteristic>() {
@Override
public void onDone(BluetoothGattCharacteristic result) {
setEnableSpiViews(false);
}
});
} catch (NullPointerException e) {
// TODO: hotfix for https://github.com/YUKAI/konashi-android-sdk/issues/170
noticeForNoSpiDevices();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="title.receivedData">受信データ</string>
<string name="action.close">閉じる</string>
<string name="action.open">開く</string>
<string name="message.spi.notSupported">この konashi は SPI をサポートしていません。</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="message.noConnection">No konashi connection</string>
<string name="message.connected">Connected</string>
<string name="message.disconnected">Disconnected</string>
<string name="message.spi.notSupported">SPI is not supported on this konashi.</string>
<string name="title.aio" translatable="false">Analog Input</string>
<string name="title.battery">Battery</string>
<string name="title.communication" translatable="false">UART, I2C</string>
Expand Down

0 comments on commit 9284fe2

Please sign in to comment.