Skip to content

Commit

Permalink
Merge pull request #1 from pacoito123/untested
Browse files Browse the repository at this point in the history
2019-07-06 Merge
  • Loading branch information
pacoito123 authored Jul 6, 2019
2 parents dc8bd20 + bdd1d98 commit 22f880e
Show file tree
Hide file tree
Showing 33 changed files with 1,712 additions and 718 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
24 changes: 24 additions & 0 deletions BSD_License_for_WPILib_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
* Copyright (c) 2009 FIRST
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the FIRST nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY FIRST AND CONTRIBUTORS``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FIRST OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,103 @@
# lib6647
Team 6647's library to allow JSON-oriented object building and initialization.

Team 6647's library for JSON-oriented object initialization, among other things.
<p align="left"><a href="https://github.com/pacoito123/lib6647" target="_blank"><img src="https://i.imgur.com/F4focyC.png"></a></p>

## Getting Started

### Usage

Team 6647 encourages the usage of this library by any team that may find it useful, period. Giving us credit is optional, but greatly appreciated. Please feel free to request anything your team might need that could be added to this library.

### Compiling lib6647 to your Robot

**lib6647** must be added as a dependency in your Robot's _build.gradle_ file, as well as the **json-simple** library that it requires.

First, add the following url to your Maven repositories in _build.gradle_: https://jitpack.io. This is to effortlessly compile code from any github project release or commit into your code.

Your _build.gradle_ file should look like this (if no other Maven repositories are present):

```
repositories {
mavenCentral()
maven {
url 'https://jitpack.io'
}
}
```

Then, add **lib6647** and **json-simple** as a dependency in your _build.gradle_'s dependencies like so:

```
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
// lib6647
compile group: 'com.github.pacoito123', name: 'lib6647', version: '-SNAPSHOT'
```

Your dependencies in should look like this (again, if none other than WPILib's dependencies are present):

```
dependencies {
// WPILib dependencies.
compile wpi.deps.wpilib()
compile wpi.deps.vendor.java()
nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)
testCompile 'junit:junit:4.12'
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
// lib6647
compile group: 'com.github.pacoito123', name: 'lib6647', version: '-SNAPSHOT'
}
```

Of course, you can change the version of the library to any specific commit in any branch, just make sure that your code is compatible with it.

Now you're ready to begin instantiating and initializing objects through JSON!

## To do (for now)

- [ ] Implement every HyperComponent Wrapper (if needed).
- [ ] HyperAHRS
- [ ] HyperAnalogPotentiometer
- [ ] HyperCompressor
- [ ] HyperDigitalInput
- [ ] HyperDoubleSolenoid
- [ ] HyperEncoder
- [ ] HyperPDP
- [x] HyperSolenoid
- [x] HyperTalon
- [ ] HyperUltrasonic
- [x] HyperVictor
- [ ] Implement SuperComponents.
- [ ] SuperAHRS
- [ ] SuperAnalogPotentiometer
- [x] SuperCompressor
- [x] SuperDigitalInput
- [x] SuperDoubleSolenoid
- [x] SuperEncoder
- [x] SuperPDP
- [x] SuperSolenoid
- [x] SuperTalon
- [x] SuperUltrasonic
- [x] SuperVictor
- [x] Improve Controller initialization.
- [x] Create JController class, for initializing Buttons along with a Controller.
- [x] Add Buttons for each POV and axis found for the Controller.
- [ ] Add more flexibility and configuration options.
- [x] Document everything properly.
- [x] Do proper Exception handling.
- [ ] Write proper documentation for this library.
- [x] Blame mechanical.

## Authors

* **Francisco Rubio** - [pacoito123](https://github.com/pacoito123)

## License

This project is under the BSD License for WPILib code, see: [BSD_License_for_WPILib_code.txt](BSD_License_for_WPILib_code.txt).
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ repositories {
maven {
url "http://devsite.ctr-electronics.com/maven/release"
}
maven {
url "https://repo1.maven.org/maven2/"
}
}

dependencies {
Expand All @@ -46,6 +49,9 @@ dependencies {
//Phoenix dependencies.
compile group: 'com.ctre.phoenix', name: 'api-java', version: '5.14.1'
compile group: 'com.ctre.phoenix', name: 'wpiapi-java', version: '5.14.1'

//Kauai Labs dependencies.
compile group: 'com.kauailabs.navx.frc', name: 'navx-java', version: '3.1.367'
}

// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar')
Expand Down
53 changes: 32 additions & 21 deletions src/main/java/org/usfirst/lib6647/oi/ButtonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.buttons.Button;

/**
* Helper class for registering button input.
* Helper class for registering {@link Button} input.
*/
public class ButtonHelper {

/**
* HashMap where declared {@link JController joysticks} are stored.
*/
public HashMap<String, JController> joysticks;
String fileName;

/**
* Constructor for the class.
* Location of the JSON file for {@link Button} nicknames.
*/
private String fileName;

/**
* Helper class for registering {@link Button} input.
*
* @param fileName
*/
Expand All @@ -31,75 +38,79 @@ public ButtonHelper(String fileName) {
}

/**
* Method for getting a button with a JSON name from a given joystick. Returns
* null if no button is found.
* Method for getting a {@link Button} with a friendly name (declared in the
* JSON configuration) from a given {@link #joysticks joystick}. Returns null if
* no {@link Button} is found at that key.
*
* @param joystick
* @param joystickName
* @param buttonName
* @return button from the given joystick
* @return {@link Button}
*/
public Button oiButton(String joystickName, String buttonName) {
try {
// Create a new JSONParser and JSONObject with the given key.
JSONParser parser = new JSONParser();
Reader file = new FileReader(fileName);
JSONObject jsonJoystick = (JSONObject) ((JSONObject) parser.parse(file))
.get(joysticks.get(joystickName).getName());

// Create Button object and initialize it with values from the JSONObject.
Button button = joysticks.get(joystickName).buttons.get(jsonJoystick.get(buttonName).toString());

// Clear JSONObject and JSONParser after use, and close Reader. Not sure if it
// does anything, but it might free some unused memory.
jsonJoystick.clear();
file.close();
parser.reset();

// Finally, return Button object from the friendly name.
return button;
} catch (IOException e) {
DriverStation.reportError("[!] OIBUTTON " + buttonName + " IO ERROR: " + e.getMessage(), false);
System.out.println("[!] OIBUTTON " + buttonName + " IO ERROR: " + e.getMessage());
System.exit(1);
} catch (ParseException e) {
DriverStation.reportError("[!] OIBUTTON " + buttonName + " PARSE ERROR: " + e.getMessage(), false);
System.out.println("[!] OIBUTTON " + buttonName + " PARSE ERROR: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
DriverStation.reportError("[!] OIBUTTON " + buttonName + " ERROR: " + e.getMessage(), false);
System.out.println("[!] OIBUTTON " + buttonName + " ERROR: " + e.getMessage());
System.exit(1);
}
return null;
}

/**
* Method for getting a button from a given joystick.
* Method for getting a {@link Button} from a given {@link JController#joysticks
* joystick}.
*
* @param joystickName
* @param button
* @return button from the given joystick
* @return button from the given {@link JController#joysticks joystick}
*/
public Button oiButton(String joystickName, int button) {
return joysticks.get(joystickName).buttons.get("Button" + button);
}

/**
* Method for getting an axisButton from a given joystick.
* Method for getting a {@link Button} from an axisButton or dPadButton, from a
* given {@link JController#joysticks joystick}.
*
* @param joystickName
* @param type
* @param axis
* @return axisButton from the given joystick, for the given axis
* @return {@link Button} from the given {@link JController#joysticks joystick},
* for the given axis or dPad
*/
public Button oiButton(String joystickName, String type, int axis) {
return joysticks.get(joystickName).buttons.get(type + axis);
}

/**
* Method for getting an axisButton from a given joystick, at a specific angle.
* Method for getting a {@link Button} from an axisButton or dPadButton, from a
* given {@link JController#joysticks joystick}, at a specific angle.
*
* @param joystickName
* @param type
* @param axis
* @param angle
* @return axisButton from the given joystick, for the given axis, for the given
* angle
* @return {@link Button} from the given {@link JController#joysticks joystick},
* for the given axis or dPad, for the given angle or value
*/
public Button oiButton(String joystickName, String type, int axis, int angle) {
return joysticks.get(joystickName).buttons.get(type + axis + "_" + angle);
Expand Down
Loading

0 comments on commit 22f880e

Please sign in to comment.