AI2 Component  (Version nb184)
LegoMindstormsEv3Base.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2011-2012 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 
15 import java.util.Collections;
16 
23 @SimpleObject
25  implements BluetoothConnectionListener, Component, Deleteable {
26 
27  private static final int TOY_ROBOT = 0x0804;
28  protected int commandCount;
29  protected final String logTag;
31 
32  protected LegoMindstormsEv3Base(ComponentContainer container, String logTag) {
33  super(container.$form());
34  this.logTag = logTag;
35  }
36 
37  protected LegoMindstormsEv3Base() {
38  super(null);
39  logTag = null;
40  }
41 
42  @SimpleProperty(description = "The BluetoothClient component that should be used for communication.",
43  category = PropertyCategory.BEHAVIOR)
45  return bluetooth;
46  }
47 
53  defaultValue = "")
55  public void BluetoothClient(BluetoothClient bluetoothClient) {
56  if (bluetooth != null) {
57  bluetooth.removeBluetoothConnectionListener(this);
58  bluetooth.detachComponent(this);
59  bluetooth = null;
60  }
61 
62  if (bluetoothClient != null) {
63  bluetooth = bluetoothClient;
64  bluetooth.attachComponent(this, Collections.singleton(TOY_ROBOT));
65  bluetooth.addBluetoothConnectionListener(this);
66  if (bluetooth.IsConnected()) {
67  // We missed the real afterConnect event.
69  }
70  }
71  }
72 
73  protected final boolean isBluetoothConnected(String functionName) {
74  if (bluetooth == null) {
76  return false;
77  }
78 
79  if (!bluetooth.IsConnected()) {
81  return false;
82  }
83 
84  return true;
85  }
86 
87  protected final byte[] sendCommand(String functionName, byte[] command, boolean doReceiveReply) {
88  // check connecttivity
89  if (!isBluetoothConnected(functionName))
90  return null;
91 
92  // prepend header and send payload
93  byte[] header = Ev3BinaryParser.pack("hh", (short) (command.length + 2), (short) commandCount);
94  commandCount++;
95 
96  bluetooth.write(functionName, header);
97  bluetooth.write(functionName, command);
98 
99  // receive reply if required
100  if (doReceiveReply) {
101  header = bluetooth.read(functionName, 4);
102 
103  if (header.length == 4) {
104  Object[] decodedHeader = Ev3BinaryParser.unpack("hh", header);
105  int replySize = (int) ((Short) decodedHeader[0]) - 2;
106  int replyCount = (int) ((Short) decodedHeader[1]);
107  byte[] reply = bluetooth.read(functionName, replySize);
108 
109  if (reply.length == replySize)
110  return reply;
111  else
112  {
114  return null;
115  }
116  }
117 
118  // handle errors
120  return null;
121  } else {
122  return null;
123  }
124  }
125 
126  protected final int sensorPortLetterToPortNumber(String letter) {
127  if (letter.length() != 1)
128  throw new IllegalArgumentException("String \"" + letter + "\" is not a valid sensor port letter");
129 
130  int portNumber = letter.charAt(0) - '1';
131 
132  if (portNumber < 0 || portNumber > 3)
133  throw new IllegalArgumentException("String \"" + letter + "\" is not a valid sensor port letter");
134 
135  return portNumber;
136  }
137 
138  protected final String portNumberToSensorPortLetter(int portNumber) {
139  if (portNumber < 0 || portNumber > 3)
140  throw new IllegalArgumentException(portNumber + " is not a valid port number");
141 
142  return "" + ('1' + portNumber);
143  }
144 
145  protected final int motorPortLettersToBitField(String letters) {
146  if (letters.length() > 4)
147  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
148 
149  int portABit = 0;
150  int portBBit = 0;
151  int portCBit = 0;
152  int portDBit = 0;
153 
154  for (int i = 0; i < letters.length(); i++)
155  {
156  switch (letters.charAt(i))
157  {
158  case 'A':
159  if (portABit != 0)
160  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
161  portABit = 1;
162  break;
163 
164  case 'B':
165  if (portBBit != 0)
166  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
167  portBBit = 2;
168  break;
169 
170  case 'C':
171  if (portCBit != 0)
172  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
173  portCBit = 4;
174  break;
175 
176  case 'D':
177  if (portDBit != 0)
178  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
179  portDBit = 8;
180  break;
181 
182  default:
183  throw new IllegalArgumentException("Malformed motor port letters \"" + letters + "\"");
184  }
185  }
186 
187  return portABit | portBBit | portCBit | portDBit;
188  }
189 
190  protected final String bitFieldToMotorPortLetters(int bitField) {
191  if (bitField < 0 || bitField > 15)
192  throw new IllegalArgumentException("Invalid bit field number " + bitField);
193 
194  String portLetters = "";
195 
196  if ((bitField & 1) != 0)
197  portLetters += "A";
198 
199  if ((bitField & 2) != 0)
200  portLetters += "B";
201 
202  if ((bitField & 4) != 0)
203  portLetters += "C";
204 
205  if ((bitField & 8) != 0)
206  portLetters += "D";
207 
208  return portLetters;
209  }
210 
211  @Override
212  public void afterConnect(BluetoothConnectionBase bluetoothConnection) {
213  // Subclasses may wish to do something.
214  }
215 
216  @Override
217  public void beforeDisconnect(BluetoothConnectionBase bluetoothConnection) {
218  // Subclasses may wish to do something.
219  }
220 
221  // interface Deleteable implementation
222  @Override
223  public void onDelete() {
224  if (bluetooth != null) {
225  bluetooth.removeBluetoothConnectionListener(this);
226  bluetooth.detachComponent(this);
227  bluetooth = null;
228  }
229  }
230 }
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.commandCount
int commandCount
Definition: LegoMindstormsEv3Base.java:28
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.afterConnect
void afterConnect(BluetoothConnectionBase bluetoothConnection)
Definition: LegoMindstormsEv3Base.java:212
com.google.appinventor.components.runtime.BluetoothConnectionBase.write
void write(String functionName, byte b)
Definition: BluetoothConnectionBase.java:513
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.motorPortLettersToBitField
final int motorPortLettersToBitField(String letters)
Definition: LegoMindstormsEv3Base.java:145
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.runtime.LegoMindstormsEv3Base.bluetooth
BluetoothClient bluetooth
Definition: LegoMindstormsEv3Base.java:30
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_NOT_CONNECTED_TO_ROBOT
static final int ERROR_EV3_NOT_CONNECTED_TO_ROBOT
Definition: ErrorMessages.java:214
com.google.appinventor.components
com.google.appinventor.components.runtime.util.Ev3BinaryParser
Definition: Ev3BinaryParser.java:21
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_BLUETOOTH_NOT_SET
static final int ERROR_EV3_BLUETOOTH_NOT_SET
Definition: ErrorMessages.java:213
com.google.appinventor.components.runtime.BluetoothConnectionBase.IsConnected
final boolean IsConnected()
Definition: BluetoothConnectionBase.java:204
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.beforeDisconnect
void beforeDisconnect(BluetoothConnectionBase bluetoothConnection)
Definition: LegoMindstormsEv3Base.java:217
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.onDelete
void onDelete()
Definition: LegoMindstormsEv3Base.java:223
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.LegoMindstormsEv3Base
LegoMindstormsEv3Base(ComponentContainer container, String logTag)
Definition: LegoMindstormsEv3Base.java:32
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.runtime.util.Ev3BinaryParser.pack
static byte[] pack(String format, Object... values)
Definition: Ev3BinaryParser.java:54
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BLUETOOTHCLIENT
static final String PROPERTY_TYPE_BLUETOOTHCLIENT
Definition: PropertyTypeConstants.java:28
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.sensorPortLetterToPortNumber
final int sensorPortLetterToPortNumber(String letter)
Definition: LegoMindstormsEv3Base.java:126
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.BluetoothClient
Definition: BluetoothClient.java:49
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
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.BluetoothConnectionBase
Definition: BluetoothConnectionBase.java:41
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.logTag
final String logTag
Definition: LegoMindstormsEv3Base.java:29
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.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.sendCommand
final byte[] sendCommand(String functionName, byte[] command, boolean doReceiveReply)
Definition: LegoMindstormsEv3Base.java:87
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.BluetoothConnectionBase.read
final byte[] read(String functionName, int numberOfBytes)
Definition: BluetoothConnectionBase.java:778
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.bitFieldToMotorPortLetters
final String bitFieldToMotorPortLetters(int bitField)
Definition: LegoMindstormsEv3Base.java:190
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.isBluetoothConnected
final boolean isBluetoothConnected(String functionName)
Definition: LegoMindstormsEv3Base.java:73
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.LegoMindstormsEv3Base
LegoMindstormsEv3Base()
Definition: LegoMindstormsEv3Base.java:37
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.portNumberToSensorPortLetter
final String portNumberToSensorPortLetter(int portNumber)
Definition: LegoMindstormsEv3Base.java:138
com.google.appinventor
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.BluetoothClient
void BluetoothClient(BluetoothClient bluetoothClient)
Definition: LegoMindstormsEv3Base.java:55