// 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() {}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15