예제 #1
0
    def _write(self, string):
        """Helper function to call write_data on the provided FTDI device and
        verify it succeeds.
        """
        # Get modem status. Useful to enable for debugging.
        #ret, status = ftdi.poll_modem_status(self._ctx)
        #if ret == 0:
        #	logger.debug('Modem status {0:02X}'.format(status))
        #else:
        #	logger.debug('Modem status error {0}'.format(ret))
        length = len(string)

        try:
            ret = ftdi.write_data(self._ctx, string)
            #compatible with libFtdi 1.3
        except TypeError:
            global FT232HPrintedOnce
            if (FT232HPrintedOnce == 0):
                print 'WARNING: Using ftdi.write_data function compatible with libFtdi 1.2'
                FT232HPrintedOnce = 1
            ret = ftdi.write_data(self._ctx, string,
                                  length)  #compatible with libFtdi 1.2
        # Log the string that was written in a python hex string format using a very
        # ugly one-liner list comprehension for brevity.
        #logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(ord(x)) for x in string])))
        if ret < 0:
            raise RuntimeError(
                'ftdi_write_data failed with error {0}: {1}'.format(
                    ret, ftdi.get_error_string(self._ctx)))
        if ret != length:
            raise RuntimeError(
                'ftdi_write_data expected to write {0} bytes but actually wrote {1}!'
                .format(length, ret))
예제 #2
0
 def _write(self, string):
     """Helper function to call write_data on the provided FTDI device and
     verify it succeeds.
     """
     # Get modem status. Useful to enable for debugging.
     #ret, status = ftdi.poll_modem_status(self._ctx)
     #if ret == 0:
     #	logger.debug('Modem status {0:02X}'.format(status))
     #else:
     #	logger.debug('Modem status error {0}'.format(ret))
     #length =
     ftdi.write_data(self._ctx, string, len(string))
예제 #3
0
 def _write(self, string):
     length = len(string)
     if self._buffer:
         if self._output is None:
             self._output = string
         else:
             self._output += string
         self._debug(
             5, "MPSSE: Buffering: " + "".join("{:02x}".format(ord(c))
                                               for c in self._output))
         return
     self._debug(
         5, "MPSSE: Write: " + "".join("{:02x}".format(ord(c))
                                       for c in string))
     ftdi.write_data(self._ctx, string, length)
예제 #4
0
def ftwrite(string):
    length = len(string)
    ret = ftdi.write_data(ctx, string, length)
    if ret < 0:
        raise RuntimeError('ftdi_write_data failed with error {0}: {1}'.format(
            ret, ftdi.get_error_string(self._ctx)))
    if ret != length:
        raise RuntimeError(
            'ftdi_write_data expected to write {0} bytes but actually wrote {1}!'
            .format(length, ret))
예제 #5
0
 def _write(self, string):
     """Helper function to call write_data on the provided FTDI device and
     verify it succeeds.
     """
     # Get modem status. Useful to enable for debugging.
     #ret, status = ftdi.poll_modem_status(self._ctx)
     #if ret == 0:
     #       logger.debug('Modem status {0:02X}'.format(status))
     #else:
     #       logger.debug('Modem status error {0}'.format(ret))
     length = len(string)
     try:
         ret = ftdi.write_data(self._ctx, string, length)
     except TypeError:
         ret = ftdi.write_data(self._ctx, string); #compatible with libFtdi 1.3
     # Log the string that was written in a python hex string format using a very
     # ugly one-liner list comprehension for brevity.
     #logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(x) for x in bytearray(string)])))
     if ret < 0:
         raise RuntimeError('ftdi_write_data failed with error {0}: {1}'.format(ret, ftdi.get_error_string(self._ctx)))
     if ret != length:
         raise RuntimeError('ftdi_write_data expected to write {0} bytes but actually wrote {1}!'.format(length, ret))
예제 #6
0
 def _ftdi_write(self, byte_list, max_iter=None):
     """Write data to the FTDI chip."""
     if self._debug_ftdi:
         self._debug("[FTDI] write",
                     "[%s]"%(" ".join("%02X" % b for b in byte_list)))
     bytes_left = byte_list
     iter_left = max_iter
     while bytes_left:
         if iter_left == 0:
             break
         n = ftdi.write_data(self.ftdic,
                 ''.join(map(chr, bytes_left)), len(bytes_left))
         if n < 0:
             raise IOError('USB write error (error code %i: %s)'
                           % (n, USB_ERROR_CODE[n]
                                 if n in USB_ERROR_CODE else 'unknown'))
         bytes_left = bytes_left[n:]
         if iter_left is not None:
             iter_left -= 1
     # number of bytes that were written
     return len(byte_list)-len(bytes_left)
예제 #7
0
파일: gpio.py 프로젝트: swipswaps/robophery
 def _write(self, string):
     """
     Helper function to call write_data on the provided FTDI device and
     verify it succeeds.
     """
     # Get modem status. Useful to enable for debugging.
     # ret, status = ftdi.poll_modem_status(self._bus)
     # if ret == 0:
     #   logger.debug('Modem status {0:02X}'.format(status))
     # else:
     #   logger.debug('Modem status error {0}'.format(ret))
     length = len(string)
     ret = ftdi.write_data(self._bus, string, length)
     # Log the string that was written in a python hex string format using
     # a very ugly one-liner list comprehension for brevity.
     # logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(ord(x)) for x in string])))
     if ret < 0:
         raise RuntimeError(
             'ftdi_write_data failed with error {0}: {1}'.format(
                 ret, ftdi.get_error_string(self._bus)))
     if ret != length:
         raise RuntimeError(
             'ftdi_write_data expected to write {0} bytes but actually wrote {1}!'
             .format(length, ret))
예제 #8
0
def update():
    #  print 'current is ' + bin(current)
    ftdi.write_data(c, str(chr(current)), 1)
예제 #9
0
def red(context):
    actuate(context, RED_ON)
    actuate(context, GREEN_OFF)


def green(context):
    # turn on Green light
    actuate(context, GREEN_ON)
    actuate(context, RED_OFF)


def off(context):
    actuate(context, RED_OFF)
    actuate(context, GREEN_OFF)


context = ftd.new()
ftd.usb_open(context, 0x0403, 0x6001)
ftd.set_bitmode(context, 0xff, ftd.BITMODE_BITBANG)
# Everything off --- shouldn't be necessary.
ftd.write_data(context, bytes([0x00]))

if len(sys.argv) == 1:
    off(context)
elif sys.argv[1] == 'red':
    red(context)
elif sys.argv[1] == 'green':
    green(context)
else:
    print("Usage: lavalamps [red|green]")
예제 #10
0
def actuate(context, value):
    ftd.write_data(context, value)
    sleep(0.3)
    ftd.write_data(context, NEUTRAL)
예제 #11
0
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)

# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)

#for i in range( 8 ):
val = 0x0
print('enabling bit #%d (0x%02x)' % (i, val))
ftdi.write_data(ftdic, chr(val))
time.sleep(5.5)

val = 0x2
print('enabling bit #%d (0x%02x)' % (i, val))
ftdi.write_data(ftdic, chr(val))
time.sleep(5.5)

#ftdi.write_data( ftdic, chr(0x00))
time.sleep(0.2)

ftdi.disable_bitbang(ftdic)
print('')

# close usb
ret = ftdi.usb_close(ftdic)
예제 #12
0
    i += 1

# open usb
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)

# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)
print('turning everything on')
ftdi.write_data(ftdic, chr(0xff), 1)
time.sleep(1)
print('turning everything off\n')
ftdi.write_data(ftdic, chr(0x00), 1)
time.sleep(1)
for i in range(8):
    val = 2**i
    print('enabling bit #%d (0x%02x)' % (i, val))
    ftdi.write_data(ftdic, chr(val), 1)
    time.sleep(1)
ftdi.disable_bitbang(ftdic)
print('')

# read pins
# FIXME: read_pins fails with python3, so I disabled it for now
# tested on ubuntu 12.04 ( python3.2.3 / swig 2.0.4 )
예제 #13
0
def send_msg_led(ctx, channel, keycode, value):
    msg = construct_msg_led(channel, keycode, value)
    ftdi.write_data(ctx, msg)
#     i += 1

# open usb
ret = ftdi.usb_open(ftdic, 0x0403, 0x6014)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)

# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)
print('turning everything on')
ftdi.write_data(ftdic, chr(0xff).encode('utf-8'))
time.sleep(1)
print('turning everything off\n')
ftdi.write_data(ftdic, chr(0x00).encode('utf-8'))
time.sleep(1)
for i in range(8):
    val = 2**i
    print('enabling bit #%d (0x%02x)' % (i, val))
    ftdi.write_data(ftdic, chr(val).encode('utf-8'))
    time.sleep(1)
ftdi.disable_bitbang(ftdic)
print('')

# read pins
ret, pins = ftdi.read_pins(ftdic)
if (ret == 0):
예제 #15
0
def send_msg_slider(ctx, channel, value):
    msg = construct_msg_slider(channel, value)
    ftdi.write_data(ctx, msg)
예제 #16
0
	def write(self, inStr):
		#print "writing", hexify(inStr), "length of", len(inStr)
		ret = ftdi1.write_data(self.ftdic, inStr, len(inStr))
    def writeConfigReg(self, device, register):
        str = device + " 0b" + register.bin + " (0x" + register.hex + ")"
        self.logger.debug(str)
        trans = BitArray("0b00000000")
        # set the refselectbit but everything else is low
        trans[self.ftdi_bits["ref_sel"]] = self.refselectMode[self.refselect]
        # self.logger.debug(trans.bin)

        # keep LE 1 for "4" time to satisfy min pulse width
        for i in xrange(4):
            ftdi.write_data(self.ftdic, chr(trans.uint), 1)
            # self.logger.debug(trans.bin)

        # Note says assert now but it's just the same trans as befor
        ftdi.write_data(self.ftdic, chr(trans.uint), 1)
        # self.logger.debug(trans.bin)

        for i in xrange(len(register)):
            dbit = register[i]
            trans[self.ftdi_bits["data"]] = dbit
            trans[self.ftdi_bits["clk"]] = 0
            # self.logger.debug(trans.bin)
            ftdi.write_data(self.ftdic, chr(trans.uint), 1)
            trans[self.ftdi_bits["clk"]] = 1
            # self.logger.debug(trans.bin)
            ftdi.write_data(self.ftdic, chr(trans.uint), 1)

            # Now deal with the case of an attenuator
            # Where the clk has to be held low for another cycle
            if device is "atten1" or "atten2":
                trans[self.ftdi_bits["clk"]] = 0
                ftdi.write_data(self.ftdic, chr(trans.uint), 1)
                # self.logger.debug(trans.bin)

        # Now keep clock low for a few more cycles (copying tk code)
        trans[self.ftdi_bits["clk"]] = 0
        for i in xrange(3):
            ftdi.write_data(self.ftdic, chr(trans.uint), 1)
            # self.logger.debug(trans.bin)

        # And strobe the LE bit
        trans[self.ftdi_bits[device]] = 1
        for i in xrange(2):
            ftdi.write_data(self.ftdic, chr(trans.uint), 1)
            # self.logger.debug(trans.bin)

        trans[self.ftdi_bits[device]] = 0
        print ftdi.write_data(self.ftdic, chr(trans.uint), 1)
예제 #18
0
# open usb
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)


# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)
print('turning everything on')
ftdi.write_data(ftdic, chr(0xff), 1)
time.sleep(1)
print('turning everything off\n')
ftdi.write_data(ftdic, chr(0x00), 1)
time.sleep(1)
for i in range(8):
    val = 2 ** i
    print('enabling bit #%d (0x%02x)' % (i, val))
    ftdi.write_data(ftdic, chr(val), 1)
    time.sleep(1)
ftdi.disable_bitbang(ftdic)
print('')


# read pins
ret, pins = ftdi.read_pins(ftdic)
예제 #19
0
파일: reset.py 프로젝트: fincham/test-tama
#!/usr/bin/python3

import ftdi1
f = ftdi1.new()
ftdi1.usb_open(f, 0x0403, 0x6001)
ftdi1.set_bitmode(f, 1, 0)
ftdi1.write_data(f, bytes([0]))
ftdi1.write_data(f, bytes([1]))
ftdi1.usb_close(f)