#Lego   #IR   #Android   #Controll   #Library   #Opensource #Easy   #RemoteControl   #RC   #Infrared   #PowerFunctions   #38k  

My library (opensource license, just name me in your project as a project helper) for controlling legoir.

Just create a new LegoIRController(anyActivity) and play with the motors by Motor.R/B0-3.setSpeed(-7 to 7). Don't forget to use
<uses-permission android:name="android.permission.TRANSMIT_IR" /> in your AndroidManifest.xml


package me.antonio.infrared;

import android.annotation.SuppressLint;
import android.hardware.ConsumerIrManager;

public class LegoIRController implements Runnable {

private ConsumerIrManager man;
private static boolean active = false, shouldrun = true;
public static boolean isActive(){return active;}
public static void requestStop(){shouldrun = false;}

public LegoIRController(Activity act){this.man = (ConsumerIrManager)act.getSystemService(Context.CONSUMER_IR_SERVICE);}

private static final int // in ms
START = (int)(26.3 * 39),
ZERO = (int)(26.3 * 10),
ONE = (int)(26.3 * 21),
TR = (int)(26.3 * 6);

private static int i(int data, int pos){
return ((data>>pos)&1) != 0 ? ONE : ZERO;
}

@Override public void run(){
active = true;
try {
while(shouldrun){
// ... send the signals
for(Channel c : Channel.values()){
if(c.RED.active>0 || c.BLUE.active>0){
int s0 = 4 + c.id;
int s1 = c.BLUE.speed.value;
int s2 = c.RED.speed.value;
int s3 = 15 ^ s0 ^ s1 ^ s2;
// send 01id, bbbb, aaaa llll
man.transmit(38000, new int[]{

TR,START,

TR,i(s0,3),
TR,i(s0,2),
TR,i(s0,1),
TR,i(s0,0),

TR,i(s1,3),
TR,i(s1,2),
TR,i(s1,1),
TR,i(s1,0),

TR,i(s2,3),
TR,i(s2,2),
TR,i(s2,1),
TR,i(s2,0),

TR,i(s3,3),
TR,i(s3,2),
TR,i(s3,1),
TR,i(s3,0),

TR,START

});

c.RED.tick();
c.BLUE.tick();
}
}
// ... wait a little
Thread.sleep(50);
}
} catch (InterruptedException e) {}
active = false;
}

public static enum Speed {
FLOAT(0),
P1(1), P2(2), P3(3), P4(4), P5(5), P6(6), P7(7),
BREAK_THEN_FLOAT(-8),
M7(-7), M6(-6), M5(-5), M4(-4), M3(-3), M2(-2), M1(-1);
public final int value;
private Speed(int id){this.value = id;}
public static Speed byInt(int speed){
return Speed.values()[speed & 15];
}
}

public static enum Motor {
R0(0,false),B0(0,true),
R1(1,false),B1(1,true),
R2(2,false),B2(2,true),
R3(3,false),B3(3,true);
public final boolean blue;
public final int channel;
private Speed speed = Speed.FLOAT;
private int active = 0;
private Motor(int channel, boolean blue){
this.channel = channel;
this.blue = blue;
}

public Speed getSpeed(){return speed;}
public void setSpeed(Speed speed){
if(speed==this.speed){
return;
} else if(speed!=Speed.FLOAT && speed!=Speed.BREAK_THEN_FLOAT){active = 3;}
this.speed = speed;
}


public void setSpeed(int i) {
setSpeed(Speed.byInt(i));
}

private void tick(){
if((speed == Speed.FLOAT || speed == Speed.BREAK_THEN_FLOAT) && active>0){
active--;
}
}
}

public static enum Channel {
C0(0, Motor.R0, Motor.B0),
C1(1, Motor.R1, Motor.B1),
C2(2, Motor.R2, Motor.B2),
C3(3, Motor.R3, Motor.B3);
public final Motor RED, BLUE;
public final int id;
public final Motor[] motors;
private Channel(int id, Motor red, Motor blue){
this.id=id;RED=red;BLUE=blue;
motors = new Motor[]{red,blue};
}
}
}

254115136s ago, by Antonio