# Path: pinguino/qtgui/pinguino_core/uploader/uploader.py
# class baseUploader(object):
# 
#     # PyUSB Core module switch
#     # ------------------------------------------------------------------
# 
#     PYUSB_USE_CORE                  =   0 # (0=legacy, 1=core)
# 
#     # Hex format record types
#     # ------------------------------------------------------------------
#     # Data_Record = 00
#     # End_Of_File_Record = 01
#     # Extended_Segment_Address_Record = 02
#     # Start_Segment_Address_Record = 03
#     # Extended_Linear_Address_Record = 04
#     # Start_Linear_Address_Record = 05
# 
#     # Python3 compatibility (octals)
#     Data_Record                     = 0o0
#     End_Of_File_Record              = 0o1
#     Extended_Segment_Address_Record = 0o2
#     Start_Segment_Address_Record    = 0o3
#     Extended_Linear_Address_Record  = 0o4
#     Start_Linear_Address_Record     = 0o5
# 
# 
#     # Error codes returned by various functions
#     # ------------------------------------------------------------------
#     ERR_NONE                        = 100
#     ERR_CMD_ARG                     = 101
#     ERR_CMD_UNKNOWN                 = 102
#     ERR_DEVICE_NOT_FOUND            = 103
#     ERR_USB_INIT1                   = 104
#     ERR_USB_INIT2                   = 105
#     ERR_USB_OPEN                    = 106
#     ERR_USB_WRITE                   = 107
#     ERR_USB_READ                    = 108
#     ERR_HEX_OPEN                    = 109
#     ERR_HEX_STAT                    = 110
#     ERR_HEX_MMAP                    = 111
#     ERR_HEX_SYNTAX                  = 112
#     ERR_HEX_CHECKSUM                = 113
#     ERR_HEX_RECORD                  = 114
#     ERR_VERIFY                      = 115
#     ERR_EOL                         = 116
#     ERR_USB_ERASE                   = 117
# 
#     # Configuration
#     # ------------------------------------------------------------------
# 
#     INTERFACE_ID                    = 0x00
#     ACTIVE_CONFIG                   = 0x01
#     VSC_ACTIVE_CONFIG               = 0x02
# 
#     # ------------------------------------------------------------------
#     def add_report(self, message):
#         """ display message in the log window """
#         self.report.append(message)
#         logging.info(message)
# 
#     # ------------------------------------------------------------------
#     def getDevice(self, board):
#         """ Scans connected USB devices until it finds a Pinguino board """
# 
#         logging.info("Looking for a Pinguino device ...")
# 
#         if self.PYUSB_USE_CORE:
# 
#             device = usb.core.find(idVendor=board.vendor, idProduct=board.product)
#             if device is None :
#                 return self.ERR_DEVICE_NOT_FOUND
#             else :
#                 return device
# 
#         else:
# 
#             busses = usb.busses()
#             for bus in busses:
#                 for device in bus.devices:
#                     if (device.idVendor, device.idProduct) == (board.vendor, board.product):
#                         return device
#             return self.ERR_DEVICE_NOT_FOUND
# 
#     # ------------------------------------------------------------------
#     def initDevice(self, device):
#         """ Init a Pinguino device """
# 
#         #logging.info("OS is %s" % os.getenv("PINGUINO_OS_NAME"))
# 
#         if os.getenv("PINGUINO_OS_NAME") == "linux":
#             if device.idProduct == 0x003C: #self.P32_ID:
#                 # make sure the hid kernel driver is not active
#                 # functionality not available on Darwin or Windows
#                 if self.PYUSB_USE_CORE:
#                     if device.is_kernel_driver_active(self.INTERFACE_ID):
#                         try:
#                             device.detach_kernel_driver(self.INTERFACE_ID)
#                         except usb.core.USBError as e:
#                             sys.exit("Could not detach kernel driver: %s" % str(e))
#                 else:
#                     try:
#                         handle.detachKernelDriver(self.INTERFACE_ID)
#                     except:
#                         #sys.exit("Could not detach kernel driver")
#                         pass
# 
#                 #logging.info("Kernel driver detached")
# 
# 
#         if self.PYUSB_USE_CORE:
#             try:
#                 device.set_configuration(self.ACTIVE_CONFIG)
#             except usb.core.USBError as e:
#                 sys.exit("Could not set configuration: %s" % str(e))
#             #logging.info("Configuration set")
# 
#             try:
#                 usb.util.claim_interface(device, self.INTERFACE_ID)
#                 #print("Claimed device")
#             except:
#                 sys.exit("Could not claim the device: %s" % str(e))
#             #logging.info("Interface claimed")
# 
#             return device
# 
#         else:
#             handle = device.open()
#             if handle:
#                 handle.setConfiguration(self.ACTIVE_CONFIG)
#                 handle.claimInterface(self.INTERFACE_ID)
#                 return handle
#             return ERR_USB_INIT1
# 
#         #logging.info("Everything OK so far")
# 
#     # ------------------------------------------------------------------
#     def closeDevice(self, handle):
#         """ Close currently-open USB device """
# 
#         if self.PYUSB_USE_CORE:
#             usb.util.release_interface(handle, self.INTERFACE_ID)
#         else:
#             handle.releaseInterface()


import sys
import os
from .uploader import baseUploader

"""-------------------------------------------------------------------------
    Pinguino Universal Uploader

    (c) 2011 Regis Blanchot <rblanchot@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-------------------------------------------------------------------------"""

# This class is based on :
# - Docker software licensed (LGPL) by Pierre Gaufillet <pierre.gaufillet@magic.fr>
# - Vasco  software licensed (LGPL) by Jean-Pierre Mandon <jp.mandon@gmail.com>
# PyUSB Doc can be found here: http://wiki.erazor-zone.de/wiki:projects:python:pyusb:pydoc
# To get the Device Descriptors : lsusb -v -d 04d8:feaa

#import usb			# checked in check.py



# Path: pinguino/qtgui/pinguino_core/uploader/uploader.py
# class baseUploader(object):
# 
#     # PyUSB Core module switch
#     # ------------------------------------------------------------------
# 
#     PYUSB_USE_CORE                  =   0 # (0=legacy, 1=core)
# 
#     # Hex format record types
#     # ------------------------------------------------------------------
#     # Data_Record = 00
#     # End_Of_File_Record = 01
#     # Extended_Segment_Address_Record = 02
#     # Start_Segment_Address_Record = 03
#     # Extended_Linear_Address_Record = 04
#     # Start_Linear_Address_Record = 05
# 
#     # Python3 compatibility (octals)
#     Data_Record                     = 0o0
#     End_Of_File_Record              = 0o1
#     Extended_Segment_Address_Record = 0o2
#     Start_Segment_Address_Record    = 0o3
#     Extended_Linear_Address_Record  = 0o4
#     Start_Linear_Address_Record     = 0o5
# 
# 
#     # Error codes returned by various functions
#     # ------------------------------------------------------------------
#     ERR_NONE                        = 100
#     ERR_CMD_ARG                     = 101
#     ERR_CMD_UNKNOWN                 = 102
#     ERR_DEVICE_NOT_FOUND            = 103
#     ERR_USB_INIT1                   = 104
#     ERR_USB_INIT2                   = 105
#     ERR_USB_OPEN                    = 106
#     ERR_USB_WRITE                   = 107
#     ERR_USB_READ                    = 108
#     ERR_HEX_OPEN                    = 109
#     ERR_HEX_STAT                    = 110
#     ERR_HEX_MMAP                    = 111
#     ERR_HEX_SYNTAX                  = 112
#     ERR_HEX_CHECKSUM                = 113
#     ERR_HEX_RECORD                  = 114
#     ERR_VERIFY                      = 115
#     ERR_EOL                         = 116
#     ERR_USB_ERASE                   = 117
# 
#     # Configuration
#     # ------------------------------------------------------------------
# 
#     INTERFACE_ID                    = 0x00
#     ACTIVE_CONFIG                   = 0x01
#     VSC_ACTIVE_CONFIG               = 0x02
# 
#     # ------------------------------------------------------------------
#     def add_report(self, message):
#         """ display message in the log window """
#         self.report.append(message)
#         logging.info(message)
# 
#     # ------------------------------------------------------------------
#     def getDevice(self, board):
#         """ Scans connected USB devices until it finds a Pinguino board """
# 
#         logging.info("Looking for a Pinguino device ...")
# 
#         if self.PYUSB_USE_CORE:
# 
#             device = usb.core.find(idVendor=board.vendor, idProduct=board.product)
#             if device is None :
#                 return self.ERR_DEVICE_NOT_FOUND
#             else :
#                 return device
# 
#         else:
# 
#             busses = usb.busses()
#             for bus in busses:
#                 for device in bus.devices:
#                     if (device.idVendor, device.idProduct) == (board.vendor, board.product):
#                         return device
#             return self.ERR_DEVICE_NOT_FOUND
# 
#     # ------------------------------------------------------------------
#     def initDevice(self, device):
#         """ Init a Pinguino device """
# 
#         #logging.info("OS is %s" % os.getenv("PINGUINO_OS_NAME"))
# 
#         if os.getenv("PINGUINO_OS_NAME") == "linux":
#             if device.idProduct == 0x003C: #self.P32_ID:
#                 # make sure the hid kernel driver is not active
#                 # functionality not available on Darwin or Windows
#                 if self.PYUSB_USE_CORE:
#                     if device.is_kernel_driver_active(self.INTERFACE_ID):
#                         try:
#                             device.detach_kernel_driver(self.INTERFACE_ID)
#                         except usb.core.USBError as e:
#                             sys.exit("Could not detach kernel driver: %s" % str(e))
#                 else:
#                     try:
#                         handle.detachKernelDriver(self.INTERFACE_ID)
#                     except:
#                         #sys.exit("Could not detach kernel driver")
#                         pass
# 
#                 #logging.info("Kernel driver detached")
# 
# 
#         if self.PYUSB_USE_CORE:
#             try:
#                 device.set_configuration(self.ACTIVE_CONFIG)
#             except usb.core.USBError as e:
#                 sys.exit("Could not set configuration: %s" % str(e))
#             #logging.info("Configuration set")
# 
#             try:
#                 usb.util.claim_interface(device, self.INTERFACE_ID)
#                 #print("Claimed device")
#             except:
#                 sys.exit("Could not claim the device: %s" % str(e))
#             #logging.info("Interface claimed")
# 
#             return device
# 
#         else:
#             handle = device.open()
#             if handle:
#                 handle.setConfiguration(self.ACTIVE_CONFIG)
#                 handle.claimInterface(self.INTERFACE_ID)
#                 return handle
#             return ERR_USB_INIT1
# 
#         #logging.info("Everything OK so far")
# 
#     # ------------------------------------------------------------------
#     def closeDevice(self, handle):
#         """ Close currently-open USB device """
# 
#         if self.PYUSB_USE_CORE:
#             usb.util.release_interface(handle, self.INTERFACE_ID)
#         else:
#             handle.releaseInterface()


# Path: pinguino/qtgui/pinguino_core/uploader/uploaderVSC.py
import sys
import os
from .uploader import baseUploader


"""-------------------------------------------------------------------------
    Pinguino Universal Uploader

    (c) 2011 Regis Blanchot <rblanchot@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-------------------------------------------------------------------------"""

# This class is based on :
# - Docker software licensed (LGPL) by Pierre Gaufillet <pierre.gaufillet@magic.fr>
# - Vasco  software licensed (LGPL) by Jean-Pierre Mandon <jp.mandon@gmail.com>
# PyUSB Doc can be found here: http://wiki.erazor-zone.de/wiki:projects:python:pyusb:pydoc
# To get the Device Descriptors : lsusb -v -d 04d8:feaa

#import usb			# checked in check.py



class uploaderVSC(baseUploader):