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

' Pins for direct switch 

CONST Pins.Up = 35 ' digital up - pin 35
CONST Pins.Down = 36 ' digital down - pin 36
CONST Pins.Left = 38 ' digital left - pin 38
CONST Pins.Right = 29 ' digital right - pin 29

CONST Pins.Right_compat = 40
' digital right - previous
' version - may reset options if pin 40 is
' left plugged in during power up.



CONST Pins.ButtonA = 32 ' Button A - pin 32
CONST Pins.ButtonB = 33 ' Button B - pin 33

CONST Pins.Button_compat = 37 ' Button - alternative version

SUB Pins.InitDigital p
  ' Set pin number p as digital input, with pullup.
  SetPin p, din, pullup
END SUB

SUB Controllers.Init players
  ' default argument for players is interpreted as 1 player

  if players > 1 then print "multiple players not supported yet"

  Pins.InitDigital Pins.Up
  Pins.InitDigital Pins.Down
  Pins.InitDigital Pins.Left
  Pins.InitDigital Pins.Right
  Pins.InitDigital Pins.ButtonA
  Pins.InitDigital Pins.ButtonB

  Pins.InitDigital Pins.Right_compat
  Pins.InitDigital Pins.Button_compat

END SUB

FUNCTION Controllers.Up()
  ' Pins are low (0) for active
  Controllers.Up = pin(Pins.Up) = 0
END FUNCTION

FUNCTION Controllers.Down()
  ' Pins are low (0) for active
  Controllers.Down = pin(Pins.Down) = 0
END FUNCTION

FUNCTION Controllers.Left()
  ' Pins are low (0) for active
  Controllers.Left = pin(Pins.Left) = 0
END FUNCTION

FUNCTION Controllers.Right()
  ' Pins are low (0) for active
  Controllers.Right = (pin(Pins.Right) = 0) or (pin(Pins.Right_compat) = 0)

END FUNCTION

FUNCTION Controllers.ButtonA()
  ' Pins are low (0) for active
  Controllers.ButtonA = (pin(Pins.ButtonA) = 0) or (pin(Pins.Button_compat) = 0)



END FUNCTION

FUNCTION Controllers.ButtonB()
  ' Pins are low (0) for active
  Controllers.ButtonB = pin(Pins.ButtonB) = 0
END FUNCTION


Controllers.init 1

do
  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
