Commit efaa79ba authored by anthraxx's avatar anthraxx
Browse files

implemented fifo compatible scanner

this can be used for local testing and unit tests
parent 491935d2
......@@ -2,14 +2,13 @@
import logging
from sys import exit
from evdev import ecodes
from signal import signal, SIGINT
from sqlalchemy import create_engine
from argparse import ArgumentParser
from foobarpay.db import Database
from foobarpay.display import Display
from foobarpay.scanner import scancodes, Scanner
from foobarpay.scanner import EvdevScanner, FifoScanner
from foobarpay.logic import Logic
from foobarpay.model.product import Product
......@@ -23,7 +22,10 @@ class FooBarPay:
self.database = Database(cli_arguments.database, debug=cli_arguments.debug_sql)
self.display = Display(cli_arguments.display)
self.scanner = Scanner(cli_arguments.scanner)
if cli_arguments.scanner_driver == "fifo":
self.scanner = FifoScanner(cli_arguments.scanner)
else:
self.scanner = EvdevScanner(cli_arguments.scanner)
self.initialize_products()
self.logic = Logic(self.display, self.database)
......@@ -35,15 +37,8 @@ class FooBarPay:
def start(self):
logging.info("Welcome to foobarpay")
signal(SIGINT, lambda s, f: exit(0))
input_buffer = ""
while True:
for event in self.scanner.read_loop():
if event.type == ecodes.EV_KEY and event.value == 1:
if event.code == 28:
self.logic.handle_scanned_text(input_buffer)
input_buffer = ""
else:
input_buffer += scancodes.get(event.code) or ""
self.logic.handle_scanned_text(self.scanner.read())
if __name__ == "__main__":
parser = ArgumentParser()
......@@ -51,6 +46,7 @@ if __name__ == "__main__":
parser.add_argument('--debug-sql', dest='debug_sql', action='store_true', help='sql debug messages')
parser.add_argument('--display', dest='display', default=FooBarPay.DEFAULT_DISPLAY, help='specify display device')
parser.add_argument('--scanner', dest='scanner', default=FooBarPay.DEFAULT_SCANNER, help='specify scanner device')
parser.add_argument('--scanner-driver', dest='scanner_driver', choices=['evdev', 'fifo'], default='evdev', help='specify scanner driver')
parser.add_argument('--db', dest='database', default=FooBarPay.DEFAULT_DATABASE, help='specify database file')
parser.set_defaults(debug=False, debug_sql=False)
......
from evdev import InputDevice
from enum import Enum
from evdev import InputDevice, ecodes
scancodes = {
2: u'1',
3: u'2',
4: u'3',
5: u'4',
6: u'5',
7: u'6',
8: u'7',
9: u'8',
10: u'9',
11: u'0',
28: u'\n',
30: u'A',
48: u'B',
46: u'C',
32: u'D',
18: u'E',
33: u'F',
34: u'G',
35: u'H',
23: u'I',
36: u'J',
37: u'K',
38: u'L',
50: u'M',
49: u'N',
24: u'O',
25: u'P',
16: u'Q',
19: u'R',
31: u'S',
20: u'T',
22: u'U',
47: u'V',
17: u'W',
45: u'X',
21: u'Y',
44: u'Z',
12: u'-'
}
class Scanner(object):
class EvdevScanner(object):
def __init__(self, device):
self.device = InputDevice(device)
self.scancodes = {
2: u'1',
3: u'2',
4: u'3',
5: u'4',
6: u'5',
7: u'6',
8: u'7',
9: u'8',
10: u'9',
11: u'0',
28: u'\n',
30: u'A',
48: u'B',
46: u'C',
32: u'D',
18: u'E',
33: u'F',
34: u'G',
35: u'H',
23: u'I',
36: u'J',
37: u'K',
38: u'L',
50: u'M',
49: u'N',
24: u'O',
25: u'P',
16: u'Q',
19: u'R',
31: u'S',
20: u'T',
22: u'U',
47: u'V',
17: u'W',
45: u'X',
21: u'Y',
44: u'Z',
12: u'-'
}
self.device.grab()
def read_loop(self):
return self.device.read_loop()
def read(self):
input_buffer = ""
for event in self.device.read_loop():
if event.type == ecodes.EV_KEY and event.value == 1:
if event.code == 28:
break
else:
input_buffer += self.scancodes.get(event.code) or ""
return input_buffer
class FifoScanner(object):
def __init__(self, device):
self.fifo = open(device, 'r')
def read(self):
return self.fifo.readline()[:-1]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment