Commit 8ae1b7cb authored by Marek Vrbka's avatar Marek Vrbka
Browse files

Update lcd.py

parent ceaa2362
Loading
Loading
Loading
Loading
+23 −17
Original line number Diff line number Diff line
@@ -12,6 +12,12 @@ class LCDMessage:
    rw: bool
    data: int

@dataclass
class LCDRawPacket:
    rs: bool
    rw: bool
    data: int


class RAMType(Enum):
    DDRAM = 1
@@ -191,29 +197,16 @@ class LCDSpyDevice:
        nibble = d4 | d5 << 1 | d6 << 2 | d7 << 3
        return rs, rw, nibble

    def process_data(self, data: [int]) -> [LCDMessage]:
        messages = []
        while len(data) >= 2:
            high_tx, low_tx, *data = data
            high_rs, high_rw, high_nibble = self.extract(high_tx)
            print(f"RS: {high_rs} | RW: {high_rw} | Data: {high_nibble}")
            low_rs, low_rw, low_nibble = self.extract(low_tx)
            message_data = high_nibble << 4 | low_nibble
            if high_rs != low_rs or high_rw != low_rw:
                # It's possible the first capture is garbage
                print(f"RS: {low_rs}, {high_rs} | RW: {low_rw}, {high_rw} | Data: {message_data}")
                data = [low_tx] + data
                continue
            message = LCDMessage(rs=high_rs, rw=high_rw, data=message_data)
            messages.append(message)
        return messages
    def process_data(self, data: [int]) -> [RawLCDPacket]:
        extract = lambda x: self.extract(x)
        return list(map(extract, data))

    def get_data_raw(self) -> [int]:
        status = self.digilent_device.status(True)
        available, lost, corrupt = self.digilent_device.statusRecord()
        return self.digilent_device.statusData(available)

    def finish_spying(self) -> [LCDMessage]:
    def finish_spying(self) -> [RawLCDPacket]:
        if not self.spying:
            raise errors.DeviceNotSpyingError
        self.spying = False
@@ -222,3 +215,16 @@ class LCDSpyDevice:
    
    def reset(self):
        self.spying = False


def packets_to_messages(packets: [RawLCDPacket]) -> [LCDMessage]:
    messages = []
    while len(packets) > 2:
        high, low, *packets = packets
        if high.rs != low.rs or high.rw != low.rw:
            raise errors.InvalidDataError
        data = high.data << 4 | low.data
        message = LCDMessage(rs=high.rs, rw=high.rw, data)
        messages.append(message)
    return messages