import os
import time
import ctypes
BT848_GPIO_DATA = 0x200
os.chdir('c:/apps/pyremote')
devices = [  #(vendor_id, device_id) pairs
    (0x109e, 0x0350),
    (0x109e, 0x0351),
    (0x109e, 0x036e),
    (0x109e, 0x036f)]
ERROR_SUCCESS = 0
dsdrv = ctypes.windll.dsdrv
for vendor_id, device_id in devices:
  physical_address = ctypes.c_ulong(0)
  memory_lentgh = ctypes.c_ulong(0)
  subsystem_id = ctypes.c_ulong(0)
  ret = dsdrv.pciGetHardwareResources(
      vendor_id,
      device_id,
      ctypes.pointer(physical_address),
      ctypes.pointer(memory_lentgh),
      ctypes.pointer(subsystem_id))
  if ret == ERROR_SUCCESS:
    mapped_base = dsdrv.memoryMap(physical_address, memory_lentgh)
    break
else:
  print "An appropriate error message"

gpio = 0
while 1:
  old_gpio = gpio
  gpio = dsdrv.memoryReadDWORD(BT848_GPIO_DATA)
  if gpio != old_gpio:
    print '%08x' % gpio
  time.sleep(20/1000.0)

dsdrv.memoryUnmap(physical_address, memory_lentgh);
