-
-
Save JpEncausse/4aa0524cb3223fe91e58 to your computer and use it in GitHub Desktop.
Offline Accelerometer Log Fragment
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 com.mbientlab.metawear.app; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.content.pm.ActivityInfo; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.content.FileProvider; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import com.mbientlab.metawear.api.MetaWearController; | |
import com.mbientlab.metawear.api.Module; | |
import com.mbientlab.metawear.api.controller.Accelerometer; | |
import com.mbientlab.metawear.api.controller.Logging; | |
import com.mbientlab.metawear.api.util.BytesInterpreter; | |
import com.mbientlab.metawear.api.util.LoggingTrigger; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.Locale; | |
public class WhatsHomeFragment extends ModuleFragment { | |
private Logging loggingController; | |
// ==================================================================================== | |
// INNERCLASS: SENSOR | |
// ==================================================================================== | |
private ProgressDialog logDLProgress = null; | |
public abstract class WHSensor extends Logging.Callbacks { | |
private boolean ready; | |
private int totalEntries; | |
protected Logging.LogEntry firstEntry; | |
public boolean dataReady() { return ready; } | |
public void setupLogger() { | |
ready = false; | |
} | |
public abstract void stopSensors(); | |
public abstract void processData(double offset, Logging.LogEntry entry); | |
public abstract File[] saveDataToFile() throws IOException; | |
@Override | |
public void receivedTriggerId(byte triggerId) { | |
System.out.println(">>>>> receivedTriggerId " + triggerId); | |
loggingController.startLogging(); | |
Toast.makeText(getActivity(), R.string.label_lob_start_message, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void receivedLogEntry(Logging.LogEntry entry) { | |
System.out.println(">>>>> receivedLogEntry"); | |
if (firstEntry == null) { | |
firstEntry = entry; | |
} | |
processData(entry.offset(firstEntry) / 1000.0, entry); | |
} | |
@Override | |
public void receivedTotalEntryCount(int totalEntries) { | |
System.out.println(">>>>> receivedTotalEntryCount"); | |
if (logDLProgress == null || !logDLProgress.isShowing()) { | |
this.totalEntries= totalEntries; | |
logDLProgress= new ProgressDialog(getActivity()); | |
logDLProgress.setOwnerActivity(getActivity()); | |
logDLProgress.setTitle("Log Download"); | |
logDLProgress.setMessage("Downloading log..."); | |
logDLProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); | |
logDLProgress.setProgress(0); | |
logDLProgress.setMax(totalEntries); | |
logDLProgress.show(); | |
loggingController.downloadLog(totalEntries, (int) (totalEntries * 0.01)); | |
} | |
} | |
@Override | |
public void receivedDownloadProgress(int nEntriesLeft) { | |
System.out.println(">>>>> receivedDownloadProgress"+nEntriesLeft); | |
logDLProgress.setProgress(totalEntries - nEntriesLeft); | |
} | |
@Override | |
public void downloadCompleted() { | |
System.out.println(">>>>> downloadCompleted"); | |
if (logDLProgress.isShowing()) { | |
logDLProgress.dismiss(); | |
} | |
firstEntry = null; | |
ready = true; | |
loggingController.removeAllTriggers(); | |
startEmailIntent(); | |
} | |
} | |
// ==================================================================================== | |
// CLASS: MODULE FRAGMENT | |
// ==================================================================================== | |
@Override | |
public void controllerReady(MetaWearController mwController) { | |
loggingController = (Logging) mwController.getModuleController(Module.LOGGING); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); | |
super.onAttach(activity); | |
} | |
@Override | |
public void onPause() { | |
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); | |
super.onPause(); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_whatshome, container, false); | |
} | |
@Override | |
public void onViewCreated(final View view, Bundle savedInstanceState) { | |
((Button) view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
System.out.println(">>>>> SETUP LOGGER"); | |
if (mwMnger.controllerReady()) { | |
mwMnger.getCurrentController().addModuleCallback(sensor); | |
sensor.setupLogger(); | |
} else { | |
Toast.makeText(getActivity(), R.string.error_connect_board, Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
((Button) view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mwMnger.controllerReady()) { | |
System.out.println(">>>>> STOP SENSOR"); | |
mwMnger.getCurrentController().addModuleCallback(sensor); | |
sensor.stopSensors(); | |
if (sensor.dataReady()) { | |
System.out.println(">>>>> startEmailIntent"); | |
startEmailIntent(); | |
} else { | |
System.out.println(">>>>> readTotalEntryCount"); | |
loggingController.stopLogging(); | |
loggingController.readTotalEntryCount(); | |
} | |
} else { | |
Toast.makeText(getActivity(), R.string.error_connect_board, Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} | |
// ------------------------------------------ | |
// ACCELEROMETER | |
// ------------------------------------------ | |
private WHSensor sensor = new WHSensor(){ | |
private byte xAxisId = -1, yAxisId = -1, zAxisId = -1; | |
private ArrayList<double[]> xData = null, yData = null, zData = null; | |
private Accelerometer.SamplingConfig config; | |
@Override | |
public void stopSensors() { | |
System.out.println(">>>>> stopSensors "); | |
final Accelerometer accelController= (Accelerometer) mwMnger.getCurrentController().getModuleController(Module.ACCELEROMETER); | |
accelController.stopComponents(); | |
} | |
@Override | |
public void setupLogger() { | |
System.out.println(">>>>> setupLogger "); | |
super.setupLogger(); | |
xData= new ArrayList<>(); | |
yData= new ArrayList<>(); | |
zData= new ArrayList<>(); | |
xAxisId= -1; yAxisId= -1; zAxisId= -1; | |
loggingController.addTrigger(LoggingTrigger.ACCELEROMETER_X_AXIS); | |
loggingController.addTrigger(LoggingTrigger.ACCELEROMETER_Y_AXIS); | |
loggingController.addTrigger(LoggingTrigger.ACCELEROMETER_Z_AXIS); | |
final Accelerometer accelController= (Accelerometer) mwMnger.getCurrentController().getModuleController(Module.ACCELEROMETER); | |
config = accelController.enableXYZSampling(); | |
config.withFullScaleRange(Accelerometer.SamplingConfig.FullScaleRange.FSR_2G) | |
.withOutputDataRate(Accelerometer.SamplingConfig.OutputDataRate.ODR_50_HZ) | |
.withSilentMode(); | |
accelController.startComponents(); | |
} | |
@Override | |
public void receivedTriggerId(byte triggerId) { | |
if (xAxisId == -1) { xAxisId= triggerId; } | |
else if (yAxisId == -1) { yAxisId= triggerId; } | |
else { zAxisId= triggerId; super.receivedTriggerId(triggerId); } | |
} | |
@Override | |
public void processData(double offset, Logging.LogEntry entry) { | |
if (entry.triggerId() == xAxisId) { xData.add(new double[] { offset, BytesInterpreter.logBytesToGs(entry.data(), (byte) 0) }); } | |
else if (entry.triggerId() == yAxisId) { yData.add(new double[] { offset, BytesInterpreter.logBytesToGs(entry.data(), (byte) 0) }); } | |
else if (entry.triggerId() == zAxisId) { zData.add(new double[] { offset, BytesInterpreter.logBytesToGs(entry.data(), (byte) 0) }); } | |
} | |
@Override | |
public File[] saveDataToFile() throws IOException { | |
System.out.println(">>>>> saveDataToFile "); | |
File file = getActivity().getFileStreamPath("Accelerometer_data.csv"); | |
FileOutputStream fos = new FileOutputStream(file); | |
for (double[] it : xData) { fos.write(String.format(Locale.US, "%.3f,%.3f,%.3f,%.3f%n", it[0], it[1], 0.0f, 0.0f).getBytes()); } fos.write("\n".getBytes()); | |
for (double[] it : yData) { fos.write(String.format(Locale.US, "%.3f,%.3f,%.3f,%.3f%n", it[0], 0.0f, it[1], 0.0f).getBytes()); } fos.write("\n".getBytes()); | |
for (double[] it : zData) { fos.write(String.format(Locale.US, "%.3f,%.3f,%.3f,%.3f%n", it[0], 0.0f, 0.0f, it[1]).getBytes()); } | |
fos.close(); | |
return new File[]{ file }; | |
} | |
}; | |
// ------------------------------------------ | |
// UTILITY | |
// ------------------------------------------ | |
private void startEmailIntent() { | |
mwMnger.getCurrentController().removeModuleCallback(sensor); | |
ArrayList<Uri> fileUris= new ArrayList<>(); | |
try { | |
for(File it: sensor.saveDataToFile()) { | |
fileUris.add(FileProvider.getUriForFile(getActivity(), "com.mbientlab.metawear.app.fileprovider", it)); | |
} | |
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); | |
intent.setType("text/plain"); | |
intent.putExtra(Intent.EXTRA_SUBJECT, String.format(Locale.US, "Logged %s data - %tY-%<tm-%<tdT%<tH-%<tM-%<tS", sensor.toString(), Calendar.getInstance().getTime())); | |
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris); | |
startActivity(Intent.createChooser(intent, "Send email...")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment