示例#1
0
    def __init__(self, pin):
        self.times = deque()
        self.maxlen = 10

        self.result_falling = None
        self.start_falling = 0
        self.result_rising = None
        self.start_rising = 0

        self.pin = Pin(pin, Pin.IN, pull=Pin.PULL_UP)
        self.pin.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING),
                     handler=self.trig_falling_or_rising)
    def _get_penalty_score(self):
        """Calculates and returns the penalty score based on state of this QR Code's current modules.
		This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score."""
        result = 0
        size = self._size
        modules = self._modules

        # Adjacent modules in row having same color, and finder-like patterns
        for y in range(size):
            runhistory = collections.deque([0] * 7)
            color = False
            runx = 0
            for x in range(size):
                if modules[y][x] == color:
                    runx += 1
                    if runx == 5:
                        result += QrCode._PENALTY_N1
                    elif runx > 5:
                        result += 1
                else:
                    runhistory.appendleft(runx)
                    if not color and QrCode.has_finder_like_pattern(
                            runhistory):
                        result += QrCode._PENALTY_N3
                    color = modules[y][x]
                    runx = 1
            runhistory.appendleft(runx)
            if color:
                runhistory.appendleft(0)  # Dummy run of white
            if QrCode.has_finder_like_pattern(runhistory):
                result += QrCode._PENALTY_N3
        # Adjacent modules in column having same color, and finder-like patterns
        for x in range(size):
            runhistory = collections.deque([0] * 7)
            color = False
            runy = 0
            for y in range(size):
                if modules[y][x] == color:
                    runy += 1
                    if runy == 5:
                        result += QrCode._PENALTY_N1
                    elif runy > 5:
                        result += 1
                else:
                    runhistory.appendleft(runy)
                    if not color and QrCode.has_finder_like_pattern(
                            runhistory):
                        result += QrCode._PENALTY_N3
                    color = modules[y][x]
                    runy = 1
            runhistory.appendleft(runy)
            if color:
                runhistory.appendleft(0)  # Dummy run of white
            if QrCode.has_finder_like_pattern(runhistory):
                result += QrCode._PENALTY_N3

        # 2*2 blocks of modules having same color
        for y in range(size - 1):
            for x in range(size - 1):
                if modules[y][x] == modules[y][x + 1] == modules[
                        y + 1][x] == modules[y + 1][x + 1]:
                    result += QrCode._PENALTY_N2

        # Balance of black and white modules
        black = sum((1 if cell else 0) for row in modules for cell in row)
        total = size**2  # Note that size is odd, so black/total != 1/2
        # Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
        k = (abs(black * 20 - total * 10) + total - 1) // total - 1
        result += k * QrCode._PENALTY_N4
        return result
示例#3
0
 def __init__(self, maxsize=0):
     self.maxsize = maxsize
     self._queue = deque()