This commit is contained in:
2024-08-08 20:47:44 -05:00
parent fe9a4df7a9
commit b6eac70c2c
9 changed files with 429 additions and 88 deletions
+2 -2
View File
@@ -142,8 +142,8 @@ bool get_flipped_switch(SwitchKey* switch_) {
return false;
}
uint8_t get_switch_state(uint8_t* switch_flags) {
return (uint8_t)(button_state && 0xF);
uint8_t get_switch_state() {
return (uint8_t)(~button_state & 0xF);
}
bool get_touch_state(void) {
+1 -1
View File
@@ -59,7 +59,7 @@ uint8_t get_button_state();
bool get_flipped_up_switch(SwitchKey* switch_);
bool get_flipped_down_switch(SwitchKey* switch_);
bool get_flipped_switch(SwitchKey* switch_);
uint8_t get_switch_state(uint8_t* switch_flags);
uint8_t get_switch_state();
bool get_touch_state(void);
bool get_touch_pressed(void);
+2
View File
@@ -3,7 +3,9 @@ set(SOURCES
"step0.cpp"
"step1.cpp"
"step2.cpp"
"step3.cpp"
"step4.cpp"
"step5.cpp"
"step6.cpp"
"wires_puzzle.cpp"
"tetris.c"
+339
View File
@@ -0,0 +1,339 @@
#include "step3.h"
static const char *TAG = "step3";
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> puzzle_dist(0, 7);
static std::uniform_int_distribution<> led_picker_dist(0, 20);
static std::uniform_int_distribution<> led_color_dist(0, 5);
static const int INDICATOR_RED[6] = {30, 0, 0, 25, 15, 10};
static const int INDICATOR_GREEN[6] = {0, 0, 30, 5, 0, 10};
static const int INDICATOR_BLUE[6] = {0, 30, 0, 0, 15, 10};
void step3(void) {
int solved_puzzles = 0;
while (solved_puzzles < 3) {
// int puzzle = puzzle_dist(gen);
int puzzle = 2;
switch (puzzle) {
case 0: {
lcd_print(&lcd, "Clear");
set_module_time(10000);
start_module_timer();
std::uniform_int_distribution<> green_indicators_dist(2, 15);
// create all green indicators
uint8_t green_indicators = green_indicators_dist(gen);
std::set<int> indicators;
// ESP_LOGI(TAG, "green indicators: %i", green_indicators);
while (indicators.size() < green_indicators) {
int led = led_picker_dist(gen);
indicators.insert(led);
// ESP_LOGI(TAG, "added green led at %i", led);
}
for (std::set<int>::iterator it = indicators.begin(); it != indicators.end(); it++) {
ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, 0, 10, 0));
}
// add non-green indicators
const int NON_GREEN_INDICATOR_RED[3] = {30, 0, 15};
const int NON_GREEN_INDICATOR_BLUE[3] = {0, 30, 15};
std::uniform_int_distribution<> non_green_indicator_color_dist(0, 2);
std::uniform_int_distribution<> remaining_indicators_dist(0, 20 - green_indicators);
int non_green_indicators = remaining_indicators_dist(gen);
std::set<int> remaining_indicators;
// ESP_LOGI(TAG, "non-green indicators: %i", non_green_indicators);
while (remaining_indicators.size() < non_green_indicators) {
int led_attempt = led_picker_dist(gen);
if (indicators.find(led_attempt) == indicators.end()) {
remaining_indicators.insert(led_attempt);
// ESP_LOGI(TAG, "added non-green led at %i", led_attempt);
}
}
for (std::set<int>::iterator it = remaining_indicators.begin(); it != remaining_indicators.end(); it++) {
int color = non_green_indicator_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, NON_GREEN_INDICATOR_RED[color], 0, NON_GREEN_INDICATOR_BLUE[color]));
}
ESP_ERROR_CHECK(led_strip_refresh(leds));
// wait for time
while (1) {
if (get_module_time() <= 0) {
uint8_t state = get_switch_state();
uint8_t flipped_state = 0;
flipped_state |= (state & 0x01) << 3;
flipped_state |= (state & 0x02) << 1;
flipped_state |= (state & 0x04) >> 1;
flipped_state |= (state & 0x08) >> 3;
if (flipped_state == green_indicators) {
solved_puzzles++;
} else {
strike("Incorrect switch combination! (step 3, puzzle 0)");
}
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
break;
}
case 1: {
lcd_print(&lcd, "Blank");
set_module_time(10000);
start_module_timer();
std::uniform_int_distribution<> on_indicators_dist(16, 21);
uint8_t indicators_num = on_indicators_dist(gen);
std::set<int> indicators;
while (indicators.size() < indicators_num) {
indicators.insert(led_picker_dist(gen));
}
for (std::set<int>::iterator it = indicators.begin(); it != indicators.end(); it++) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
ESP_ERROR_CHECK(led_strip_refresh(leds));
// ESP_LOGI(TAG, "puzzle 1 LEDs set (%i leds)", indicators_num);
if (indicators_num < 18) {
while (1) {
if (get_module_time() <= 0) {
if (get_switch_state() == 0b1111) {
solved_puzzles++;
// ESP_LOGI(TAG, "puzzle 1 solved (switches)!");
} else {
strike("Switch state was changed! (step 3, puzzle 1)");
}
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
} else {
uint8_t starting_switch_state = get_switch_state();
KeypadKey key;
std::string pressed_keys;
while (1) {
if (get_pressed_keypad(&key)) {
pressed_keys += char_of_keypad_key(key);
// ESP_LOGI(TAG, "key %c pressed", char_of_keypad_key(key));
}
if (get_module_time() <= 0) {
if (starting_switch_state == get_switch_state()) {
if (pressed_keys == "ADCB") {
solved_puzzles++;
// ESP_LOGI(TAG, "puzzle 1 solved (keypad)!");
}
} else {
strike("Switch state was changed! (step 3, puzzle 1)");
}
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
break;
}
case 2: {
set_module_time(10000);
start_module_timer();
std::uniform_int_distribution<> lit_led_dist(0, 1);
bool rfid_lit = lit_led_dist(gen);
// ESP_LOGI(TAG, "rfid lit: %i", rfid_lit);
bool lcd_lit = lit_led_dist(gen);
bool speaker_lit = lit_led_dist(gen);
bool keypad_lit = lit_led_dist(gen);
bool tft_lit = lit_led_dist(gen);
Led rfid_led = rfid;
Led lcd_led = char_lcd;
Led speaker_led = speaker;
Led keypad_led = keypad;
Led tft_led = tft;
if (rfid_lit) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, rfid_led, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
if (lcd_lit) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, lcd_led, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
if (speaker_lit) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, speaker_led, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
if (keypad_lit) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, keypad_led, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
if (tft_lit) {
int color = led_color_dist(gen);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, tft_led, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]));
}
ESP_ERROR_CHECK(led_strip_refresh(leds));
int green_button_pressed = 0;
// ESP_LOGI(TAG, "green pressed: %i", green_button_pressed);
int blue_button_pressed = 0;
int fingerprint_sensor_pressed = 0;
std::string keypad_string;
ButtonKey button;
KeypadKey key;
SwitchKey switch1 = s1;
SwitchKey switch2 = s2;
while (1) {
if (get_pressed_button(&button)) {
uint8_t button_state = get_button_state();
// ESP_LOGI(TAG, "Button pressed! (%i)", button_state());
if ((button_state & 0b1) == 0b1) {
green_button_pressed++;
if ((green_button_pressed > 1) || !rfid_lit) {
strike("Green button pressed too many times! (step 3, puzzle 2)");
break;
}
}
if ((button_state & 0b1000) == 0b1000) {
blue_button_pressed++;
if ((blue_button_pressed > 1) || !lcd_lit) {
strike("Blue button pressed too many times! (step 3, puzzle 2)");
break;
}
}
}
if (get_touch_pressed()) {
fingerprint_sensor_pressed++;
if ((fingerprint_sensor_pressed > 2) || !speaker_lit) {
strike("Fingerprint sensor pressed too many times! (step 3, puzzle 2)");
break;
}
}
if (get_pressed_keypad(&key)) {
bool wrong = false;
keypad_string += char_of_keypad_key(key);
switch (keypad_string.length()) {
case 1: {
if (keypad_string != "1") {
strike("1 was not pressed on Keypad! (step 3, puzzle 2)");
wrong = true;
}
break;
}
case 2: {
if (keypad_string != "12") {
strike("12 was not pressed on Keypad! (step 3, puzzle 2)");
wrong = true;
}
break;
}
default: {
strike("keypad length was more than 2! (step 3, puzzle 2)");
wrong = true;
break;
}
}
if (wrong) {
break;
}
}
if (get_flipped_up_switch(&switch1) || get_flipped_up_switch(&switch2)) {
if (!tft_lit) {
strike("Switch 1 or 2 were flipped up! (step 3, puzzle 2)");
break;
}
}
if (get_module_time() <= 0) {
bool rfid_correct = !(rfid_lit && (green_button_pressed != 1));
bool lcd_correct = !(lcd_lit && (blue_button_pressed != 1));
bool speaker_correct = !(speaker_lit && (fingerprint_sensor_pressed != 2));
bool keypad_correct = !(keypad_lit && (keypad_string != "12"));
bool tft_correct = !(tft_lit && ((get_switch_state() & 0b11) != 0b11));
if (rfid_correct && lcd_correct && speaker_correct && keypad_correct && tft_correct) {
solved_puzzles++;
} else {
strike("Final state was not correct! (step 3, puzzle 2)");
}
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
break;
}
case 3: {
lcd_print(&lcd, "Nothing");
set_module_time(10000);
start_module_timer();
break;
}
case 4: {
lcd_print(&lcd, "Blink");
set_module_time(10000);
start_module_timer();
break;
}
case 5: {
lcd_print(&lcd, "Ummm");
set_module_time(10000);
start_module_timer();
break;
}
case 6: {
lcd_print(&lcd, "Plank");
set_module_time(10000);
start_module_timer();
break;
}
case 7: {
lcd_print(&lcd, "What");
set_module_time(10000);
start_module_timer();
break;
}
case 8: {
lcd_print(&lcd, "Plink");
set_module_time(10000);
start_module_timer();
break;
}
}
clean_bomb();
}
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef STEP_3_H
#define STEP_3_H
#include "../drivers/bottom_half.h"
#include "../drivers/game_timer.h"
#include "../drivers/char_lcd.h"
#include "../drivers/leds.h"
#include "../drivers/wires.h"
#include "../helper.h"
#include <random>
#include <iostream>
#include <set>
static const char *STEP3_TAG = "step3";
void step3(void);
#endif /* STEP_3_H */
-51
View File
@@ -1,51 +0,0 @@
#ifndef STEP_3_HPP
#define STEP_3_HPP
#include <random>
#include "../drivers/bottom_half.h"
#include <iostream>
#include "../drivers/game_timer.h"
#include <set>
static const char *STEP3_TAG = "step3";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> puzzle_dist(0, 7);
std::uniform_int_distribution<> puzzle_zero_dist(0, 15);
std::uniform_int_distribution<> led_dist(0, 20);
void step3(void) {
set_module_time(3000);
// int puzzle = puzzle_dist(gen);
int puzzle = 0;
switch (puzzle) {
case 0:
lcd_print(&lcd, "Clear");
int green_indicators = puzzle_zero_dist(gen);
std::set<int> indicators;
while (indicators.size() < green_indicators) {
indicators.insert(led_dist(gen));
}
for (std::set<int>::iterator it = indicators.begin(); it != indicators.end(); it++) {
ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, 0, 60, 0));
}
while (1) {
if (get_module_time() <= 0) {
ESP_LOGI(STEP3_TAG, "switch state: %i", get_switch_state(0));
// get_switch_state();
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
}
#endif /* STEP_3_HPP */
+39 -32
View File
@@ -1,16 +1,12 @@
#ifndef STEP_5_HPP
#define STEP_5_HPP
#include "step5.h"
#include "../drivers/bottom_half.h"
#include <iostream>
#include <random>
static const char *TAG = "step5";
static const char *STEP5_TAG = "step5";
Led speaker_led = speaker;
const uint8_t SSEG_NUMS[10] = {0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111};
const uint8_t SSEG_MAPS[5][4] = {
static Led speaker_led = speaker;
// one: 0b00000110
// seven: 0b00000111
static const uint8_t SSEG_NUMS[8] = {0b00111111, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b01111111, 0b01101111};
static const uint8_t SSEG_MAPS[5][4] = {
{0b0101101, 0b1111010, 0b1000010, 0b1010100},
{0b01000101, 0b00100100, 0b00110110, 0b01111011},
{0b00101010, 0b00000010, 0b00010111, 0b00111100},
@@ -18,25 +14,36 @@ const uint8_t SSEG_MAPS[5][4] = {
{0b01000111, 0b00011001, 0b01111000, 0b00111110}
};
const int INDICATOR_RED[5] = {60, 0, 0, 35, 20};
const int INDICATOR_GREEN[5] = {0, 0, 60, 25, 20};
const int INDICATOR_BLUE[5] = {0, 60, 0, 0, 20};
static const int INDICATOR_RED[5] = {30, 0, 0, 25, 10};
static const int INDICATOR_GREEN[5] = {0, 0, 30, 5, 10};
static const int INDICATOR_BLUE[5] = {0, 30, 0, 0, 10};
// random number generators
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> answer_dist(0, 9);
std::uniform_int_distribution<> map_dist(0, 4);
std::uniform_int_distribution<> display_dist(0, 3);
std::uniform_int_distribution<> random_segment_dist(0, 127);
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> answer_dist(0, 7);
static std::uniform_int_distribution<> map_dist(0, 4);
static std::uniform_int_distribution<> display_dist(0, 3);
static std::uniform_int_distribution<> random_segment_dist(0, 127);
int answer = 0;
uint8_t answer_sseg = SSEG_NUMS[0];
char answer_char = '0';
uint8_t display_map[4] = {0b00000000, 0b00000000, 0b00000000, 0b00000000};
int chosen_map = 0;
static int answer = 0;
static uint8_t answer_sseg = SSEG_NUMS[0];
static char answer_char = '0';
static uint8_t display_map[4] = {0b00000000, 0b00000000, 0b00000000, 0b00000000};
static int chosen_map = 0;
void new_puzzle(void) {
std::map<int, int> number_map = {
{0, 0},
{1, 2},
{2, 3},
{3, 4},
{4, 5},
{5, 6},
{6, 8},
{7, 9},
};
static void new_puzzle(void) {
// scramble lights
for (int i = 0; i < 5; i++) {
led_strip_set_pixel(leds, speaker_led, INDICATOR_RED[map_dist(gen)], INDICATOR_GREEN[map_dist(gen)], INDICATOR_BLUE[map_dist(gen)]);
@@ -52,14 +59,16 @@ void new_puzzle(void) {
answer = answer_dist(gen);
answer_sseg = SSEG_NUMS[answer];
// convert answer to number value to account for missing 1 and 7
answer = number_map[answer];
answer_char = '0' + answer;
// ESP_LOGI(STEP5_TAG, "Answer: %i", answer);
// ESP_LOGI(TAG, "Answer: %i", answer);
chosen_map = map_dist(gen);
for (int i = 0; i < 4; ++i) {
display_map[i] = SSEG_MAPS[chosen_map][i];
}
// ESP_LOGI(STEP5_TAG, "Chosen Map: %i", chosen_map);
// ESP_LOGI(TAG, "Chosen Map: %i", chosen_map);
ESP_ERROR_CHECK(led_strip_set_pixel(leds, speaker_led, INDICATOR_RED[chosen_map], INDICATOR_GREEN[chosen_map], INDICATOR_BLUE[chosen_map]));
ESP_ERROR_CHECK(led_strip_refresh(leds));
@@ -70,7 +79,7 @@ void new_puzzle(void) {
// choose display and flip bit
int display = display_dist(gen);
display_map[display] ^= (1 << i);
// ESP_LOGI(STEP5_TAG, "Flipping bit %i on display %i", i, display);
// ESP_LOGI(TAG, "Flipping bit %i on display %i", i, display);
}
}
}
@@ -85,7 +94,7 @@ void step5(void) {
set_module_sseg_raw(display_map);
if (get_pressed_keypad(&key)) {
char c = char_of_keypad_key(key);
// ESP_LOGI(STEP0_TAG, "Pressed: %c", c);
// ESP_LOGI(TAG, "Pressed: %c", c);
if (c == answer_char) {
solved_times++;
if (solved_times < 3) {
@@ -102,5 +111,3 @@ void step5(void) {
}
}
#endif /* STEP_5_HPP */
+14
View File
@@ -0,0 +1,14 @@
#ifndef STEP_5_H
#define STEP_5_H
#include "../drivers/bottom_half.h"
#include "../drivers/wires.h"
#include "../drivers/game_timer.h"
#include "../drivers/leds.h"
#include <iostream>
#include <random>
#include <map>
void step5(void);
#endif /* STEP_5_H */