Skip to content

Commit

Permalink
adding IR remote button testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspesla committed Oct 8, 2015
1 parent 418e530 commit 2654656
Show file tree
Hide file tree
Showing 4 changed files with 923 additions and 17 deletions.
114 changes: 97 additions & 17 deletions ev3_scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
{
waitingCallbacks[x] = [];
global_touch_pressed[x] = false;
global_ir_button_pressed[x] = false;
global_sensor_queried[x] = 0;
}
}
Expand Down Expand Up @@ -99,8 +100,9 @@ function startupBatteryCheckCallback(result)
connecting = false;

playStartUpTones();

// setupWatchdog();

// no watchdog right now. reconnection is too flakey so there is no point
// setupWatchdog();
}

function setupWatchdog()
Expand Down Expand Up @@ -263,6 +265,7 @@ function playStartUpTones()
var waitingCallbacks = [[],[],[],[],[],[],[],[], []];
var waitingQueries = [];
var global_touch_pressed = [false, false, false, false,false, false, false, false, false];
var global_ir_button_pressed = [false, false, false, false,false, false, false, false, false];
var global_sensor_queried = [0, 0, 0, 0, 0, 0, 0, 0, 0];

function receive_handler(data)
Expand Down Expand Up @@ -309,7 +312,14 @@ function playStartUpTones()

else if (mode == IR_SENSOR)
{
theResult = getFloatResult(inputData);
if (modeType == IR_PROX)
theResult = getFloatResult(inputData);
else if (modeType == IR_REMOTE)
theResult = getIRButtonNameForCode(getFloatResult(inputData));
}
else if (mode == GYRO_SENSOR)
{
theResult = getFloatResult(inputData);
}
else if (mode == READ_FROM_MOTOR)
{
Expand All @@ -325,8 +335,10 @@ function playStartUpTones()

global_touch_pressed[this_is_from_port] = theResult;
global_sensor_queried[this_is_from_port]--;
global_ir_button_pressed[this_is_from_port] = theResult;
while(callback = waitingCallbacks[this_is_from_port].shift())
{
console.log("result: " + theResult);
callback(theResult);
}
}
Expand All @@ -343,6 +355,18 @@ function playStartUpTones()
return c[0];
}

function getIRButtonNameForCode(inButtonCode)
{
for (var i = 0; i < IRbuttonCodes.length; i++)
{
if (inButtonCode == IRbuttonCodes[i])
{
return IRbuttonNames[i];
}
}
return "";
}

// add counter and byte length encoding prefix. return Uint8Array of final message
function createMessage(str)
{
Expand Down Expand Up @@ -472,20 +496,20 @@ function playStartUpTones()
var ULTRSONIC_SI_CM = "03";
var ULTRSONIC_SI_INCH = "04";
var ULTRSONIC_DC_CM = "05";
var ULTRSONIC_DC_INCH = "06"; //I'm just putting this in for the sake of knowing I didn't miss any.
var WHY_IS_THERE_A_GAP_HERE = "1F"; //Just so I don't think I'm missing one
var ULTRSONIC_DC_INCH = "06";

var GYRO_SENSOR = "20";
var GYRO_ANGLE = "00";
var GYRO_RATE = "01";
var GYRO_FAST = "02"; //very descriptive, LEGO firmware writers
var GYRO_RATE_AND_ANGLE = "03"; //I kid you not, this is a real thing. WHYYYY?
var GYRO_FAST = "02";
var GYRO_RATE_AND_ANGLE = "03";
var GYRO_CALIBRATION = "04";
var IR_SENSOR = "21";
var IR_PROX = "00";
var IR_SEEKER = "01";
var IR_REMOTE = "02"
var IR_REMOTE_ADVANCE = "03"; //I have no clue what this is.
var IR_CALIBRATION = "05"; //Yep, no clue what some of these do. I don't think many, if any people do.
var IR_REMOTE_ADVANCE = "03";
var IR_CALIBRATION = "05";
var REFLECTED_INTENSITY = "00";
var AMBIENT_INTENSITY = "01";
var COLOR_VALUE = "02";
Expand Down Expand Up @@ -524,6 +548,9 @@ function playStartUpTones()

var colors = [ "none", "black", "blue", "green", "yellow", "red", "white"];

var IRbuttonNames = ['Top Left', 'Bottom Left', 'Top Right', 'Bottom Right', 'Top Bar'];
var IRbuttonCodes = [1, 2, 3, 4, 9];

ext.playTone = function(tone, duration, callback)
{
var freq = frequencies[tone];
Expand Down Expand Up @@ -644,7 +671,10 @@ function playFreqM2M(freq, duration)
driveCallback = callback;
driveTimer = window.setTimeout(function()
{
motorsStop('coast');
if (duration > 0) // allow zero duration to run motors asynchronously
{
motorsStop('coast');
}
callback();
} , duration*1000);
}
Expand All @@ -658,6 +688,15 @@ function playFreqM2M(freq, duration)
}
}

function readIRRemoteSensor(portInt)
{
if (global_sensor_queried[portInt] == 0)
{
global_sensor_queried[portInt]++;
readFromSensor2(portInt, IR_SENSOR, IR_REMOTE);
}
}

ext.whenButtonPressed = function(port)
{
if (!device || !connected)
Expand All @@ -666,7 +705,18 @@ function playFreqM2M(freq, duration)
readTouchSensor(portInt);
return global_touch_pressed[portInt];
}


ext.whenRemoteButtonPressed = function(IRbutton, port)
{
if (!device || !connected)
return false;

var portInt = parseInt(port) - 1;
readIRRemoteSensor(portInt);

return (global_ir_button_pressed[portInt] == IRbutton);
}

ext.readTouchSensorPort = function(port, callback)
{
var portInt = parseInt(port) - 1;
Expand All @@ -692,6 +742,21 @@ function playFreqM2M(freq, duration)
}
}

ext.readGyroPort = function(mode, port, callback)
{
var modeCode = GYRO_ANGLE;
if (mode == 'rate') { modeCode = GYRO_RATE; }

var portInt = parseInt(port) - 1;

waitingCallbacks[portInt].push(callback);
if (global_sensor_queried[portInt] == 0)
{
global_sensor_queried[portInt]++;
readFromSensor2(portInt, GYRO_SENSOR, modeCode);
}
}

ext.readDistanceSensorPort = function(port, callback)
{
var portInt = parseInt(port) - 1;
Expand All @@ -700,9 +765,18 @@ function playFreqM2M(freq, duration)
if (global_sensor_queried[portInt] == 0)
{
global_sensor_queried[portInt]++;
readFromSensor2(portInt, IR_SENSOR, mode0);
readFromSensor2(portInt, IR_SENSOR, IR_PROX);
}
}

ext.readRemoteButtonPort = function(port, callback)
{
var portInt = parseInt(port) - 1;

waitingCallbacks[portInt].push(callback);

readIRRemoteSensor(portInt);
}

function readFromSensor(port, type, mode)
{
Expand Down Expand Up @@ -745,6 +819,7 @@ function playFreqM2M(freq, duration)
}
}

// this routine is awful similar to readFromSensor2...
function readFromAMotor(port, type, mode)
{

Expand Down Expand Up @@ -793,15 +868,18 @@ function playFreqM2M(freq, duration)
var descriptor = {
blocks: [
['w', 'drive %m.dualMotors %m.turnStyle %n seconds', 'steeringControl', 'B+C', 'forward', 3],
[' ', 'start motor %m.whichMotorPort speed %n', 'allMotorsOn', 'B+C', 100],
[' ', 'start motor %m.whichMotorPort speed %n', 'allMotorsOn', 'B+C', 100],
[' ', 'stop all motors %m.breakCoast', 'allMotorsOff', 'break'],
['h', 'when button pressed %m.whichInputPort', 'whenButtonPressed','1'],
['h', 'when button pressed on port %m.whichInputPort', 'whenButtonPressed','1'],
['h', 'when IR remote %m.buttons pressed port %m.whichInputPort', 'whenRemoteButtonPressed','Top Left', '1'],
['R', 'button pressed %m.whichInputPort', 'readTouchSensorPort', '1'],
['w', 'play note %m.note duration %n ms', 'playTone', 'C5', 500],
['w', 'play frequency %n duration %n ms', 'playFreq', '262', 500],
['R', 'light sensor %m.whichInputPort %m.lightSensorMode', 'readColorSensorPort', '1', 'color'],
['R', 'measure distance %m.whichInputPort', 'readDistanceSensorPort', '1'],
['R', 'motor %m.motorInputMode %m.whichMotorIndividual', 'readFromMotor', 'position', 'B'],
['R', 'measure distance %m.whichInputPort', 'readDistanceSensorPort', '1'],
['R', 'remote button %m.whichInputPort', 'readRemoteButtonPort', '1'],
// ['R', 'gyro %m.gyroMode %m.whichInputPort', 'readGyroPort', 'angle', '1'],
['R', 'motor %m.motorInputMode %m.whichMotorIndividual', 'readFromMotor', 'position', 'B'],

// ['R', 'battery level', 'readBatteryLevel'],
// [' ', 'reconnect', 'reconnectToDevice'],
Expand All @@ -813,9 +891,11 @@ function playFreqM2M(freq, duration)
turnStyle: ['forward', 'reverse', 'right', 'left'],
breakCoast: ['break', 'coast'],
lightSensorMode: ['reflected', 'ambient', 'color'],
motorInputMode: ['position', 'speed'],
motorInputMode: ['position', 'speed'],
gyroMode: ['angle', 'rate'],
note:["C4","D4","E4","F4","G4","A4","B4","C5","D5","E5","F5","G5","A5","B5","C6","D6","E6","F6","G6","A6","B6","C#4","D#4","F#4","G#4","A#4","C#5","D#5","F#5","G#5","A#5","C#6","D#6","F#6","G#6","A#6"],
whichInputPort: ['1', '2', '3', '4'],
buttons: IRbuttonNames,
},
};

Expand Down
Loading

0 comments on commit 2654656

Please sign in to comment.