diff --git a/java/src/mimis/Main.java b/java/src/mimis/Main.java index 9ccd0d3..b3744a8 100644 --- a/java/src/mimis/Main.java +++ b/java/src/mimis/Main.java @@ -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(), diff --git a/java/src/mimis/application/irtoy/Command.java b/java/src/mimis/application/irtoy/Command.java new file mode 100644 index 0000000..f568d1c --- /dev/null +++ b/java/src/mimis/application/irtoy/Command.java @@ -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; + } +} diff --git a/java/src/mimis/application/irtoy/Config.java b/java/src/mimis/application/irtoy/Config.java new file mode 100644 index 0000000..4d394c6 --- /dev/null +++ b/java/src/mimis/application/irtoy/Config.java @@ -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 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(); + 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); + } + } + } + } +} diff --git a/java/src/mimis/application/irtoy/Remote.java b/java/src/mimis/application/irtoy/Remote.java new file mode 100644 index 0000000..1131940 --- /dev/null +++ b/java/src/mimis/application/irtoy/Remote.java @@ -0,0 +1,30 @@ +package mimis.application.irtoy; + +import java.util.ArrayList; + +public class Remote { + protected String name; + protected ArrayList commandList; + + public Remote(String name) { + this.name = name; + commandList = new ArrayList(); + } + + 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; + } +} diff --git a/java/src/mimis/application/irtoy/ipod/iPodApplication.java b/java/src/mimis/application/irtoy/ipod/iPodApplication.java new file mode 100644 index 0000000..248a39a --- /dev/null +++ b/java/src/mimis/application/irtoy/ipod/iPodApplication.java @@ -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); + } + }; +} \ No newline at end of file diff --git a/java/src/mimis/application/itunes/iTunesApplication.java b/java/src/mimis/application/itunes/iTunesApplication.java index 707848d..950732d 100644 --- a/java/src/mimis/application/itunes/iTunesApplication.java +++ b/java/src/mimis/application/itunes/iTunesApplication.java @@ -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; diff --git a/java/src/mimis/device/lirc/LircDevice.java b/java/src/mimis/device/lirc/LircDevice.java index 6a51699..c270b1b 100644 --- a/java/src/mimis/device/lirc/LircDevice.java +++ b/java/src/mimis/device/lirc/LircDevice.java @@ -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 buttonMap = new HashMap(); - 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(); } diff --git a/java/src/mimis/device/lirc/LircService.java b/java/src/mimis/device/lirc/LircService.java index f5b27d4..f0f0297 100644 --- a/java/src/mimis/device/lirc/LircService.java +++ b/java/src/mimis/device/lirc/LircService.java @@ -36,18 +36,22 @@ public class LircService extends Worker { protected HashMap buttonMap; protected String send; - public LircService(HashMap buttonMap) { - this(buttonMap, IP, PORT); + public LircService() { + this(IP, PORT); + buttonMap = new HashMap(); send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password"); } - public LircService(HashMap buttonMap, String ip, int port) { - this.buttonMap = buttonMap; + public LircService(String ip, int port) { this.ip = ip; this.port = port; lircButtonListenerList = new ArrayList(); } + 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; } } diff --git a/java/src/mimis/device/lirc/remote/WC02IPOButton.java b/java/src/mimis/device/lirc/remote/WC02IPOButton.java new file mode 100644 index 0000000..4894e88 --- /dev/null +++ b/java/src/mimis/device/lirc/remote/WC02IPOButton.java @@ -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; + } +} diff --git a/java/src/mimis/util/Reader.java b/java/src/mimis/util/Reader.java new file mode 100644 index 0000000..271860d --- /dev/null +++ b/java/src/mimis/util/Reader.java @@ -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(); + } +} diff --git a/java/src/mimis/util/Sound.java b/java/src/mimis/util/Sound.java index 9e22935..7c477ab 100644 --- a/java/src/mimis/util/Sound.java +++ b/java/src/mimis/util/Sound.java @@ -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()); diff --git a/java/wiiuse.dll b/java/wiiuse.dll index a9c5291..0e46215 100644 Binary files a/java/wiiuse.dll and b/java/wiiuse.dll differ