Close

Controller detection logic

A project log for Stingray

Nintendo Classic controller adapter with keypad emulation for Atari 5200

danjovicdanjovic 03/15/2020 at 12:340 Comments

Working on controller detection logic. I will support Nunchucks too. 

The adapter is now able to detect the presence of either the Classic controller or the Nunchuck and react accordingly (hot-plugging) without adding overhead to the sampling (aka multiple readings).

/*   Stingray  -  Controller Logic */

#include <NintendoExtensionCtrl.h>

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

void setup() {
  Serial.begin(9600);
  controller.begin();
}

void loop() {
  if (controller.connect()) { // valid controller was connected
    while (controller.update()) {
      ExtensionType conType = controller.getControllerType();
      switch (conType) {
        case (ExtensionType::Nunchuk):
          mapNunchuckData();
          break;
        case (ExtensionType::ClassicController):
          mapClassicData();
          break;
        default:
          Serial.println("Other controller connected!");
          disableOutputs();
      } // Switch
      processControllerData();
    } // while
    disableOutputs();
  } else { // Controller not connected}
    Serial.println("No controller found!");
    disableOutputs();
  }
  delay(200);
}

void disableOutputs() {
  Serial.println("Disable Outputs");
}

void  processControllerData() {
  Serial.println("Process Data");
}

void mapNunchuckData() { 
    nchuk.printDebug();  
}

void mapClassicData() {
  classic.printDebug();
}  

Discussions