// https://forum.arduino.cc/t/i-need-help-with-my-library/1167276

/* Morse code table in ASCII ordering from ' ' to '_'.
 *
 * The first 1 marks the sequence start, `.` is encoded as 0, `-` as 1.
 *
 * Example: `A` is at position 33, its value (0x06) is 0b110 in binary. The
 * first 1 is discarded, leaving us with 0b10, which is translated to `-.`.
 */
uint8_t const morse[] PROGMEM {
  0x01, 0x75, 0x52, 0x00, 0xc8, 0x00, 0x22, 0x5e, 0x2d, 0x6d,
  0x00, 0x2a, 0x73, 0x61, 0x6a, 0x29, 0x3f, 0x3e, 0x3c, 0x38,
  0x30, 0x20, 0x21, 0x23, 0x27, 0x2f, 0x47, 0x55, 0x00, 0x31,
  0x00, 0x4c, 0x56, 0x06, 0x11, 0x15, 0x09, 0x02, 0x14, 0x0b,
  0x10, 0x04, 0x1e, 0x0d, 0x12, 0x07, 0x05, 0x0f, 0x16, 0x1b,
  0x0a, 0x08, 0x03, 0x0c, 0x18, 0x0e, 0x19, 0x1d, 0x13, 0x00,
  0x00, 0x00, 0x00, 0x6c};


uint8_t sequence(char const letter) {
  if (letter < ' ' or letter > '_') {
    return 0x00;
  }
  return pgm_read_byte(morse + letter - ' ');
}

void encode(void (* const action)(bool const), char const letter) {
  uint8_t const seq {sequence(letter)};

  uint8_t i {0x80};
  for (; i and not (seq & i); i >>= 1);  // Find the first 1.
  while (i >>= 1) {
    action(seq & i);
  }
}


void printMorse(bool const data) {
  Serial.print(data ? '-' : '.');
}


void setup() {
  Serial.begin(9600);

  for (char const& c: "MORSE") {
    encode(printMorse, c);
    Serial.print(' ');
  }
}

void loop() {}