1+ /**
2+ StenoByte: a stenotype inspired keyboard app for typing out bytes.
3+
4+ StenoByte_Core.c is the source file for implementing resources related to processing the Bits into a Byte and
5+ for producing the result as an output.
6+
7+ This file is intended to be Operating System agnostic and to be compiled for any Operating System.
8+
9+ Copyright 2025 Asami De Almeida
10+
11+ Licensed under the Apache License, Version 2.0 (the "License");
12+ you may not use this file except in compliance with the License.
13+ You may obtain a copy of the License at
14+
15+ http://www.apache.org/licenses/LICENSE-2.0
16+
17+ Unless required by applicable law or agreed to in writing, software
18+ distributed under the License is distributed on an "AS IS" BASIS,
19+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+ See the License for the specific language governing permissions and
21+ limitations under the License.
22+ */
23+
24+ #include "StenoByte_Core.h"
25+
26+ // Arrays & Variables
27+ // Bit Array that contains the bits that forms a byte
28+ bool bit_arr [BITS_ARR_SIZE ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; // ordered from b0 to b7 during initialisation
29+ char keys_arr [BITS_ARR_SIZE ] = {';' , 'L' , 'K' , 'J' , 'F' , 'D' , 'S' , 'A' }; // ';' = b0, 'L' = b1, ... 'A' = b7
30+ u_int8_t subvalues_arr [BITS_ARR_SIZE ];
31+ bool ready_to_compute_byte = false; // the state for whether to convert the bit array into a byte and process it
32+
33+ u_int8_t current_byte = 0x00 ; // The byte last computed from the bit array
34+
35+
36+ // Methods & Functions
37+
38+ /*
39+ * Generates the Byte based on the bits in the array
40+ */
41+ void compute_byte () {
42+ current_byte = 0x00 ; // Resets the Byte to zero
43+ for (int i = 0 ; i < BITS_ARR_SIZE ; i ++ ) {
44+ current_byte = current_byte ^ bit_arr [i ] << i ;
45+ }
46+ ready_to_compute_byte = false;
47+ }
48+
49+ /*
50+ * Sets up the sub-values array for labelling
51+ */
52+ void setup_subvalues_array () {
53+ for (int i = BITS_ARR_SIZE ; i >= 0 ; i -- ) {
54+ const u_int8_t val = 0 ;
55+ subvalues_arr [i ] = val ^ 1 << i ;
56+ }
57+ }
58+
59+ /*
60+ * Gets Byte Summary as a String (an array of chars)
61+ * Assumes msg has a minimum length of 35. Assumes value of current_byte will not exceed 255.
62+ */
63+ void get_byte_summary (char * msg ) {
64+ sprintf (msg + strlen (msg ), "Last Computed Byte as decimal: %d\n" , current_byte ); // Prints between 33 and 35 chars
65+ }
66+
67+ /*
68+ * Prints the Byte Summary
69+ */
70+ void print_byte_summary () {
71+ char msg [35 ];
72+ get_byte_summary (msg );
73+ printf ("%s" , msg );
74+ }
75+
76+ /*
77+ * Prints the current state of the Bit Array
78+ * TODO: The repeated for loops could probably be simplified into a dedicated method
79+ */
80+ void print_bit_arr_summary () {
81+ char msg [654 ] = "" ;
82+
83+ sprintf (msg + strlen (msg ), "\nBits in Array:\n" ); // Prints 16 chars
84+ sprintf (msg + strlen (msg ), "\tBit Value:\t| " ); // Prints 14 chars
85+ for (int i = 7 ; i >= 0 ; i -- ) { // Repeats 8 times
86+ sprintf (msg + strlen (msg ), "\t%d\t|" , bit_arr [i ]); // Prints between 4 and 6 chars
87+ }
88+ sprintf (msg + strlen (msg ), "\n" ); // Prints 1 char
89+
90+ for (int i = 0 ; i < 24 + 16 * BITS_ARR_SIZE ; i ++ ) { // Repeats 24+(16*8) times, which is 152
91+ sprintf (msg + strlen (msg ), "-" ); // Prints 1 char
92+ }
93+
94+ sprintf (msg + strlen (msg ), "\n\tSub-Value:\t|" ); // Prints 14 chars
95+ for (int i = 7 ; i >= 0 ; i -- ) { // Repeats 8 times
96+ sprintf (msg + strlen (msg ), "\t[%d]\t|" , subvalues_arr [i ]); // Prints between 6 and 8 chars
97+ }
98+
99+ sprintf (msg + strlen (msg ), "\n\tBit Index:\t|" ); // Prints 14 chars
100+ for (int i = 7 ; i >= 0 ; i -- ) { // Repeats 8 times
101+ sprintf (msg + strlen (msg ), "\t[b%d]\t|" , i ); // Prints 7 chars
102+ }
103+ sprintf (msg + strlen (msg ), "\n\tKey:\t\t|" ); // Prints 9 times
104+
105+ for (int i = 7 ; i >= 0 ; i -- ) { // Repeats 8 times
106+ sprintf (msg + strlen (msg ), "\t[%c]\t|" , keys_arr [i ]); // Prints 6 chars
107+ }
108+ sprintf (msg + strlen (msg ), "\n" ); // Prints 1 char
109+ get_byte_summary (msg ); // Prints between 33 and 35 chars
110+ sprintf (msg + strlen (msg ), "\nPress & Hold the keys corresponding to the bits in the"
111+ " byte you would like to set to 1." ); // Prints 88 chars
112+ sprintf (msg + strlen (msg ), "\nBits will be 0 if keys are not pressed." ); // Prints 40 chars
113+ sprintf (msg + strlen (msg ), "\nPress SPACE BAR to compute Byte\t\t|\t"
114+ "Press ESC to exit\n" ); // Prints 53 chars
115+
116+ printf ("%s" , msg );
117+ }
0 commit comments