Android Open Source - MachinaMidiJacketSDK Jacket O S C Server






From Project

Back to project page MachinaMidiJacketSDK.

License

The source code is released under:

GNU General Public License

If you think the Android project MachinaMidiJacketSDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//
//  JacketOSCServer.java
//  JacketSDK/*  w  w w .  j  a  v  a2 s  .co m*/
//
//  Created by Henry Serrano on 6/20/14.
//  Copyright (c) 2014 Machina Wearable Technology SAPI de CV.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
package com.machina.jacketsdk;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import android.util.Log;

public class JacketOSCServer extends Thread{
  private JacketOSCServerListener delegate;
  private Integer receivePort;
  
  public JacketOSCServer(JacketOSCServerListener delegate, Integer receivePort) {
    this.delegate = delegate;
    this.receivePort = receivePort;
  }
  
    public void run(){
      try{
      DatagramSocket serverSocket = new DatagramSocket(receivePort);             
      byte[] receiveData = new byte[1024];             
      
      Log.d("OSC Server Started", "True");
      
      while(true){        
        try{
          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);                   
          serverSocket.receive(receivePacket);           
          
          //Log.d("Received Packet", "a");
          OSCMessage message = OSCParser.parseOSCMessage(receivePacket.getData());
          
          if(message.messageExists){
            //Log.d("OSC Address", message.address);
            if(message.address.equals("/ker")){
              if(message.arguments.size() == 11){
                //Log.d("Arguments", "11");
                JacketData jacketData = new JacketData();
                jacketData.mode = JacketData.KERMODE;
                
                Number auxNumber = (Number)message.arguments.get(0);
                Integer auxInteger = auxNumber.intValue();
                
                jacketData.membrane = (auxInteger & 0xFF0000) >> 16;
                jacketData.drumpad = auxInteger & 0xFF;
                
                /* NERVA */
                auxNumber = (Number)message.arguments.get(1);
                auxInteger = auxNumber.intValue();
                
                jacketData.nerva_accY = (auxInteger & 0xFF0000) >> 16;
                jacketData.nerva_accX = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(2);
                auxInteger = auxNumber.intValue();
                
                jacketData.nerva_accZ = (auxInteger & 0xFF0000) >> 16;
                jacketData.nerva_gyroZ = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(3);
                auxInteger = auxNumber.intValue();
                
                jacketData.nerva_gyroY = (auxInteger & 0xFF0000) >> 16;
                jacketData.nerva_gyroX = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(4);
                auxInteger = auxNumber.intValue();
                
                jacketData.nerva_switch1 = (auxInteger & 0xFF0000) >> 16;
                jacketData.nerva_switch2 = auxInteger & 0xFF;
                
                /* NERVB */
                auxNumber = (Number)message.arguments.get(5);
                auxInteger = auxNumber.intValue();
                
                jacketData.nervb_accY = (auxInteger & 0xFF0000) >> 16;
                jacketData.nervb_accX = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(6);
                auxInteger = auxNumber.intValue();
                
                jacketData.nervb_accZ = (auxInteger & 0xFF0000) >> 16;
                jacketData.nervb_gyroZ = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(7);
                auxInteger = auxNumber.intValue();
                
                jacketData.nervb_gyroY = (auxInteger & 0xFF0000) >> 16;
                jacketData.nervb_gyroX = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(8);
                auxInteger = auxNumber.intValue();
                
                jacketData.nervb_switch1 = (auxInteger & 0xFF0000) >> 16;
                jacketData.nervb_switch2 = auxInteger & 0xFF;
                
                //Pitch - Roll
                auxNumber = (Number)message.arguments.get(9);
                auxInteger = auxNumber.intValue();
                
                jacketData.nerva_pitch = (auxInteger & 0xFF0000) >> 16;
                jacketData.nerva_roll = auxInteger & 0xFF;
                
                auxNumber = (Number)message.arguments.get(10);
                auxInteger = auxNumber.intValue();
                
                jacketData.nervb_pitch = (auxInteger & 0xFF0000) >> 16;
                jacketData.nervb_roll = auxInteger & 0xFF;
                
                delegate.didReceiveData(jacketData);
              }
            }else if(message.address.equals("/nerva/pitch")){
              JacketData jacketData = new JacketData();
              jacketData.mode = JacketData.KERPITCH;
              
              Number auxNumber = (Number)message.arguments.get(0);
              //Log.d("NERVA PITCH", String.valueOf(auxNumber.intValue()));
              jacketData.nerva_pitch = auxNumber.intValue();
              
              delegate.didReceiveData(jacketData);
            }else if(message.address.equals("/nervb/roll")){
              JacketData jacketData = new JacketData();
              jacketData.mode = JacketData.KERROLL;
              
              Number auxNumber = (Number)message.arguments.get(0);
              //Log.d("NERVA PITCH", String.valueOf(auxNumber.intValue()));
              jacketData.nervb_roll = auxNumber.intValue();
              
              delegate.didReceiveData(jacketData);
            }
          }
        }catch(IOException ioEx){
          Log.d("IOException", ioEx.toString());
        }               
      }
    }catch(SocketException sEx){
      Log.d("SocketException", sEx.toString());
    }
    }
}




Java Source Code List

com.machina.jacketsdk.JacketData.java
com.machina.jacketsdk.JacketOSCServerListener.java
com.machina.jacketsdk.JacketOSCServer.java
com.machina.jacketsdk.OSCMessage.java
com.machina.jacketsdk.OSCParser.java