-
-
Save JpEncausse/7d4766cb2b2207a49ab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.encausse.metaware.metadice; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.CompoundButton; | |
import android.widget.Switch; | |
import android.widget.ImageView; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.mbientlab.metawear.AsyncOperation; | |
import com.mbientlab.metawear.Message; | |
import com.mbientlab.metawear.RouteManager; | |
import com.mbientlab.metawear.UnsupportedModuleException; | |
import com.mbientlab.metawear.app.ModuleFragmentBase; | |
import com.mbientlab.metawear.app.help.HelpOptionAdapter; | |
import com.mbientlab.metawear.DataSignal; | |
import com.mbientlab.metawear.data.CartesianFloat; | |
import com.mbientlab.metawear.processor.Comparison; | |
import com.mbientlab.metawear.module.DataProcessor; | |
import com.mbientlab.metawear.module.Bmi160Accelerometer ; | |
import com.mbientlab.metawear.module.IBeacon; | |
import com.mbientlab.metawear.module.Led; | |
import com.mbientlab.metawear.module.Settings; | |
import java.util.Map; | |
public class DiceFragment extends ModuleFragmentBase { | |
private static final String STREAM_KEY = "dice_stream"; | |
protected final AsyncOperation.CompletionHandler<RouteManager> accelXYZStreamManager = new AsyncOperation.CompletionHandler<RouteManager>() { | |
@Override | |
public void success(RouteManager result) { | |
accelRouteManager = result; | |
result.subscribe(STREAM_KEY+"_accel", new RouteManager.MessageHandler() { | |
@Override | |
public void process(Message message) { | |
final CartesianFloat axes = message.getData(CartesianFloat.class); | |
if (axes.x() < -0.8){ | |
setDice(5); // A | |
} else if (axes.x() > 0.8){ | |
setDice(3); // B | |
} else if (axes.y() < -0.8){ | |
setDice(4); // C | |
} else if (axes.y() > 0.8){ | |
setDice(2); // D | |
} else if (axes.z() < -0.8){ | |
setDice(1); // E | |
} else if (axes.z() > 0.8){ | |
setDice(6); // F | |
} | |
} | |
}); | |
} | |
}; | |
private int previous = 0; | |
private long timemillis = 0; | |
private void setDice(int value) { | |
if (value == previous) return; previous = value; | |
ImageView image = (ImageView) getView().findViewById(R.id.imageDice); | |
switch(value) { | |
case 1 : { image.setImageResource(R.mipmap.dice1); break; } | |
case 2 : { image.setImageResource(R.mipmap.dice2); break; } | |
case 3 : { image.setImageResource(R.mipmap.dice3); break; } | |
case 4 : { image.setImageResource(R.mipmap.dice4); break; } | |
case 5 : { image.setImageResource(R.mipmap.dice5); break; } | |
case 6 : { image.setImageResource(R.mipmap.dice6); break; } | |
} | |
timemillis = System.currentTimeMillis(); | |
new android.os.Handler().postDelayed( new Runnable() { public void run() { | |
if (System.currentTimeMillis() - timemillis >= 300){ | |
sendHTTPRequest(previous+""); | |
} | |
}}, 300); | |
} | |
public DiceFragment() { | |
super(R.string.navigation_fragment_dice); | |
} | |
@Override | |
protected void boardReady() throws UnsupportedModuleException { | |
settingsModule = mwBoard.getModule(Settings.class); | |
ledModule = mwBoard.getModule(Led.class); | |
iBeaconModule = mwBoard.getModule(IBeacon.class); | |
iBeaconModule.readConfiguration().onComplete(readConfigHandler); | |
accelModule = mwBoard.getModule(Bmi160Accelerometer.class); | |
} | |
@Override | |
protected void fillHelpOptionAdapter(HelpOptionAdapter adapter) { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
setRetainInstance(true); | |
View v = inflater.inflate(R.layout.fragment_dice, container, false); | |
return v; | |
} | |
@Override | |
public void onViewCreated(final View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
((Switch) view.findViewById(R.id.sample_control)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { | |
if (b) { | |
setupIBeacon(); | |
setupAccel(); | |
} else { | |
clean(); | |
mwBoard.removeRoutes(); | |
} | |
} | |
}); | |
} | |
// ------------------------------------------ | |
// LED | |
// ------------------------------------------ | |
private Led ledModule = null; | |
private Settings settingsModule = null; | |
// ------------------------------------------ | |
// Accelerometer | |
// ------------------------------------------ | |
private Bmi160Accelerometer accelModule = null; | |
private RouteManager accelRouteManager = null; | |
protected void setupAccel() { | |
Log.i("ACCEL", "ACCEL SETUP"); | |
accelModule.setOutputDataRate(100.f); | |
accelModule.setAxisSamplingRange(4.f); | |
AsyncOperation<RouteManager> xyzManagerResult = accelModule.routeData().fromAxes().stream(STREAM_KEY + "_accel").commit(); | |
xyzManagerResult.onComplete(accelXYZStreamManager); | |
accelModule.routeData().fromYAxis().split() | |
.branch().process(new Comparison(Comparison.Operation.LTE, -0.8)).monitor(new DiceHandler((short)4)) | |
.branch().process(new Comparison(Comparison.Operation.GTE, 0.8)).monitor(new DiceHandler((short)2)) | |
.end().commit(); | |
accelModule.routeData().fromXAxis().split() | |
.branch().process(new Comparison(Comparison.Operation.LTE, -0.8)).monitor(new DiceHandler((short)5)) | |
.branch().process(new Comparison(Comparison.Operation.GTE, 0.8)).monitor(new DiceHandler((short)3)) | |
.end().commit(); | |
AsyncOperation<RouteManager> routeManagerResult = | |
accelModule.routeData().fromZAxis().split() | |
.branch().process(new Comparison(Comparison.Operation.LTE, -0.8)).monitor(new DiceHandler((short)1)) | |
.branch().process(new Comparison(Comparison.Operation.GTE, 0.8)).monitor(new DiceHandler((short)6)) | |
.end().commit(); | |
routeManagerResult.onComplete(new AsyncOperation.CompletionHandler<RouteManager>() { | |
@Override | |
public void success(RouteManager result) { | |
accelModule.enableAxisSampling(); | |
accelModule.start(); | |
} | |
}); | |
} | |
protected void clean() { | |
if (accelModule != null) { | |
accelModule.stop(); | |
accelModule = null; | |
} | |
} | |
// ------------------------------------------ | |
// iBeacon | |
// ------------------------------------------ | |
private IBeacon iBeaconModule; | |
private IBeacon.Configuration ibeaconResult; | |
private AsyncOperation.CompletionHandler<IBeacon.Configuration> readConfigHandler = new AsyncOperation.CompletionHandler<IBeacon.Configuration>() { | |
@Override | |
public void success(IBeacon.Configuration result) { | |
ibeaconResult = result; | |
} | |
}; | |
protected void setupIBeacon(){ | |
settingsModule.configure().setAdInterval((short) 200, (byte) 2); | |
/* | |
ledModule.configureColorChannel(Led.ColorChannel.BLUE) | |
.setRiseTime((short) 0).setPulseDuration((short) 1000) | |
.setRepeatCount((byte) 2).setHighTime((short) 500) | |
.setHighIntensity((byte) 16).setLowIntensity((byte) 16) | |
.commit(); | |
ledModule.play(true); | |
*/ | |
iBeaconModule.enable(); | |
if (ibeaconResult == null) return; | |
iBeaconModule.configure() | |
.setUUID(ibeaconResult.adUuid()) | |
.setMajor((short) 654) | |
.setMinor((short) 0) | |
.setAdPeriod(ibeaconResult.adPeriod()) | |
.setRxPower(ibeaconResult.rxPower()) | |
.setTxPower(ibeaconResult.txPower()) | |
.commit(); | |
} | |
protected class DiceHandler implements DataSignal.ActivityHandler { | |
private short side = 0; | |
public DiceHandler(short side){ | |
this.side = side; | |
} | |
@Override | |
public void onSignalActive(Map<String, DataProcessor> processors, DataSignal.DataToken token) { | |
Log.i("SIGNAL", "ACTIVE"); | |
IBeacon.ConfigEditor editor = iBeaconModule.configure(); | |
editor.setMinor(side).commit(); | |
settingsModule.startAdvertisement(); | |
/* | |
Led.ColorChannel color = Led.ColorChannel.RED; | |
if (side == 1 || side == 6) { color = Led.ColorChannel.BLUE; } | |
if (side == 5 || side == 3) { color = Led.ColorChannel.GREEN; } | |
ledModule.stop(false); | |
ledModule.configureColorChannel(color) | |
.setRiseTime((short) 0).setPulseDuration((short) 400) | |
.setRepeatCount((byte) 2).setHighTime((short) 200) | |
.setHighIntensity((byte) 16).setLowIntensity((byte) 16) | |
.commit(); | |
ledModule.play(true); | |
*/ | |
} | |
} | |
// ------------------------------------------ | |
// HTTP Request | |
// ------------------------------------------ | |
public static final String TAG = "DICE"; | |
private RequestQueue queue = null; | |
protected void sendHTTPRequest(String value) { | |
if (queue == null){ queue = Volley.newRequestQueue(getContext()); } | |
queue.cancelAll(TAG); | |
StringRequest request = new StringRequest(Request.Method.GET, "http://192.168.1.104:8080/sarah/boardgame?square="+value, new com.android.volley.Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
/* ok */ | |
} | |
}, new com.android.volley.Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
/* error */ | |
} | |
}); | |
request.setTag(TAG); | |
queue.add(request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment