0

I have a usb barcode scanner which I want to use it in raspberry pi. Barcode scanner is a plug and play device i.e where ever is the current cursor focus it prints the output. My query is that how can I get that output in a pygame rect box, is there a way that whenever we scan the barcode the output appears on the pygame rec box. I couldn't get the cursor in pygame. I have to use pygame because our application is in pygame (written by previous colleague)and I have a very little familiarity with pygame. Is there an example which I can check?

Thanks,

class RectLabel:
def __init__(self, app, pos, dim, text, font, fontCol = 0, bcgCol = 0, txtMode = 'mpte', scaleFactor = 1.0):
    self.app = app
    self.disp = self.app.disp
    self.x = pos[0]
    self.y = pos[1]
    self.xdim = dim[0]
    self.ydim = dim[1]

    self.text = str(text)

    self.font = font

    self.txtMode = txtMode

    if not fontCol:
        self.setFontCol(self.app.font_col)
    else:
        self.setFontCol(fontCol)  

    if not bcgCol:
        self.setBcgCol(self.app.textView_col)
    else:
        self.setBcgCol(bcgCol)  

    self.bcgSurface = pg.Surface((self.xdim, self.ydim), pg.SRCALPHA)  # per-pixel alpha

    self.setHasBorder(False)

    self.scaleFactor = scaleFactor
def setFontCol(self, fontCol):
    self.fontCol = fontCol
    self.app.updateScreen()
def setBcgCol(self, bcgCol):
    self.bcgCol = bcgCol
    self.app.updateScreen()

def setText(self, text):
    self.text = str(text)
    self.app.updateScreen()
def setHasBorder(self, hasBorder):
    self.hasBorder = hasBorder
    self.app.updateScreen()
def display(self):
    self.show(self.x, self.y)

def displayWithY(self, y):
    self.show(self.x, y)

def show(self, x, y):
    self.bcgSurface.fill(self.bcgCol)
    self.disp.blit(self.bcgSurface, (x - self.xdim / 2, y - self.ydim / 2, self.xdim, self.ydim))

    if self.hasBorder:
        pg.draw.rect(self.disp, [0, 0, 255], (x - self.xdim/2,y - self.ydim / 2,self.xdim,self.ydim), 5)

    if self.txtMode == 'spu':
        txt.spu(self.disp,
                self.text,
                (x, y),
                self.font,
                self.fontCol)
    elif self.txtMode == 'spt':
        txt.spt(self.disp,
                self.text,
                (x, y),
                self.xdim * self.scaleFactor,
                self.font,
                self.fontCol)

    elif self.txtMode == 'mpue':
        txt.mpue(self.disp,
                self.text,
                (x, y),
                self.ydim * self.scaleFactor,
                self.font,
                self.fontCol)

    elif self.txtMode == 'mpta':
        txt.mpta(self.disp,
                self.text,
                (x, y),
                (self.xdim * self.scaleFactor, self.ydim * self.scaleFactor),
                self.font,
                self.fontCol)

    elif self.txtMode == 'mpte':
        txt.mpte(disp = self.disp,
                text = self.text,
                pos = (x, y),
                dim = (self.xdim * self.scaleFactor, self.ydim * self.scaleFactor),
                font = self.font,
                fontCol = self.fontCol)
A1g
  • 101
  • 3
  • 13
  • How does the barcode data arrive? Typically these emulate a keyboard, right? Is it a simple numeric barcode? Or something else - like an alphanumeric data-matrix code, etc. etc. etc.? How is it handled by the existing app? What is the normal work-flow with the barcode? – Kingsley Mar 15 '19 at 00:19
  • 1
    I've been thinking about this. It *sounds* like the PyGame app simply needs to handle the key-press events. There's an answer here: https://stackoverflow.com/a/22958344/1730895 that lists how to handle keyboard number (digit) input. There's some answers about text input-box modules here - https://stackoverflow.com/questions/16744043/text-input-box-in-pygame-python . Please edit your question to include more problem details if these links do not help. – Kingsley Mar 15 '19 at 02:56
  • as far as I know, you can just use a `pygame.font.Font()` instance and then center it into a `pygame.Surface()`, but I can't provide an answer unless you provide more code on how the program even works. – Mercury Platinum Mar 15 '19 at 11:38
  • Thank you @Kingsley, this was very helpful. Barcode data is just a alphanumeric data (1d and 2d barcode data). It prints out the output where ever the cursor is focused. The code I have is creating its own Rect Label and keyboard. Pi is connected to the 8 keys via GPIO to navigate that keyboard. and button press on that location selects the that letter. What I wanted to do is have a barcode output on that rect label so user have choices for both either manually enter the text or use barcode scanner. Current code doesnt have any cursor so I stuck on how to get that thing, attached is the code – A1g Mar 15 '19 at 15:39

0 Answers0