PyQt5 series tutorial (5) create a fastboot writer
Software and hardware environment
- Windows 7
- Python 3.4.2
- PyQt 5.5.1
- PyCharm 5.0.2
Preface
Fastboot is a flash brush method for Android devices. It is more underlying than recovery, and the Flash speed is faster. The project to be completed in this article is to use PyQt5 to add a GUI layer for fastboot, making the operation easier and more convenient.
Requirement Analysis
First, you need to know what fastboot can do. You can enter fastboot in cmd to view its help information.
This project mainly implements the fastboot burning function. Other information related to getting and setting images is not involved, and you can add them as needed.
Another special requirement is that the burned image needs to be modified. I call it BBCB here, and some of its data is input by users.
Interface Design
With QtDesigner, we only use label, pushButton, and lineedit, as well as horizontal layout, vertical layout, and menu bar.
Code Writing update the UI in QThread
Use signal and slot mechanisms. First, create a class inherited from QThread and send a signal when updating the UI.
class detectDeviceThread(QThread): detectSignal = pyqtSignal(object) def __int__(self): QThread.__init__(self) def run(self): while True: deviceNum = lj_list_device_id(self) self.detectSignal.emit(deviceNum) time.sleep(3)
At the UI Layer, bind the signal and slot, and then implement the slot. The UI update is executed in the slot.
Def onDeviceDetected (self, data): ''' the operation after the Android device is online is detected: return: ''' font = QtGui. QFont () font. setFamily (" Light") font. setPointSize (12) font. setBold (True) self. textBrowser_device_id.setFont (font) if (common. PREVIOUS_DATA! = Data): common. PREVIOUS_DATA = data self. textBrowser_device_id.setText (data) if not data. strip (): common. FLAG_DEVICE_ONLINE = False else: common. FLAG_DEVICE_ONLINE = Truedef startDetectDevice (self): ''' enable the thread to detect Android devices going online: return: ''' self. detectThread = detectDeviceThread () self. detectThread. detectSignal. connect (self. onDeviceDetected) self. detectThread. start ()
Struct processing binary data
Here I need to change the data of several bytes of the BBCB image (similar to the structured data type in C) and use the struct module. Look at the code segment
fp = open("toc\\nvram.toc","rb+")fp.seek(common.BBCB_OFFSET + 2,os.SEEK_SET) manufacturer_id = fp.read(1) manufacturer_id_new = int(self.lineEdit_manu_id.text()) if(manufacturer_id != manufacturer_id_new): logging.debug("newmanufacturer_id: " + str(manufacturer_id_new)) fp.seek(-1,os.SEEK_CUR) fp.write(struct.pack("B",manufacturer_id_new)) fp.flush()
First, open the file, locate the file, and write the data to be written to the file in a specific format. The following table lists the formats supported by struct:
In the preceding example, the value of manufacturer_id_new is written into the file in the format of "B", that is, the unsigned char in the C language. According to the table above, if it is a string of 5 characters, the format should be "5s"
Use of the logging Module
Here we use the logging that comes with python as the log system. Because it needs to be used in multiple modules (multiple files), we perform global initialization at the project entrance.
Def initLogConfiguration (): '''initialize log configuration ''' logging. basicConfig (level = logging. DEBUG, filename = common. LOGFILE, filemode = 'a + ', format =' % (asctime) s-% (filename) s-line % (lineno)-4d-% (levelname) s-% (message) s', datefmt = '% m-% d % H: % m ')
Here is the output to the file. You can also output it to the console. You can specify it through the stream parameter. If both filename and stream are specified, logging will ignore stream. After initialization, you can use it directly in other modules that need to output logs.
logging.debug("some message")
Cross-platform
It mainly judges the current platform and then processes it in different ways. The platform library is used here.
import platformPLATFORM_SYSTEM = platform.system()OS_MAC = (PLATFORM_SYSTEM == "Darwin")OS_WIN = (PLATFORM_SYSTEM == "Windows")OS_LINUX = (PLATFORM_SYSTEM == "Linux")
Source Code address
Https://github.com/djstava/PyQt5Fastboot