Morse-code based RPN calculator (#164)

* Added Morse code based RPN calculator

* added manual and memory register

* fixed morsecalc negative indicator, edited header comment

* adjusted stack display controls

* Fixed warnings. Added calculator token aliasing ability. Added binary shorthand for numeral entry. Extended morse code binary tree.

* ui tweaks

* Update movement_config.h

* silence warning

Co-authored-by: Christian Chapman <user@debian>
Co-authored-by: joeycastillo <joeycastillo@utexas.edu>
This commit is contained in:
Christian Chapman
2023-01-20 20:15:28 -05:00
committed by GitHub
parent 7ef2d6541a
commit 027e3bb42e
11 changed files with 1229 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "calc.h"
#include "calc_fns.h"
#define CALC_NAN (0.0/0.0)
/* calc_init
* Initialize calculator
*/
int calc_init(calc_state_t *cs) {
memset(cs->stack, CALC_NAN, N_STACK*sizeof(cs->stack[0]));
cs->s = 0;
cs->mem = 0.0;
return 0;
}
/* calc_input_function
* Try to execut the token as a calculator function
* TODO: Maybe replace this loop with binary search for token in a sorted calc_dict
*/
int calc_input_function(calc_state_t *cs, char *token) {
for(uint8_t idx=0; idx<sizeof(calc_dict)/sizeof(calc_dict[0]); idx++) {
for(uint8_t idxn=0; idxn<sizeof(calc_dict[idx].names)/sizeof(calc_dict[idx].names[0]); idxn++) {
if(0 == strcmp(calc_dict[idx].names[idxn], token)) { // Found a match
return (*calc_dict[idx].fn)(cs); // Run calculator function
}
}
}
return -1; // Unrecognized function name
}
/* calc_input_float
* Read the token as a float.
* For convenience, numerals can be written in binary:
* 0 1 2 3 4 5 6 7 8 9
* . - -. -- -.. -.- --. --- -... -..-
* e t n m d k g o b x
*
* Exponent signs must be entered as "p".
* Decimal place "." can be entered as "h" (code ....)
* Sign "-" can be entered as "Ch digraph" (code ----)
*
* e.g. "4.2e-3" can be entered directly or as "4h2pC3"
* similarly, "0.0042" can be "eheedn"
*/
#define REPCHAR(X,Y) for(idx=0; idx<strlen(token); idx++) \
if(X==token[idx]) token[idx] = Y
int calc_input_float(calc_state_t *cs, char *token) {
uint8_t idx;
REPCHAR('e', '0');
REPCHAR('t', '1');
REPCHAR('n', '2');
REPCHAR('m', '3');
REPCHAR('d', '4');
REPCHAR('k', '5');
REPCHAR('g', '6');
REPCHAR('o', '7');
REPCHAR('b', '8');
REPCHAR('x', '9');
REPCHAR('h', '.');
REPCHAR('C', '-');
REPCHAR('p', 'E');
char *endptr;
double d = calc_strtof(token, &endptr);
if(!endptr || (uint8_t)(endptr-token)<strlen(token)) return -1; // Bad format
if(cs->s >= N_STACK) return -2; // Stack full
cs->stack[cs->s++] = d;
return 0;
}
/* calc_input
* Manipulate the stack using the entered token.
* If the token isn't a calculator function, try to convert it to a number and
* add it to the stack.
*
* Return values:
* 0 if function completed successfully.
* -1 if token isn't a calculator function and couldn't convert to float.
* -2 if stack is too full or too empty
* -3 for something else
*/
int calc_input(calc_state_t *cs, char *token) {
int retval = calc_input_function(cs, token);
if(retval == -1) retval = calc_input_float(cs, token);
return retval;
}

View File

@@ -0,0 +1,44 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef CALC_H_INCLUDED
#define CALC_H_INCLUDED
#include <stdint.h>
#define N_STACK 10
typedef struct {
double stack[N_STACK];
double mem;
uint8_t s; // # of items in stack
} calc_state_t;
int calc_init(calc_state_t *cs);
int calc_input(calc_state_t *cs, char *token);
int calc_input_function(calc_state_t *cs, char *token);
int calc_input_float(calc_state_t *cs, char *token);
double calc_strtof(const char *str, char **endptr);
#endif

View File

@@ -0,0 +1,230 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <math.h>
#include "calc_fns.h"
#define STACK_CHECK_0_IN_1_OUT if(cs->s >= N_STACK) return -2
#define STACK_CHECK_1_IN_0_OUT if(cs->s < 1) return -2
#define STACK_CHECK_1_IN_1_OUT if(cs->s < 1) return -2
#define STACK_CHECK_2_IN_1_OUT if(cs->s < 2) return -2
#define STACK_CHECK_2_IN_2_OUT if(cs->s < 2) return -2
static const double to_rad = M_PI/180;
static const double to_deg = 180/M_PI;
// Stack and memory control
int calc_delete(calc_state_t *cs) {
if(cs->s < 1) return -2; // Check stack
cs->s--;
return 0;
}
int calc_clear_stack(calc_state_t *cs) {
memset(cs->stack, (0.0/0.0), N_STACK*sizeof(cs->stack[0]));
cs->s = 0;
return 0;
}
int calc_flip(calc_state_t *cs) {
STACK_CHECK_2_IN_2_OUT;
double buff = cs->stack[cs->s-2];
cs->stack[cs->s-2] = cs->stack[cs->s-1];
cs->stack[cs->s-1] = buff;
return 0;
}
int calc_mem_clear(calc_state_t *cs) {
cs->mem = 0.0;
return 0;
}
int calc_mem_recall(calc_state_t *cs) {
STACK_CHECK_0_IN_1_OUT;
cs->stack[cs->s++] = cs->mem;
return 0;
}
int calc_mem_add(calc_state_t *cs) {
STACK_CHECK_1_IN_0_OUT;
cs->mem += cs->stack[cs->s-1];
cs->s--;
return 0;
}
int calc_mem_subtract(calc_state_t *cs) {
STACK_CHECK_1_IN_0_OUT;
cs->mem -= cs->stack[cs->s-1];
cs->s--;
return 0;
}
// Basic operations
int calc_add(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] += cs->stack[cs->s-1];
cs->s--;
return 0;
}
int calc_subtract(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] -= cs->stack[cs->s-1];
cs->s--;
return 0;
}
int calc_negate(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = -cs->stack[cs->s-1];
return 0;
}
int calc_multiply(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] *= cs->stack[cs->s-1];
cs->s--;
return 0;
}
int calc_divide(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] /= cs->stack[cs->s-1];
cs->s--;
return 0;
}
int calc_invert(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = 1.0/cs->stack[cs->s-1];
return 0;
}
// Constants
int calc_e(calc_state_t *cs) {
STACK_CHECK_0_IN_1_OUT;
cs->stack[cs->s++] = M_E;
return 0;
}
int calc_pi(calc_state_t *cs) {
STACK_CHECK_0_IN_1_OUT;
cs->stack[cs->s++] = M_PI;
return 0;
}
// Exponential/logarithmic
int calc_exp(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = exp(cs->stack[cs->s-1]);
return 0;
}
int calc_pow(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] = pow(cs->stack[cs->s-2], cs->stack[cs->s-1]);
cs->s--;
return 0;
}
int calc_ln(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = log(cs->stack[cs->s-1]);
return 0;
}
int calc_log(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = log10(cs->stack[cs->s-1]);
return 0;
}
int calc_sqrt(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = sqrt(cs->stack[cs->s-1]);
return 0;
}
// Trigonometric
int calc_sin(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = sin(cs->stack[cs->s-1]);
return 0;
}
int calc_cos(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = cos(cs->stack[cs->s-1]);
return 0;
}
int calc_tan(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = tan(cs->stack[cs->s-1]);
return 0;
}
int calc_asin(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = asin(cs->stack[cs->s-1]);
return 0;
}
int calc_acos(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = acos(cs->stack[cs->s-1]);
return 0;
}
int calc_atan(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = atan(cs->stack[cs->s-1]);
return 0;
}
int calc_atan2(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] = atan2(cs->stack[cs->s-2], cs->stack[cs->s-1]);
cs->s--;
return 0;
}
int calc_sind(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = sin(cs->stack[cs->s-1]*to_rad);
return 0;
}
int calc_cosd(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = cos(cs->stack[cs->s-1]*to_rad);
return 0;
}
int calc_tand(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = tan(cs->stack[cs->s-1]*to_rad);
return 0;
}
int calc_asind(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = asin(cs->stack[cs->s-1])*to_deg;
return 0;
}
int calc_acosd(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = acos(cs->stack[cs->s-1])*to_deg;
return 0;
}
int calc_atand(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = atan(cs->stack[cs->s-1])*to_deg;
return 0;
}
int calc_atan2d(calc_state_t *cs) {
STACK_CHECK_2_IN_1_OUT;
cs->stack[cs->s-2] = atan2(cs->stack[cs->s-2], cs->stack[cs->s-1])*to_deg;
cs->s--;
return 0;
}

View File

@@ -0,0 +1,123 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "calc.h"
// Stack and register control
int calc_delete(calc_state_t *cs);
int calc_clear_stack(calc_state_t *cs);
int calc_flip(calc_state_t *cs);
int calc_mem_clear(calc_state_t *cs);
int calc_mem_recall(calc_state_t *cs);
int calc_mem_add(calc_state_t *cs);
int calc_mem_subtract(calc_state_t *cs);
// Basic operations
int calc_add(calc_state_t *cs);
int calc_subtract(calc_state_t *cs);
int calc_negate(calc_state_t *cs);
int calc_multiply(calc_state_t *cs);
int calc_divide(calc_state_t *cs);
int calc_invert(calc_state_t *cs);
// Constants
int calc_e(calc_state_t *cs);
int calc_pi(calc_state_t *cs);
// Exponential/logarithmic
int calc_exp(calc_state_t *cs);
int calc_pow(calc_state_t *cs);
int calc_ln(calc_state_t *cs);
int calc_log(calc_state_t *cs);
int calc_sqrt(calc_state_t *cs);
// Trigonometric
int calc_sin(calc_state_t *cs);
int calc_cos(calc_state_t *cs);
int calc_tan(calc_state_t *cs);
int calc_asin(calc_state_t *cs);
int calc_acos(calc_state_t *cs);
int calc_atan(calc_state_t *cs);
int calc_atan2(calc_state_t *cs);
int calc_sind(calc_state_t *cs);
int calc_cosd(calc_state_t *cs);
int calc_tand(calc_state_t *cs);
int calc_asind(calc_state_t *cs);
int calc_acosd(calc_state_t *cs);
int calc_atand(calc_state_t *cs);
int calc_atan2d(calc_state_t *cs);
// Dictionary definition
typedef int (*calc_fn_t)(calc_state_t *cs);
typedef struct {
char *names[3]; // Token to use to run this function
calc_fn_t fn; // Pointer to function
} calc_dict_entry_t;
static const calc_dict_entry_t calc_dict[] = {
// Stack and register control
{{"x"}, &calc_delete},
{{"xx"}, &calc_clear_stack},
{{"xxx"}, &calc_init},
{{"f"}, &calc_flip},
{{"mc"}, &calc_mem_clear},
{{"mr"}, &calc_mem_recall},
{{"ma"}, &calc_mem_add},
{{"ms"}, &calc_mem_subtract},
// Basic operations
{{"a"}, &calc_add},
{{"s"}, &calc_subtract},
{{"n"}, &calc_negate},
{{"m"}, &calc_multiply},
{{"d"}, &calc_divide},
{{"i"}, &calc_invert},
// Constants
{{"e"}, &calc_e},
{{"pi"}, &calc_pi},
// Exponential/logarithmic
{{"exp"}, &calc_exp},
{{"pow"}, &calc_pow},
{{"ln"}, &calc_ln},
{{"log"}, &calc_log},
{{"sqrt"}, &calc_sqrt},
// Trigonometric
{{"sin", "sn"}, &calc_sin},
{{"cos"}, &calc_cos},
{{"tan"}, &calc_tan},
{{"asin"}, &calc_asin},
{{"acos"}, &calc_acos},
{{"atan"}, &calc_atan},
{{"atan2"}, &calc_atan2},
{{"sind"}, &calc_sind},
{{"cosd"}, &calc_cosd},
{{"tand"}, &calc_tand},
{{"asind"}, &calc_asind},
{{"acosd"}, &calc_acosd},
{{"atand"}, &calc_atand},
{{"atan2d"}, &calc_atan2d},
};

View File

@@ -0,0 +1,144 @@
//
// strtod.c
//
// Convert string to double
//
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the project nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include "calc.h"
double calc_strtof(const char *str, char **endptr) {
double number;
int exponent;
int negative;
char *p = (char *) str;
double p10;
int n;
int num_digits;
int num_decimals;
// Skip leading whitespace
while (isspace((int) *p)) p++;
// Handle optional sign
negative = 0;
switch (*p) {
case '-': negative = 1; // Fall through to increment position
__attribute__ ((fallthrough));
case '+': p++;
}
number = 0.;
exponent = 0;
num_digits = 0;
num_decimals = 0;
// Process string of digits
while (isdigit((int) *p)) {
number = number * 10. + (*p - '0');
p++;
num_digits++;
}
// Process decimal part
if (*p == '.') {
p++;
while (isdigit((int) *p)) {
number = number * 10. + (*p - '0');
p++;
num_digits++;
num_decimals++;
}
exponent -= num_decimals;
}
if (num_digits == 0) {
if (endptr) *endptr = p;
return 0.0;
}
// Correct for sign
if (negative) number = -number;
// Process an exponent string
if (*p == 'e' || *p == 'E') {
// Handle optional sign
negative = 0;
switch (*++p) {
case '-': negative = 1; // Fall through to increment pos
__attribute__ ((fallthrough));
case '+': p++;
}
// Process string of digits
n = 0;
while (isdigit((int) *p)) {
n = n * 10 + (*p - '0');
p++;
}
if (negative) {
exponent -= n;
} else {
exponent += n;
}
}
if (exponent < DBL_MIN_EXP || exponent > DBL_MAX_EXP) {
return HUGE_VAL;
}
// Scale the result
p10 = 10.;
n = exponent;
if (n < 0) n = -n;
while (n) {
if (n & 1) {
if (exponent < 0) {
number /= p10;
} else {
number *= p10;
}
}
n >>= 1;
p10 *= p10;
}
if (endptr) *endptr = p;
return number;
}

View File

@@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include "mc.h"
/* mc_reset Initialize or reset an MC buffer
* Input: mc = location of buffer to reset
*/
void mc_reset(mc_state_t * mc) {
memset(mc->b, '\0', BUFFLEN*sizeof(mc->b[0]));
mc->bidx = 0;
return;
return;
}
/* mc_input Read an input into a morse code buffer
* Input: mc = buffer to read into
* c = character to read into buffer ('.' or '-', ignored otherwise).
* If the buffer is full, reset it instead of entering the new character.
*/
void mc_input(mc_state_t * mc, char c) {
if(mc->bidx >= BUFFLEN) mc_reset(mc);
else if( ('.' == c) || ('-' == c) ) {
mc->b[mc->bidx] = c;
mc->bidx++;
}
return;
}
/* mc_dec Decode a Morse code character (descend MC_DEC_KEY[])
* Input: b = BUFFLEN-length char array with '.'s and '-'s
* Output: c = Character b represents, or '\0' if not a Morse code.
*/
char mc_dec(char b[BUFFLEN]) {
uint8_t pos = 1; // Binary tree position ('.'=0; '-'=1)
for(uint8_t idx=0; idx<BUFFLEN; idx++) {
if('.' == b[idx]) pos = 2*pos; // Descend in . direction
else if('-' == b[idx]) pos = 2*pos+1; // Descend in - direction
else break; // End of morse code segment; finished descending
}
return MC_DEC_KEY[pos-1];
}

View File

@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2023 Christian Chapman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* mc Morse code reading methods
*/
#include "stdint.h"
#define BUFFLEN 5
typedef struct {
char b[BUFFLEN];
uint8_t bidx;
} mc_state_t;
// MC_DEC_KEY represents a binary tree of International Morse Code.
// where '.' = 0 and '-' = 1. Levels of the tree are concatenated.
//
// Capitals denote special characters:
// C = Ch digraph
// V = VERIFY (ITU-R "UNDERSTOOD")
// R = REPEAT
// W = WAIT
// S = START TRANSMISSION
// E = END OF WORK
static const char MC_DEC_KEY[] = " etianmsurwdkgohvf\0l\0pjbxcyzq\0C\x35\x34V\x33\0R\0\x32W\0+\0\0\0\0\x31\x36=/\0\0S(\0\x37\0\0\0\x38\0\x39\x30\0\0\0\0\0E\0\0\0\0\0\0?_\0\0\0\0\"\0\0.\0\0\0\0@\0\0\0'\0\0-\0\0\0\0\0\0\0\0;!\0)\0\0\0\0\0,\0\0\0\0:\0\0\0\0\0\0\0";
void mc_reset(mc_state_t * mcb);
void mc_input(mc_state_t * mc, char c);
char mc_dec(char b[BUFFLEN]);