Android Open Source - MakeWithMotoSampleApp M A Kr






From Project

Back to project page MakeWithMotoSampleApp.

License

The source code is released under:

GNU General Public License

If you think the Android project MakeWithMotoSampleApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.makewithmoto.boards;
/*  w  w w .  j av a  2s .c  om*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.InvalidParameterException;
import java.util.Vector;

import com.makewithmoto.example.Application;

import android.app.Activity;
import android.util.Log;
import android_serialport_api.SerialPort;

public class MAKr {

  protected Application mApplication;
  protected Activity c;
  protected SerialPort mSerialPort;
  protected OutputStream mOutputStream;
  private InputStream mInputStream;
  private ReadThread mReadThread;

  Vector<MAKrListener> listeners = new Vector<MAKr.MAKrListener>();

  private static final String TAG = "SerialReader";
  private static final String MAKR_ENABLE = "/sys/class/makr/makr/5v_enable";

  private String receivedData;
  private boolean isStarted = false;

  public interface MAKrListener {
    public void onMessageReceived(String value);
    public void onRawDataReceived(final byte[] buffer, final int size);
  }

  public MAKr(Activity c) {
    this.c = c;
    mApplication = (Application) ((Activity) c).getApplication();

    try {
      mSerialPort = getSerialPort();
      mOutputStream = mSerialPort.getOutputStream();
      mInputStream = mSerialPort.getInputStream();

      /* Create a receiving thread */
      mReadThread = new ReadThread();
      mReadThread.start();
    } catch (SecurityException e) {
      // DisplayError(R.string.error_security);
    } catch (IOException e) {
      // DisplayError(R.string.error_unknown);
    } catch (InvalidParameterException e) {
      // DisplayError(R.string.error_configuration);
    }

  }
  
  

  public void start() {
    initializeMAKR();
    isStarted = true;
  }

  public void stop() {
    if (mReadThread != null)
      mReadThread.interrupt();
    closeSerialPort();
    mSerialPort = null;
  }

  private class ReadThread extends Thread {

    @Override
    public void run() {
      super.run();
      while (!isInterrupted()) {
        final int size;
        try {
          final byte[] buffer = new byte[64];
          if (mInputStream == null)
            return;
          size = mInputStream.read(buffer);
          if (size > 0) {

            for (final MAKrListener l : listeners) {
              l.onRawDataReceived(buffer, size);

              c.runOnUiThread(new Runnable() {
                public void run() {

                  receivedData = new String(buffer, 0, size);
                  
                  l.onMessageReceived(receivedData);
                }
              });

            }
          }
        } catch (IOException e) {
          e.printStackTrace();
          return;
        }
      }
    }
  }

  /*
   * private void DisplayError(int resourceId) { AlertDialog.Builder b = new
   * AlertDialog.Builder(c); b.setTitle("Error"); b.setMessage(resourceId);
   * b.setPositiveButton("OK", new OnClickListener() { public void
   * onClick(DialogInterface dialog, int which) { c.finish(); } }); b.show();
   * }
   */

  public boolean isEnabled() {
    boolean status = false;
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader(MAKR_ENABLE));
      String rv = br.readLine();
      if (rv.matches("on")) {
        return true;
      }
    } catch (FileNotFoundException e) {
      Log.e(TAG, e.getMessage());
    } catch (IOException e) {
      Log.e(TAG, e.getMessage());
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
        }
      }
    }
    return status;
  }

  /*
   * Turn on or off the device
   */
  public void enable(boolean value) {
    BufferedWriter writer = null;
    try {
      FileOutputStream fos = new FileOutputStream(MAKR_ENABLE);
      OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
      writer = new BufferedWriter(osw);
      if (value)
        writer.write("on\n");
      else
        writer.write("off\n");
    } catch (FileNotFoundException e) {
      Log.e(TAG, e.getMessage());
    } catch (IOException e) {
      Log.e(TAG, e.getMessage());
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException e) {
        }
      }
    }
  }

  public void initializeMAKR() {

    // enable the device
    if (!isEnabled()) {
      enable(true);
    }
  }

  public void writeSerial(String cmd) {

    try {
      cmd = cmd + "\n";
      mOutputStream.write(cmd.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  public void addListener(MAKrListener makrListener) {
    listeners.add(makrListener);
  }

  public void removeListener(MAKrListener makrListener) {
    listeners.remove(makrListener);
  }

  public void resume() {
    if (isStarted ) { 
      start();    
    }
  }
  
  public void pause() {
    if (isStarted ) { 
      stop();    
    }
  }

  
  
  
  /* Serial port stuff */ 
  public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException {
    if (mSerialPort == null) {
      String path = "/dev/ttyHSL2";
      int baudrate = 9600;

      /* Check parameters */
      if ( (path.length() == 0) || (baudrate == -1)) {
        throw new InvalidParameterException();
      }

      /* Open the serial port */
      mSerialPort = new SerialPort(new File(path), baudrate, 0);
    }
    return mSerialPort;
  }

  public void closeSerialPort() {
    if (mSerialPort != null) {
      mSerialPort.close();
      mSerialPort = null;
    }
  }
  
  
}




Java Source Code List

android_serialport_api.SerialPort.java
com.makewithmoto.boards.MAKr.java
com.makewithmoto.boards.MIOIOListener.java
com.makewithmoto.bt.BluetoothSerialService.java
com.makewithmoto.bt.BluetoothViewer.java
com.makewithmoto.bt.DeviceListActivity.java
com.makewithmoto.bt.SimpleBT.java
com.makewithmoto.example.ActivityBT.java
com.makewithmoto.example.ActivityMAKr.java
com.makewithmoto.example.ActivityMOIO.java
com.makewithmoto.example.Application.java
com.makewithmoto.example.LauncherActivity.java
com.makewithmoto.extras.AccelerometerManager.java
com.makewithmoto.extras.CustomSensorListener.java
com.makewithmoto.extras.CustomSensorManager.java
com.makewithmoto.extras.WhatIsRunningInterface.java
com.makewithmoto.extras.WhatIsRunning.java
com.makewithmoto.fragments.CameraFragment.java
com.makewithmoto.fragments.DebugFragment.java
com.makewithmoto.fragments.EmptyFragment.java
com.makewithmoto.fragments.GraphFragment.java
com.makewithmoto.fragments.ImageFragment.java
com.makewithmoto.fragments.Utils.java
com.makewithmoto.fragments.VideoPlayerFragment.java
com.makewithmoto.fragments.VideoTextureFragment.java
com.makewithmoto.views.CanvasUtils.java
com.makewithmoto.views.PlotView.java