Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Podogov committed Nov 14, 2017
0 parents commit 1cefb0f
Show file tree
Hide file tree
Showing 69 changed files with 2,872 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "27.0.0"
defaultConfig {
applicationId "com.ginkage.gamepad"
minSdkVersion 26
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.guava:guava:23.0-android'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
implementation project(':bthid')
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ginkage.gamepad">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.GamepadActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ginkage.gamepad.bluetooth;

/** Interface to send the Battery data with. */
interface BatteryDataSender {
/**
* Send the Battery data to the connected HID Host device.
*
* @param level Current battery level
*/
void sendBatteryLevel(float level);
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/ginkage/gamepad/bluetooth/BatteryReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ginkage.gamepad.bluetooth;

/** Helper class to store the battery state and retrieve the binary report. */
public class BatteryReport {
private final byte[] batteryData = new byte[] {0};

/**
* Store the current battery level in the report.
*
* @param level Battery level, must be in the [0.0, 1.0] interval
* @return Byte array that represents the report
*/
public byte[] setValue(float level) {
int val = (int) Math.ceil(level * 255);
batteryData[0] = (byte) (val & 0xff);
return batteryData;
}

public byte[] getReport() {
return batteryData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ginkage.gamepad.bluetooth;

import android.support.annotation.MainThread;

/** Helper class to register for the device state broadcasts. */
public interface DeviceStateBus {
/**
* Start listening for device connection state changes.
*
* @param listener Callback that will receive the new device connection state.
*/
@MainThread
void register(DeviceStateListener listener);

/** Stop listening for device connection state changes. */
@MainThread
void unregister();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ginkage.gamepad.bluetooth;

import android.bluetooth.BluetoothDevice;

/** Used to call back when a device connection state has changed. */
public interface DeviceStateListener {
/**
* Callback that receives the new device connection state.
*
* @param device Device that was connected or disconnected.
* @param state New connection state, see {@link BluetoothProfile.EXTRA_STATE}.
*/
void onDeviceStateChanged(BluetoothDevice device, int state);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ginkage.gamepad.bluetooth;

/** Interface to send the Mouse data with. */
public interface GamepadDataSender {
/**
* Send the Gamepad data to the connected HID Host device.
*/
void sendGamepad(GamepadState state);
}
42 changes: 42 additions & 0 deletions app/src/main/java/com/ginkage/gamepad/bluetooth/GamepadReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ginkage.gamepad.bluetooth;

import java.util.Arrays;

public class GamepadReport {
private final byte[] gamepadData = "BDLLRRTT".getBytes();

public GamepadReport() {
Arrays.fill(gamepadData, (byte) 0);
}

public byte[] setValue(GamepadState s) {
// 11 buttons: A, B, X, Y, L1, R1, L3, R3, Power, Back, Home (1 bit per button), 1 bit padding
// 4 bits for D-pad rotation values 0-7 -> 0-315 (360 - 45)
// 6x 8-bit values for LX/LY, RX/RY, L2/R2
gamepadData[0] = 0;
gamepadData[0] |= (byte) (s.a ? 0x01 : 0);
gamepadData[0] |= (byte) (s.b ? 0x02 : 0);
gamepadData[0] |= (byte) (s.x ? 0x04 : 0);
gamepadData[0] |= (byte) (s.y ? 0x08 : 0);
gamepadData[0] |= (byte) (s.l1 ? 0x10 : 0);
gamepadData[0] |= (byte) (s.r1 ? 0x20 : 0);
gamepadData[0] |= (byte) (s.l3 ? 0x40 : 0);
gamepadData[0] |= (byte) (s.r3 ? 0x80 : 0);
gamepadData[1] = 0;
gamepadData[1] |= (byte) (s.power ? 0x01 : 0);
gamepadData[1] |= (byte) (s.back ? 0x02 : 0);
gamepadData[1] |= (byte) (s.home ? 0x04 : 0);
gamepadData[1] |= (byte) (s.dpad << 4);
gamepadData[2] = (byte) s.lx;
gamepadData[3] = (byte) s.ly;
gamepadData[4] = (byte) s.rx;
gamepadData[5] = (byte) s.ry;
gamepadData[6] = (byte) s.l2;
gamepadData[7] = (byte) s.r2;
return gamepadData;
}

public byte[] getReport() {
return gamepadData;
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/ginkage/gamepad/bluetooth/GamepadState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ginkage.gamepad.bluetooth;

public class GamepadState {
public boolean a;
public boolean b;
public boolean x;
public boolean y;
public boolean l1;
public boolean r1;
public boolean l3;
public boolean r3;
public boolean power;
public boolean back;
public boolean home;

// 0=up, 2=right, 4=down, 6=left, 8=release
public int dpad;

// Sticks: Up=0, Down=255, Left=0, Right=255, Center=128
public int lx;
public int ly;
public int rx;
public int ry;

// Triggers: Released=0, Pressed=255
public int l2;
public int r2;
}
Loading

0 comments on commit 1cefb0f

Please sign in to comment.