81 lines
2.2 KiB
C
81 lines
2.2 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <vcc/ssc.h>
|
|
#include "internal.h"
|
|
|
|
unsigned char ssc_begin_phrase(ssc_phrase *phrase, const char *text)
|
|
{
|
|
unsigned char length = 0;
|
|
if (!phrase || !text)
|
|
return SSC_INVALID;
|
|
while (text[length]) {
|
|
unsigned char c = text[length];
|
|
if (length >= SSC_MAX_TEXT || c < 32 || c > 126)
|
|
return SSC_INVALID;
|
|
++length;
|
|
}
|
|
if (!length)
|
|
return SSC_INVALID;
|
|
phrase->next = text;
|
|
phrase->remaining = length + 2;
|
|
return SSC_OK;
|
|
}
|
|
|
|
unsigned char ssc_service(ssc_phrase *phrase)
|
|
{
|
|
unsigned char value;
|
|
if (!phrase || phrase->remaining > SSC_MAX_TEXT + 2)
|
|
return SSC_INVALID;
|
|
if (!phrase->remaining)
|
|
return SSC_OK;
|
|
if (phrase->remaining > 2) {
|
|
value = *phrase->next;
|
|
if (value >= 'a' && value <= 'z')
|
|
value -= 'a' - 'A';
|
|
} else {
|
|
value = phrase->remaining == 2 ? ' ' : 13;
|
|
}
|
|
if (ssc_try_write(value)) {
|
|
if (phrase->remaining > 2)
|
|
++phrase->next;
|
|
--phrase->remaining;
|
|
}
|
|
return phrase->remaining ? SSC_PENDING : SSC_OK;
|
|
}
|
|
|
|
static unsigned char submit_phrase(ssc_phrase *phrase, unsigned int poll_limit)
|
|
{
|
|
unsigned int budget = poll_limit;
|
|
while (budget) {
|
|
unsigned char before = phrase->remaining;
|
|
if (ssc_service(phrase) == SSC_OK)
|
|
return SSC_OK;
|
|
if (before == phrase->remaining)
|
|
--budget;
|
|
else
|
|
budget = poll_limit;
|
|
}
|
|
return SSC_TIMEOUT;
|
|
}
|
|
|
|
unsigned char ssc_say(const char *text, unsigned int poll_limit)
|
|
{
|
|
ssc_phrase phrase;
|
|
unsigned char result = ssc_begin_phrase(&phrase, text);
|
|
if (result != SSC_OK)
|
|
return result;
|
|
return submit_phrase(&phrase, poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_store_phrase(unsigned char slot, const char *text,
|
|
unsigned int poll_limit)
|
|
{
|
|
ssc_phrase phrase;
|
|
unsigned char command;
|
|
if (slot > 7 || ssc_begin_phrase(&phrase, text) != SSC_OK)
|
|
return SSC_INVALID;
|
|
command = 0x90 + slot;
|
|
if (ssc_write_bytes(&command, 1, poll_limit) != SSC_OK)
|
|
return SSC_TIMEOUT;
|
|
return submit_phrase(&phrase, poll_limit);
|
|
}
|