diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 884b71f..82eb136 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -30,7 +30,7 @@ jobs: # os: [ubuntu-latest, windows-latest] os: [ubuntu-latest] build_type: [Release] - c_compiler: [gcc, clang, cl] + c_compiler: [gcc, cl] include: # - os: windows-latest # c_compiler: cl diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bb79bc..0c4be64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.30) project(StenoByte_Prototype C) +project(StenoByte_Writer C) set(CMAKE_C_STANDARD 23) @@ -38,6 +39,11 @@ else () endif () # Main App -add_executable(StenoByte_Prototype main.c) +add_executable(StenoByte_Prototype src/stenobyte_demo.c) target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS} StenoByte_Library) -target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES} StenoByte_Library) \ No newline at end of file +target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES} StenoByte_Library) + +# Writer App +add_executable(StenoByte_Writer src/stenobyte_writer.c) +target_include_directories(StenoByte_Writer PRIVATE ${LIBEVDEV_INCLUDE_DIRS} StenoByte_Library) +target_link_libraries(StenoByte_Writer PRIVATE ${LIBEVDEV_LIBRARIES} StenoByte_Library) \ No newline at end of file diff --git a/includes/StenoByte_Core.c b/includes/StenoByte_Core.c index dd60e6d..7843f56 100644 --- a/includes/StenoByte_Core.c +++ b/includes/StenoByte_Core.c @@ -29,12 +29,36 @@ bool bit_arr[BITS_ARR_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; // ordered from b0 to b7 char keys_arr[BITS_ARR_SIZE] = {';', 'L', 'K', 'J', 'F', 'D', 'S', 'A'}; // ';' = b0, 'L' = b1, ... 'A' = b7 u_int8_t subvalues_arr[BITS_ARR_SIZE]; bool ready_to_compute_byte = false; // the state for whether to convert the bit array into a byte and process it +const char* output_file_path; // The path to the file write to +FILE *output_file_ptr; // The pointer of the file itself to write to +enum stenobyte_mode mode = NOT_SET; u_int8_t current_byte = 0x00; // The byte last computed from the bit array // Methods & Functions +int setup_stenobyte_demo() { + printf("Starting StenoType...\n"); + mode = DEMO; + return setup_stenobyte(); +} + +int setup_stenobyte_writer(int argc, const char* argv[]) { + if (argc < 2) { + // Default File Path if none is provided + output_file_path = "./output.txt"; + } else { + // File Path Provided by Command Line Arguments + output_file_path = argv[1]; + } + + printf("Welcome to StenoByte Writer.\nWriting to file: %s\n", output_file_path); + output_file_ptr = fopen(output_file_path, "w"); + mode = WRITER; + return setup_stenobyte(); +} + /* * Generates the Byte based on the bits in the array */ @@ -74,13 +98,37 @@ void print_byte_summary() { printf("%s", msg); } +void print_current_mode(char* msg) { + // Min Chars Printed: 11 + // Max Chars Printed: 19 + sprintf(msg+strlen(msg), "Mode: "); // Prints 6 chars + + switch (mode) { + case DEMO: + sprintf(msg+strlen(msg), "DEMO"); // Prints 4 chars + break; + case WRITER: + sprintf(msg+strlen(msg), "WRITER"); // Prints 6 chars + break; + case NOT_SET: + sprintf(msg+strlen(msg), "NOT_SET"); // Prints 7 chars + break; + default: + sprintf(msg+strlen(msg), "UNKNOWN MODE"); // Prints 12 chars + break; + } + + sprintf(msg+strlen(msg), "\n"); // Prints 1 char +} + /* * Prints the current state of the Bit Array * TODO: The repeated for loops could probably be simplified into a dedicated method */ void print_bit_arr_summary() { - char msg[654] = ""; + char msg[673] = ""; + print_current_mode(msg); // Prints between 11 and 19 chars sprintf(msg + strlen(msg), "\nBits in Array:\n"); // Prints 16 chars sprintf(msg + strlen(msg), "\tBit Value:\t| "); // Prints 14 chars for (int i = 7; i >= 0; i--) { // Repeats 8 times @@ -115,4 +163,16 @@ void print_bit_arr_summary() { "Press ESC to exit\n"); // Prints 53 chars printf("%s", msg); +} + +void write_byte_to_file() { + fprintf(output_file_ptr, "%c", (char) current_byte); +} + +/* + * Closes the File and frees up memory safely + */ +void end_stenobyte_writer() { + fclose(output_file_ptr); + end_stenobyte(); } \ No newline at end of file diff --git a/includes/StenoByte_Core.h b/includes/StenoByte_Core.h index 650faf2..d7e6323 100644 --- a/includes/StenoByte_Core.h +++ b/includes/StenoByte_Core.h @@ -24,6 +24,12 @@ #include "StenoByte_Helper.h" +enum stenobyte_mode { + NOT_SET = 0, + DEMO, + WRITER +}; + // Number of Bits in the Bit Array (should be 8) # define BITS_ARR_SIZE 8 @@ -34,6 +40,10 @@ extern char keys_arr[BITS_ARR_SIZE]; // ';' = b0, 'L' = b1, ... 'A' = b7 extern u_int8_t subvalues_arr[BITS_ARR_SIZE]; extern bool ready_to_compute_byte; // the state for whether to convert the bit array into a byte and process it extern u_int8_t current_byte; // The byte last computed from the bit array +extern const char* output_file_path; // The path to the file write to +extern FILE *output_file_ptr; // The pointer of the file itself to write to +extern enum stenobyte_mode mode; // What mode is the program currently in + // Externally Declared Methods & Functions (expected to be declared & defined StenoByte_Helper files) @@ -43,10 +53,15 @@ extern void run_stenobyte(); extern void end_stenobyte(); // Methods & Functions +int setup_stenobyte_demo(); +int setup_stenobyte_writer(int argc, const char* argv[]); void compute_byte(); void setup_subvalues_array(); void get_byte_summary(char* msg); void print_byte_summary(); +void print_current_mode(char* msg); void print_bit_arr_summary(); +void write_byte_to_file(); +void end_stenobyte_writer(); #endif //STENOBYTE_CORE_H diff --git a/includes/StenoByte_Helper.h b/includes/StenoByte_Helper.h index aa3fa5d..c8bea9a 100644 --- a/includes/StenoByte_Helper.h +++ b/includes/StenoByte_Helper.h @@ -32,6 +32,7 @@ #include #include #include +#include // Key Press States #define EV_KEY_RELEASED 0 diff --git a/includes/StenoByte_Helper_for_Linux.c b/includes/StenoByte_Helper_for_Linux.c index 281fe3a..e586cb0 100644 --- a/includes/StenoByte_Helper_for_Linux.c +++ b/includes/StenoByte_Helper_for_Linux.c @@ -35,8 +35,6 @@ const int event_file_device; * Returns 0 if there were no errors, 1 if there were errors */ int setup_stenobyte() { - printf("Starting StenoType...\n"); - setup_subvalues_array(); const int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to the correct device @@ -138,6 +136,9 @@ void run_stenobyte() { if (ready_to_compute_byte) { compute_byte(); + if (mode == WRITER) { + write_byte_to_file(); + } } usleep(1000); // Small delay @@ -215,7 +216,7 @@ void process_key_presses(const struct input_event* current_event) { return; } - // Exits method if key even is not a key press or key repeat + // Exits method if key event is not a key press or key repeat if (current_event->value != EV_KEY_PRESSED && current_event->value != EV_KEY_REPEATED) { return; } diff --git a/main.c b/src/stenobyte_demo.c similarity index 90% rename from main.c rename to src/stenobyte_demo.c index 953185e..be276f3 100644 --- a/main.c +++ b/src/stenobyte_demo.c @@ -16,12 +16,12 @@ limitations under the License. */ -#include "includes/StenoByte_Core.h" +#include "../includes/StenoByte_Core.h" int main() { // Performs setup; exits app if there was an error while setting up - const int setup_result = setup_stenobyte(); + const int setup_result = setup_stenobyte_demo(); if (setup_result != 0) { return setup_result; } diff --git a/src/stenobyte_writer.c b/src/stenobyte_writer.c new file mode 100644 index 0000000..11db921 --- /dev/null +++ b/src/stenobyte_writer.c @@ -0,0 +1,39 @@ +/** +StenoByte: a stenotype inspired keyboard app for typing out bytes. + + Copyright 2025 Asami De Almeida + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include "../includes/StenoByte_Core.h" + + +int main(int argc, const char* argv[]) { + + // fprintf(output_file_ptr, "Some Text 123!\n"); + + // Performs setup; exits app if there was an error while setting up + const int setup_result = setup_stenobyte_writer(argc, argv); + if (setup_result != 0) { + return setup_result; + } + + // Runs the loop + run_stenobyte(); + + // Frees up Memory Safely & Closes the File + end_stenobyte_writer(); + + return 0; +}