Skip to content

Instantly share code, notes, and snippets.

@sabas1080
Forked from pavi2410/Arduino.java
Created February 23, 2019 17:35

Revisions

  1. @pavi2410 pavi2410 revised this gist Jul 5, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Arduino.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Copyright 2017 Pavitra, All rights reserved
    // Copyright 2017-2018 Pavitra, All rights reserved
    // Released under the Apache License, Version 2.0
    // http://www.apache.org/licenses/LICENSE-2.0

  2. @pavi2410 pavi2410 revised this gist Jul 5, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,7 @@
    package com.pavitra;

    import android.content.Context;
    import android.app.Activity;
    import android.util.Log;
    import android.widget.Toast;

    import com.physicaloid.lib.*;

  3. @pavi2410 pavi2410 revised this gist Jul 5, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -68,17 +68,19 @@ public void BaudRate(int baudRate) {
    public void Read() {
    byte[] buf = new byte[256];
    boolean success = true;
    String data;
    String data = "";

    if (mPhysicaloid.read(buf) > 0) {
    try {
    data = new String(buf, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    success = false;
    data = "";
    Log.e(LOG_TAG, e.getMessage());
    }
    } else {
    success = false;
    }

    AfterRead(success, data);
    }

  4. @pavi2410 pavi2410 revised this gist Jul 5, 2018. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@

    import java.io.UnsupportedEncodingException;

    @DesignerComponent(version = 5,
    @DesignerComponent(version = 6,
    description = "Arduino USB Serial Extension by Pavitra.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    @@ -67,19 +67,19 @@ public void BaudRate(int baudRate) {
    @SimpleFunction(description = "Read from Serial")
    public void Read() {
    byte[] buf = new byte[256];
    boolean success = true;
    String data;

    int readSize = 0;
    readSize = mPhysicaloid.read(buf);

    if (readSize > 0) {
    if (mPhysicaloid.read(buf) > 0) {
    try {
    AfterRead(true, new String(buf, "UTF-8"));
    return;
    data = new String(buf, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    success = false;
    data = "";
    Log.e(LOG_TAG, e.getMessage());
    }
    }
    AfterRead(false, "");
    AfterRead(success, data);
    }

    @SimpleFunction(description = "Write Data to Serial")
  5. @pavi2410 pavi2410 revised this gist Jul 5, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Arduino.java
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@

    import java.io.UnsupportedEncodingException;

    @DesignerComponent(version = 4,
    @DesignerComponent(version = 5,
    description = "Arduino USB Serial Extension by Pavitra.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
  6. @pavi2410 pavi2410 revised this gist Jun 14, 2018. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,13 @@
    import com.google.appinventor.components.runtime.*;
    import com.google.appinventor.components.common.*;

    import java.io.UnsupportedEncodingException;

    @DesignerComponent(version = 4,
    description = "Arduino USB Serial Extension by Pavitra.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "images/extension.png")
    description = "Arduino USB Serial Extension by Pavitra.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "images/extension.png")
    @SimpleObject(external = true)
    @UsesLibraries(libraries = "physicaloid.jar")
    public class Arduino extends AndroidNonvisibleComponent implements Component {
    @@ -50,7 +52,7 @@ public boolean Open() {
    }

    @SimpleFunction
    public void Close() {
    public boolean Close() {
    Log.d(LOG_TAG, "Closing connection");
    return mPhysicaloid.close();
    }
    @@ -70,10 +72,14 @@ public void Read() {
    readSize = mPhysicaloid.read(buf);

    if (readSize > 0) {
    AfterRead(true, new String(buf, "UTF-8"));
    } else {
    AfterRead(false, "");
    try {
    AfterRead(true, new String(buf, "UTF-8"));
    return;
    } catch (UnsupportedEncodingException e) {
    Log.e(LOG_TAG, e.getMessage());
    }
    }
    AfterRead(false, "");
    }

    @SimpleFunction(description = "Write Data to Serial")
  7. @pavi2410 pavi2410 revised this gist Jun 14, 2018. 1 changed file with 76 additions and 113 deletions.
    189 changes: 76 additions & 113 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -1,133 +1,96 @@
    /*
    Copyright 2017 Pavitra, All rights reserved
    Released under the Apache License, Version 2.0
    http://www.apache.org/licenses/LICENSE-2.0
    */
    // Copyright 2017 Pavitra, All rights reserved
    // Released under the Apache License, Version 2.0
    // http://www.apache.org/licenses/LICENSE-2.0

    package com.pavitra;

    import android.content.Context;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    //import java.io.UnsupportedEncodingException;

    import com.physicaloid.lib.*;

    import com.google.appinventor.components.annotations.*;
    import com.google.appinventor.components.runtime.*;
    import com.google.appinventor.components.common.*;

    @DesignerComponent(version = Arduino.VERSION,
    description = "Arduino USB Serial Extension by Pavitra.\nVersion: " + Arduino.VERSION,
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "images/extension.png")
    @DesignerComponent(version = 4,
    description = "Arduino USB Serial Extension by Pavitra.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "images/extension.png")
    @SimpleObject(external = true)
    @UsesLibraries(libraries = "physicaloid.jar")

    public class Arduino extends AndroidNonvisibleComponent implements Component {
    public static final int VERSION = 3;
    private ComponentContainer container;
    private Context context;
    private final Activity activity;
    private static final String LOG_TAG = "Arduino USB Serial Extension";
    private boolean suppressToast = false;
    private String str;
    private int baudRate = 9600;

    Physicaloid mPhysicaloid;

    public Arduino(ComponentContainer container) {
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
    activity = (Activity) container.$context();
    Log.d(LOG_TAG, "Arduino USB Serial Extension Created");
    }

    @SimpleFunction(description="Initialize")
    public void Initialize() {
    mPhysicaloid = new Physicaloid(this.context);

    showToast("Arduino Initialised");
    }

    @SimpleFunction(description="Open connection. Default baud rate is 9600 bps")
    public void Open() {
    mPhysicaloid.open();

    showToast("Opened");
    }

    @SimpleFunction(description="Close connection")
    public void Close() {
    mPhysicaloid.close();

    showToast("Closed");
    }

    @SimpleFunction(description="Baud Rate")
    public void BaudRate(int baudRate) {
    this.baudRate = baudRate;
    mPhysicaloid.setBaudrate(baudRate);

    showToast("Closed");
    }

    @SimpleFunction(description="Read from Serial")
    public String Read() {
    byte[] buf = new byte[256];

    int readSize=0;
    readSize = mPhysicaloid.read(buf, buf.length);

    if(readSize>0) {
    try {
    str = new String(buf);
    } catch (RuntimeException e) {
    showToast(e.toString());
    }
    private static final String LOG_TAG = "Arduino USB Serial Extension";

    private Context context;

    Physicaloid mPhysicaloid;

    private int baudRate = 9600;

    public Arduino(ComponentContainer container) {
    super(container.$form());
    context = container.$context();
    Log.d(LOG_TAG, "Created");
    }

    @SimpleFunction
    public void Initialize() {
    mPhysicaloid = new Physicaloid(context);
    Log.d(LOG_TAG, "Initialized");
    }

    @SimpleFunction
    public boolean Open() {
    Log.d(LOG_TAG, "Opening connection");
    return mPhysicaloid.open();
    }

    @SimpleFunction
    public void Close() {
    Log.d(LOG_TAG, "Closing connection");
    return mPhysicaloid.close();
    }

    @SimpleFunction(description = "Default baud rate is 9600 bps")
    public void BaudRate(int baudRate) {
    this.baudRate = baudRate;
    mPhysicaloid.setBaudrate(baudRate);
    Log.d(LOG_TAG, "Baud Rate: " + baudRate);
    }

    @SimpleFunction(description = "Read from Serial")
    public void Read() {
    byte[] buf = new byte[256];

    int readSize = 0;
    readSize = mPhysicaloid.read(buf);

    if (readSize > 0) {
    AfterRead(true, new String(buf, "UTF-8"));
    } else {
    AfterRead(false, "");
    }
    }

    @SimpleFunction(description = "Write Data to Serial")
    public void Write(String writeData) {
    if (!writeData.isEmpty()) {
    byte[] buf = writeData.getBytes();
    mPhysicaloid.write(buf);
    }

    /* mPhysicaloid.read(buf, buf.length);
    String str = new String(buf);*/

    showToast("Reading: " + str);

    return str;
    }

    @SimpleFunction(description="Write Data to Serial")
    public void Write(String writeData) {
    String str = writeData;
    if(str.length()>0) {
    byte[] buf = str.getBytes();
    mPhysicaloid.write(buf, buf.length);

    showToast("Writing: " + str);
    }
    }

    //Show Toast
    @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN,
    defaultValue = "false")
    @SimpleProperty
    public void SuppressToast(boolean suppressToast) {
    this.suppressToast = suppressToast;
    }

    @SimpleProperty(category = PropertyCategory.BEHAVIOR,
    description = "whether Toast should be suppressed")
    public boolean SuppressToast() {
    return suppressToast;
    }

    private void showToast(String message) {
    if (!suppressToast) {
    Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();
    }
    }

    @SimpleFunction
    public boolean IsOpened() {
    return mPhysicaloid.isOpened();
    }

    @SimpleEvent
    public void AfterRead(boolean success, String data) {
    EventDispatcher.dispatchEvent(this, "AfterRead", success, data);
    }
    }
  8. @pavi2410 pavi2410 created this gist May 21, 2017.
    133 changes: 133 additions & 0 deletions Arduino.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    /*
    Copyright 2017 Pavitra, All rights reserved
    Released under the Apache License, Version 2.0
    http://www.apache.org/licenses/LICENSE-2.0
    */

    package com.pavitra;

    import android.content.Context;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    //import java.io.UnsupportedEncodingException;

    import com.physicaloid.lib.*;

    import com.google.appinventor.components.annotations.*;
    import com.google.appinventor.components.runtime.*;
    import com.google.appinventor.components.common.*;

    @DesignerComponent(version = Arduino.VERSION,
    description = "Arduino USB Serial Extension by Pavitra.\nVersion: " + Arduino.VERSION,
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "images/extension.png")
    @SimpleObject(external = true)
    @UsesLibraries(libraries = "physicaloid.jar")

    public class Arduino extends AndroidNonvisibleComponent implements Component {
    public static final int VERSION = 3;
    private ComponentContainer container;
    private Context context;
    private final Activity activity;
    private static final String LOG_TAG = "Arduino USB Serial Extension";
    private boolean suppressToast = false;
    private String str;
    private int baudRate = 9600;

    Physicaloid mPhysicaloid;

    public Arduino(ComponentContainer container) {
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
    activity = (Activity) container.$context();
    Log.d(LOG_TAG, "Arduino USB Serial Extension Created");
    }

    @SimpleFunction(description="Initialize")
    public void Initialize() {
    mPhysicaloid = new Physicaloid(this.context);

    showToast("Arduino Initialised");
    }

    @SimpleFunction(description="Open connection. Default baud rate is 9600 bps")
    public void Open() {
    mPhysicaloid.open();

    showToast("Opened");
    }

    @SimpleFunction(description="Close connection")
    public void Close() {
    mPhysicaloid.close();

    showToast("Closed");
    }

    @SimpleFunction(description="Baud Rate")
    public void BaudRate(int baudRate) {
    this.baudRate = baudRate;
    mPhysicaloid.setBaudrate(baudRate);

    showToast("Closed");
    }

    @SimpleFunction(description="Read from Serial")
    public String Read() {
    byte[] buf = new byte[256];

    int readSize=0;
    readSize = mPhysicaloid.read(buf, buf.length);

    if(readSize>0) {
    try {
    str = new String(buf);
    } catch (RuntimeException e) {
    showToast(e.toString());
    }
    }

    /* mPhysicaloid.read(buf, buf.length);
    String str = new String(buf);*/

    showToast("Reading: " + str);

    return str;
    }

    @SimpleFunction(description="Write Data to Serial")
    public void Write(String writeData) {
    String str = writeData;
    if(str.length()>0) {
    byte[] buf = str.getBytes();
    mPhysicaloid.write(buf, buf.length);

    showToast("Writing: " + str);
    }
    }

    //Show Toast
    @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN,
    defaultValue = "false")
    @SimpleProperty
    public void SuppressToast(boolean suppressToast) {
    this.suppressToast = suppressToast;
    }

    @SimpleProperty(category = PropertyCategory.BEHAVIOR,
    description = "whether Toast should be suppressed")
    public boolean SuppressToast() {
    return suppressToast;
    }

    private void showToast(String message) {
    if (!suppressToast) {
    Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();
    }
    }
    }