-
-
Save sabas1080/f73f9c8d07880b8cd82ea1e2f7ef8ce0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment