
' Controllers
' initial support for:
' digital joystick only
' single joystick only
' direct switch-pin connection on

' testing:
' support one joystick or dpad on wii controller

''''''''''''''''
' Test program '
''''''''''''''''

OPTION EXPLICIT

#INCLUDE "controllers.inc"

SUB Controllers.Detect
  print "checking front port (port 3) for nunchuk or classic controller"
  print
  on error skip
  wii nunchuk open
  if mm.errno <> 0 then
    print "No Wii controller detected. Try joystick"
  else
    local FrontPort=3
    local t_id
    t_id = nunchuk(T,FrontPort)
    'testing value:
    ' t_id = 255+65536
    print "Wii controller detected"
    print "ID: ";Hex$(t_id)
    SELECT CASE t_id
      CASE wii.id.nunchuk
        print "nunchuk detected"
      CASE wii.id.classic
        print "classic controller detected"
      CASE wii.id.balance
        print "balance board detected - not supported"
      CASE wii.id.classicpro
        print "classic pro detected - similar to classic - to be supported soon"
      CASE ELSE
        print "controller type unknown"
        print "expecting: one of these:"
        print Hex$(wii.id.nunchuk)
        print Hex$(wii.id.classic)
        print Hex$(wii.id.classicpro)
    END SELECT
    wii nunchuk close
  end if
  
END SUB

dim i$

do
CLS

print:print "Press Q to quit"

print "1 - Detect controller"
print "2 - Test autodetect"
print "3 - Test controls"
print
print "Supported types:"
print
print "Atari/Commodore/Arcade joystick"
print "Wii nunchuk"
print "Wii classic controller"

  do
    i$=inkey$
  loop until i$="q" or i$="Q" or i$="1" or i$="2" or i$="3"

  SELECT CASE i$
    CASE "1"
      Controllers.Detect
      pause 1500
    CASE "2"
      Controllers.Autodetect
      SELECT CASE Controllers.Type
        CASE Controllers.Type.Nunchuk
          print "nunchuk detected"
        CASE Controllers.Type.Classic
          print "classic controller detected"
        CASE Controllers.Type.DigitalPins
          print "No Wii controller, using arcade interface"
        CASE ELSE
          print "autodetection result unknown"
      END SELECT
      pause 1500

    CASE "3"
      CLS
      Controllers.init 1
      Controllers.Test
      pause 500
      
  END SELECT
loop until i$="q" or i$="Q"

end

SUB Controllers.Test
do
  print @(10,50) "Press space to return to menu";
  print @(10,10) "Status: ";
  if Controllers.Left() then
    print "LEFT ";
  else
    print "____ ";
  endif
  if Controllers.Right() then
    print "RIGHT ";
  else
    print "_____ ";
  endif
  if Controllers.Up() then
    print "UP ";
  else
    print "__ ";
  endif
  if Controllers.Down() then
    print "DOWN ";
  else
    print "____ ";
  endif
  if Controllers.ButtonA() then
    print "BUTTON ";
  else
    print "______ ";
  endif
  if Controllers.ButtonB() then
    print "AUX ";
  else
    print "___ ";
  endif
loop until inkey$=" "
END SUB

