spokechar.py

来自「旋转16个LED灯控制程序」· Python 代码 · 共 1,009 行 · 第 1/3 页

PY
1,009
字号
        
        # create the panel with the UI elements
        
        lowerpanel = wx.Panel(mainpanel)
        lowerpanelsizer = wx.BoxSizer(wx.HORIZONTAL)
        
        # create the panel with the buttons
        
        buttonpanel = wx.Panel(lowerpanel)
        buttonpanelsizer = wx.GridSizer(1, 3, 5, 10)
        
        writebutton = wx.Button(buttonpanel, -1, "Upload Charset")
        verifybutton = wx.Button(buttonpanel, -1, "Verify Charset")
        messagebutton = wx.Button(buttonpanel, -1, "Upload Message")

        self.Bind(wx.EVT_BUTTON, self.OnUploadButton, writebutton);
        self.Bind(wx.EVT_BUTTON, self.OnVerifyButton, verifybutton);
        self.Bind(wx.EVT_BUTTON, self.OnMessageButton, messagebutton);

        buttonpanelsizer.Add(writebutton, 0, wx.ALL, 0)
        buttonpanelsizer.Add(verifybutton, 0, wx.ALL, 0)
        buttonpanelsizer.Add(messagebutton, 0, wx.ALL, 0)
        buttonpanel.SetSizer(buttonpanelsizer)
        buttonpanel.Layout()

        # add button panel to lower panel
        
        lowerpanelsizer.Add(buttonpanel, 1, wx.EXPAND|wx.ALL, 10)
        
        # size and layout the lower panel
        
        lowerpanel.SetSizer(lowerpanelsizer)
        lowerpanelsizer.Layout();

        # add lower panel to main panel
        
        mainpanelsizer.Add(lowerpanel, 0, wx.ALL | wx.ALIGN_CENTER, 10)
      
        # size and layout the main panel
        
        mainpanel.SetSizer(mainpanelsizer)
        mainpanel.Layout()
        
    # menu handlers for wrap, center
    
    def OnWrap(self,evt):
    
        self.Wrap()
        
    def OnCenter(self,evt):
    
        self.Center()
    
    # wrap the text in the editor
    
    def Wrap(self):
    
        # get the text from the editor
        
        theLines = self.ed.GetText()

        # start with an empty output buffer
        
        newLines = []
        
        # wrap each line in turn
        
        for cLine in theLines:
            while len(cLine) > 16:
                newLines = newLines + [cLine[:16]]
                cLine = cLine[16:]
            newLines = newLines + [cLine]
            
        # update the editor
        
        self.ed.SetText(newLines)

    def Center(self):
    
        # the first thing we do is wrap the text, because
        # we can't center on 16 character lines without wrapping
        
        self.Wrap()
        
        # get the text from the editor
        
        theLines = self.ed.GetText()

        # start with an empty output buffer
        
        newLines = []
        
        # and preset a line of blanks
        
        blanks = '                '
        
        # center each line in turn
        
        for cLine in theLines:
        
            # first, strip of leading and trailing blanks
            
            cLine = cLine.strip()
            
            # if the line isn't empty (pointless to center)
            
            if cLine != "":
            
                # how many characters do we need to prepend?
                
                n = int((16-len(cLine))/2)
                
                # if that number is > 0, add the characters
                
                if n > 0:
                
                    cLine = blanks[:n] + cLine
                    
            # add the revised line to the buffer we'll be updating
                    
            newLines = newLines + [cLine]
            
        # update the editor
        
        self.ed.SetText(newLines)

    # stupid dialog box function
    
    def Alert(self,title,msg):
        dlg = wx.MessageDialog(self, msg,
                               title,
                               wx.OK | wx.ICON_INFORMATION
                               #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                               )
        dlg.ShowModal()
        dlg.Destroy()

    # load a txt file and stuff it in the editor
    
    def OnOpen(self, evt):

        # the first step is to determine what charset must be loaded
        
        dialog = wx.FileDialog(self,
                               "Select Message File...",  # title
                               "", "",          # no defaults
                               "TEXT files (*.txt)|*.txt",  # TXT only 
                               wx.OPEN);        # open a file

        # quit if they cancelled on us
        
        if (dialog.ShowModal() == wx.ID_CANCEL):
            return


        try:
        
            # open the file
            
            theFile = open(dialog.GetPath(),'r')
            
            # read the lines
            
            lines = theFile.readlines()
            
            # close the file
            
            theFile.close()
            
            # set the lines
            
            self.ed.SetText(lines)
    
            # update the filename
            
            filename = dialog.GetPath()
            
            self.SetTitle("SpokeChar - SpokePOV Character Set Utility - " + self.filename)
            
            # since we now have a filename, we can enable the
            # save menu...
            
            self.filemenu.Enable(1,True)

        except IOError:
            self.SetStatus("Error reading the file!")
        
    # save the text file
    
    def OnSave(self, evt):

        # we will only be called when self.filename has a value
        
        try:
            f = open(self.filename, 'w')
            
            theLines = self.ed.GetText()
            
            for cLine in theLines:
                f.write(cLine + '\n')
            
            f.close()
        
        except IOError:
            self.SetStatus("Error writing the file!")
        
    # save the text file as a new file
    
    def OnSaveAs(self, evt):

        # ask where to save file
        
        if self.filename == None:
            dFName = ""
        else:
            dFname = self.filename
            
        dialog = wx.FileDialog(self,
                               "Save Message File as...",  # title
                               "", dFName,          # defaults
                               "TEXT file (*.txt)|*.txt",  # TXT only 
                               wx.SAVE);        # open a file
        
        # did he want us to do so?

        if (dialog.ShowModal() == wx.ID_CANCEL):
            return
            
        # save the file, update filename
        
        try:
        
            # same basic procedure as the load
            
            f = open(dialog.GetPath(), 'w')
            
            theLines = self.ed.GetText()
            
            for cLine in theLines:
                f.write(cLine + '\n')
                
            f.close()
        
            self.filename = dialog.GetPath()
            
            self.SetTitle("SpokeChar - SpokePOV Character Set Utility - " + self.filename)

            self.filemenu.Enable(1,True)

        except IOError:
            self.SetStatus("Error writing the file!")
            
        
    # read the EEPROM and verify that it contains the current charset
    
    def OnVerifyButton(self, evt):
                    
        # the first step is to determine what charset must be loaded
        
        dialog = wx.FileDialog(self,
                               "Choose an charset to verify against EEPROM...",  # title
                               "", "",          # no defaults
                               "BMP files (*.bmp)|*.bmp",  # BMP only 
                               wx.OPEN);        # open a file

        # quit if they cancelled on us
        
        if (dialog.ShowModal() == wx.ID_CANCEL):
            return
        
        # get the image from the file

        charset = wx.Image(dialog.GetPath())
        
        # ensure it's a charset (more or less, mostly less)
        
        if (charset.GetWidth() != 768) or (charset.GetHeight() != 32):
            self.SetStatusText("Cannot load - A SpokePOV character set is a 768 x 32 bitmap; the file you selected is %d x %d " % (charset.GetWidth(), charset.GetHeight()) )
            return
            
        # convert the image to monochrome (it ought to be that already)
        # and get access to the bits
        
        charset.ConvertToMono(0,0,0)
        imagedata = charset.GetData()

        # now we check the character set against the SpokePOV.  This
        # code is heavily cribbed from SpokePOV.py.  Basically,
        # we send the character set in blocks of 16 bytes.  Each
        # 32 pixel column in the character set is 4 bytes, so we
        # check 4 columns at a time.
        
        spov = SpokePOVComm()
        
        try:
        
            # x is the actual column number in the bitmap
            
            x = 0
            lasttime = time.time();
            
            # while we still have columns to check
            
            while x < 768:
            
                # clear out our buffer of 16 bytes
                
                buff16 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
                
                # assume the bits are in simple order
                
                # for each byte in the block
                
                # print ""
                # print "16 bytes starting at column ",x
                
                # I must mention that the python "range" function
                # defies all common sense.  range(0,15) actually only
                # executes values 0-14!  Someone was on serious
                # drugs...
                
                for b in range(0,16):
                
                    # print ""
                    # print "   Byte ",b
                    
                    # what column is this byte actually in?
                    
                    x1 = x + int(b/4)
                    
                    # and what row do we start at?
                    
                    y1 = 8*(b%4)
                    
                    # assemble the byte
                    
                    foo = 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?