tests/wiimote/py_ext_0@py_ext/pyfile.xml
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 25 May 2018 17:23:15 +0300
changeset 2166 5ce6d08ff2c7
parent 1185 b36755d7c19e
permissions -rw-r--r--
make clipboard open minimal time as wxPython documentation recommends

https://wxpython.org/Phoenix/docs/html/wx.Clipboard.html#wx.Clipboard.GetData

"Call wx.Clipboard.Open to get ownership of the clipboard. If this
operation returns True, you now own the clipboard. Call
wx.Clipboard.SetData to put data on the clipboard, or
wx.Clipboard.GetData to retrieve data from the clipboard. Call
wx.Clipboard.Close to close the clipboard and relinquish ownership.
You should keep the clipboard open only momentarily."

Maybe it makes situation with pretty annoying error 'clipboard already
open' a little bit better.

traceback:
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/BeremizIDE.py", line 955, in OnSaveProjectMenu
self.RefreshAfterSave()
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/BeremizIDE.py", line 946, in RefreshAfterSave
self._Refresh(TITLE, FILEMENU, EDITMENU, PAGETITLES)
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/IDEFrame.py", line 926, in _Refresh
self.RefreshFunctions[element]()
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/BeremizIDE.py", line 766, in RefreshEditMenu
IDEFrame.RefreshEditMenu(self)
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/IDEFrame.py", line 1185, in RefreshEditMenu
if self.GetCopyBuffer() is not None:
File "/home/developer/WorkData/PLC/beremiz/avangard-beremiz-ide/src/../../beremiz/IDEFrame.py", line 956, in GetCopyBuffer
if wx.TheClipboard.Open():
File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk3/wx/_misc.py", line 5793, in Open
return _misc_.Clipboard_Open(*args, **kwargs)
<class 'wx._core.PyAssertionError'>: C++ assertion "!m_open" failed at ../src/gtk/clipbrd.cpp(598) in Open(): clipboard already open
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PyFile>
  <variables>
    <variable name="WiiNunchuckStickX" type="INT"/>
    <variable name="WiiNunchuckStickY" type="INT"/>
    <variable name="WiiNunchuckAccX" type="INT"/>
    <variable name="WiiNunchuckAccY" type="INT"/>
    <variable name="WiiNunchuckAccZ" type="INT"/>
    <variable name="WiiNunchuckButtons" type="WORD"/>
    <variable name="WiiButtons" type="WORD"/>
  </variables>
  <globals>
<![CDATA[
import cwiid,commands,sys,re,os,time

wiimote = None
WIIMOTE_ADDR_MODEL = re.compile("((?:[0-9A-F]{2})(?::[0-9A-F]{2}){5})\s*Nintendo")
nunchuckzero = None

def Wiimote_cback(messages, time):
    global nunchuckzero
    state = dict(messages)
    bts = state.get(cwiid.MESG_BTN, None)
    if bts is not None:
        PLCGlobals.WiiButtons = bts
    nunchuck = state.get(cwiid.MESG_NUNCHUK, None)
    if nunchuck is not None:
        PLCGlobals.WiiNunchuckButtons = nunchuck['buttons']
        X,Y = nunchuck['stick']
        PLCGlobals.WiiNunchuckAccX = nunchuck['acc'][cwiid.X]
        PLCGlobals.WiiNunchuckAccY = nunchuck['acc'][cwiid.Y]
        PLCGlobals.WiiNunchuckAccZ = nunchuck['acc'][cwiid.Z]
        if nunchuckzero is None:
            nunchuckzero = X,Y
        (PLCGlobals.WiiNunchuckStickX,
         PLCGlobals.WiiNunchuckStickY) = X-nunchuckzero[0],Y-nunchuckzero[1]

def Connect_Wiimote(connected_callback):
    global wiimote,nunchuckzero
    mac_addr = ''
    try:
        mac_addr = file("wiimac.txt","rt").read()
    except:
        PLCObject.LogMessage("Wiimote MAC unknown, scanning bluetooth")
        output = commands.getoutput("hcitool scan")
        result = WIIMOTE_ADDR_MODEL.search(output)
        if result is not None:
            mac_addr = result.group(1)
            PLCObject.LogMessage("Found Wiimote with MAC %s"%mac_addr)
            file("wiimac.txt","wt").write(mac_addr)
 
    # Connect to wiimote
    if not mac_addr:
        PLCObject.LogMessage("Connection to unknown Wiimote...")
        wiimote = cwiid.Wiimote()
    else:
        PLCObject.LogMessage("Connection to Wiimote %s..."%mac_addr)
        wiimote = cwiid.Wiimote(mac_addr)

    if wiimote is not None:
        nunchuckzero = None
        wiimote.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT
        # use the callback interface
        wiimote.mesg_callback = Wiimote_cback
        wiimote.enable(cwiid.FLAG_MESG_IFC)
        connected_callback(mac_addr)
        PLCObject.LogMessage("Wiimote %s Connected"%mac_addr)
    else:
        PLCObject.LogMessage("Wiimote %s not found"%mac_addr)
        os.remove("wiimac.txt")
        connected_callback(None)

def Disconnect_Wiimote():
  global wiimote
  if wiimote is not None:
    wiimote.disable(cwiid.FLAG_MESG_IFC)
    time.sleep(0.1)
    wiimote.close()
    wiimote = None
    PLCObject.LogMessage("Wiimote disconnected")

]]>
  </globals>
  <init>
<![CDATA[
]]>
  </init>
  <cleanup>
<![CDATA[
Disconnect_Wiimote()

]]>
  </cleanup>
  <start>
<![CDATA[
]]>
  </start>
  <stop>
<![CDATA[
]]>
  </stop>
</PyFile>