From 272cfd47e32180b7474391c8a3a67c03f2a5d0af Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Sat, 9 Mar 2024 19:59:37 +0000 Subject: [PATCH] feat: initial commit --- .envrc | 1 + .gitignore | 1 + big-sign.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++ flake.nix | 24 ++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 big-sign.py create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b42106 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.direnv/ diff --git a/big-sign.py b/big-sign.py new file mode 100644 index 0000000..60be555 --- /dev/null +++ b/big-sign.py @@ -0,0 +1,80 @@ +import serial +import sys +import time + +PACKET_HEADER = b'\0\0\0\0\0\x01' +SIGN_TYPE = b'Z' +SIGN_ADDRESS = b'00' +VALID_NAMES = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + +def encode_packet(command_code, data): + return PACKET_HEADER + SIGN_TYPE + SIGN_ADDRESS + b'\x02' + command_code + data + b'\x04' + +def generate_memory_config(files: list[tuple[bytes, int]]): + data = b'$' + for name, size in files: + if len(name) != 1: + raise ValueError() + if name not in VALID_NAMES: + raise ValueError() + data += name + data += b'AU' + data += hex(size).upper()[2:].encode().rjust(4, b'0') +# data += b'00FF' + data += b'FFFF' + return encode_packet(b'E', data) + +def generate_run_order(files: list[bytes]): + data = b'.TU' + for name in files: + if len(name) != 1: + raise ValueError() + if name not in VALID_NAMES: + raise ValueError() + data += name + return encode_packet(b'E', data) + +def generate_write_text_file(name: bytes, content: bytes): + return encode_packet(b'A', name + content) + +def main(args: list[str]): + if len(args) < 3: + print(f'Usage: {args[0]} ') + exit(1) + file_name = args[1] + serial_port = args[2] + with open(file_name) as f: + lines = f.readlines() + if len(lines) > len(VALID_NAMES): + print('Too many lines! I dont have enough files!') + exit(1) + + run_order = [] + files = [] + text_packets = [] + + for file_name, line in zip(VALID_NAMES, lines): + file_name = bytes([file_name]) + data = line.strip().encode() + length = len(data) + run_order.append(file_name) + files.append((file_name, length)) + text_packets.append(generate_write_text_file(file_name, data)) + + with serial.Serial(serial_port, 9600) as ser: + print('Config memory') + ser.write(generate_memory_config(files)) + print('+', generate_memory_config(files).hex()) + time.sleep(1) + print('Send files') + for tp in text_packets: + ser.write(tp) + print('+', tp.hex()) + time.sleep(1) + print('Send run order') + ser.write(generate_run_order(run_order)) + print('+', generate_run_order(run_order).hex()) + print('Done!') + +if __name__ == '__main__': + main(sys.argv) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..af251d3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1696261572, + "narHash": "sha256-s8TtSYJ1LBpuITXjbPLUPyxzAKw35LhETcajJjCS5f0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0c7ffbc66e6d78c50c38e717ec91a2a14e0622fb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3f6c966 --- /dev/null +++ b/flake.nix @@ -0,0 +1,24 @@ +{ + inputs = { + nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable; + flake-utils.url = github:numtide/flake-utils; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + dependencies= ps: with ps; [ + pyserial + ]; + in + { + devShells.default = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + (python311.withPackages dependencies) + ]; + }; + }); +}