Afstandbediening voor iPod toegevoegd. Omdat deze met raw codes werkt, is eerst geprobeerd een command line programma (irtoy) te interfacen. Het blijkt makkelijker om de irtoy plugin voor winlirc aan te passen. De command line code wordt alsnog gecommit, om niet verloren te gaan.

This commit is contained in:
2011-07-24 12:41:45 +00:00
parent 1b00e4043f
commit 1dcb88002a
12 changed files with 365 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ package mimis;
import mimis.application.cmd.windows.gomplayer.GomPlayerApplication;
import mimis.application.cmd.windows.winamp.WinampApplication;
import mimis.application.cmd.windows.wmp.WMPApplication;
import mimis.application.irtoy.ipod.iPodApplication;
import mimis.application.itunes.iTunesApplication;
import mimis.application.mpc.MPCApplication;
import mimis.application.vlc.VLCApplication;
@@ -35,7 +36,8 @@ public class Main {
new WMPApplication(),
new MPCApplication(),
new VLCApplication(),
new WinampApplication()};
new WinampApplication(),
new iPodApplication()};
deviceArray = new Device[] {
new LircDevice(),
new WiimoteDevice(),

View File

@@ -0,0 +1,19 @@
package mimis.application.irtoy;
public class Command {
protected String name;
protected String data;
public Command(String name, String data) {
this.name = name;
this.data = data;
}
public String getName() {
return name;
}
public String getData() {
return data;
}
}

View File

@@ -0,0 +1,84 @@
package mimis.application.irtoy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import mimis.util.Reader;
public class Config {
public static final String FILE = "C:\\Users\\Rik\\Dropbox\\Gedeeld\\Mimis\\IRToy\\WinLIRC\\remote.txt";
protected Log log = LogFactory.getLog(getClass());
protected static Config config;
protected File file;
protected ArrayList<Remote> remoteList;
private Config() {
file = new File(FILE);
}
public static Config getInstance() {
if (config == null) {
config = new Config();
}
return config;
}
public Remote getRemote(String name) {
if (remoteList == null) {
remoteList = new ArrayList<Remote>();
try {
parse();
} catch (IOException e) {
log.error(e);
return null;
}
}
for (Remote remote : remoteList) {
if (remote.getName().equals(name)) {
return remote;
}
}
return null;
}
protected void parse() throws IOException {
String contents = Reader.getContents(file);
Pattern pattern = Pattern.compile("begin remote(.+?)end remote", Pattern.DOTALL);
Matcher remoteMatcher = pattern.matcher(contents);
while (remoteMatcher.find()) {
pattern = Pattern.compile("begin raw_codes(.+)end raw_codes", Pattern.DOTALL);
Matcher rawMatcher = pattern.matcher(remoteMatcher.group(1));
if (rawMatcher.find()) {
String raw = rawMatcher.group(1);
pattern = Pattern.compile("name[ ]+(.+)");
Matcher nameMatcher = pattern.matcher(remoteMatcher.group(1));
if (nameMatcher.find()) {
String name = nameMatcher.group(1);
pattern = Pattern.compile("name[ ]+([^\n]+)(.*?)(?=name)", Pattern.DOTALL);
Remote remote = new Remote(name);
Matcher commandMatcher = pattern.matcher(raw);
while (commandMatcher.find()) {
String data = commandMatcher.group(2);
pattern = Pattern.compile("([0-9]+)");
Matcher dataMatcher = pattern.matcher(data);
StringBuffer stringBuffer = new StringBuffer();
while (dataMatcher.find()) {
stringBuffer.append((char) Math.round(Double.valueOf(dataMatcher.group(1)) / 21.3333));
}
stringBuffer.append(0xffff);
Command command = new Command(commandMatcher.group(1), stringBuffer.toString());
remote.add(command);
}
remoteList.add(remote);
}
}
}
}
}

View File

@@ -0,0 +1,30 @@
package mimis.application.irtoy;
import java.util.ArrayList;
public class Remote {
protected String name;
protected ArrayList<Command> commandList;
public Remote(String name) {
this.name = name;
commandList = new ArrayList<Command>();
}
public void add(Command command) {
commandList.add(command);
}
public String getName() {
return name;
}
public Command getCommand(String name) {
for (Command command : commandList) {
if (command.getName().equals(name)) {
return command;
}
}
return null;
}
}

View File

@@ -0,0 +1,139 @@
package mimis.application.irtoy.ipod;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import mimis.Application;
import mimis.Worker;
import mimis.application.irtoy.Command;
import mimis.application.irtoy.Config;
import mimis.application.irtoy.Remote;
import mimis.device.lirc.LircService;
import mimis.device.lirc.remote.WC02IPOButton;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.value.Action;
public class iPodApplication extends Application {
public static void main(String[] args) {
Config config = Config.getInstance();
Remote remote = config.getRemote("WC02-IPO");
if (remote != null) {
Command command = remote.getCommand("PLAY");
if (command != null) {
try {
File file = File.createTempFile("irtoy", ".bin");
file.deleteOnExit();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(command.getData());
bufferedWriter.close();
String cmd = String.format("irtoy -d COM3 -f %s -p -a 0", file.getAbsoluteFile());
System.out.println(cmd);
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
process.getOutputStream().write(byteArrayOutputStream.toByteArray());
System.out.println(byteArrayOutputStream.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
protected static final String TITLE = "iPod";
protected static final boolean QUIT = true;
protected static final int VOLUME_SLEEP = 100;
protected LircService lircService;
protected VolumeWorker volumeWorker;
public iPodApplication() {
super(TITLE);
lircService = new LircService();
lircService.put(WC02IPOButton.NAME, WC02IPOButton.values());
volumeWorker = new VolumeWorker();
}
public void activate() throws ActivateException {
lircService.activate();
super.activate();
}
public boolean active() {
return active = lircService.active();
}
public void deactivate() throws DeactivateException {
super.deactivate();
lircService.deactivate();
}
public void stop() {
super.stop();
volumeWorker.stop();
}
protected void begin(Action action) {
log.trace("iPodApplication begin: " + action);
if (!active) return;
switch (action) {
case VOLUME_UP:
try {
volumeWorker.activate(1);
} catch (ActivateException e) {
log.error(e);
}
break;
case VOLUME_DOWN:
try {
volumeWorker.activate(-1);
} catch (ActivateException e) {
log.error(e);
}
break;
}
}
protected void end(Action action) {
log.trace("iPodApplication end: " + action);
if (!active) return;
switch (action) {
case PLAY:
lircService.send(WC02IPOButton.PLAY);
break;
case NEXT:
lircService.send(WC02IPOButton.NEXT);
break;
case PREVIOUS:
lircService.send(WC02IPOButton.PREVIOUS);
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
break;
}
}
protected class VolumeWorker extends Worker {
protected int volumeChangeRate;
public void activate(int volumeChangeRate) throws ActivateException {
super.activate();
this.volumeChangeRate = volumeChangeRate;
lircService.send(volumeChangeRate > 0 ? WC02IPOButton.PLUS : WC02IPOButton.MINUS);
}
public void work() {
lircService.send(WC02IPOButton.HOLD);
sleep(VOLUME_SLEEP);
}
};
}

View File

@@ -15,7 +15,7 @@ import com.dt.iTunesController.iTunesEventsInterface;
public class iTunesApplication extends Application implements iTunesEventsInterface {
protected static final String TITLE = "iTunes";
protected static final String PROGRAM = "iTunes.exe";
protected static final boolean QUIT = true;
protected static final boolean QUIT = false;
protected static final int VOLUME_CHANGE_RATE = 5;
protected static final int VOLUME_SLEEP = 100;
@@ -28,7 +28,7 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
protected boolean quiting;
public iTunesApplication() {
super(TITLE);
super(TITLE);
iTunes = new iTunes();
volumeWorker = new VolumeWorker();
handle = quiting = false;

View File

@@ -1,7 +1,5 @@
package mimis.device.lirc;
import java.util.HashMap;
import mimis.Button;
import mimis.Device;
import mimis.device.lirc.remote.DenonRC176Button;
@@ -25,17 +23,12 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
public LircDevice() {
super(TITLE);
/* Initialise remotes */
HashMap<String, LircButton[]> buttonMap = new HashMap<String, LircButton[]>();
buttonMap.put(PhiliphsRCLE011Button.NAME, PhiliphsRCLE011Button.values());
buttonMap.put(DenonRC176Button.NAME, DenonRC176Button.values());
buttonMap.put(DenonRC176Button.NAME, DenonRC176Button.values());
multiplexer = new Multiplexer(this);
lircService = new LircService(buttonMap);
lircService = new LircService();
lircService.put(PhiliphsRCLE011Button.NAME, PhiliphsRCLE011Button.values());
lircService.put(DenonRC176Button.NAME, DenonRC176Button.values());
lircService.put(DenonRC176Button.NAME, DenonRC176Button.values());
lircService.add(this);
eventMapCycle = new LircEventMapCycle();
}

View File

@@ -36,18 +36,22 @@ public class LircService extends Worker {
protected HashMap<String, LircButton[]> buttonMap;
protected String send;
public LircService(HashMap<String, LircButton[]> buttonMap) {
this(buttonMap, IP, PORT);
public LircService() {
this(IP, PORT);
buttonMap = new HashMap<String, LircButton[]>();
send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password");
}
public LircService(HashMap<String, LircButton[]> buttonMap, String ip, int port) {
this.buttonMap = buttonMap;
public LircService(String ip, int port) {
this.ip = ip;
this.port = port;
lircButtonListenerList = new ArrayList<LircButtonListener>();
}
public void put(String name, LircButton[] LircButtonArray) {
buttonMap.put(name, LircButtonArray);
}
public void add(LircButtonListener lircButtonListener) {
lircButtonListenerList.add(lircButtonListener);
}
@@ -97,9 +101,13 @@ public class LircService extends Worker {
public void work() {
try {
String string = bufferedReader.readLine();
String line = bufferedReader.readLine();
while (line.equals("BEGIN")) {
while (!bufferedReader.readLine().equals("END"));
line = bufferedReader.readLine();
}
try {
LircButton lircButton = parseButton(new Scanner(string));
LircButton lircButton = parseButton(new Scanner(line));
for (LircButtonListener lircbuttonListener : lircButtonListenerList) {
lircbuttonListener.add(lircButton);
}
@@ -134,27 +142,16 @@ public class LircService extends Worker {
throw new UnknownButtonException();
}
public boolean send(LircButton button) {
return send(button, 0);
public void send(LircButton button) {
send(button, 0);
}
public boolean send(LircButton button, int repeat) {
public void send(LircButton button, int repeat) {
if (send == null) {
return false;
return;
}
String command = String.format("%s %s %s\n", send, button.getName(), button.getCode());
log.debug(command);
String command = String.format("%s %s %s \n", send, button.getName(), button.getCode());
printWriter.append(command);
printWriter.flush();
try {
bufferedReader.readLine();
bufferedReader.readLine();
String result = bufferedReader.readLine();
bufferedReader.readLine();
return result.equals("SUCCESS");
} catch (IOException e) {
log.error(e);
}
return false;
}
}

View File

@@ -0,0 +1,28 @@
package mimis.device.lirc.remote;
import mimis.device.lirc.LircButton;
public enum WC02IPOButton implements LircButton {
MINUS ("MINUS"),
PLUS ("PLUS"),
NEXT ("NEXT"),
PREVIOUS ("PREVIOUS"),
PLAY ("PLAY"),
HOLD ("HOLD");
public static final String NAME = "WC02-IPO";
protected String code;
private WC02IPOButton(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public String getName() {
return NAME;
}
}

View File

@@ -0,0 +1,19 @@
package mimis.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Reader {
public static String getContents(File file) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + "\n");
}
return stringBuffer.toString();
}
}

View File

@@ -13,11 +13,22 @@ import wiiusej.Wiimote;
import mimis.device.wiimote.WiimoteDevice;
import mimis.device.wiimote.WiimoteService;
//Sample Rate - equation is y = -280x + 7280, where x is the actual sample rate. ex: sample rate of 4200 = 0x0B
//Freq (y=real, x=wii) y=-1070*ln(x)+4442.6 of 2788.1*e^(-0.041*x)
public class Sound {
public static final byte PCM = 64;
public static final byte ADPCM = 0;
public static final byte PCM = 0x40; // signed 8-bit PCM
public static final byte ADPCM = 0x00; // Yamaha 4-bit ADPCM
public static final byte BLOCK_SIZE = 20;
public static final int yamaha_indexscale[] = {
230, 230, 230, 230, 307, 409, 512, 614,
230, 230, 230, 230, 307, 409, 512, 614};
public static final int yamaha_difflookup[] = {
1, 3, 5, 7, 9, 11, 13, 15,
-1, -3, -5, -7, -9, -11, -13, -15};
public Object object = new Object();
public static void main(String[] args) {
@@ -30,16 +41,15 @@ public class Sound {
WiimoteDevice wiimoteDevice = new WiimoteDevice();
Wiimote wiimote = wiimoteService.getDevice(wiimoteDevice);
File file = new File("sound2.wav");
File file = new File("sound.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
BufferedSound bufferedSound = bufferSound(audioInputStream);
wiimote.activateSpeaker();
wiimote.setSpeakerConfig(ADPCM, bufferedSound.getSampleRate(), 0.1);
wiimote.setSpeakerConfig(PCM, bufferedSound.getSampleRate(), 1);
AudioFormat audioFormat = audioInputStream.getFormat();
double sampleSizeInBytes = audioFormat.getSampleSizeInBits() / 8D;
double samplesPerBlock = BLOCK_SIZE / sampleSizeInBytes;
@@ -83,15 +93,14 @@ public class Sound {
}
j += read;
} while (j < BLOCK_SIZE);
int length = BLOCK_SIZE;
for (j = 0; j < BLOCK_SIZE; ++j) {
if ((i * 20 + j) > size) {
length = j;
break;
}
sound[i][j + 1] = (byte) (Math.random() * 0xff) ;//block[j];
sound[i][j] = block[j];
//sound[i][j] = 0x34;
sound[i][j] = (byte) (Math.random() * 0xff);
}
sound[i][0] = (byte) (length << 3); // Todo: add later
}
audioInputStream.close();
return new BufferedSound(sound, (int) audioFormat.getSampleRate(), audioFormat.getSampleSizeInBits());