Skip to content

Commit

Permalink
Merge pull request #167 from YUKAI/release/1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
sagiii committed Dec 24, 2015
2 parents 28bdf61 + b27fce4 commit 963ec08
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

```groovy
dependencies {
compile 'com.uxxu.konashi:konashi-android-sdk:1.0.1'
compile 'com.uxxu.konashi:konashi-android-sdk:1.1.0'
}
```

Expand Down
4 changes: 2 additions & 2 deletions konashi-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'groovyx.grooid.groovy-android'

def versionMajor = 1
def versionMinor = 0
def versionPatch = 1
def versionMinor = 1
def versionPatch = 0
def versionBuild = 0

def sdkVersionCode = versionMajor * 1000 + versionMinor * 100 + versionPatch * 10 + versionBuild
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

import com.uxxu.konashi.lib.dispatcher.DispatcherContainer;
import com.uxxu.konashi.lib.util.KonashiUtils;
import com.uxxu.konashi.lib.util.SpiUtils;
import com.uxxu.konashi.lib.util.UartUtils;

import org.jdeferred.DoneCallback;

import java.util.UUID;

import info.izumin.android.bletia.Bletia;
Expand Down Expand Up @@ -69,9 +72,17 @@ public void onCharacteristicChanged(Bletia bletia, BluetoothGattCharacteristic c
mDispatcherContainer.getUartDispatcher().onDone(characteristic);
mEmitter.emitUpdateUartRx(mManager, UartUtils.removeLengthByte(characteristic.getValue()));
} else if (KonashiUUID.SPI_NOTIFICATION_UUID.equals(uuid)) {
mEmitter.emitUpdateSpiMiso(mManager, characteristic.getValue());
mManager.spiRead().then(new DoneCallback<BluetoothGattCharacteristic>() {
@Override
public void onDone(BluetoothGattCharacteristic result) {
mEmitter.emitUpdateSpiMiso(mManager, SpiUtils.getDataFromResult(result.getValue()));
}
});
} else if (KonashiUUID.HARDWARE_LOW_BAT_NOTIFICATION_UUID.equals(uuid)) {
mEmitter.emitUpdateBatteryLevel(mManager, KonashiUtils.getBatteryLevel(characteristic));
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ public void run() {
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.uxxu.konashi.lib.util;

import android.bluetooth.BluetoothGattCharacteristic;

import com.uxxu.konashi.lib.Konashi;

/**
Expand Down Expand Up @@ -42,4 +44,13 @@ public static boolean isTooLongData(int length) {
public static boolean isTooShortData(int length) {
return length <= 0;
}

public static byte[] getDataFromResult(byte[] result) {
byte data[] = new byte[result.length];
for(int i = 0; i< result.length; i++) {
data[i] = (byte)((result[i] & 0xff));
}

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ class SpiUtilsSpec extends Specification {
[0] as byte[] | [] as byte[]
[4, 0x74, 0x65, 0x73, 0x74] as byte[] | [0x74, 0x65, 0x73, 0x74] as byte[]
}

def ".getDataFromResult()"() {

expect:
SpiUtils.getDataFromResult(data) == result

where:
data | result
[] as byte[] | [] as byte[]
[0x74, 0x65, 0x73, 0x74] as byte[] | [0x74, 0x65, 0x73, 0x74] as byte[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,6 @@ public Promise<BluetoothGattCharacteristic, BletiaException, Void> pipeDone(Blue
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) );
}
mResultText.setText(Arrays.toString(data));
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Expand Down Expand Up @@ -174,7 +158,7 @@ public void onUpdateUartRx(KonashiManager manager, byte[] value) {

@Override
public void onUpdateSpiMiso(KonashiManager manager, byte[] value) {
Log.d("onUpdateSpiMiso()", Arrays.toString(value));
mResultText.setText(Arrays.toString(value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public void onNavigationDrawerItemSelected(int position) {
case 4:
fragment = new CommunicationFragment();
break;
case 5:
fragment = new SpiFragment();
break;
}
if (fragment != null) {
getFragmentManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
PioFragment.TITLE,
PwmFragment.TITLE,
AioFragment.TITLE,
CommunicationFragment.TITLE
CommunicationFragment.TITLE,
SpiFragment.TITLE
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package com.uxxu.konashi.test_all_functions;

import android.app.Fragment;
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import com.uxxu.konashi.lib.Konashi;
import com.uxxu.konashi.lib.KonashiListener;
import com.uxxu.konashi.lib.KonashiManager;

import org.jdeferred.DoneCallback;
import org.jdeferred.DonePipe;
import org.jdeferred.FailCallback;
import org.jdeferred.Promise;

import java.util.Arrays;

import info.izumin.android.bletia.BletiaException;

/**
* Created by e10dokup on 12/16/15
*/
public final class SpiFragment extends Fragment {
public static final String TITLE = "SPI";

private KonashiManager mKonashiManager;

private Switch mSpiSwitch;
private Spinner mSpiSpeedSpinner;
private EditText mSpiDataEditText;
private Button mSpiDataSendButton;
private EditText mSpiResultEditText;
private Button mSpiResultClearButton;

private byte[] mValue;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(TITLE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spi, container, false);

initSpiViews(view);

return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mKonashiManager = Konashi.getManager();
mKonashiManager.addListener(mKonashiListener);
}

@Override
public void onDestroy() {
mKonashiManager.removeListener(mKonashiListener);
super.onDestroy();
}

private void initSpiViews(final View parent) {
mSpiSpeedSpinner = (Spinner) parent.findViewById(R.id.spiSpeedSpinner);
mSpiSpeedSpinner.setSelection(1);
mSpiSpeedSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
resetSpi();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

mSpiSwitch = (Switch) parent.findViewById(R.id.spiSwitch);
mSpiSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
resetSpi();
}
});

mSpiDataEditText = (EditText) parent.findViewById(R.id.spiDataEditText);

mSpiDataSendButton = (Button) parent.findViewById(R.id.spiDataSendButton);
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) );
}
mSpiResultEditText.setText(Arrays.toString(data));
}
})
.fail(new FailCallback<BletiaException>() {
@Override
public void onFail(BletiaException result) {
Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});

mSpiResultEditText = (EditText) parent.findViewById(R.id.spiResultEditText);

mSpiResultClearButton = (Button) parent.findViewById(R.id.spiResultClearButton);
mSpiResultClearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSpiResultEditText.setText("");
}
});
setEnableSpiViews(false);
}

private void resetSpi() {
if (!mKonashiManager.isReady()) {
return;
}
if (mSpiSwitch.isChecked()) {
int i = mSpiSpeedSpinner.getSelectedItemPosition();
String[] labels = getResources().getStringArray(R.array.spi_speeds_labels);
String label = labels[i];
int speed = Utils.spiLabelToValue(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();
}
});
} 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);
}
});
}
}


private void setEnableSpiViews(boolean enable) {
mSpiSpeedSpinner.setEnabled(enable);
mSpiDataEditText.setEnabled(enable);
mSpiDataSendButton.setEnabled(enable);
}

private final KonashiListener mKonashiListener = new KonashiListener() {
@Override public void onConnect(KonashiManager manager) {}
@Override public void onDisconnect(KonashiManager manager) {}
@Override public void onError(KonashiManager manager, BletiaException e) {}
@Override public void onUpdatePioOutput(KonashiManager manager, int value) {}
@Override public void onUpdateUartRx(KonashiManager manager, byte[] value) {}

@Override
public void onUpdateSpiMiso(KonashiManager manager, byte[] value) {
mValue = value;
mSpiResultEditText.append(new String(mValue));
}

@Override public void onUpdateBatteryLevel(KonashiManager manager, int level) {}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,26 @@ public TableRow build() {
return tableRow;
}
}

public static int spiLabelToValue(String spiLabel) {
if (spiLabel.equals("200K")) {
return Konashi.SPI_SPEED_200K;
}
if (spiLabel.equals("500K")) {
return Konashi.SPI_SPEED_500K;
}
if (spiLabel.equals("1M")) {
return Konashi.SPI_SPEED_1M;
}
if (spiLabel.equals("2M")) {
return Konashi.SPI_SPEED_2M;
}
if (spiLabel.equals("3M")) {
return Konashi.SPI_SPEED_3M;
}
if (spiLabel.equals("6M")) {
return Konashi.SPI_SPEED_6M;
}
return 0;
}
}
Loading

0 comments on commit 963ec08

Please sign in to comment.