Skip to content

Commit

Permalink
Merge pull request #494 from kdmukai/qr_encoding_refactor
Browse files Browse the repository at this point in the history
QR Encoder refactor + fountain encoder `restart()`
  • Loading branch information
newtonick authored Apr 23, 2024
2 parents 1b9fc3f + ea10653 commit b17677c
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 433 deletions.
5 changes: 3 additions & 2 deletions src/seedsigner/gui/screens/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from seedsigner.gui.keyboard import Keyboard, TextEntryDisplay
from seedsigner.gui.renderer import Renderer
from seedsigner.hardware.buttons import HardwareButtonsConstants, HardwareButtons
from seedsigner.models.encode_qr import BaseQrEncoder
from seedsigner.models.settings import SettingsConstants
from seedsigner.models.threads import BaseThread, ThreadsafeCounter

Expand Down Expand Up @@ -657,10 +658,10 @@ def swap_selected_button(new_selected_button: int):

@dataclass
class QRDisplayScreen(BaseScreen):
qr_encoder: 'EncodeQR' = None
qr_encoder: BaseQrEncoder = None

class QRDisplayThread(BaseThread):
def __init__(self, qr_encoder: 'EncodeQR', qr_brightness: ThreadsafeCounter, renderer: Renderer,
def __init__(self, qr_encoder: BaseQrEncoder, qr_brightness: ThreadsafeCounter, renderer: Renderer,
tips_start_time: ThreadsafeCounter):
super().__init__()
self.qr_encoder = qr_encoder
Expand Down
13 changes: 12 additions & 1 deletion src/seedsigner/helpers/ur2/fountain_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(self, message, max_fragment_len, first_seq_num = 0, min_fragment_le
self.fragment_len = FountainEncoder.find_nominal_fragment_length(self.message_len, min_fragment_len, max_fragment_len)
self.fragments = FountainEncoder.partition_message(message, self.fragment_len)
self.seq_num = first_seq_num
self.current_part: Part = None

@staticmethod
def find_nominal_fragment_length(message_len, min_fragment_len, max_fragment_len):
Expand Down Expand Up @@ -143,7 +144,17 @@ def next_part(self):
indexes = choose_fragments(self.seq_num, self.seq_len(), self.checksum)
mixed = self.mix(indexes)
data = bytes(mixed)
return Part(self.seq_num, self.seq_len(), self.message_len, self.checksum, data)
self.current_part = Part(self.seq_num, self.seq_len(), self.message_len, self.checksum, data)
return self.current_part


def restart(self):
"""
Restart from the beginning; each cycle's first n frames are full data frames
(not XOR composites).
"""
self.seq_num = 0


def mix(self, indexes):
result = [0] * self.fragment_len
Expand Down
18 changes: 16 additions & 2 deletions src/seedsigner/helpers/ur2/ur_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@ def is_complete(self):
def is_single_part(self):
return self.fountain_encoder.is_single_part()

def next_part(self):
part = self.fountain_encoder.next_part()
def next_part(self) -> str:
if self.is_single_part():
return UREncoder.encode(self.ur)
else:
part = self.fountain_encoder.next_part()
return UREncoder.encode_part(self.ur.type, part)

def current_part(self) -> str:
if self.is_single_part():
return UREncoder.encode(self.ur)
else:
part = self.fountain_encoder.current_part
if not part:
part = self.fountain_encoder.next_part()
return UREncoder.encode_part(self.ur.type, part)


def restart(self):
self.fountain_encoder.restart()


@staticmethod
def encode_part(type, part):
Expand Down
Loading

0 comments on commit b17677c

Please sign in to comment.