import keyboardJS from 'keyboardjs';

export const keyboard = function(motors) {

    var intervalUp,
        intervalDown,
        intervalLeft,
        intervalRight;

    const UPDATE_INTERVAL = 100;
    const INC = 10;
    const SPEED_LIMIT = 100;

    var speed = 0;

    // FORWARD
    keyboardJS.bind('w', function(e) {
        e.preventRepeat();
        console.log("[keyboard] UP");
        intervalUp = setInterval(function() {
            if (speed <= SPEED_LIMIT) {

                motors.set(speed, motors.direction.forward);
                console.log('[keyboard]', speed);
                speed = speed + INC;
            }
        }, UPDATE_INTERVAL);
    }, function(e) {
        clearInterval(intervalUp);
        speed = 0;
        motors.stop();
    });

    // BACKWARD
    keyboardJS.bind('s', function (e) {
        e.preventRepeat();
        console.log("[keyboard] DOWN");
        intervalDown = setInterval(function () {
            if (speed <= SPEED_LIMIT) {

                motors.set(speed, motors.direction.backward);
                console.log('[keyboard]', speed);
                speed = speed + INC;
            }
        }, UPDATE_INTERVAL);

    }, function (e) {
        clearInterval(intervalDown);
        speed = 0;
        motors.stop();
    });

    // LEFT
    keyboardJS.bind('a', function (e) {
        e.preventRepeat();
        console.log("[keyboard] LEFT");
        intervalLeft = setInterval(function () {
            if (speed <= SPEED_LIMIT) {

                motors.set(speed, motors.direction.left);
                console.log('[keyboard]', speed);
                speed = speed + INC;
            }
        }, UPDATE_INTERVAL);

    }, function (e) {
        clearInterval(intervalLeft);
        speed = 0;
        motors.stop();
    });

    // RIGHT
    keyboardJS.bind('d', function (e) {
        e.preventRepeat();
        console.log("[keyboard] RIGHT");
        intervalRight = setInterval(function () {
            if (speed <= SPEED_LIMIT) {

                motors.set(speed, motors.direction.right);
                console.log('[keyboard]', speed);
                speed = speed + INC;
            }
        }, UPDATE_INTERVAL);

    }, function (e) {
        clearInterval(intervalRight);
        speed = 0;
        motors.stop();
    });
};
@@ -206,19 +206,26 @@
   const 
-INC
+SPEED_STEP
  = 10;%0A 
@@ -614,35 +614,42 @@
 speed = speed + 
-INC
+SPEED_STEP
 ;%0A            %7D%0A
@@ -1133,35 +1133,42 @@
 speed = speed + 
-INC
+SPEED_STEP
 ;%0A            %7D%0A
@@ -1648,35 +1648,42 @@
 speed = speed + 
-INC
+SPEED_STEP
 ;%0A            %7D%0A
@@ -2179,11 +2179,18 @@
 d + 
-INC
+SPEED_STEP
 ;%0A  

rename INC to SPEED_STEP