Skip to content

Commit

Permalink
Merge pull request #41 from dmadison/base-revert
Browse files Browse the repository at this point in the history
Swap ExtensionPort / ExtensionController class names
  • Loading branch information
dmadison authored Dec 9, 2018
2 parents 3c574a9 + 7bc1c5d commit 16e3b21
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 115 deletions.
2 changes: 1 addition & 1 deletion examples/Any/DebugPrint/DebugPrint.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <NintendoExtensionCtrl.h>

ExtensionController controller;
ExtensionPort controller;

void setup() {
Serial.begin(115200);
Expand Down
2 changes: 1 addition & 1 deletion examples/Any/IdentifyController/IdentifyController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <NintendoExtensionCtrl.h>

ExtensionController controller;
ExtensionPort controller;

void setup() {
Serial.begin(115200);
Expand Down
14 changes: 7 additions & 7 deletions examples/Any/MultipleTypes/MultipleTypes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@

#include <NintendoExtensionCtrl.h>

ExtensionController controller; // Port for communicating with extension controllers
ExtensionPort port; // Port for communicating with extension controllers

Nunchuk::Shared nchuk(controller); // Read Nunchuk formatted data from the port
ClassicController::Shared classic(controller); // Read Classic Controller formatted data from the port
Nunchuk::Shared nchuk(port); // Read Nunchuk formatted data from the port
ClassicController::Shared classic(port); // Read Classic Controller formatted data from the port

void setup() {
Serial.begin(115200);
controller.begin();
port.begin();

while (!controller.connect()) {
while (!port.connect()) {
Serial.println("No controller found!");
delay(1000);
}
}

void loop() {
boolean success = controller.update(); // Get new data from the controller
boolean success = port.update(); // Get new data from the controller

if (success == true) { // We've got data!
ExtensionType conType = controller.getControllerType();
ExtensionType conType = port.getControllerType();

switch (conType) {
case(ExtensionType::Nunchuk):
Expand Down
2 changes: 1 addition & 1 deletion examples/Any/SpeedTest/SpeedTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <NintendoExtensionCtrl.h>

ExtensionController controller; // Generic controller, 6 bytes
ExtensionPort controller; // Generic controller port, 6 bytes

const long TestDuration = 1000; // Length of each test, in milliseconds

Expand Down
16 changes: 8 additions & 8 deletions extras/AddingControllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ If you want to use an extension controller that is not currently supported by th
## Step #1: Creating The Class Framework
The first step in adding support for your controller is building it a class. The header (.h) and implementation (.cpp) files live within the `controllers` folder in the source directory. You'll need to create both of these files with the name of your controller.

Controller classes live inside the library namespace and inherit from the `ExtensionPort` class, which contains methods for communicating with the controllers and manipulating the control surface data. This includes combining multi-byte data and extracting bits that correspond to button presses. To use this you'll need to include the "ExtensionController.h" header, which is in the `internal` source directory.
Controller classes live inside the library namespace and inherit from the `ExtensionController` class, which contains methods for communicating with the controllers and manipulating the control surface data. This includes combining multi-byte data and extracting bits that correspond to button presses. To use this you'll need to include the "ExtensionController.h" header, which is in the `internal` source directory.

The class name for your controller is going to be the "Shared" version of the class, which uses a reference to port and control data that exists elsewhere - thus it has a `_Shared` suffix. Here is what the start of the `ClassicController_Shared` class looks like:

```C++
#include "internal/ExtensionController.h"

namespace NintendoExtensionCtrl {
class ClassicController_Shared : public ExtensionPort {
class ClassicController_Shared : public ExtensionController {
public:
ClassicController_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::ClassicController) {}
ExtensionController(dataRef, ExtensionType::ClassicController) {}

ClassicController_Shared(ExtensionController &controller) :
ClassicController_Shared(ExtensionPort &controller) :
ClassicController_Shared(controller.getExtensionData()) {}
```

Note how the constructors use a reference to an `ExtensionData` class *external* to the controller class itself. You can also see that the controller's identity is passed to the `ExtensionPort` class to limit what types of controllers can connect (more on that in a bit).
Note how the constructors use a reference to an `ExtensionData` class *external* to the controller class itself. You can also see that the controller's identity is passed to the `ExtensionController` class to limit what types of controllers can connect (more on that in a bit).

## Step #2: Building Your Data Maps
The next step is to add the data maps for your controller. These define where the data for each control input lies within in the data array.
Expand Down Expand Up @@ -72,7 +72,7 @@ The resulting bit from the control data is extracted and inverted, as extension
```C++
BitMap ButtonA = { 5, 4 };
```
Full definitions of these data types can be found in the [`NXC_DataMaps.h`](../src/internal/NXC_DataMaps.h) file. Methods for using them are defined in the `ExtensionPort` class definition ([`ExtensionController.h`](../src/internal/ExtensionController.h)).
Full definitions of these data types can be found in the [`NXC_DataMaps.h`](../src/internal/NXC_DataMaps.h) file. Methods for using them are defined in the `ExtensionController` class definition ([`ExtensionController.h`](../src/internal/ExtensionController.h)).

---

Expand Down Expand Up @@ -159,13 +159,13 @@ Once that's done, head back to your controller's header file and add that identi

```
ClassicController_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::ClassicController) {}
ExtensionController(dataRef, ExtensionType::ClassicController) {}
```

You will also need to edit the switch statement in the `IdentifyControllers` example to add your controller to the 'switch' statement.

## Step #6: Create the Combined Class
The last step to get your controller working is to create a combined class that inherits from your `_Shared` class and bundles it with a set of extension port data to use. This creates an easy to use class for most users who are looking to get data from just one controller. Just copy this line, replacing all instances of `YourController` with your controller's name:
The last step to get your controller working is to create a combined class that inherits from your `_Shared` class and bundles it with a set of extension data to use. This creates an easy to use class for most users who are looking to get data from just one controller. Just copy this line, replacing all instances of `YourController` with your controller's name:

```C++
using YourController = NintendoExtensionCtrl::BuildControllerClass
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/ClassicController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "internal/ExtensionController.h"

namespace NintendoExtensionCtrl {
class ClassicController_Shared : public ExtensionPort {
class ClassicController_Shared : public ExtensionController {
public:
struct Maps {
constexpr static ByteMap LeftJoyX = ByteMap(0, 6, 0, 0);
Expand Down Expand Up @@ -59,10 +59,10 @@ namespace NintendoExtensionCtrl {
};

ClassicController_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::ClassicController) {}
ExtensionController(dataRef, ExtensionType::ClassicController) {}

ClassicController_Shared(ExtensionController &controller) :
ClassicController_Shared(controller.getExtensionData()) {}
ClassicController_Shared(ExtensionPort &port) :
ClassicController_Shared(port.getExtensionData()) {}

uint8_t leftJoyX() const; // 6 bits, 0-63
uint8_t leftJoyY() const;
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/DJTurntable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ClassicController.h" // For joystick and +/- control maps

namespace NintendoExtensionCtrl {
class DJTurntableController_Shared : public ExtensionPort {
class DJTurntableController_Shared : public ExtensionController {
public:
struct Maps {
constexpr static ByteMap JoyX = ClassicController_Shared::Maps::LeftJoyX;
Expand Down Expand Up @@ -56,10 +56,10 @@ namespace NintendoExtensionCtrl {
};

DJTurntableController_Shared(ExtensionData& dataRef) :
ExtensionPort(dataRef, ExtensionType::DJTurntableController), left(*this), right(*this) {}
ExtensionController(dataRef, ExtensionType::DJTurntableController), left(*this), right(*this) {}

DJTurntableController_Shared(ExtensionController &controller) :
DJTurntableController_Shared(controller.getExtensionData()) {}
DJTurntableController_Shared(ExtensionPort &port) :
DJTurntableController_Shared(port.getExtensionData()) {}

enum class TurntableConfig {
BaseOnly,
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/DrumController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ClassicController.h" // For joystick and +/- control maps

namespace NintendoExtensionCtrl {
class DrumController_Shared : public ExtensionPort {
class DrumController_Shared : public ExtensionController {
public:
struct Maps {
constexpr static ByteMap JoyX = ClassicController_Shared::Maps::LeftJoyX;
Expand All @@ -52,10 +52,10 @@ namespace NintendoExtensionCtrl {
};

DrumController_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::DrumController) {}
ExtensionController(dataRef, ExtensionType::DrumController) {}

DrumController_Shared(ExtensionController &controller) :
DrumController_Shared(controller.getExtensionData()) {}
DrumController_Shared(ExtensionPort &port) :
DrumController_Shared(port.getExtensionData()) {}

enum VelocityID : uint8_t {
None = 0x1F,
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/GuitarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ClassicController.h" // For joystick and +/- control maps

namespace NintendoExtensionCtrl {
class GuitarController_Shared : public ExtensionPort {
class GuitarController_Shared : public ExtensionController {
public:
struct Maps {
constexpr static ByteMap JoyX = ClassicController_Shared::Maps::LeftJoyX;
Expand All @@ -51,10 +51,10 @@ namespace NintendoExtensionCtrl {
};

GuitarController_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::GuitarController) {}
ExtensionController(dataRef, ExtensionType::GuitarController) {}

GuitarController_Shared(ExtensionController &controller) :
GuitarController_Shared(controller.getExtensionData()) {}
GuitarController_Shared(ExtensionPort &port) :
GuitarController_Shared(port.getExtensionData()) {}

uint8_t joyX() const; // 6 bits, 0-63
uint8_t joyY() const;
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/Nunchuk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "internal/ExtensionController.h"

namespace NintendoExtensionCtrl {
class Nunchuk_Shared : public ExtensionPort {
class Nunchuk_Shared : public ExtensionController {
public:
struct Maps {
constexpr static CtrlIndex JoyX = 0;
Expand All @@ -46,10 +46,10 @@ namespace NintendoExtensionCtrl {
};

Nunchuk_Shared(ExtensionData &dataRef) :
ExtensionPort(dataRef, ExtensionType::Nunchuk) {}
ExtensionController(dataRef, ExtensionType::Nunchuk) {}

Nunchuk_Shared(ExtensionController &controller) :
Nunchuk_Shared(controller.getExtensionData()) {}
Nunchuk_Shared(ExtensionPort &port) :
Nunchuk_Shared(port.getExtensionData()) {}

uint8_t joyX() const; // 8 bits, 0-255
uint8_t joyY() const;
Expand Down
44 changes: 21 additions & 23 deletions src/internal/ExtensionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@

#include "ExtensionController.h"

namespace NintendoExtensionCtrl {
using namespace NintendoExtensionCtrl;

ExtensionPort::ExtensionPort(ExtensionData& dataRef)
: ExtensionPort(dataRef, ExtensionType::AnyController) {}
ExtensionController::ExtensionController(ExtensionData& dataRef)
: ExtensionController(dataRef, ExtensionType::AnyController) {}

ExtensionPort::ExtensionPort(ExtensionData& dataRef, ExtensionType conID)
ExtensionController::ExtensionController(ExtensionData& dataRef, ExtensionType conID)
: i2c(dataRef.i2c), id(conID), data(dataRef) {}

void ExtensionPort::begin() {
void ExtensionController::begin() {
data.i2c.begin(); // Initialize the bus
}

boolean ExtensionPort::connect() {
boolean ExtensionController::connect() {
disconnect(); // Clear current data
return reconnect();
}

boolean ExtensionPort::reconnect() {
boolean ExtensionController::reconnect() {
boolean success = false;

if (initialize(data.i2c)) {
Expand All @@ -53,21 +53,21 @@ boolean ExtensionPort::reconnect() {
return success;
}

void ExtensionPort::disconnect() {
void ExtensionController::disconnect() {
data.connectedType = ExtensionType::NoController; // Nothing connected
memset(&data.controlData, 0x00, ExtensionData::ControlDataSize); // Clear control data
}

void ExtensionPort::reset() {
void ExtensionController::reset() {
disconnect();
requestSize = MinRequestSize; // Request size back to minimum
}

void ExtensionPort::identifyController() {
void ExtensionController::identifyController() {
data.connectedType = NintendoExtensionCtrl::identifyController(data.i2c); // Polls the controller for its identity
}

boolean ExtensionPort::controllerIDMatches() const {
boolean ExtensionController::controllerIDMatches() const {
if (data.connectedType == id) {
return true; // Match!
}
Expand All @@ -78,41 +78,41 @@ boolean ExtensionPort::controllerIDMatches() const {
return false; // Enforced types or no controller connected
}

ExtensionType ExtensionPort::getControllerType() const {
ExtensionType ExtensionController::getControllerType() const {
return data.connectedType;
}

boolean ExtensionPort::update() {
boolean ExtensionController::update() {
if (controllerIDMatches() && requestControlData(data.i2c, requestSize, data.controlData)) {
return verifyData(data.controlData, requestSize);
}

return false; // Something went wrong :(
}

uint8_t ExtensionPort::getControlData(uint8_t controlIndex) const {
uint8_t ExtensionController::getControlData(uint8_t controlIndex) const {
return data.controlData[controlIndex];
}

void ExtensionPort::setControlData(uint8_t index, uint8_t val) {
void ExtensionController::setControlData(uint8_t index, uint8_t val) {
data.controlData[index] = val;
}

ExtensionData & ExtensionPort::getExtensionData() const {
ExtensionController::ExtensionData & ExtensionController::getExtensionData() const {
return data;
}

void ExtensionPort::setRequestSize(size_t r) {
void ExtensionController::setRequestSize(size_t r) {
if (r >= MinRequestSize && r <= MaxRequestSize) {
requestSize = (uint8_t) r;
}
}

void ExtensionPort::printDebug(Print& output) const {
void ExtensionController::printDebug(Print& output) const {
printDebugRaw(output);
}

void ExtensionPort::printDebugID(Print& output) const {
void ExtensionController::printDebugID(Print& output) const {
uint8_t idData[ID_Size];
boolean success = requestIdentity(data.i2c, idData);

Expand All @@ -125,15 +125,13 @@ void ExtensionPort::printDebugID(Print& output) const {
}
}

void ExtensionPort::printDebugRaw(Print& output) const {
void ExtensionController::printDebugRaw(Print& output) const {
printDebugRaw(HEX, output);
}

void ExtensionPort::printDebugRaw(uint8_t baseFormat, Print& output) const {
void ExtensionController::printDebugRaw(uint8_t baseFormat, Print& output) const {
output.print("Raw[");
output.print(requestSize);
output.print("]: ");
printRaw(data.controlData, requestSize, baseFormat, output);
}

} // End "NintendoExtensionCtrl" namespace
Loading

0 comments on commit 16e3b21

Please sign in to comment.