diff --git a/js/examples/test-all-classes.js b/js/examples/test-all-classes.js deleted file mode 100644 index 2d768a1..0000000 --- a/js/examples/test-all-classes.js +++ /dev/null @@ -1,83 +0,0 @@ -var ev3dev = require('../bin/index.js'); - -// Run motor -console.log('Motor --------------'); -var motor = new ev3dev.Motor(); -['dutyCycle', 'dutyCycleSp', 'portName', 'position', - 'positionSp', 'pulsesPerSecond', 'pulsesPerSecondSp', - 'rampDownSp', 'rampUpSp', 'regulationMode', 'run', - 'runMode', 'speedRegulationP', 'state', 'stopMode', - 'stopModes', 'timeSp', 'type' -].forEach(function (value) { - console.log(value + ": " + motor[value]); -}); - -console.log('Setting motor properties...'); -motor.rampUpSp = 100; -motor.rampDownSp = 100; -motor.runMode = 'time'; -motor.timeSp = 1000; -motor.dutyCycleSp = 50; -console.log('Running motor...'); -motor.run = 1; -console.log('--------------------'); - -//Read sensor -console.log('Sensor -------------'); -var sensor = new ev3dev.Sensor(); -[ 'portName', 'numValues', 'typeName', - 'mode', 'modes' -].forEach(function (value) { - console.log(value + ": " + sensor[value]); -}); - -console.log('Testing sensor read...'); -for(var i = 0; i < sensor.numValues; i++) { - console.log('value ' + i + ': ' + sensor.getValue(i) + ', ' + sensor.getFloatValue(i)); -} -console.log('--------------------') -console.log("Core motor and sensor test complete"); - -console.log('Battery ------------'); -var battery = new ev3dev.PowerSupply(); -[ 'currentNow', 'voltageNow', 'voltageMaxDesign', - 'voltageMinDesign', 'technology', 'type', - 'currentAmps', 'voltageVolts' -].forEach(function (value) { - console.log(value + ": " + battery[value]); -}); -console.log('--------------------') -console.log('LED ----------------'); -var leds = []; -[ 'ev3:green:left', 'ev3:red:left', - 'ev3:green:right', 'ev3:red:right' -].forEach(function (value) { - leds.push(new ev3dev.LED(value)); -}); - -//Just running through the properties isn't helpful as a test -// This will transition through the range of colors - -for(var i = 0; i < 1; i += (1/10)) { - for(var ledI = 0; ledI < leds.length; ledI++) { - var val = i * leds[ledI].maxBrightness; - if(ledI % 2 == 0) - val = (1 - i) * leds[ledI].maxBrightness; - - leds[ledI].brightness = Math.round(val); - } - - { //Hack to sleep for time - // SHOULD NOT BE USED IN PRODUCTION CODE - var stop = new Date().getTime(); - while(new Date().getTime() < stop + 100) { - ; - } - } -} - -for(var i in leds) { - leds[i].brightness = 0; -} - -console.log('--------------------') \ No newline at end of file diff --git a/js/examples/test-battery-led.js b/js/examples/test-battery-led.js new file mode 100644 index 0000000..8ff6590 --- /dev/null +++ b/js/examples/test-battery-led.js @@ -0,0 +1,52 @@ +var ev3dev = require('../bin/index.js'); + + +console.log('Battery ------------'); +var battery = new ev3dev.PowerSupply(); +[ 'currentNow', 'voltageNow', 'voltageMaxDesign', + 'voltageMinDesign', 'technology', 'type', + 'currentAmps', 'voltageVolts' +].forEach(function (value) { + console.log(value + ": " + battery[value]); +}); +console.log('--------------------') +console.log('LED ----------------'); +console.log('fading LEDs from green to red...'); +var leds = []; +[ 'ev3:green:left', 'ev3:red:left', + 'ev3:green:right', 'ev3:red:right' +].forEach(function (value) { + leds.push(new ev3dev.LED(value)); +}); + +//Just running through the properties isn't helpful as a test +// This will transition through the range of colors + +for(var i = 0; i < 100; i++) { + for(var ledI = 0; ledI < leds.length; ledI++) { + var val = (i / 100) * leds[ledI].maxBrightness; + if(ledI % 2 == 0) + val = (100 - i) / 100 * leds[ledI].maxBrightness; + + leds[ledI].brightness = Math.round(val); + } + + if(i % 10 == 0) + console.log(i + '%'); + + { //Hack to sleep for time + // SHOULD NOT BE USED IN PRODUCTION CODE + var stop = new Date().getTime(); + while(new Date().getTime() < stop + 100) { + ; + } + } +} + +console.log('done'); + +for(var i in leds) { + leds[i].brightness = 0; +} + +console.log('--------------------') \ No newline at end of file diff --git a/js/examples/test-motor-sensor.js b/js/examples/test-motor-sensor.js new file mode 100644 index 0000000..07ec7a2 --- /dev/null +++ b/js/examples/test-motor-sensor.js @@ -0,0 +1,39 @@ +var ev3dev = require('../bin/index.js'); + +// Run motor +console.log('Motor --------------'); +var motor = new ev3dev.Motor(); +['dutyCycle', 'dutyCycleSp', 'portName', 'position', + 'positionSp', 'pulsesPerSecond', 'pulsesPerSecondSp', + 'rampDownSp', 'rampUpSp', 'regulationMode', 'run', + 'runMode', 'speedRegulationP', 'state', 'stopMode', + 'stopModes', 'timeSp', 'type' +].forEach(function (value) { + console.log(value + ": " + motor[value]); +}); + +console.log('Setting motor properties...'); +motor.rampUpSp = 100; +motor.rampDownSp = 100; +motor.runMode = 'time'; +motor.timeSp = 1000; +motor.dutyCycleSp = 50; +console.log('Running motor...'); +motor.run = 1; +console.log('--------------------'); + +//Read sensor +console.log('Sensor -------------'); +var sensor = new ev3dev.Sensor(); +[ 'portName', 'numValues', 'typeName', + 'mode', 'modes' +].forEach(function (value) { + console.log(value + ": " + sensor[value]); +}); + +console.log('Testing sensor read...'); +for(var i = 0; i < sensor.numValues; i++) { + console.log('value ' + i + ': ' + sensor.getValue(i) + ', ' + sensor.getFloatValue(i)); +} +console.log('--------------------') +console.log("Core motor and sensor test complete"); \ No newline at end of file diff --git a/js/motor.ts b/js/motor.ts index d042399..f00d350 100644 --- a/js/motor.ts +++ b/js/motor.ts @@ -42,10 +42,10 @@ class MotorBase extends Device { (port == ports.OUTPUT_AUTO) || (port == undefined) || (portName === port) - ) && ( + ) && ( (type == undefined || type == '') || motorType == type - ); + ); if (satisfiesCondition) { this._deviceIndex = Number(file.substring('motor'.length)); @@ -103,7 +103,7 @@ class Motor extends MotorBase { constructor(port: string, type: string) { this.deviceDir = '/sys/class/tacho-motor/'; - + super(port, type); } @@ -291,7 +291,7 @@ class DCMotor extends MotorBase { constructor(port: string) { this.deviceDir = '/sys/class/dc-motor/'; - + super(port); } @@ -299,7 +299,7 @@ class DCMotor extends MotorBase { set command(value: string) { this.setString("command", value); } - + get commands(): string[] { return this.getString("commands").split(' '); } @@ -310,7 +310,7 @@ class DCMotor extends MotorBase { set dutyCycle(value: number) { this.setNumber("duty_cycle", value); } - + get typeName(): string { return this.getString("name"); } @@ -325,14 +325,14 @@ class DCMotor extends MotorBase { set rampDownMs(value: number) { this.setNumber("ramp_down_ms", value); } - + get rampUpMs(): number { return this.getNumber("ramp_up_ms"); } set rampUpMs(value: number) { this.setNumber("ramp_up_ms", value); } - + get polarity(): string { return this.getString("polarity"); } @@ -340,16 +340,16 @@ class DCMotor extends MotorBase { this.setString("polarity", value); } } - + //Servo Motor class ServoMotor extends MotorBase { constructor(port: string) { this.deviceDir = '/sys/class/servo-motor/'; - + super(port); } - + //PROPERTIES get command(): string { return this.getString("command"); @@ -357,7 +357,7 @@ class ServoMotor extends MotorBase { set command(value: string) { this.setString("command", value); } - + get typeName(): string { return this.getString("name"); } @@ -372,35 +372,35 @@ class ServoMotor extends MotorBase { set maxPulseMs(value: number) { this.setNumber("max_pulse_ms", value); } - + get midPulseMs(): number { return this.getNumber("mid_pulse_ms"); } set midPulseMs(value: number) { this.setNumber("mid_pulse_ms", value); } - + get minPulseMs(): number { return this.getNumber("min_pulse_ms"); } set minPulseMs(value: number) { this.setNumber("min_pulse_ms", value); } - + get polarity(): string { return this.getString("polarity"); } set polarity(value: string) { this.setString("polarity", value); } - + get position(): number { return this.getNumber("position"); } set position(value: number) { this.setNumber("position", value); } - + get rate(): number { return this.getNumber("rate"); }