Unverified Commit a6a4b578 authored by Diego Barreiro's avatar Diego Barreiro Committed by GitHub

Add Serial Component (#1732)

parent 2ade5c0e
......@@ -630,6 +630,12 @@ public interface Images extends Resources {
@Source("com/google/appinventor/images/logo.png")
ImageResource logo();
/**
* Designer palette item: Arduino component
*/
@Source("com/google/appinventor/images/arduino.png")
ImageResource arduino();
/**
* Media icon: image
*/
......
......@@ -169,6 +169,7 @@ public final class SimpleComponentDescriptor {
bundledImages.put("images/polygon.png", images.polygon());
bundledImages.put("images/featurecollection.png", images.featurecollection());
bundledImages.put("images/navigation.png", images.navigationComponent());
bundledImages.put("images/arduino.png", images.arduino());
imagesInitialized = true;
}
......
......@@ -151,6 +151,7 @@
<copy toFile="${public.deps.dir}/commons-pool.jar" file="${lib.dir}/commons-pool/commons-pool2-2.0.jar" />
<copy toFile="${public.deps.dir}/guava-14.0.1.jar" file="${lib.dir}/guava/guava-14.0.1.jar" />
<copy toFile="${public.deps.dir}/core.jar" file="${lib.dir}/QRGenerator/core.jar" />
<copy toFile="${public.deps.dir}/physicaloid.jar" file="${lib.dir}/physicaloid/physicaloid-library.jar" />
<!-- BEGIN Android Support Libraries -->
<copy todir="${public.deps.dir}/">
<fileset dir="${support.dir}/" includes="*.aar,*.jar" excludes="testing-support-R-classes.jar" />
......
......@@ -505,6 +505,7 @@ public class YaVersion {
// - WEBVIEWER_COMPONENT_VERSION was incremented to 9
// For YOUNG_ANDROID_VERSION 204:
// - NAVIGATION_COMPONENT_VERSION was initialized to 1
// - SERIAL_COMPONENT_VERSION was initialized to 1
public static final int YOUNG_ANDROID_VERSION = 204;
......@@ -1331,6 +1332,9 @@ public class YaVersion {
//For PROXIMITYSENSOR_COMPONENT_VERSION: Initial Version
public static final int PROXIMITYSENSOR_COMPONENT_VERSION = 1;
//For SERIAL_COMPONENT_VERSION: Initial Version
public static final int SERIAL_COMPONENT_VERSION = 1;
// Rendezvous Server Location
public static final String RENDEZVOUS_SERVER = "rendezvous.appinventor.mit.edu";
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2016-2019 MIT, All rights reserved
// Copyright 2017-2019 Kodular, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.runtime;
import android.content.Context;
import android.util.Log;
import com.physicaloid.lib.Physicaloid;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import java.io.UnsupportedEncodingException;
@DesignerComponent(version = YaVersion.SERIAL_COMPONENT_VERSION,
description = "Serial component which can be used to connect to devices like Arduino",
category = ComponentCategory.CONNECTIVITY,
nonVisible = true,
iconName = "images/arduino.png",
androidMinSdk = 12)
@SimpleObject
@UsesLibraries(libraries = "physicaloid.jar")
public class Serial extends AndroidNonvisibleComponent implements Component {
private static final String LOG_TAG = "Serial Component";
private Context context;
private Physicaloid mPhysicaloid;
private int baudRate = 9600;
private int bytes = 256;
public Serial(ComponentContainer container) {
super(container.$form());
context = container.$context();
Log.d(LOG_TAG, "Created");
}
@SimpleFunction(description = "Initializes serial connection.")
public void InitializeSerial() {
mPhysicaloid = new Physicaloid(context);
BaudRate(this.baudRate);
Log.d(LOG_TAG, "Initialized");
}
@SimpleFunction(description = "Opens serial connection. Returns true when opened.")
public boolean OpenSerial() {
Log.d(LOG_TAG, "Opening connection");
if (mPhysicaloid == null) {
form.dispatchErrorOccurredEvent(Serial.this, "OpenSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
return false;
}
return mPhysicaloid.open();
}
@SimpleFunction(description = "Closes serial connection. Returns true when closed.")
public boolean CloseSerial() {
Log.d(LOG_TAG, "Closing connection");
if (mPhysicaloid == null) {
form.dispatchErrorOccurredEvent(Serial.this, "CloseSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
return false;
}
return mPhysicaloid.close();
}
@SimpleFunction(description = "Reads data from serial.")
public String ReadSerial() {
String data = "";
if (mPhysicaloid == null) {
form.dispatchErrorOccurredEvent(Serial.this, "ReadSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
} else {
byte[] buf = new byte[this.bytes];
if (mPhysicaloid.read(buf) > 0) {
try {
data = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException mEr) {
Log.e(LOG_TAG, mEr.getMessage());
}
}
}
return data;
}
@SimpleFunction(description = "Writes given data to serial.")
public void WriteSerial(String data) {
if (!data.isEmpty() && mPhysicaloid != null) {
byte[] buf = data.getBytes();
int result = mPhysicaloid.write(buf);
if (result == -1)
form.dispatchErrorOccurredEvent(Serial.this, "WriteSerial", ErrorMessages.ERROR_SERIAL_WRITING);
} else if (mPhysicaloid == null) {
form.dispatchErrorOccurredEvent(Serial.this, "WriteSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
}
}
@SimpleFunction(description = "Writes given data to serial, and appends a new line at the end.")
public void PrintSerial(String data) {
if (!data.isEmpty())
WriteSerial(data + "\n");
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns true when the Serial connection is open.")
public boolean IsOpen() {
if (mPhysicaloid == null) {
form.dispatchErrorOccurredEvent(Serial.this, "IsOpen", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
return false;
}
return mPhysicaloid.isOpened();
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns true when the Serial has been initialized.")
public boolean IsInitialized() {
return mPhysicaloid != null;
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns the current baud rate")
public int BaudRate() {
return this.baudRate;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER, defaultValue = "9600")
@SimpleProperty
public void BaudRate(int baudRate) {
this.baudRate = baudRate;
Log.d(LOG_TAG, "Baud Rate: " + baudRate);
if (mPhysicaloid != null)
mPhysicaloid.setBaudrate(baudRate);
else
Log.w(LOG_TAG, "Could not set Serial Baud Rate to " + baudRate + ". Just saved, not applied to serial! Maybe you forgot to initialize it?");
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns the buffer size in bytes")
public int BufferSize() {
return this.bytes;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER, defaultValue = "256")
@SimpleProperty
public void BufferSize(int bytes) {
this.bytes = bytes;
Log.d(LOG_TAG, "Buffer Size: " + bytes);
}
}
......@@ -269,12 +269,17 @@ public final class ErrorMessages {
public static final int ERROR_SERVER = 3808;
public static final int ERROR_SPEECH_TIMEOUT = 3809;
// Serial errors
public static final int ERROR_SERIAL_NOT_INITIALIZED = 3901;
public static final int ERROR_SERIAL_WRITING = 3902;
// Navigation Errors
public static final int ERROR_INVALID_API_KEY = 4001;
public static final int ERROR_UNABLE_TO_REQUEST_DIRECTIONS = 4002;
public static final int ERROR_ROUTING_SERVICE_ERROR = 4003;
public static final int ERROR_NO_ROUTE_FOUND = 4004;
// Start the next group of errors at 4100
// Mapping of error numbers to error message format strings.
......@@ -676,6 +681,10 @@ public final class ErrorMessages {
errorMessages.put(ERROR_SERVER, "Error From Server");
errorMessages.put(ERROR_SPEECH_TIMEOUT, "No Speech Input");
// Serial
errorMessages.put(ERROR_SERIAL_NOT_INITIALIZED, "Serial was not initialized");
errorMessages.put(ERROR_SERIAL_WRITING, "Error writing data to serial");
// Navigation Errors
errorMessages.put(ERROR_INVALID_API_KEY, "No api key provided");
errorMessages.put(ERROR_UNABLE_TO_REQUEST_DIRECTIONS,
......
......@@ -134,6 +134,7 @@
<li><a href="#ActivityStarter">ActivityStarter</a></li>
<li><a href="#BluetoothClient">BluetoothClient</a></li>
<li><a href="#BluetoothServer">BluetoothServer</a></li>
<li><a href="#Serial">Serial</a></li>
<li><a href="#Web">Web</a></li>
</ul>
......@@ -471,6 +472,38 @@ set the following properties:
<dd>Stop accepting an incoming connection.</dd>
</dl>
<h2 id="Serial">Serial</h2>
<h3 id="Serial-Properties">Properties</h3>
<dl class="properties">
<dt id="Serial.BaudRate" class="number do"><em>BaudRate</em></dt>
<dd>Specifies the baud rate for the connection.</dd>
<dt id="Serial.BufferSize" class="number do"><em>BufferSize</em></dt>
<dd>Specifies the buffer size in bytes for the connection.</dd>
<dt id="Serial.IsInitialized" class="boolean bo"><em>IsInitialized</em></dt>
<dd>Returns true when the component has been initialized.</dd>
<dt id="Serial.IsOpen" class="boolean bo"><em>IsOpen</em></dt>
<dd>Returns true when the connection has been opened.</dd>
</dl>
<h3 id="Serial-Methods">Methods</h3>
<dl class="methods">
<dt id="Serial.CloseSerial" class="method returns boolean"><i></i> CloseSerial()</dt>
<dd>Attempts to close the serial connection, returning true when success.</dd>
<dt id="Serial.InitializeSerial" class="method"><i></i> InitializeSerial()</dt>
<dd>Initializes the serial component to be ready to use.</dd>
<dt id="Serial.OpenSerial" class="method returns boolean"><i></i> OpenSerial()</dt>
<dd>Attempts to close the serial connection, returning true when success.</dd>
<dt id="Serial.PrintSerial" class="method"><i></i> PrintSerial(<em class="text">data</em>)</dt>
<dd>Sends the given data to the serial connection, and appends a new line.</dd>
<dt id="Serial.ReadSerial" class="method"><i></i> ReadSerial()</dt>
<dd>Starts reading the serial port. Will raise AfterReadSerial event.</dd>
<dt id="Serial.WriteSerial" class="method"><i></i> WriteSerial(<em class="text">data</em>)</dt>
<dd>Sends the given data to the serial connection.</dd>
</dl>
<h2 id="Web">Web</h2>
<p>Non-visible component that provides functions for HTTP GET, POST, PUT, and DELETE requests.</p>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment