Skip to content

Commit

Permalink
GUI tab for firmware update
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1co committed Nov 4, 2016
1 parent f0518f8 commit cc957b5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
121 changes: 89 additions & 32 deletions pmca-gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,76 @@ def doAfter(self, result):
self.ui.installButton.config(state=NORMAL)


class InstallerUi(UiRoot):
MODE_APP = 0
MODE_APK = 1
class FirmwareUpdateTask(BackgroundTask):
"""Task to run firmwareUpdateCommand()"""
def doBefore(self):
self.ui.fwUpdateButton.config(state=DISABLED)
return self.ui.getSelectedDat()

def do(self, datFile):
try:
if datFile:
print ''
with open(datFile, 'rb') as f:
firmwareUpdateCommand(f)
except Exception:
traceback.print_exc()

def doAfter(self, result):
self.ui.fwUpdateButton.config(state=NORMAL)


class InstallerUi(UiRoot):
"""Main window"""
def __init__(self, title):
UiRoot.__init__(self)

self.title(title)
self.geometry('450x500')

mainFrame = Frame(self, padding=10)
mainFrame.pack(fill=X)
tabs = Notebook(self, padding=5)
tabs.pack(fill=X)

tabs.add(InfoFrame(self, padding=10), text='Camera info')
tabs.add(InstallerFrame(self, padding=10), text='Install app')
tabs.add(FirmwareFrame(self, padding=10), text='Update firmware')

self.logText = ScrollingText(self)
self.logText.text.configure(state=DISABLED)
self.logText.pack(fill=BOTH, expand=True)

self.redirectStreams()

def log(self, msg):
self.logText.text.configure(state=NORMAL)
self.logText.text.insert(END, msg)
self.logText.text.configure(state=DISABLED)
self.logText.text.see(END)

def redirectStreams(self):
for stream in ['stdout', 'stderr']:
setattr(sys, stream, PrintRedirector(lambda str: self.run(lambda: self.log(str)), getattr(sys, stream)))


class InfoFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)

self.infoButton = Button(self, text='Get camera info', command=InfoTask(self).run, padding=5)
self.infoButton.pack(fill=X)


class InstallerFrame(UiFrame):
MODE_APP = 0
MODE_APK = 1

def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)

self.modeVar = IntVar()
self.modeVar.set(self.MODE_APP)

appFrame = Labelframe(mainFrame, padding=5)
appFrame = Labelframe(self, padding=5)
appFrame['labelwidget'] = Radiobutton(appFrame, text='Select an app from the app list', variable=self.modeVar, value=self.MODE_APP)
appFrame.pack(fill=X)

Expand All @@ -110,7 +162,7 @@ def __init__(self, title):
self.appLoadButton = Button(appFrame, text='Refresh', command=AppLoadTask(self).run)
self.appLoadButton.pack()

apkFrame = Labelframe(mainFrame, padding=5)
apkFrame = Labelframe(self, padding=5)
apkFrame['labelwidget'] = Radiobutton(apkFrame, text='Select an apk', variable=self.modeVar, value=self.MODE_APK)
apkFrame.pack(fill=X)

Expand All @@ -120,22 +172,10 @@ def __init__(self, title):
self.apkSelectButton = Button(apkFrame, text='Open apk...', command=self.openApk)
self.apkSelectButton.pack()

buttonFrame = Labelframe(mainFrame, padding=5)
buttonFrame['labelwidget'] = Label(buttonFrame, text='Actions')
buttonFrame.pack(fill=X)

self.infoButton = Button(buttonFrame, text='Get camera info', command=InfoTask(self).run)
self.infoButton.pack(fill=X)

self.installButton = Button(buttonFrame, text='Install selected app', command=InstallTask(self).run)
self.installButton.pack(fill=X)
self.installButton = Button(self, text='Install selected app', command=InstallTask(self).run, padding=5)
self.installButton.pack(fill=X, pady=(5, 0))

self.logText = ScrollingText(self)
self.logText.text.configure(state=DISABLED)
self.logText.pack(fill=BOTH, expand=True)

self.redirectStreams()
AppLoadTask(self).run()
self.run(AppLoadTask(self).run)

def getMode(self):
return self.modeVar.get()
Expand All @@ -150,16 +190,6 @@ def openApk(self):
def getSelectedApk(self):
return self.apkFile.get()

def log(self, msg):
self.logText.text.configure(state=NORMAL)
self.logText.text.insert(END, msg)
self.logText.text.configure(state=DISABLED)
self.logText.text.see(END)

def redirectStreams(self):
for stream in ['stdout', 'stderr']:
setattr(sys, stream, PrintRedirector(lambda str: self.run(lambda: self.log(str)), getattr(sys, stream)))

def setAppList(self, apps):
self.appList = apps
self.appCombo['values'] = [''] + [app.name for app in apps]
Expand All @@ -170,6 +200,33 @@ def getSelectedApp(self):
return self.appList[self.appCombo.current() - 1]


class FirmwareFrame(UiFrame):
def __init__(self, parent, **kwargs):
UiFrame.__init__(self, parent, **kwargs)

datFrame = Labelframe(self, padding=5)
datFrame['labelwidget'] = Label(datFrame, text='Firmware file')
datFrame.pack(fill=X)

self.datFile = Entry(datFrame)
self.datFile.pack(side=LEFT, fill=X, expand=True)

self.datSelectButton = Button(datFrame, text='Open...', command=self.openDat)
self.datSelectButton.pack()

self.fwUpdateButton = Button(self, text='Update firmware', command=FirmwareUpdateTask(self).run, padding=5)
self.fwUpdateButton.pack(fill=X, pady=(5, 0))

def openDat(self):
fn = askopenfilename(filetypes=[('Firmware files', '.dat'), ('All files', '.*')])
if fn:
self.datFile.delete(0, END)
self.datFile.insert(0, fn)

def getSelectedDat(self):
return self.datFile.get()


def main():
"""Gui main"""
ui = InstallerUi('pmca-gui' + (' ' + version if version else ''))
Expand Down
6 changes: 5 additions & 1 deletion pmca/commands/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ def firmwareUpdateCommand(file, driverName=None):

with importDriver(driverName) as driver:
device = getDevice(driver)
if device and not isinstance(device, SonyMtpAppInstaller):
if device:
if isinstance(device, SonyMtpAppInstaller):
print 'Error: Cannot use camera in app install mode. Please restart the device.'
return

dev = SonyUpdaterCamera(device)

print 'Initializing firmware update'
Expand Down
9 changes: 9 additions & 0 deletions pmca/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def _processQueue(self):
self.after(100, self._processQueue)


class UiFrame(Frame):
def __init__(self, parent, **kwargs):
Frame.__init__(self, parent, **kwargs)
self._parent = parent

def run(self, func):
self._parent.run(func)


class BackgroundTask:
"""Similar to Android's AsyncTask"""
def __init__(self, ui):
Expand Down

0 comments on commit cc957b5

Please sign in to comment.