class BluetoothJoystickController: # @param mbedPort [str] The serial port to use on the mbed # @param mbedBaud [int] The baud rate to use with the mbed # @param mbedUpdateInterval [float] Time in ms to wait between sending updated state to the mbed def __init__(self, mbedPort='/dev/ttyAMA0', mbedBaud=115200, mbedUpdateInterval=1.25 , useLJ = True): self._mbedSerial = serial.Serial(mbedPort, baudrate=mbedBaud, timeout=None, bytesize=serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE) # timeout = None means blocking, timeout = 0 means non-blocking self._mbedUpdateInterval = mbedUpdateInterval self._joystick = FishJoystick(joystick='snes', useLJ=useLJ) # The main loop to update state from joystick and send to mbed # @param runTime [flaot or None] The time to run for, or None for infinite loop def run(self, runTime=None): self._mbedSerial.flushInput() self._mbedSerial.flushOutput() lastSendTime = 0 lastPrintTime = 0 startTime = time() while runTime is None or time() - startTime < runTime: self._joystick.scan() sleep(0.01) # not sure if this is needed, but seems nice to reduce polling load on device? if time() - lastSendTime > self._mbedUpdateInterval: stateBytes = self._joystick.getStateBytes('bytearray') self._mbedSerial.write(stateBytes) #self._mbedSerial.write(bytearray([1, 1, 1, 1, 1, 0])) if stateBytes[-1] != 8: self._mbedSerial.write(bytearray([8])) self._mbedSerial.flush() # wait until everything is written lastSendTime = time() if time() - lastPrintTime > 1: print self._joystick.getStateBytes('dict') lastPrintTime = time()
class FishJoystickController: # @param mbedPort [str] The serial port to use on the mbed # @param mbedBaud [int] The baud rate to use with the mbed # @param mbedUpdateInterval [float] Time in ms to wait between sending updated state to the mbed def __init__(self, mbedPort='/dev/ttyAMA0', mbedBaud=115200, mbedUpdateInterval=0.05, useLJ = True): self._mbedSerial = serial.Serial(mbedPort, baudrate=mbedBaud, timeout=None) # timeout = None means blocking, timeout = 0 means non-blocking self._mbedUpdateInterval = mbedUpdateInterval self._joystick = FishJoystick(useLJ=useLJ) # The main loop to update state from joystick and send to mbed # @param runTime [flaot or None] The time to run for, or None for infinite loop def run(self, runTime=None): self._mbedSerial.flushInput() self._mbedSerial.flushOutput() lastSendTime = 0 lastPrintTime = 0 startTime = time() while runTime is None or time() - startTime < runTime: self._joystick.scan() sleep(0.01) # not sure if this is needed, but seems nice to reduce polling load on device? if time() - lastSendTime > self._mbedUpdateInterval: stateBytes = self._joystick.getStateBytes('bytearray') self._mbedSerial.write(stateBytes) if stateBytes[-1] != 0: self._mbedSerial.write(bytearray([0])) self._mbedSerial.flush() # wait until everything is written lastSendTime = time() if time() - lastPrintTime > 1: print self._joystick.getStateBytes('dict') lastPrintTime = time()
def joystickReader(self, joystickState, joystickStateNum): scanInterval = 0.01 # seconds printInterval = 2 # seconds lastPrintTime = 0 joystick = FishJoystick(joystick='snes') # read state and update joystickState while True: # get the latest joystick events joystick.scan() state = joystick.getStateBytes('dict') # convert the state to a state index stateNum = 0 stateNum += state['start'] stateNum += state['pitch'] << 1 stateNum += state['yaw'] << 4 stateNum += state['thrust'] << 7 stateNum += state['frequency'] << 9 # share state with other processes for (k, v) in state.iteritems(): joystickState[k] = v joystickStateNum.value = stateNum # wait until the next scan time sleep(scanInterval) if time() - lastPrintTime > printInterval: print joystickState, '\t', joystickStateNum lastPrintTime = time()