class usb_vcp_tmcl_interface(tmcl_module_interface, tmcl_host_interface): def __init__(self, port=0, data_rate=None, host_id=2, module_id=1, debug=False): del data_rate tmcl_module_interface.__init__(self, host_id, module_id, debug) tmcl_host_interface.__init__(self, host_id, module_id, debug) self.__vcp = USB_VCP(port) self.__vcp.init() self.__vcp.setinterrupt(-1) def __enter__(self): return self def __exit__(self, exitType, value, traceback): del exitType, value, traceback self.close() def close(self): self.__vcp.setinterrupt(3) self.__vcp.close() return 0 def data_available(self, hostID, moduleID): del hostID, moduleID return self.__vcp.any() def _send(self, hostID, moduleID, data): del hostID, moduleID self.__vcp.write(data) def _recv(self, hostID, moduleID): del hostID, moduleID read = bytearray(0) while(len(read) < 9): read += self.__vcp.read(9) if(not(read)): read = bytearray(0) return read def printInfo(self): pass def enableDebug(self, enable): self._debug = enable @staticmethod def supportsTMCL(): return True @staticmethod def supportsCANopen(): return False @staticmethod def available_ports(): return set([0])
class USB_Port: def __init__(self): self.usb_serial = USB_VCP() self.recv_buf = bytearray(1) # Disable Control-C on the USB serial port in case one comes in the # data. self.usb_serial.setinterrupt(-1) def read_byte(self): """Reads a byte from the usb serial device.""" if self.usb_serial.any(): bytes_read = self.usb_serial.recv(self.recv_buf) if bytes_read > 0: return self.recv_buf[0] def write(self, data): """Writes an entire packet to the serial port.""" self.usb_serial.write(data)
class USB_Port: """Implements a port which can be used to receive bioloid device commands from a host. """ def __init__(self): self.usb_serial = USB_VCP() self.baud = 0 self.recv_buf = bytearray(1) # Disable Control-C on the USB serail port in case one comes in the # data. self.usb_serial.setinterrupt(-1) def any(self): """Returns a truthy value if characters are available to be read.""" return self.usb_serial.any() def read_byte(self): """Reads a byte from the usb serial device. This function will return None if no character was read within the designated timeout. The max Return Delay time is 254 x 2 usec = 508 usec (the default is 500 usec). This represents the minimum time between receiving a packet and sending a response. """ bytes_read = self.usb_serial.recv(self.recv_buf, timeout=2) if bytes_read > 0: return self.recv_buf[0] def set_baud(self, baud): """Sets the baud rate. Note that for USB Serial, this is essentially a no-op. We store the baud rate that was set, so that """ self.baud = baud def write_packet(self, packet_data): """Writes an entire packet to the serial port.""" self.usb_serial.write(packet_data)
import utime, ustruct, pyb import math from array import array from pyb import USB_VCP usb = USB_VCP() usb.setinterrupt(-1) dac1 = pyb.DAC(pyb.Pin('X5'), bits=12) dac2 = pyb.DAC(pyb.Pin('X6'), bits=12) pwmGain = pyb.Pin('X1') pwmSlew = pyb.Pin('X2') pwmBias = pyb.Pin('X3') tim2 = pyb.Timer(2, freq=10000) tim5 = pyb.Timer(5, freq=1000000) pwmBiasCh = tim5.channel(3, pyb.Timer.PWM, pin=pwmBias) pwmGainCh = tim5.channel(1, pyb.Timer.PWM, pin=pwmGain) pwmBiasCh.pulse_width_percent(50) pwmSlewCh = tim2.channel(2, pyb.Timer.PWM, pin=pwmSlew) amplitude, Stop = 999, True frequency = 10000 duty = 10 while Stop: cmd = usb.recv(4, timeout=5000) usb.write(cmd) if (cmd == b'strt'): pyb.LED(1).on() pyb.LED(2).off() pyb.LED(3).off() utime.sleep(1) tim2 = pyb.Timer(2, freq=10000)
# USB serial example. # # WARNING: # This script should NOT be run from the IDE or command line, it should be saved as main.py # Note the following commented script shows how to receive the image from the host side. import sensor, image, time, ustruct from pyb import USB_VCP, LED usb = USB_VCP() led = LED(1) sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time = 2000) # Wait for settings take effect. usb.setinterrupt(3) # input while(True): cmd = usb.recv(1, timeout=5000) if (cmd == b'\x01'): led.toggle() for item in range(65, 91): usb.write(ustruct.pack('<b', item)) else: continue