aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Faase <lm.faase@student.avans.nl>2024-05-11 23:28:33 +0200
committerLars Faase <lm.faase@student.avans.nl>2024-05-11 23:28:33 +0200
commit69bad64f94693bc45902a0eae27b1f5688636c9f (patch)
tree8c4048b385ebe1d482d535a534d0862a3f12a212
parent488899377dc72989a178173662f95d25fc69c59f (diff)
Merge master branch into prot/Hardware_Puzzle
-rw-r--r--main/puzzle/hardware/main.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/main/puzzle/hardware/main.c b/main/puzzle/hardware/main.c
new file mode 100644
index 0000000..2fe8c4e
--- /dev/null
+++ b/main/puzzle/hardware/main.c
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <time.h>
+
+#define MORSE_CODE_LENGTH 4
+#define MAX_CODE_LENGTH 5
+#define NUMBER_OF_POTENTIOMETERS 4
+
+typedef enum {
+ UNINITIALIZED,
+ IDLE,
+ PLAYING,
+ SOLVED
+} HardwarePuzzleState;
+
+typedef struct {
+ HardwarePuzzleState current_state;
+ char code[MAX_CODE_LENGTH + 1];
+ bool tone_playing;
+ int morse_code_step;
+ int morse_code_number_step;
+ unsigned long ms_delay;
+ int potentiometer_values[NUMBER_OF_POTENTIOMETERS];
+ int correct_potentiometer_values[NUMBER_OF_POTENTIOMETERS];
+ int logic_gate_values[8];
+} PuzzleState;
+
+void generate_random_four_digit_code(PuzzleState *state) {
+ printf("Generating random morse code...\nCode: ");
+ for (int i = 0; i < MORSE_CODE_LENGTH; i++) {
+ state->correct_potentiometer_values[i] = rand() % 10;
+ state->code[i] = state->correct_potentiometer_values[i] + '0';
+ }
+ state->code[MORSE_CODE_LENGTH] = '\0';
+ printf("%s\n", state->code);
+}
+
+void enter_state(PuzzleState *state, HardwarePuzzleState new_state) {
+ state->current_state = new_state;
+}
+
+void render(PuzzleState *state) {
+ if (state->ms_delay == 0) {
+ state->ms_delay = millis() + play_morse_code(state);
+ } else if (millis() >= state->ms_delay) {
+ state->ms_delay = 0;
+ }
+ if (state->current_state == MORSECODE) {
+ int values = state->potentiometer_values[0] * 1000 +
+ state->potentiometer_values[1] * 100 +
+ state->potentiometer_values[2] * 10 +
+ state->potentiometer_values[3] * 1;
+ // GpioManager::instance().set_display_value(values); // Voeg GPIO-besturing toe voor Arduino
+ }
+}
+
+void process_state(PuzzleState *state) {
+ get_user_input(state);
+ render(state);
+ puzzle_check(state);
+}
+
+void change_gamestage(PuzzleState *state, HardwarePuzzleState new_state) {
+ state->current_state = new_state;
+}
+
+int play_morse_code(PuzzleState *state) {
+ if (state->current_state == MORSECODE) {
+ // Implementeer de morsecode logica
+ }
+ return 0;
+}
+
+void get_user_input(PuzzleState *state) {
+ if (state->current_state == LOGICGATES) {
+ // Implementeer de logica voor het verkrijgen van gebruikersinvoer voor LOGICGATES
+ } else if (state->current_state == MORSECODE) {
+ // Implementeer de logica voor het verkrijgen van gebruikersinvoer voor MORSECODE
+ }
+}
+
+void puzzle_check(PuzzleState *state) {
+ if (state->current_state == LOGICGATES) {
+ // Implementeer de logica voor het controleren van de puzzel voor LOGICGATES
+ } else if (state->current_state == MORSECODE) {
+ // Implementeer de logica voor het controleren van de puzzel voor MORSECODE
+ }
+}
+
+void initialize_game(PuzzleState *state) {
+ srand(time(NULL)); // Initialiseer de random seed met de huidige tijd
+ state->current_state = UNINITIALIZED;
+ enter_state(state, IDLE); // Ga naar de IDLE-state na initialisatie
+ generate_random_four_digit_code(state);
+ change_gamestage(state, PLAYING); // Ga naar de PLAYING-state om de puzzel te spelen
+}
+
+int main() {
+ PuzzleState puzzle_state;
+ initialize_game(&puzzle_state);
+
+ // Simuleer de loop van de state machine
+ while (puzzle_state.current_state != SOLVED) {
+ process_state(&puzzle_state); // Blijf in de huidige staat
+ }
+
+ return 0;
+}