Skip to content

Commit

Permalink
js: finish tests and fully implement and test all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
WasabiFan committed Nov 28, 2014
1 parent 03063dd commit 92da121
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 100 deletions.
83 changes: 0 additions & 83 deletions js/examples/test-all-classes.js

This file was deleted.

52 changes: 52 additions & 0 deletions js/examples/test-battery-led.js
Original file line number Diff line number Diff line change
@@ -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('--------------------')
39 changes: 39 additions & 0 deletions js/examples/test-motor-sensor.js
Original file line number Diff line number Diff line change
@@ -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");
34 changes: 17 additions & 17 deletions js/motor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -103,7 +103,7 @@ class Motor extends MotorBase {

constructor(port: string, type: string) {
this.deviceDir = '/sys/class/tacho-motor/';

super(port, type);
}

Expand Down Expand Up @@ -291,15 +291,15 @@ class DCMotor extends MotorBase {

constructor(port: string) {
this.deviceDir = '/sys/class/dc-motor/';

super(port);
}

//PROPERTIES
set command(value: string) {
this.setString("command", value);
}

get commands(): string[] {
return this.getString("commands").split(' ');
}
Expand All @@ -310,7 +310,7 @@ class DCMotor extends MotorBase {
set dutyCycle(value: number) {
this.setNumber("duty_cycle", value);
}

get typeName(): string {
return this.getString("name");
}
Expand All @@ -325,39 +325,39 @@ 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");
}
set polarity(value: string) {
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");
}
set command(value: string) {
this.setString("command", value);
}

get typeName(): string {
return this.getString("name");
}
Expand All @@ -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");
}
Expand Down

0 comments on commit 92da121

Please sign in to comment.