Skip to content

Commit

Permalink
Add Lego NXT Dropdown Blocks (#22)
Browse files Browse the repository at this point in the history
* Add Nxt option list definitions

* Do basic DirectCommands upgrade

* Add overloads

* Upgrade other Nxt components

* Add upgraders

* Fix formatting

* Add new errors

* Pr comments
  • Loading branch information
BeksOmega committed Aug 31, 2020
1 parent c4112d1 commit 3d88677
Show file tree
Hide file tree
Showing 21 changed files with 744 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ private static void upgradeComponentProperties(Map<String, JSONValue> componentP
srcCompVersion = upgradeEv3GyroSensorProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("Ev3UltrasonicSensor")) {
srcCompVersion = upgradeEv3UltrasonicSensorProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("NxtDirectCommands")) {
srcCompVersion = upgradeNxtDirectCommandsProperties(componentProperties, srcCompVersion);
}

if (srcCompVersion < sysCompVersion) {
Expand Down Expand Up @@ -1867,6 +1869,15 @@ private static int upgradeEv3UltrasonicSensorProperties(
// Add UnltrasonicSensorMode dropdown block.
srcCompVersion = 2;
}
}

private static int upgradeNxtDirectCommandsProperties(
Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 2) {
// Adds dropdown blocks.
srcCompVersion = 2;
}
return srcCompVersion;
}

Expand Down Expand Up @@ -1909,5 +1920,4 @@ public void onClick(Widget sender) {
dialogBox.center();
dialogBox.show();
}

}
37 changes: 36 additions & 1 deletion appinventor/blocklyeditor/src/versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ Blockly.Versioning.getOptionListValueMap = function(workspace, key) {
var optionList = db.getOptionList(key);
for (var i = 0, option; option = optionList.options[i]; i++) {
map[option.value] = option.name;
map[option.value.toLowerCase()] = option.name;
}
return map;
}
Expand Down Expand Up @@ -2274,7 +2275,41 @@ Blockly.Versioning.AllUpgradeMaps =
"NxtDirectCommands": {

//This is initial version. Placeholder for future upgrades
1: "noUpgrade"
1: "noUpgrade",

// Add dropdown blocks.
2: [Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetOutputState', 0, 'NxtMotorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetOutputState', 2, 'NxtMotorMode'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetOutputState', 3, 'NxtRegulationMode'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetOutputState', 5, 'NxtRunState'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetInputMode', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetInputMode', 1, 'NxtSensorType'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'SetInputMode', 2, 'NxtSensorMode'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'GetOutputState', 0, 'NxtMotorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'GetInputValues', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'ResetInputScaledValue', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'ResetMotorPosition', 0, 'NxtMotorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'LsGetStatus', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'LsWrite', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'LsRead', 0, 'NxtSensorPort'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'MessageRead', 0, 'NxtMailbox'),
Blockly.Versioning.makeMethodUseDropdown(
'NxtDirectCommands', 'MessageWrite', 0, 'NxtMailbox')]

}, // End NxtDirectCommands upgraders

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtMailbox type used by the NxtDirectCommands component.
*/
public enum NxtMailbox implements OptionList<Integer> {
// Note from Paul Gyugyi during code review: we are only supporting mailboxes 1-10, but NXT can
// use mailboxes above 10 as relays to other NXTs. We've never needed it, but if you ever see
// a feature request or bug report, all that might be required is just raising our upper limit
// on the range.
Box1(1, 0),
Box2(2, 1),
Box3(3, 2),
Box4(4, 3),
Box5(5, 4),
Box6(6, 5),
Box7(7, 6),
Box8(8, 7),
Box9(9, 8),
Box10(10, 9);

private Integer value;
private int intValue;

NxtMailbox(Integer box, int intBox) {
this.value = box;
this.intValue = intBox;
}

public Integer toUnderlyingValue() {
return value;
}

public Integer toInt() {
return intValue;
}

private static final Map<Integer, NxtMailbox> lookup = new HashMap<>();

static {
for (NxtMailbox box : NxtMailbox.values()) {
lookup.put(box.toUnderlyingValue(), box);
}
}

public static NxtMailbox fromUnderlyingValue(Integer box) {
return lookup.get(box);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtMotorMode type used by the NxtDirectCommands component.
*
* Info attained from:
* http://kio4.com/b4a/programas/Appendix%202-LEGO%20MINDSTORMS%20NXT%20Direct%20commands.pdf
* http://www.ni.com/pdf/manuals/372574c.pdf
* In combination with the documentation here:
* https://www.mindstorms.rwth-aachen.de/documents/downloads/doc/version-4.07/help/NXT_SetInputMode.html
*/
public enum NxtMotorMode implements OptionList<Integer> {
On(0x01),
Brake(0x02), // Allows for electronic breaking, which improves accuracy of motor output.
@Default
Regulated(0x04), // Allows regulation based on the regulationMode property.
Coast(0x00); // Motors will rotate freely.

private int value;

NxtMotorMode(int mode) {
this.value = mode;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, NxtMotorMode> lookup = new HashMap<>();

static {
for (NxtMotorMode mode : NxtMotorMode.values()) {
lookup.put(mode.toUnderlyingValue(), mode);
}
}

public static NxtMotorMode fromUnderlyingValue(Integer mode) {
return lookup.get(mode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtMotorPort type used by the NxtDirectCommands component.
*/
public enum NxtMotorPort implements OptionList<String> {
PortA("A", 0),
PortB("B", 1),
PortC("C", 2);

private String value;
private int intValue;

NxtMotorPort(String port, int intPort) {
this.value = port;
this.intValue = intPort;
}

public String toUnderlyingValue() {
return value;
}

public Integer toInt() {
return intValue;
}

private static final Map<String, NxtMotorPort> lookup = new HashMap<>();

static {
for(NxtMotorPort port : NxtMotorPort.values()) {
String str = port.toUnderlyingValue();
lookup.put(str, port);
lookup.put(str.toLowerCase(), port);
}
}

public static NxtMotorPort fromUnderlyingValue(String port) {
return lookup.get(port);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtRegulationMode type used by the NxtDirectCommands component.
*
* Info attained from:
* http://kio4.com/b4a/programas/Appendix%202-LEGO%20MINDSTORMS%20NXT%20Direct%20commands.pdf
* http://www.ni.com/pdf/manuals/372574c.pdf
* In combination with the documentation here:
* https://www.mindstorms.rwth-aachen.de/documents/downloads/doc/version-4.07/help/NXT_SetInputMode.html
*
* Note that this property is only important if the motor mode is set to regulated.
*/
public enum NxtRegulationMode implements OptionList<Integer> {
Disabled(0x00), // No regulation.
@Default
Speed(0x01), // Speed regulation.
Syncronization(0x02); // Enables motor syncronization.

private int value;

NxtRegulationMode(int mode) {
this.value = mode;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, NxtRegulationMode> lookup = new HashMap<>();

static {
for (NxtRegulationMode mode : NxtRegulationMode.values()) {
lookup.put(mode.toUnderlyingValue(), mode);
}
}

public static NxtRegulationMode fromUnderlyingValue(Integer mode) {
return lookup.get(mode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtRunState type used by the NxtDirectCommands component.
*
* Info attained from:
* http://kio4.com/b4a/programas/Appendix%202-LEGO%20MINDSTORMS%20NXT%20Direct%20commands.pdf
* http://www.ni.com/pdf/manuals/372574c.pdf
* In combination with the documentation here:
* https://www.mindstorms.rwth-aachen.de/documents/downloads/doc/version-4.07/help/NXT_SetInputMode.html
*/
public enum NxtRunState implements OptionList<Integer> {
Disabled(0x00),
@Default
Running(0x20),
RampUp(0x10),
RampDown(0x40);

private int value;

NxtRunState(int state) {
this.value = state;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, NxtRunState> lookup = new HashMap<>();

static {
for (NxtRunState state : NxtRunState.values()) {
lookup.put(state.toUnderlyingValue(), state);
}
}

public static NxtRunState fromUnderlyingValue(Integer state) {
return lookup.get(state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a NxtSensorMode type used by the NxtDirectCommands component.
*
* Info attained from the "LEGO MINDSTORMS NXT Direct Command" document.
* http://kio4.com/b4a/programas/Appendix%202-LEGO%20MINDSTORMS%20NXT%20Direct%20commands.pdf
* http://www.ni.com/pdf/manuals/372574c.pdf
* In combination with the documentation here:
* https://www.mindstorms.rwth-aachen.de/documents/downloads/doc/version-4.07/help/NXT_SetInputMode.html
*/
public enum NxtSensorMode implements OptionList<Integer> {
Raw(0x00),
Boolean(0x20), // 1 if > 45% else 0.
TransitionCount(0x60), // Count transitions of boolean mode.
PeriodCount(0x60), // Count periods of boolean mode.
Percentage(0x80), // Range [0, 100].
RcxCelsius(0xA0), // Range [-200, 700]. Readings in .1 degrees Celsius.
RcxFahrenheit(0xC0), // Range [-400, 1580]. Readings in .1 degrees Fahrenheit.
RcxAngleSteps(0xE0);

private int value;

NxtSensorMode(int mode) {
this.value = mode;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, NxtSensorMode> lookup = new HashMap<>();

static {
for (NxtSensorMode mode : NxtSensorMode.values()) {
lookup.put(mode.toUnderlyingValue(), mode);
}
}

public static NxtSensorMode fromUnderlyingValue(Integer mode) {
return lookup.get(mode);
}
}

0 comments on commit 3d88677

Please sign in to comment.