Skip to content

Commit

Permalink
Xbox Controller Plus (#14)
Browse files Browse the repository at this point in the history
* added xbox controller wrapper

* incremented maven version

* inremented version

* not done

* got it working finally

* version

* small changes

* added atomic boolean

* added public member for getting rumble state
  • Loading branch information
BBScholar authored Feb 5, 2019
1 parent a124ad3 commit 1703e60
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ plugins {
repositories {
mavenLocal()
mavenCentral()
maven {
url "http://devsite.ctr-electronics.com/maven/release/"
}
}

dependencies {
Expand All @@ -32,7 +35,7 @@ dependencies {
}

group = 'org.team5499'
version = '2.5.0'
version = '2.6.0'

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.team5499.monkeyLib.hardware

import edu.wpi.first.wpilibj.XboxController

import org.team5499.monkeyLib.util.time.ITimer
import org.team5499.monkeyLib.util.time.WPITimer

import java.util.concurrent.atomic.AtomicBoolean

// adapted from 1323
public class XboxControllerPlus(portNumber: Int, timer: ITimer = WPITimer()) : XboxController(portNumber) {

private val mRumbling: AtomicBoolean
private val mTimer: ITimer

public val isRumbling: Boolean
get() = mRumbling.get()

init {
mRumbling = AtomicBoolean(false)
mTimer = timer
}

public fun cancelRumble() {
mRumbling.set(false)
}

public fun rumble(rumblesPerSecond: Double, numberOfSeconds: Double) {
if (!mRumbling.get()) {
val thread = RumbleThread(rumblesPerSecond, numberOfSeconds)
thread.start()
}
}

private inner class RumbleThread(rumblesPerSecond: Double, numberOfSeconds: Double) : Thread() {
private val mRumblesPerSecond: Double
private val mInterval: Long
private val mSeconds: Double

init {
mRumblesPerSecond = rumblesPerSecond
mSeconds = numberOfSeconds
mInterval = (1 / (rumblesPerSecond * 2.0) * 1000.0).toLong()
}

public override fun start() {
mRumbling.set(true)
mTimer.stop()
mTimer.reset()
mTimer.start()
try {
while (mTimer.get() < mSeconds && mRumbling.get()) {
setRumble(RumbleType.kLeftRumble, 1.0)
setRumble(RumbleType.kRightRumble, 1.0)
sleep(mInterval)
setRumble(RumbleType.kLeftRumble, 0.0)
setRumble(RumbleType.kRightRumble, 0.0)
sleep(mInterval)
}
} catch (e: InterruptedException) {
mRumbling.set(false)
e.printStackTrace()
}
mRumbling.set(false)
}
}
}

0 comments on commit 1703e60

Please sign in to comment.