#include "general.h"
#include "morse.h"
typedef struct {
uint16_t code;
uint8_t bits;
} morse_char_s;
static const morse_char_s morse_char_lut[] = {
{0x001dU, 8U}, {0x0157U, 12U}, {0x05d7U, 14U}, {0x0057U, 10U}, {0x0001U, 4U}, {0x0175U, 12U}, {0x0177U, 12U}, {0x0055U, 10U}, {0x0005U, 6U}, {0x1dddU, 16U}, {0x01d7U, 12U}, {0x015dU, 12U}, {0x0077U, 10U}, {0x0017U, 8U}, {0x0777U, 14U}, {0x05ddU, 14U}, {0x1d77U, 16U}, {0x005dU, 10U}, {0x0015U, 8U}, {0x0007U, 6U}, {0x0075U, 10U}, {0x01d5U, 12U}, {0x01ddU, 12U}, {0x0757U, 14U}, {0x1dd7U, 16U}, {0x0577U, 14U}, };
volatile const char *morse_msg = NULL;
static volatile size_t msg_index = SIZE_MAX;
static volatile bool morse_repeat = false;
void morse(const char *const msg, const bool repeat)
{
#if PC_HOSTED == 1
if (msg)
DEBUG_WARN("%s\n", msg);
(void)repeat;
#else
morse_repeat = repeat;
msg_index = msg ? 0 : SIZE_MAX;
morse_msg = msg;
#endif
}
bool morse_update(void)
{
static uint16_t code;
static uint8_t bits;
if (msg_index == SIZE_MAX)
return false;
if (!bits) {
char morse_char = morse_msg[msg_index++];
if (!morse_char) {
if (morse_repeat) {
morse_char = morse_msg[0];
msg_index = 1U;
} else {
msg_index = SIZE_MAX;
return false;
}
}
if (morse_char >= 'A' && morse_char <= 'Z') {
const uint8_t morse_char_index = (uint8_t)morse_char - 'A';
code = morse_char_lut[morse_char_index].code;
bits = morse_char_lut[morse_char_index].bits;
} else {
code = 0U;
bits = 4U;
}
}
const bool result = code & 1U;
code >>= 1U;
--bits;
return result;
}