AI2 Component  (Version nb184)
LegoMindstormsEv3Sensor.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 
16 
23 @SimpleObject
25  protected static final String DEFAULT_SENSOR_PORT = "1";
26  protected int sensorPortNumber;
27 
28  protected LegoMindstormsEv3Sensor(ComponentContainer container, String logTag) {
29  super(container, logTag);
31  }
32 
33  @SimpleProperty(description = "The sensor port that the sensor is connected to.",
34  category = PropertyCategory.BEHAVIOR,
35  userVisible = false)
36  public String SensorPort() {
38  }
39 
45  defaultValue = DEFAULT_SENSOR_PORT)
47  public void SensorPort(String sensorPortLetter) {
48  String functionName = "SensorPort";
49  setSensorPort(functionName, sensorPortLetter);
50  }
51 
52  protected final void setSensorPort(String functionName, String sensorPortLetter) {
53  try {
55  } catch (IllegalArgumentException e) {
56  form.dispatchErrorOccurredEvent(this, functionName, ErrorMessages.ERROR_EV3_ILLEGAL_SENSOR_PORT, sensorPortLetter);
57  return;
58  }
59  }
60 
61  protected final int readInputPercentage(String functionName,
62  int layer,
63  int no,
64  int type,
65  int mode) {
66  if (layer < 0 || layer > 3 || no < 0 || no > 3 || mode < -1 || mode > 7)
67  throw new IllegalArgumentException();
68 
69  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.INPUT_DEVICE,
70  true,
71  1,
72  0,
73  "ccccccg",
75  (byte) layer,
76  (byte) no,
77  (byte) type,
78  (byte) mode,
79  (byte) 1,
80  (byte) 0);
81 
82  byte[] reply = sendCommand(functionName, command, true);
83  if (reply != null && reply.length == 2 && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
84  return (int) reply[1];
85  } else { // error
86  return -1;
87  }
88  }
89 
90  protected final double readInputSI(String functionName,
91  int layer,
92  int no,
93  int type,
94  int mode) {
95  if (layer < 0 || layer > 3 || no < 0 || no > 3 || mode < -1 || mode > 7)
96  throw new IllegalArgumentException();
97  byte[] command = Ev3BinaryParser.encodeDirectCommand(Ev3Constants.Opcode.INPUT_DEVICE,
98  true,
99  4,
100  0,
101  "ccccccg",
103  (byte) layer,
104  (byte) no,
105  (byte) type,
106  (byte) mode,
107  (byte) 1,
108  (byte) 0); //index always be 0
109 
110  byte[] reply = sendCommand(functionName, command, true);
111 
112  if (reply != null && reply.length == 5 && reply[0] == Ev3Constants.DirectReplyType.DIRECT_REPLY) {
113  Object[] values = Ev3BinaryParser.unpack("xf", reply);
114  return (double) ((Float) values[0]);
115 
116  } else {
118  return -1.0;
119  }
120  }
121 }
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.sensorPortNumber
int sensorPortNumber
Definition: LegoMindstormsEv3Sensor.java:26
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.PropertyTypeConstants.PROPERTY_TYPE_LEGO_EV3_SENSOR_PORT
static final String PROPERTY_TYPE_LEGO_EV3_SENSOR_PORT
Definition: PropertyTypeConstants.java:115
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.readInputSI
final double readInputSI(String functionName, int layer, int no, int type, int mode)
Definition: LegoMindstormsEv3Sensor.java:90
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.SensorPort
String SensorPort()
Definition: LegoMindstormsEv3Sensor.java:36
com.google.appinventor.components
com.google.appinventor.components.runtime.util.Ev3BinaryParser
Definition: Ev3BinaryParser.java:21
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.DEFAULT_SENSOR_PORT
static final String DEFAULT_SENSOR_PORT
Definition: LegoMindstormsEv3Sensor.java:25
com.google.appinventor.components.runtime.util.Ev3Constants.Opcode
Definition: Ev3Constants.java:15
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.readInputPercentage
final int readInputPercentage(String functionName, int layer, int no, int type, int mode)
Definition: LegoMindstormsEv3Sensor.java:61
com.google.appinventor.components.runtime.util.Ev3Constants.InputDeviceSubcode
Definition: Ev3Constants.java:273
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
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.LegoMindstormsEv3Sensor.LegoMindstormsEv3Sensor
LegoMindstormsEv3Sensor(ComponentContainer container, String logTag)
Definition: LegoMindstormsEv3Sensor.java:28
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.LegoMindstormsEv3Sensor.setSensorPort
final void setSensorPort(String functionName, String sensorPortLetter)
Definition: LegoMindstormsEv3Sensor.java:52
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.sensorPortLetterToPortNumber
final int sensorPortLetterToPortNumber(String letter)
Definition: LegoMindstormsEv3Base.java:126
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.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.LegoMindstormsEv3Sensor
Definition: LegoMindstormsEv3Sensor.java:24
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.appinventor.components.runtime.util.Ev3Constants.DirectReplyType
Definition: Ev3Constants.java:481
com.google
com
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.SensorPort
void SensorPort(String sensorPortLetter)
Definition: LegoMindstormsEv3Sensor.java:47
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EV3_ILLEGAL_SENSOR_PORT
static final int ERROR_EV3_ILLEGAL_SENSOR_PORT
Definition: ErrorMessages.java:218
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.runtime.util.Ev3Constants
Definition: Ev3Constants.java:14
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