opentag3d.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """OpenTag3D NDEF encoder for NTAG tags.
  2. Encodes spool data as an OpenTag3D NDEF message ready to write to NTAG
  3. starting at page 4 (after the manufacturer pages).
  4. NDEF structure:
  5. [CC: E1 10 12 00] - Capability Container (4 bytes, page 4)
  6. [TLV: 03 len] - NDEF Message TLV (2 bytes)
  7. [NDEF record header] - D2 15 payload_len (3 bytes: MB|ME|SR, TNF=MIME, type_len=21)
  8. [Type: "application/opentag3d"] - 21 bytes
  9. [Payload: OpenTag3D fields] - 102 bytes
  10. [Terminator: FE] - 1 byte
  11. """
  12. import struct
  13. from backend.app.models.spool import Spool
  14. OPENTAG3D_MIME_TYPE = b"application/opentag3d"
  15. PAYLOAD_SIZE = 102
  16. TAG_VERSION = 1000 # v1.000
  17. def _build_payload(spool: Spool) -> bytes:
  18. """Build 102-byte OpenTag3D core payload from spool fields."""
  19. buf = bytearray(PAYLOAD_SIZE)
  20. # 0x00: Tag Version (2 bytes, big-endian)
  21. struct.pack_into(">H", buf, 0x00, TAG_VERSION)
  22. # 0x02: Base Material (5 bytes, UTF-8, space-padded)
  23. material = (spool.material or "")[:5].ljust(5)
  24. buf[0x02:0x07] = material.encode("utf-8")[:5]
  25. # 0x07: Material Modifiers (5 bytes, UTF-8, space-padded)
  26. modifiers = (spool.subtype or "")[:5].ljust(5)
  27. buf[0x07:0x0C] = modifiers.encode("utf-8")[:5]
  28. # 0x0C: Reserved (15 bytes, zero-fill) — already zero
  29. # 0x1B: Manufacturer (16 bytes, UTF-8, space-padded)
  30. brand = (spool.brand or "")[:16].ljust(16)
  31. buf[0x1B:0x2B] = brand.encode("utf-8")[:16]
  32. # 0x2B: Color Name (32 bytes, UTF-8, space-padded)
  33. color_name = (spool.color_name or "")[:32].ljust(32)
  34. buf[0x2B:0x4B] = color_name.encode("utf-8")[:32]
  35. # 0x4B: Color 1 RGBA (4 bytes)
  36. rgba_hex = spool.rgba or "00000000"
  37. try:
  38. rgba_bytes = bytes.fromhex(rgba_hex[:8].ljust(8, "0"))
  39. except ValueError:
  40. rgba_bytes = b"\x00\x00\x00\x00"
  41. buf[0x4B:0x4F] = rgba_bytes[:4]
  42. # 0x4F: Colors 2-4 (12 bytes, zero-fill) — already zero
  43. # 0x5C: Target Diameter (2 bytes, big-endian) — 1750 = 1.75mm
  44. struct.pack_into(">H", buf, 0x5C, 1750)
  45. # 0x5E: Target Weight (2 bytes, big-endian)
  46. struct.pack_into(">H", buf, 0x5E, spool.label_weight or 0)
  47. # 0x60: Print Temp (1 byte) — nozzle_temp_min / 5
  48. buf[0x60] = (spool.nozzle_temp_min or 0) // 5
  49. # 0x61: Bed Temp (1 byte) — not tracked
  50. # 0x62: Density (2 bytes) — not tracked
  51. # 0x64: Transmission Distance (2 bytes) — not tracked
  52. # All zero — already zero
  53. return bytes(buf)
  54. def encode_opentag3d(spool: Spool) -> bytes:
  55. """Encode spool data as OpenTag3D NDEF message (CC + TLV + record + terminator).
  56. Returns raw bytes ready to write to NTAG starting at page 4.
  57. """
  58. payload = _build_payload(spool)
  59. mime_type = OPENTAG3D_MIME_TYPE
  60. # NDEF record: MB|ME|SR (0xD0) | TNF=MIME (0x02) => 0xD2
  61. # Type length = 21
  62. # Payload length = 102 (fits in SR single byte)
  63. record_header = bytes([0xD2, len(mime_type), len(payload)])
  64. ndef_record = record_header + mime_type + payload
  65. # TLV: type=0x03 (NDEF Message), length
  66. ndef_len = len(ndef_record)
  67. if ndef_len < 0xFF:
  68. tlv = bytes([0x03, ndef_len])
  69. else:
  70. tlv = bytes([0x03, 0xFF, (ndef_len >> 8) & 0xFF, ndef_len & 0xFF])
  71. # Capability Container (page 4)
  72. cc = bytes([0xE1, 0x10, 0x12, 0x00])
  73. # Terminator TLV
  74. terminator = bytes([0xFE])
  75. return cc + tlv + ndef_record + terminator