Morsecalc refactor (#229)

* 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

* Reorganized codebase and simplified morse code reading routines.

* added 'quit if submission is empty' behavior

* reverted rules.mk change for merge into main

* corrected timeout behavior

* consolidated morsecode lib into one file; deleted old mc.c mc.h

* consolidated morsecode lib into one file; deleted old mc.c mc.h

* removed specious null in morsecode bintree string

---------

Co-authored-by: Christian Chapman <user@debian>
Co-authored-by: joeycastillo <joeycastillo@utexas.edu>
This commit is contained in:
Christian Chapman 2023-04-16 10:01:03 -04:00 committed by GitHub
parent 16a96d16b2
commit 0a836cecea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 360 additions and 282 deletions

View file

@ -24,6 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "calc.h" #include "calc.h"
#include "calc_fns.h" #include "calc_fns.h"
@ -41,12 +42,11 @@ int calc_init(calc_state_t *cs) {
} }
/* calc_input_function /* calc_input_function
* Try to execut the token as a calculator function * Try to execute 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) { 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 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++) { for(uint8_t idxn=0; idxn<calc_dict[idx].n_names; idxn++) {
if(0 == strcmp(calc_dict[idx].names[idxn], token)) { // Found a match if(0 == strcmp(calc_dict[idx].names[idxn], token)) { // Found a match
return (*calc_dict[idx].fn)(cs); // Run calculator function return (*calc_dict[idx].fn)(cs); // Run calculator function
} }
@ -108,6 +108,6 @@ int calc_input_float(calc_state_t *cs, char *token) {
*/ */
int calc_input(calc_state_t *cs, char *token) { int calc_input(calc_state_t *cs, char *token) {
int retval = calc_input_function(cs, token); int retval = calc_input_function(cs, token);
if(retval == -1) retval = calc_input_float(cs, token); if(-1 == retval) retval = calc_input_float(cs, token);
return retval; return retval;
} }

View file

@ -227,4 +227,14 @@ int calc_atan2d(calc_state_t *cs) {
cs->s--; cs->s--;
return 0; return 0;
} }
int calc_torad(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = cs->stack[cs->s-1]*to_rad;
return 0;
}
int calc_todeg(calc_state_t *cs) {
STACK_CHECK_1_IN_1_OUT;
cs->stack[cs->s-1] = cs->stack[cs->s-1]*to_deg;
return 0;
}

View file

@ -67,57 +67,62 @@ int calc_asind(calc_state_t *cs);
int calc_acosd(calc_state_t *cs); int calc_acosd(calc_state_t *cs);
int calc_atand(calc_state_t *cs); int calc_atand(calc_state_t *cs);
int calc_atan2d(calc_state_t *cs); int calc_atan2d(calc_state_t *cs);
int calc_torad(calc_state_t *cs);
int calc_todeg(calc_state_t *cs);
// Dictionary definition // Dictionary definition
typedef int (*calc_fn_t)(calc_state_t *cs); typedef int (*calc_fn_t)(calc_state_t *cs);
typedef struct { typedef struct {
char *names[3]; // Token to use to run this function uint8_t n_names; // Number of aliases
const char ** names; // Token to use to run this function
calc_fn_t fn; // Pointer to function calc_fn_t fn; // Pointer to function
} calc_dict_entry_t; } calc_dict_entry_t;
static const calc_dict_entry_t calc_dict[] = { static const calc_dict_entry_t calc_dict[] = {
// Stack and register control // Stack and register control
{{"x"}, &calc_delete}, {1, (const char*[]){"x"}, &calc_delete},
{{"xx"}, &calc_clear_stack}, {1, (const char*[]){"xx"}, &calc_clear_stack},
{{"xxx"}, &calc_init}, {1, (const char*[]){"xxx"}, &calc_init},
{{"f"}, &calc_flip}, {1, (const char*[]){"f"}, &calc_flip},
{{"mc"}, &calc_mem_clear}, {1, (const char*[]){"mc"}, &calc_mem_clear},
{{"mr"}, &calc_mem_recall}, {1, (const char*[]){"mr"}, &calc_mem_recall},
{{"ma"}, &calc_mem_add}, {1, (const char*[]){"ma"}, &calc_mem_add},
{{"ms"}, &calc_mem_subtract}, {1, (const char*[]){"ms"}, &calc_mem_subtract},
// Basic operations // Basic operations
{{"a"}, &calc_add}, {1, (const char*[]){"a"}, &calc_add},
{{"s"}, &calc_subtract}, {1, (const char*[]){"s"}, &calc_subtract},
{{"n"}, &calc_negate}, {1, (const char*[]){"n"}, &calc_negate},
{{"m"}, &calc_multiply}, {1, (const char*[]){"m"}, &calc_multiply},
{{"d"}, &calc_divide}, {1, (const char*[]){"d"}, &calc_divide},
{{"i"}, &calc_invert}, {1, (const char*[]){"i"}, &calc_invert},
// Constants // Constants
{{"e"}, &calc_e}, {1, (const char*[]){"e"}, &calc_e},
{{"pi"}, &calc_pi}, {1, (const char*[]){"pi"}, &calc_pi},
// Exponential/logarithmic // Exponential/logarithmic
{{"exp"}, &calc_exp}, {1, (const char*[]){"exp"}, &calc_exp},
{{"pow"}, &calc_pow}, {1, (const char*[]){"pow"}, &calc_pow},
{{"ln"}, &calc_ln}, {1, (const char*[]){"ln"}, &calc_ln},
{{"log"}, &calc_log}, {1, (const char*[]){"log"}, &calc_log},
{{"sqrt"}, &calc_sqrt}, {1, (const char*[]){"sqrt"}, &calc_sqrt},
// Trigonometric // Trigonometric
{{"sin", "sn"}, &calc_sin}, {2, (const char*[]){"sin", "sn"}, &calc_sin},
{{"cos"}, &calc_cos}, {1, (const char*[]){"cos"}, &calc_cos},
{{"tan"}, &calc_tan}, {1, (const char*[]){"tan"}, &calc_tan},
{{"asin"}, &calc_asin}, {1, (const char*[]){"asin"}, &calc_asin},
{{"acos"}, &calc_acos}, {1, (const char*[]){"acos"}, &calc_acos},
{{"atan"}, &calc_atan}, {1, (const char*[]){"atan"}, &calc_atan},
{{"atan2"}, &calc_atan2}, {1, (const char*[]){"atan2"}, &calc_atan2},
{{"sind"}, &calc_sind}, {1, (const char*[]){"sind"}, &calc_sind},
{{"cosd"}, &calc_cosd}, {1, (const char*[]){"cosd"}, &calc_cosd},
{{"tand"}, &calc_tand}, {1, (const char*[]){"tand"}, &calc_tand},
{{"asind"}, &calc_asind}, {1, (const char*[]){"asind"}, &calc_asind},
{{"acosd"}, &calc_acosd}, {1, (const char*[]){"acosd"}, &calc_acosd},
{{"atand"}, &calc_atand}, {1, (const char*[]){"atand"}, &calc_atand},
{{"atan2d"}, &calc_atan2d}, {1, (const char*[]){"atan2d"}, &calc_atan2d},
{1, (const char*[]){"tor"}, &calc_torad},
{1, (const char*[]){"tod"}, &calc_todeg},
}; };

View file

@ -0,0 +1,147 @@
/*
* 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 "watch_private_display.h"
#include "morsecalc_display.h"
// Display float on screen
void morsecalc_display_float(double d) {
// Special cases
if(d == 0) {
watch_display_string(" 0", 4);
return;
}
else if(isnan(d)) {
watch_display_string(" nan", 4);
return;
}
else if(d == (1.0)/(0.0)) {
watch_display_string(" inf", 4);
return;
}
else if(d == (-1.0)/(0.0)) {
watch_display_character('X', 1);
watch_display_string(" inf", 4);
return;
}
// Record number properties
// Sign
int is_negative = d<0;
if(is_negative) d = -d;
// Order of magnitude
int om = (int) floor(log(d)/log(10));
int om_is_negative = (om<0);
// Get the first 4 significant figures
int digits;
digits = round(d*pow(10.0, 3-om));
if(digits>9999) {
digits = 1000;
om++;
}
// Print signs
if(is_negative) {
// Xi; see https://joeycastillo.github.io/Sensor-Watch-Documentation/segmap
watch_set_pixel(0,11);
watch_set_pixel(2,12);
watch_set_pixel(2,11);
}
else watch_display_character(' ', 1);
if(om_is_negative) watch_set_pixel(1,9);
else watch_display_character(' ', 2);
// Print first 4 significant figures
watch_display_character('0'+(digits/1000)%10, 4);
watch_display_character('0'+(digits/100 )%10, 5);
watch_display_character('0'+(digits/10 )%10, 6);
watch_display_character('0'+(digits/1 )%10, 7);
// Prinat exponent
if(om_is_negative) om = -om; // Make exponent positive for display
if(om<=99) {
watch_display_character('0'+(om/10 )%10, 8);
watch_display_character('0'+(om/1 )%10, 9);
} else { // Over/underflow
if(om_is_negative) watch_display_string(" uf", 4);
else watch_display_string(" of", 4);
if(om<9999) { // Use main display to show order of magnitude
// (Should always succeed; max double is <2e308)
watch_display_character('0'+(om/1000)%10, 4);
watch_display_character('0'+(om/100 )%10, 5);
watch_display_character('0'+(om/10 )%10, 6);
watch_display_character('0'+(om/1 )%10, 7);
}
}
return;
}
// Print current input token
void morsecalc_display_token(morsecalc_state_t *mcs) {
watch_display_string(" ", 0); // Clear display
// Print morse code buffer
char c = MORSECODE_TREE[mcs->mc]; // Decode the morse code buffer's current contents
if('\0' == c) c = ' '; // Needed for watch_display_character
watch_display_character(c, 0); // Display current morse code char in mode position
unsigned int v = mcs->mc+1;
char bidx = 0; while (v >>= 1) bidx++;
watch_display_character('0'+bidx, 3); // Display buffer position in top right
// Print last 6 chars of current input line
uint8_t nlen = strlen(mcs->token); // number of characters in token
uint8_t nprint = min(nlen,6); // number of characters to print
watch_display_string(mcs->token+nlen-nprint, 10-nprint); // print right-aligned
return;
}
// Print stack or memory register contents.
void morsecalc_display_stack(morsecalc_state_t * mcs) {
watch_display_string(" ", 0); // Clear display
char c = MORSECODE_TREE[mcs->mc];
if('m' == c) { // Display memory
morsecalc_display_float(mcs->cs->mem);
watch_display_character(c, 0);
}
else {
// If the morse code buffer has a numeral in it, print that stack item
// Otherwise print top of stack
uint8_t idx = 0;
if(c >= '0' && c <= '9') idx = c - '0';
if(idx >= mcs->cs->s) watch_display_string(" empty", 4); // Stack empty
else morsecalc_display_float(mcs->cs->stack[mcs->cs->s-1-idx]); // Print stack item
watch_display_character('0'+idx, 0); // Print which stack item this is top center
}
watch_display_character('0'+(mcs->cs->s), 3); // Print the # of stack items top right
return;
}

View file

@ -22,30 +22,14 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include "morsecalc_face.h"
/* mc Morse code reading methods // Display float on screen
*/ void morsecalc_display_float(double d);
#include "stdint.h"
#define BUFFLEN 5 // Print current input token
typedef struct { void morsecalc_display_token(morsecalc_state_t *mcs);
char b[BUFFLEN];
uint8_t bidx;
} mc_state_t;
// MC_DEC_KEY represents a binary tree of International Morse Code. // Print stack or memory register contents.
// where '.' = 0 and '-' = 1. Levels of the tree are concatenated. void morsecalc_display_stack(morsecalc_state_t *mcs);
//
// 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]);

View file

@ -22,45 +22,33 @@
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef MORSECODE_
#define MORSECODE_
#include <string.h> /*
#include "mc.h" * MC International Morse Code binary tree
* Levels of the tree are concatenated.
/* mc_reset Initialize or reset an MC buffer * '.' = 0 and '-' = 1.
* Input: mc = location of buffer to reset *
* Capitals denote special characters:
* C = Ch digraph
* V = VERIFY (ITU-R "UNDERSTOOD")
* R = REPEAT
* W = WAIT
* S = START TRANSMISSION
* E = END OF WORK
*/ */
void mc_reset(mc_state_t * mc) { static const char MORSECODE_TREE[] = " 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";
memset(mc->b, '\0', BUFFLEN*sizeof(mc->b[0]));
mc->bidx = 0;
return;
return;
}
/* mc_input Read an input into a morse code buffer /* mc_input Read an input into a morse code buffer
* Input: mc = buffer to read into * Input: mc = index of MORSECODE_TREE[]
* c = character to read into buffer ('.' or '-', ignored otherwise). * len = max morse code char length
* in = character to read into buffer (0='.', 1='-', ignored otherwise).
* If the buffer is full, reset it instead of entering the new character. * If the buffer is full, reset it instead of entering the new character.
*/ */
void mc_input(mc_state_t * mc, char c) { static void morsecode_input(unsigned int *mc, unsigned int len, char in) {
if(mc->bidx >= BUFFLEN) mc_reset(mc); if(*mc >= (unsigned int) ((1<<len)-1)) *mc = 0;
else if( ('.' == c) || ('-' == c) ) { else if((in == 0) | (in == 1)) *mc = (*mc)*2+in+1;
mc->b[mc->bidx] = c;
mc->bidx++;
}
return; return;
} }
#endif
/* 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,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.
*/
// Computer console interface to calc and morsecode for testing without involving watch stuff.
// cc calc_strtof.c calc.c calc_fns.c test_morsecalc.c -lm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "calc.h"
#include "calc_fns.h"
int main(void) {
calc_state_t cs;
calc_init(&cs);
char * word = malloc(0);
unsigned int nword = 0;
char c;
int retval = 0;
for(unsigned int ii = 0; ii < 100; ii++) {
c = getchar();
word = realloc(word, (++nword)*sizeof(char));
word[nword-1] = c;
if((nword > 0) && isspace(c)) { // Word is finished
word[nword-1] = '\0';
retval = calc_input(&cs, word); // Submit word
word = realloc(word, 0); nword = 0; // Clear word
switch(retval) {
case 0: printf("Success.\n"); break;
case -1: printf("Bad command.\n"); break;
case -2: printf("Stack over/underflow.\n"); break;
case -3: printf("Error.\n"); break;
}
if(cs.s > 0) printf("[%i]: %.4f\n", cs.s, cs.stack[cs.s-1]);
else printf("[%i]\n", cs.s);
}
}
free(word);
return 0;
}

View file

@ -41,10 +41,11 @@ SRCS += \
../lib/sunriset/sunriset.c \ ../lib/sunriset/sunriset.c \
../lib/vsop87/vsop87a_milli.c \ ../lib/vsop87/vsop87a_milli.c \
../lib/astrolib/astrolib.c \ ../lib/astrolib/astrolib.c \
../lib/morsecalc/morsecode.c \
../lib/morsecalc/calc.c \ ../lib/morsecalc/calc.c \
../lib/morsecalc/calc_fns.c \ ../lib/morsecalc/calc_fns.c \
../lib/morsecalc/calc_strtof.c \ ../lib/morsecalc/calc_strtof.c \
../lib/morsecalc/mc.c \ ../lib/morsecalc/morsecalc_display.c \
../../littlefs/lfs.c \ ../../littlefs/lfs.c \
../../littlefs/lfs_util.c \ ../../littlefs/lfs_util.c \
../movement.c \ ../movement.c \

View file

@ -26,7 +26,6 @@
## Morse-code-based RPN calculator ## Morse-code-based RPN calculator
The calculator is operated by first composing a **token** in Morse code, then submitting it to the calculator. A token specifies either a calculator operation or a float value. The calculator is operated by first composing a **token** in Morse code, then submitting it to the calculator. A token specifies either a calculator operation or a float value.
These two parts of the codebase are totally independent: These two parts of the codebase are totally independent:
1. The Morse-code reader (`mc.h`, `mc.c`) 1. The Morse-code reader (`mc.h`, `mc.c`)
@ -39,7 +38,7 @@ The user interface (`morsecalc_face.h`, `morsecalc_face.c`) lets you talk to the
- `light` is dash - `light` is dash
- `alarm` is dot - `alarm` is dot
- `mode` is "finish character" - `mode` is "finish character"
- long-press `mode` to quit - long-press `mode` or submit a blank token to switch faces
- long-press `alarm` to show stack - long-press `alarm` to show stack
- long-press `light` to toggle the light - long-press `light` to toggle the light
@ -48,7 +47,6 @@ As you enter `.`s and `-`s, the morse code char you've entered will appear in th
At the top right is the # of morse code `.`/`-` you've input so far. The character resets at the 6th `.`/`-`. At the top right is the # of morse code `.`/`-` you've input so far. The character resets at the 6th `.`/`-`.
Once you have the character you want to enter, push `mode` to enter it. Once you have the character you want to enter, push `mode` to enter it.
The character will be appended to the current token, whose 6 trailing chars are shown on the main display. The character will be appended to the current token, whose 6 trailing chars are shown on the main display.
Once you've typed in the token you want, enter a blank Morse code character and then push `mode`. Once you've typed in the token you want, enter a blank Morse code character and then push `mode`.
This submits it to the calculator. This submits it to the calculator.
@ -77,7 +75,6 @@ This can get long, so for convenience numerals can also be written in binary wit
For example: "4.2e-3" can be entered directly, or as "4h2pC3" For example: "4.2e-3" can be entered directly, or as "4h2pC3"
similarly, "0.0042" can also be entered as "eheedn" similarly, "0.0042" can also be entered as "eheedn"
Once you submit a number to the watch face, it pushes it to the top of the stack if there's room. Once you submit a number to the watch face, it pushes it to the top of the stack if there's room.
## Number display ## Number display
@ -97,7 +94,6 @@ So for example, the watch face might look like this:
... representing `+4.200e-3` is in stack location 0 (the top) and it's one of five items in the stack. ... representing `+4.200e-3` is in stack location 0 (the top) and it's one of five items in the stack.
## Looking at the stack ## Looking at the stack
To show the top of the stack, push and hold `light`/`alarm` or submit a blank token by pushing `mode` a bunch of times. To show the top of the stack, push and hold `light`/`alarm` or submit a blank token by pushing `mode` a bunch of times.
To show the N-th stack item (0 through 9): To show the N-th stack item (0 through 9):
@ -113,101 +109,12 @@ To see all the calculator operations and their token aliases, see the `calc_dict
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "morsecalc_face.h"
#include "watch.h" #include "watch.h"
#include "watch_utility.h" #include "watch_utility.h"
#include "watch_private_display.h" #include "watch_private_display.h"
// Display float on screen #include "morsecalc_face.h"
void morsecalc_print_float(double d) { #include "morsecalc_display.h"
// Special cases
if(d == 0) {
watch_display_string(" 0", 4);
return;
}
else if(isnan(d)) {
watch_display_string(" nan", 4);
return;
}
else if(d == (1.0)/(0.0)) {
watch_display_string(" inf", 4);
return;
}
else if(d == (-1.0)/(0.0)) {
watch_display_character('X', 1);
watch_display_string(" inf", 4);
return;
}
// Record number properties
// Sign
int is_negative = d<0;
if(is_negative) d = -d;
// Order of magnitude
int om = (int) floor(log(d)/log(10));
int om_is_negative = (om<0);
// Get the first 4 significant figures
int digits;
digits = round(d*pow(10.0, 3-om));
if(digits>9999) {
digits = 1000;
om++;
}
// Print signs
if(is_negative) {
// Xi; see https://joeycastillo.github.io/Sensor-Watch-Documentation/segmap
watch_set_pixel(0,11);
watch_set_pixel(2,12);
watch_set_pixel(2,11);
}
else watch_display_character(' ', 1);
if(om_is_negative) watch_set_pixel(1,9);
else watch_display_character(' ', 2);
// Print first 4 significant figures
watch_display_character('0'+(digits/1000)%10, 4);
watch_display_character('0'+(digits/100 )%10, 5);
watch_display_character('0'+(digits/10 )%10, 6);
watch_display_character('0'+(digits/1 )%10, 7);
// Prinat exponent
if(om_is_negative) om = -om; // Make exponent positive for display
if(om<=99) {
watch_display_character('0'+(om/10 )%10, 8);
watch_display_character('0'+(om/1 )%10, 9);
} else { // Over/underflow
if(om_is_negative) watch_display_string(" uf", 4);
else watch_display_string(" of", 4);
if(om<9999) { // Use main display to show order of magnitude
// (Should always succeed; max double is <2e308)
watch_display_character('0'+(om/1000)%10, 4);
watch_display_character('0'+(om/100 )%10, 5);
watch_display_character('0'+(om/10 )%10, 6);
watch_display_character('0'+(om/1 )%10, 7);
}
}
return;
}
// Print current input token
void morsecalc_print_token(morsecalc_state_t *mcs) {
watch_display_string(" ", 0); // Clear display
// Print morse code buffer
char c = mc_dec(mcs->mc->b); // Decode the morse code buffer's current contents
if('\0' == c) c = ' '; // Needed for watch_display_character
watch_display_character(c, 0); // Display current morse code char in mode position
watch_display_character('0'+(mcs->mc->bidx), 3); // Display buffer position in top right
// Print last 6 chars of current input line
uint8_t nlen = strlen(mcs->token); // number of characters in token
uint8_t nprint = min(nlen,6); // number of characters to print
watch_display_string(mcs->token+nlen-nprint, 10-nprint); // print right-aligned
return;
}
// Clear token buffer // Clear token buffer
void morsecalc_reset_token(morsecalc_state_t *mcs) { void morsecalc_reset_token(morsecalc_state_t *mcs) {
@ -216,51 +123,23 @@ void morsecalc_reset_token(morsecalc_state_t *mcs) {
return; return;
} }
// Print stack or memory register contents. // Write a completed morse code character to the calculator
void morsecalc_print_stack(morsecalc_state_t * mcs) { void morsecalc_input(morsecalc_state_t * mcs) {
watch_display_string(" ", 0); // Clear display
char c = mc_dec(mcs->mc->b);
if('m' == c) { // Display memory
morsecalc_print_float(mcs->cs->mem);
watch_display_character(c, 0);
}
else {
// If the morse code buffer has a numeral in it, print that stack item
// Otherwise print top of stack
uint8_t idx = 0;
if(c >= '0' && c <= '9') idx = c - '0';
if(idx >= mcs->cs->s) watch_display_string(" empty", 4); // Stack empty
else morsecalc_print_float(mcs->cs->stack[mcs->cs->s-1-idx]); // Print stack item
watch_display_character('0'+idx, 0); // Print which stack item this is top center
}
watch_display_character('0'+(mcs->cs->s), 3); // Print the # of stack items top right
return;
}
// Write something into the morse code buffer.
// Input: c = dot (0), dash (1), or 'complete' ('x')
void morsecalc_input(morsecalc_state_t * mcs, char c) {
int status = 0; int status = 0;
if( c != 'x' ) { // Dot or dash received char dec = MORSECODE_TREE[mcs->mc];
mc_input(mcs->mc, c); mcs->mc = 0;
morsecalc_print_token(mcs);
}
else { // Morse code character finished
char dec = mc_dec(mcs->mc->b);
mc_reset(mcs->mc);
switch(dec) { switch(dec) {
case '\0': // Invalid character, do nothing case '\0': // Invalid character, do nothing
morsecalc_print_token(mcs); morsecalc_display_token(mcs);
break; break;
case ' ': // Submit token to calculator case ' ': // Submit token to calculator
if(strlen(mcs->token) > 0) { if(mcs->idxt > 0) {
mcs->token[mcs->idxt] = '\0';
status = calc_input(mcs->cs, mcs->token); status = calc_input(mcs->cs, mcs->token);
morsecalc_reset_token(mcs); morsecalc_reset_token(mcs);
} }
morsecalc_print_stack(mcs); morsecalc_display_stack(mcs);
break; break;
case '(': // -.--. Erase previous character in token case '(': // -.--. Erase previous character in token
@ -268,24 +147,23 @@ void morsecalc_input(morsecalc_state_t * mcs, char c) {
mcs->idxt--; mcs->idxt--;
mcs->token[mcs->idxt] = '\0'; mcs->token[mcs->idxt] = '\0';
} }
morsecalc_print_token(mcs); morsecalc_display_token(mcs);
break; break;
case 'S': // -.-.- Erase entire token without submitting case 'S': // -.-.- Erase entire token without submitting
morsecalc_reset_token(mcs); morsecalc_reset_token(mcs);
morsecalc_print_stack(mcs); morsecalc_display_stack(mcs);
break; break;
default: // Add character to token default: // Add character to token
if(mcs->idxt < MORSECALC_TOKEN_LEN-1) { if(mcs->idxt < MORSECALC_TOKEN_LEN-1) {
mcs->token[mcs->idxt] = dec; mcs->token[mcs->idxt] = dec;
mcs->idxt++; mcs->idxt = min(mcs->idxt+1, MORSECALC_TOKEN_LEN);
morsecalc_print_token(mcs); morsecalc_display_token(mcs);
} }
else watch_display_string(" full", 4); else watch_display_string(" full", 4);
break; break;
} }
}
// Print errors if there are any // Print errors if there are any
switch(status) { switch(status) {
@ -308,10 +186,7 @@ void morsecalc_face_setup(movement_settings_t *settings, uint8_t watch_face_inde
mcs->cs = (calc_state_t *) malloc(sizeof(calc_state_t)); mcs->cs = (calc_state_t *) malloc(sizeof(calc_state_t));
calc_init(mcs->cs); calc_init(mcs->cs);
mcs->mc = 0;
mcs->mc = (mc_state_t *) malloc(sizeof(mc_state_t));
mc_reset(mcs->mc);
mcs->led_is_on = 0; mcs->led_is_on = 0;
} }
return; return;
@ -320,8 +195,8 @@ void morsecalc_face_setup(movement_settings_t *settings, uint8_t watch_face_inde
void morsecalc_face_activate(movement_settings_t *settings, void *context) { void morsecalc_face_activate(movement_settings_t *settings, void *context) {
(void) settings; (void) settings;
morsecalc_state_t *mcs = (morsecalc_state_t *) context; morsecalc_state_t *mcs = (morsecalc_state_t *) context;
mc_reset(mcs->mc); mcs->mc = 0;
morsecalc_print_stack(mcs); morsecalc_display_stack(mcs);
return; return;
} }
@ -331,21 +206,24 @@ bool morsecalc_face_loop(movement_event_t event, movement_settings_t *settings,
// input // input
case EVENT_ALARM_BUTTON_UP: case EVENT_ALARM_BUTTON_UP:
// dot // dot
morsecalc_input(mcs, '.'); morsecode_input(&mcs->mc, MORSECODE_LEN, 0);
morsecalc_display_token(mcs);
break; break;
case EVENT_LIGHT_BUTTON_UP: case EVENT_LIGHT_BUTTON_UP:
// dash // dash
morsecalc_input(mcs, '-'); morsecode_input(&mcs->mc, MORSECODE_LEN, 1);
morsecalc_display_token(mcs);
break; break;
case EVENT_MODE_BUTTON_UP: case EVENT_MODE_BUTTON_UP:
// submit character // submit character (or quit)
morsecalc_input(mcs, 'x'); if(mcs->mc || mcs->idxt) morsecalc_input(mcs);
else movement_move_to_next_face();
break; break;
// show stack // show stack
case EVENT_ALARM_LONG_PRESS: case EVENT_ALARM_LONG_PRESS:
morsecalc_print_stack(mcs); morsecalc_display_stack(mcs);
mc_reset(mcs->mc); mcs->mc = 0;
break; break;
// toggle light // toggle light
@ -364,7 +242,7 @@ bool morsecalc_face_loop(movement_event_t event, movement_settings_t *settings,
// quit // quit
case EVENT_TIMEOUT: case EVENT_TIMEOUT:
movement_move_to_next_face(); movement_move_to_face(0);
break; break;
case EVENT_MODE_LONG_PRESS: case EVENT_MODE_LONG_PRESS:
movement_move_to_next_face(); movement_move_to_next_face();

View file

@ -24,11 +24,13 @@
#ifndef MORSECALC_FACE_H_ #ifndef MORSECALC_FACE_H_
#define MORSECALC_FACE_H_ #define MORSECALC_FACE_H_
#define MORSECALC_TOKEN_LEN 9
#define MORSECALC_TOKEN_LEN 32
#define MORSECODE_LEN 5
#include "movement.h" #include "movement.h"
#include "calc.h" #include "calc.h"
#include "mc.h" #include "morsecode.c"
void morsecalc_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); void morsecalc_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
void morsecalc_face_activate(movement_settings_t *settings, void *context); void morsecalc_face_activate(movement_settings_t *settings, void *context);
@ -37,17 +39,14 @@ void morsecalc_face_resign(movement_settings_t *settings, void *context);
typedef struct { typedef struct {
calc_state_t *cs; calc_state_t *cs;
mc_state_t *mc; unsigned int mc; // Morse code character
char token[MORSECALC_TOKEN_LEN]; char token[MORSECALC_TOKEN_LEN];
uint8_t idxt; uint8_t idxt;
uint8_t led_is_on; uint8_t led_is_on;
} morsecalc_state_t; } morsecalc_state_t;
void morsecalc_print_float(double d);
void morsecalc_print_token(morsecalc_state_t *mcs);
void morsecalc_print_stack(morsecalc_state_t *mcs);
void morsecalc_reset_token(morsecalc_state_t *mcs); void morsecalc_reset_token(morsecalc_state_t *mcs);
void morsecalc_input(morsecalc_state_t *mcs, char c); void morsecalc_input(morsecalc_state_t *mcs);
#define morsecalc_face ((const watch_face_t){ \ #define morsecalc_face ((const watch_face_t){ \
morsecalc_face_setup, \ morsecalc_face_setup, \