class CWLiteUSB(Parameterized):
    _name = "ChipWisperer-Lite USB"

    def __init__(self):
        self._cwusb = NAEUSB()

        # Connect required modules up here
        self.fpga = FPGA(self._cwusb)
        self.xmega = XMEGAPDI(self._cwusb)
        self.avr = AVRISP(self._cwusb)
        self.usart = USART(self._cwusb)

        self.getParams().addChildren([{
            'name':
            "CW-Lite XMEGA Programmer",
            'tip':
            "Open XMEGA Programmer (ChipWhisperer-Lite Only)",
            'type':
            "menu",
            "action":
            lambda _: self.getCwliteXMEGA().show()
        }, {
            'name':
            "CW-Lite AVR Programmer",
            'tip':
            "Open AVR Programmer (ChipWhisperer-Lite Only)",
            'type':
            "menu",
            "action":
            lambda _: self.getCwliteAVR().show()
        }])

    def con(self, idProduct):
        return self._cwusb.con(idProduct=idProduct)

    # def __del__(self):
    #     print "here"

    def dis(self):
        if self.params is not None:
            self.getParams().delete()
        self.params = None
        # gc.collect()
        # print sys.getrefcount(self)
        # print gc.get_referrers(self)

    def usbdev(self):
        return self._cwusb

    def getCwliteXMEGA(self):
        if not hasattr(self, 'cwliteXMEGA'):
            self.cwliteXMEGA = XMEGAProgrammerDialog()
            self.cwliteXMEGA.setUSBInterface(self.xmega)
        return self.cwliteXMEGA

    def getCwliteAVR(self):
        if not hasattr(self, 'cwliteAVR'):
            self.cwliteAVR = AVRProgrammerDialog()
            self.cwliteAVR.setUSBInterface(self.avr)
        return self.cwliteAVR
class CWLiteUSB(Parameterized):
    _name = "ChipWisperer-Lite USB"

    def __init__(self):
        self._cwusb = NAEUSB()

        # Connect required modules up here
        self.fpga = FPGA(self._cwusb)
        self.xmega = XMEGAPDI(self._cwusb)
        self.avr = AVRISP(self._cwusb)
        self.usart = USART(self._cwusb)

        self.getParams().addChildren([
            {'name':"CW-Lite XMEGA Programmer", 'tip':"Open XMEGA Programmer (ChipWhisperer-Lite Only)", 'type':"menu", "action":lambda _:self.getCwliteXMEGA().show()},
            {'name':"CW-Lite AVR Programmer", 'tip':"Open AVR Programmer (ChipWhisperer-Lite Only)", 'type':"menu", "action":lambda _:self.getCwliteAVR().show()}
        ])

    def con(self, idProduct):
        return self._cwusb.con(idProduct=idProduct)

    # def __del__(self):
    #     print "here"

    def dis(self):
        self.getParams().delete()
        self.params = None
        # gc.collect()
        # print sys.getrefcount(self)
        # print gc.get_referrers(self)

    def usbdev(self):
        return self._cwusb

    def getCwliteXMEGA(self):
        if not hasattr(self, 'cwliteXMEGA'):
            self.cwliteXMEGA = XMEGAProgrammerDialog()
            self.cwliteXMEGA.setUSBInterface(self.xmega)
        return self.cwliteXMEGA

    def getCwliteAVR(self):
        if not hasattr(self, 'cwliteAVR'):
            self.cwliteAVR = AVRProgrammerDialog()
            self.cwliteAVR.setUSBInterface(self.avr)
        return self.cwliteAVR
class CWLiteUSB(Parameterized):
    _name = "ChipWisperer-Lite USB"

    def __init__(self):
        self._cwusb = NAEUSB()

        # Connect required modules up here
        self.fpga = FPGA(self._cwusb)
        self.xmega = XMEGAPDI(self._cwusb)
        self.avr = AVRISP(self._cwusb)
        self.usart = USART(self._cwusb)
        self.serialstm32f = STM32FSerial(cwserial=self.usart,
                                         cwapi=CWCoreAPI.getInstance())

        self.getParams().addChildren([{
            'name':
            "CW-Lite XMEGA Programmer",
            'tip':
            "Open XMEGA Programmer (ChipWhisperer-Lite Only)",
            'type':
            "menu",
            "action":
            lambda _: self.getCwliteXMEGA().show()
        }, {
            'name':
            "CW-Lite AVR Programmer",
            'tip':
            "Open AVR Programmer (ChipWhisperer-Lite Only)",
            'type':
            "menu",
            "action":
            lambda _: self.getCwliteAVR().show()
        }, {
            'name':
            'Serial STM32F Programmer',
            'tip':
            "Open STM32F Programmer (Serial/ChipWhisperer)",
            'type':
            "menu",
            "action":
            lambda _: self.getSerialSTM32F().show()
        }])

    def get_possible_devices(self, idProduct):
        return self._cwusb.get_possible_devices(idProduct=idProduct)

    def con(self, *args, **kwargs):
        return self._cwusb.con(*args, **kwargs)

    # def __del__(self):
    #     print "here"

    def dis(self):
        if self.params is not None:
            self.getParams().delete()
        self.params = None
        # gc.collect()
        # print sys.getrefcount(self)
        # print gc.get_referrers(self)

    def usbdev(self):
        return self._cwusb

    def getCwliteXMEGA(self):
        if not hasattr(self, 'cwliteXMEGA'):
            self.cwliteXMEGA = XMEGAProgrammerDialog()
            self.cwliteXMEGA.setUSBInterface(self.xmega)
        return self.cwliteXMEGA

    def getCwliteAVR(self):
        if not hasattr(self, 'cwliteAVR'):
            self.cwliteAVR = AVRProgrammerDialog()
            self.cwliteAVR.setUSBInterface(self.avr)
        return self.cwliteAVR

    def getSerialSTM32F(self):
        if not hasattr(self, 'serialSTM32F'):
            self.serialSTM32F = STM32FProgrammerDialog()
            self.serialSTM32F.setUSBInterface(self.serialstm32f)
        return self.serialSTM32F