AI2 Component  (Version nb184)
Ev3Commands.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
17 
27 @DesignerComponent(version = YaVersion.EV3_COMMANDS_COMPONENT_VERSION,
28  description = "A component that provides a low-level interface to a LEGO MINDSTORMS EV3 " +
29  "robot, with functions to send system or direct commands to EV3 robots.",
30  category = ComponentCategory.LEGOMINDSTORMS,
31  nonVisible = true,
32  iconName = "images/legoMindstormsEv3.png")
33 @SimpleObject
34 @UsesPermissions(permissionNames = "android.permission.INTERNET," +
35  "android.permission.WRITE_EXTERNAL_STORAGE," +
36  "android.permission.READ_EXTERNAL_STORAGE")
37 public class Ev3Commands extends LegoMindstormsEv3Base {
38 
42  public Ev3Commands(ComponentContainer container) {
43  super(container, "Ev3Commands");
44  }
45 
49  @SimpleFunction(description = "Keep the EV3 brick from shutdown for a period of time.")
50  public void KeepAlive(int minutes) {
51  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
52 
53  if (minutes < 0 || minutes > 0xff) {
54  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT, functionName);
55  return;
56  }
57 
58  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.KEEP_ALIVE,
59  false,
60  0,
61  0,
62  "c",
63  (byte) minutes);
64  sendCommand(functionName, command, false);
65 
66  }
67 
71  @SimpleFunction(description = "Get the battery voltage.")
72  public double GetBatteryVoltage() {
73  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
74  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
75  true,
76  4,
77  0,
78  "cg",
79  Ev3Constants.UIReadSubcode.GET_VBATT,
80  (byte) 0);
81  byte[] reply = sendCommand(functionName, command, true);
82  if (reply != null && reply.length == 5 && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
83  Object[] values = Ev3BinaryParser.unpack("xf", reply);
84  return (Float) values[0];
85  } else { // error
86  return -1.0;
87  }
88  }
89 
93  @SimpleFunction(description = "Get the battery current.")
94  public double GetBatteryCurrent() {
95  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
96  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
97  true,
98  4,
99  0,
100  "cg",
101  Ev3Constants.UIReadSubcode.GET_IBATT,
102  (byte) 0);
103  byte[] reply = sendCommand(functionName, command, true);
104  if (reply != null && reply.length == 5 && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
105  Object[] values = Ev3BinaryParser.unpack("xf", reply);
106  return (Float) values[0];
107  } else { // error
108  return -1.0;
109  }
110  }
111 
115  @SimpleFunction(description = "Get the OS version on EV3.")
116  public String GetOSVersion() {
117  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
118  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
119  true,
120  100,
121  0,
122  "ccg",
123  Ev3Constants.UIReadSubcode.GET_OS_VERS,
124  (short) 100,
125  (byte) 0);
126  byte[] reply = sendCommand(functionName, command, true);
127  if (reply != null && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
128  Object[] value = Ev3BinaryParser.unpack("xS", reply);
129  return String.valueOf(value[0]);
130  } else {
131  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_INVALID_REPLY);
132  return null;
133  }
134  }
135 
139  @SimpleFunction(description = "Get the OS build on EV3.")
140  public String GetOSBuild() {
141  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
142  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.UIReadSubcode.GET_OS_VERS,
143  true,
144  100,
145  0,
146  "ccg",
147  Ev3Constants.UIReadSubcode.GET_OS_BUILD,
148  (short) 100,
149  (byte) 0);
150  byte[] reply = sendCommand(functionName, command, true);
151 
152  if (reply != null && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
153  Object[] value = Ev3BinaryParser.unpack("xS", reply);
154  return String.valueOf(value[0]);
155  } else {
156  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_INVALID_REPLY);
157  return null;
158  }
159  }
160 
164  @SimpleFunction(description = "Get the firmware version on EV3.")
165  public String GetFirmwareVersion() {
166  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
167  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
168  true,
169  100,
170  0,
171  "ccg",
172  Ev3Constants.UIReadSubcode.GET_FW_VERS,
173  (short) 100,
174  (byte) 0);
175  byte[] reply = sendCommand(functionName, command, true);
176 
177  if (reply != null && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
178  Object[] value = Ev3BinaryParser.unpack("xS", reply);
179  return String.valueOf(value[0]);
180  } else {
181  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_INVALID_REPLY);
182  return null;
183  }
184  }
185 
189  @SimpleFunction(description = "Get the firmware build on EV3.")
190  public String GetFirmwareBuild() {
191  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
192  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
193  true,
194  100,
195  0,
196  "cg",
197  (byte) 127,
198  (byte) 0);
199  byte[] reply = sendCommand(functionName, command, true);
200 
201  if (reply != null && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
202  Object[] value = Ev3BinaryParser.unpack("xS", reply);
203  return String.valueOf(value[0]);
204  } else {
205  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_INVALID_REPLY);
206  return null;
207  }
208  }
209 
213  @SimpleFunction(description = "Get the hardware version of EV3.")
214  public String GetHardwareVersion() {
215  String functionName = Thread.currentThread().getStackTrace()[1].getMethodName();
216  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.UI_READ,
217  true,
218  100,
219  0,
220  "ccg",
221  Ev3Constants.UIReadSubcode.GET_HW_VERS,
222  (short) 100,
223  (byte) 0);
224  byte[] reply = sendCommand(functionName, command, true);
225 
226  if (reply != null && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
227  Object[] value = Ev3BinaryParser.unpack("xS", reply);
228  return String.valueOf(value[0]);
229  } else {
230  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_INVALID_REPLY);
231  return null;
232  }
233  }
234 }
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components
com.google.appinventor.components.runtime.util.Ev3BinaryParser
Definition: Ev3BinaryParser.java:21
com.google.appinventor.components.runtime.util.Ev3Constants.Opcode
Definition: Ev3Constants.java:15
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.util.Ev3Constants.UIReadSubcode
Definition: Ev3Constants.java:331
com.google.appinventor.components.runtime.util.Ev3BinaryParser.encodeDirectCommand
static byte[] encodeDirectCommand(byte opcode, boolean needReply, int globalAllocation, int localAllocation, String paramFormat, Object... parameters)
Definition: Ev3BinaryParser.java:506
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_INVALID_REPLY
static final int ERROR_EV3_INVALID_REPLY
Definition: ErrorMessages.java:215
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.Ev3Commands
Definition: Ev3Commands.java:37
com.google.appinventor.components.runtime.util.Ev3BinaryParser.unpack
static Object[] unpack(String format, byte[] bytes)
Definition: Ev3BinaryParser.java:261
com.google.appinventor.components.runtime.LegoMindstormsEv3Base
Definition: LegoMindstormsEv3Base.java:24
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.util.Ev3Constants.DirectReplyType
Definition: Ev3Constants.java:481
com.google
com
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_ILLEGAL_ARGUMENT
static final int ERROR_EV3_ILLEGAL_ARGUMENT
Definition: ErrorMessages.java:216
com.google.appinventor.components.runtime.util.Ev3Constants
Definition: Ev3Constants.java:14
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.Ev3Commands.Ev3Commands
Ev3Commands(ComponentContainer container)
Definition: Ev3Commands.java:42