// TCDSegments.h
// Segment mapping for the Back to the Future time-circuit display rows.
// Decoded from HT16K33/Samples/DTCD.ino - verified against all twelve month glyphs.
//
// Each row is one HT16K33 at 0x70 (red / destination), 0x71 (green / present),
// 0x72 (yellow / last departed). The 16-byte buffer maps straight onto the
// chip's display RAM: byte i drives COM i/2, even bytes SEG0-7, odd SEG8-15.
//
//        --A--
//       |\ | /|
//       F H J K B
//       |  \|/ |
//        G1-- G2
//       |  /|\ |
//       E N M L C
//       |/  | \|
//        --D--
//
// even byte:  bit0=A  bit1=B  bit2=C  bit3=D  bit4=E  bit5=F  bit6=G1  bit7=decimal point
// odd  byte:  bit0=G2 bit1=H  bit2=J  bit3=K  bit4=L  bit5=M  bit6=N   bit7=indicator

#pragma once
#include <stdint.h>

// ---- Buffer offsets -------------------------------------------------------
// Month characters are 14-segment: each occupies a byte PAIR (even = A..G1,
// odd = G2..N). Everything else is a plain 7-segment digit in bits 0-6.

enum : uint8_t {
  TCD_MON1      = 0,   // + 1
  TCD_MON2      = 2,   // + 3
  TCD_MON3      = 4,   // + 5
  TCD_DAY_TENS  = 6,
  TCD_DAY_ONES  = 7,
  TCD_HOUR_TENS = 8,
  TCD_YEAR_THOU = 9,
  TCD_HOUR_ONES = 10,
  TCD_YEAR_HUND = 11,
  TCD_MIN_TENS  = 12,
  TCD_YEAR_TENS = 13,
  TCD_MIN_ONES  = 14,
  TCD_YEAR_ONES = 15,
};

// ---- Indicator lamps ------------------------------------------------------
// All three live in bit 7 of a month character's odd byte.

enum : uint8_t {
  TCD_AM_BYTE    = 1,   // bit 7
  TCD_PM_BYTE    = 3,   // bit 7
  TCD_COLON_BYTE = 5,   // bit 7
};
static const uint8_t TCD_INDICATOR_BIT = 0x80;

// Per the assembly instructions each row carries four discrete LEDs: AM, PM,
// and the two dots of the HH:MM colon. The colon pair shares one driver bit,
// which is why four lamps need only three bits.

// ---- Decimal points -------------------------------------------------------
// All 128 addressable bits drive something: 42 month segments, 70 digit
// segments, 3 indicator bits and 13 decimal points.
//
// Every character has a decimal point in bit 7: the ten 7-segment digits
// (LB4056SL year, LB20562D day/hour/minute) in their single byte, and the
// three LB30541L month characters in their EVEN byte. Confirmed lit on all
// three rows on 2026-10-13 with the 01g-01j segment chase tests. Bit 7 of the
// month characters' even bytes was previously assumed to be spare.
//
// The font helpers below do not set or preserve decimal points: tcdPutChar
// and tcdPutDigit clear bit 7 of the bytes they write. Set them afterwards.

static const uint8_t TCD_DP_BIT = 0x80;   // same bit position as the indicators

// Every slot with a decimal point, in left-to-right display order.
static const uint8_t TCD_DP_SLOTS[13] = {
  TCD_MON1,      TCD_MON2,      TCD_MON3,
  TCD_DAY_TENS,  TCD_DAY_ONES,
  TCD_YEAR_THOU, TCD_YEAR_HUND, TCD_YEAR_TENS, TCD_YEAR_ONES,
  TCD_HOUR_TENS, TCD_HOUR_ONES,
  TCD_MIN_TENS,  TCD_MIN_ONES,
};

// ---- 14-segment font ------------------------------------------------------
// Packed little-endian: low byte -> even buffer slot, high byte -> odd slot.
// Glyphs for the twelve month letters are byte-identical to DTCD.ino, so the
// months render exactly as they do today. The rest complete the alphabet.

static const uint16_t TCD_FONT_ALPHA[26] = {
  0x0177,  // A  from DTCD
  0x250F,  // B  from DTCD
  0x0039,  // C  from DTCD
  0x240F,  // D  from DTCD
  0x0179,  // E  from DTCD
  0x0171,  // F  from DTCD
  0x013D,  // G  from DTCD
  0x0176,  // H
  0x2409,  // I
  0x001E,  // J  from DTCD
  0x1870,  // K
  0x0038,  // L  from DTCD
  0x0A36,  // M  from DTCD
  0x1236,  // N  from DTCD
  0x003F,  // O  from DTCD
  0x0173,  // P  from DTCD
  0x103F,  // Q
  0x1173,  // R  from DTCD
  0x030D,  // S  from DTCD
  0x2401,  // T  from DTCD
  0x003E,  // U  from DTCD
  0x4830,  // V  from DTCD  (see note below)
  0x5036,  // W
  0x5A00,  // X
  0x2A00,  // Y  from DTCD
  0x4809,  // Z
};
static const uint16_t TCD_FONT_SPACE = 0x0000;

// NOTE on 'V': DTCD.ino encodes it as E,F,K,N - left vertical plus the
// top-right and bottom-left diagonals. Preserved verbatim so NOV looks the
// way it does now, but it is the one glyph worth eyeballing on real glass;
// E,F,K (dropping N) is the more usual rendering. Only affects November.

// ---- 7-segment digits -----------------------------------------------------
// Identical to the BCD() table in DTCD.ino.

static const uint8_t TCD_FONT_DIGIT[10] = {
  0x3F, 0x06, 0x5B, 0x4F, 0x66,
  0x6D, 0x7D, 0x07, 0x7F, 0x6F,
};

// ---- Month names ----------------------------------------------------------
// Indexed 1-12. Index 0 is a deliberate blank.
//
// Careful: DTCD.ino does `switch (M % 12)`, which turns December into 0 and
// blanks the field for the entire month - the `case 12` block there is
// unreachable. Index this table directly instead of taking a modulus.

static const char* const TCD_MONTH_NAME[13] = {
  "   ",
  "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
};

// ---- Helpers --------------------------------------------------------------

// Write one 14-segment character into a month slot (TCD_MON1/2/3).
// Preserves bit 7 of the odd byte, which carries AM / PM / colon.
inline void tcdPutChar(uint8_t* buf, uint8_t slot, char c) {
  uint16_t g = TCD_FONT_SPACE;
  if (c >= 'a' && c <= 'z') c -= 32;
  if (c >= 'A' && c <= 'Z') g = TCD_FONT_ALPHA[c - 'A'];
  buf[slot]     = (uint8_t)(g & 0x7F);
  buf[slot + 1] = (uint8_t)((g >> 8) & 0x7F) | (buf[slot + 1] & TCD_INDICATOR_BIT);
}

// Write a three-character label into the month field: "OCT", "ERR", "AP".
// Strings shorter than three characters are padded with blanks.
inline void tcdPutLabel(uint8_t* buf, const char* s) {
  static const uint8_t slots[3] = { TCD_MON1, TCD_MON2, TCD_MON3 };
  bool ended = false;
  for (uint8_t i = 0; i < 3; i++) {
    if (!ended && s[i] == 0) ended = true;
    tcdPutChar(buf, slots[i], ended ? ' ' : s[i]);
  }
}

// Write a 7-segment digit. Pass a value outside 0-9 to blank the slot.
inline void tcdPutDigit(uint8_t* buf, uint8_t slot, int v) {
  buf[slot] = (v >= 0 && v <= 9) ? TCD_FONT_DIGIT[v] : 0x00;
}

inline void tcdSetIndicator(uint8_t* buf, uint8_t byteIdx, bool on) {
  if (on) buf[byteIdx] |=  TCD_INDICATOR_BIT;
  else    buf[byteIdx] &= ~TCD_INDICATOR_BIT;
}
