Android Open Source - HexNanoController_Android Transmitter






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android 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

package com.hexairbot.hexmini.modal;
//from  www .j  av  a 2s.  c  o m
import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;

import com.hexairbot.hexmini.ble.BleConnectinManager;

public class Transmitter {
  private static final int  CHANNEL_COUNT = 8;
  private static final int  FPS = 14; //max 17
  
  private static Transmitter sharedTransmitter; 
  private BleConnectinManager bleConnectionManager;
  private Timer timer;
  private byte dataPackage[] = new byte[11];
  private float[] channelList = new float[CHANNEL_COUNT];
  
  public BleConnectinManager getBleConnectionManager() {
    return bleConnectionManager;
  }

  public void setBleConnectionManager(BleConnectinManager bleConnectionManager) {
    this.bleConnectionManager = bleConnectionManager;
  }

  private Transmitter(){
    super();
    //bleConnectionManager = new BleConnectinManager();
  }
  
  public static Transmitter sharedTransmitter(){
    if (sharedTransmitter == null) {
      sharedTransmitter = new Transmitter();
    }
    
    return sharedTransmitter;
  }
  
  Handler handler = new Handler();
  
  public void start(){
    stop();
    
    initDataPackage();
    
    timer = new Timer();
    timer.schedule(new TimerTask() {
      
      @Override
      public void run() {
        transmmit();
      }
    }, 0, 1000 / FPS);
  }
  
  public void stop(){
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }
  
  public void transmmitData(byte[] data){
    if (bleConnectionManager != null && bleConnectionManager.isConnected() && data != null){
      bleConnectionManager.sendData(data);
    }
  }
  
  public boolean transmmitSimpleCommand(OSDCommon.MSPCommnand commnand){
    transmmitData(OSDCommon.getSimpleCommand(commnand));
    return true;
  }
  
  private void transmmit(){
    updateDataPackage();
      if (bleConnectionManager != null && bleConnectionManager.isConnected() && dataPackage != null) {
      bleConnectionManager.sendData(dataPackage);
    }
    /*
    handler.post(new Runnable() {
      
      @Override
      public void run() {
        Log.e("TEST", "handler.post");
        
        updateDataPackage();
          if (bleConnectionManager != null && bleConnectionManager.isConnected() && dataPackage != null) {
          bleConnectionManager.sendData(dataPackage);
        }
      }
    });*/
  }
  
  private void initDataPackage(){
    dataPackage[0] = '$';
    dataPackage[1] = 'M';
    dataPackage[2] = '<';
    dataPackage[3] = 4;
    dataPackage[4] = (byte)(OSDCommon.MSPCommnand.MSP_SET_RAW_RC_TINY.value());
    
    updateDataPackage();
  }
  
  public void setChannel(int channeIdx, float value){
    channelList[channeIdx] = value;
  }
  
  int check = 0;

  //?????????????????????????5??????????
    private void updateDataPackage(){
    byte checkSum = 0;
      
      int dataSizeIdx = 3;
      int checkSumIdx = 10;
      
      dataPackage[dataSizeIdx] = 5;
      
      checkSum ^= (dataPackage[dataSizeIdx] & 0xFF);
      checkSum ^= (dataPackage[dataSizeIdx + 1] & 0xFF);
      
      /*
      if (check == 0) {
        channelList[0] = -0.5f; 
        check = 1;
    }
      else{
        channelList[0] = 0.5f; 
        check = 0;
      }*/
      
      
      
      for(int channelIdx = 0; channelIdx < CHANNEL_COUNT - 4; channelIdx++){
          float scale =  channelList[channelIdx];
          
          
          if (scale > 1) {
              scale = 1;
          }
          else if(scale < -1){
              scale = -1;
          }
          
          byte pulseLen =  (byte) ((int)(Math.abs(500 + 500 * scale)) / 4);
      
          dataPackage[5 + channelIdx] = (byte) pulseLen;
          
          checkSum ^= (dataPackage[5 + channelIdx] & 0xFF);
      }
      
      byte auxChannels = 0x00;
      
      float aux1Scale = channelList[4];
      
      if (aux1Scale < -0.666) {
          auxChannels |= 0x00;
      }
      else if(aux1Scale < 0.3333){
          auxChannels |= 0x40;
      }
      else{
          auxChannels |= 0x80;
      }
      
      float aux2Scale = channelList[5];
      
      if (aux2Scale < -0.666) {
          auxChannels |= 0x00;
      }
      else if(aux2Scale < 0.3333){
          auxChannels |= 0x10;
      }
      else{
          auxChannels |= 0x20;
      }
      
      float aux3Scale = channelList[6];
      
      if (aux3Scale < -0.666) {
          auxChannels |= 0x00;
      }
      else if(aux3Scale < 0.3333){
          auxChannels |= 0x04;
      }
      else{
          auxChannels |= 0x08;
      }
      
      float aux4Scale = channelList[7];
      
      if (aux4Scale < -0.666) {
          auxChannels |= 0x00;
      }
      else if(aux4Scale < 0.3333){
          auxChannels |= 0x01;
      }
      else{
          auxChannels |= 0x02;
      }
      
      dataPackage[5 + 4] = (byte) auxChannels;
      checkSum ^= (dataPackage[5 + 4] & 0xFF);
         
      dataPackage[checkSumIdx] = (byte) checkSum;
  }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java