
function readpad()

  -- initiate timer
  padtimer = Timer.new()

  -- wait for input
  while true do

    -- read controls
    pad = Controls.read()

    -- filelist mode
    if padmode == "filelist" then

      -- press up
      if pad:up() and not keypress then

        -- select previous file
        mp_prevfile()

        screen:clear()
        render_filelist()
        render_statmsg("up", white)
        screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press down
      if pad:down() and not keypress then

        -- select next file
        mp_nextfile()

        screen:clear()
        render_filelist()
        render_statmsg("down", white)
        screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press triangle
      if pad:triangle() and not keypress then
        assert(loadstring(files[menu_pointer]["action_triangle"]))()
	pausepad(true)
      end




      -- press cross
      if pad:cross() and not keypress then
        assert(loadstring(files[menu_pointer]["action_cross"]))()
	pausepad(true)
      end

      -- press square
      if pad:square() and not keypress then
        assert(loadstring(files[menu_pointer]["action_square"]))()
	pausepad(true)
      end

    -- press select
    if pad:select() then
        screen:clear()
	display_help()
	screen.flip()
        screen.waitVblankStart()
	pausepad(true)
	padmode = "help"
    end

    -- press left
    if pad:left() then
	usb()
	screen.flip()
        screen.waitVblankStart()
	pausepad(true)
    end
    -- press start
    if pad:start() then
        screen:clear()
	display_credits()
	screen.flip()
        screen.waitVblankStart()
	pausepad(true)
	padmode = "credits"
    end

    -- confirm mode
    elseif padmode == "confirm" then

      -- press left
      if pad:left() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("left", white)

        confirm_option = true
	render_confirm(msgconfirm, msgcolour, confirm_option)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press right
      if pad:right() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("right", white)

        confirm_option = false
	render_confirm(msgconfirm, msgcolour, confirm_option)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press cross
      if pad:cross() and not keypress then

	screen:clear()
        render_filelist()
	
	if confirm_option then
          render_statmsg("Confirmed yes", white)
	  assert(loadstring(confirm_action))()
        else
          render_statmsg("Confirmed no", white)
	end
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    elseif padmode == "file_operations" then

      -- press up
      if pad:up() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("up", white)

        if selected_operation > 1 then
	  selected_operation = selected_operation - 1
	end
	render_file_operations(selected_operation)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press down
      if pad:down() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("down", white)

        if selected_operation < table.getn(file_operations) then
	  selected_operation = selected_operation + 1
	end
	render_file_operations(selected_operation)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press cross
      if pad:cross() and not keypress then

	pausepad(true)

        for k,v in pairs(file_operations) do
          if k == selected_operation and string.len(v.name) > 1 then
            assert(loadstring(v.action))()
	  end
	end

	padmode = "filelist"

	screen:clear()
        render_filelist()

        render_statmsg("Finished file operations", white)

	screen.flip()
        screen.waitVblankStart()

      end

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    elseif padmode == "help" then

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    end

    if (pad:up() or pad:down()) and keypress and padmode == "filelist" then
      if padpause > 0 and (padtimer:time() >= 175 or initpause) then
        if initpause then
          if padtimer:time() >= padpause then
  	    pausepad(false)
          end
	else
	  initpause = true
	end
      end
    end

    -- no keypress
    if not pad:up() and not pad:down() and not pad:left() and not pad:right() and not pad:triangle() and not pad:circle() and not pad:cross() and not pad:square() then
      initpause = false
      pausepad(false)
    end

    if debug then

      -- press l
      if pad:l() then
        break
      end

      -- press r
      if pad:r() then
        screenshot()
      end
    
    end

  end

end

function pausepad(dopause)

  -- pause pad
  if dopause then

    keypress = true
    padtimer:start()

  else

    -- unpause pad
    keypress = false
    padtimer:reset()
    padtimer:stop()

  end

end
