From b0405eef7cf77666a088ba48dfd4eff38094fb02 Mon Sep 17 00:00:00 2001 From: nimeissn Date: Wed, 13 Jul 2022 20:45:08 +0200 Subject: [PATCH] Delay entfernt, cpu auf 160MHz, Ton gerichtet --- VSCode/Pong/platformio.ini | 4 + VSCode/Pong/src/main.cpp | 231 ++++++++++++++++++++++++++++--------- 2 files changed, 180 insertions(+), 55 deletions(-) diff --git a/VSCode/Pong/platformio.ini b/VSCode/Pong/platformio.ini index ebc2324..ca04692 100755 --- a/VSCode/Pong/platformio.ini +++ b/VSCode/Pong/platformio.ini @@ -16,3 +16,7 @@ lib_deps = adafruit/Adafruit WS2801 Library@^1.1.1 lbernstone/Tone32@^1.0.0 platform = espressif32 + + +; set frequency to 160MHz +board_build.f_cpu = 160000000L diff --git a/VSCode/Pong/src/main.cpp b/VSCode/Pong/src/main.cpp index f9eae22..1eb8aad 100755 --- a/VSCode/Pong/src/main.cpp +++ b/VSCode/Pong/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include /** * Player 1: x = 0 @@ -11,6 +12,7 @@ #define PIN_JOYSTICK_1_BUTTON 12 #define PIN_JOYSTICK_2_BUTTON 26 #define PIN_PIEZO 17 +#define CHANNEL_PIEZO 1 #define PIN_JOYSTICK_1_Y 13 #define PIN_JOYSTICK_2_Y 27 #define PIN_JOYSTICK_1_X 14 @@ -39,11 +41,11 @@ #define PADDLE_MOVE_UP 1 #define PADDLE_MOVE_DOWN 2 -#define LED_TYPE_OFF 1 +#define LED_TYPE_OFF 1 #define LED_TYPE_PADDLE_BLUE 2 #define LED_TYPE_PADDLE_RED 3 -#define LED_TYPE_BALL 4 -#define LED_TYPE_BALL_YELLOW 5 +#define LED_TYPE_BALL 4 +#define LED_TYPE_BALL_YELLOW 5 #define LED_TYPE_POINTS_BLUE 6 #define LED_TYPE_POINTS_RED 7 @@ -51,10 +53,18 @@ #define TONE_WALL 2 #define TONE_BUZZ 3 -#define GAME_STATE_RUNNING 1 -#define GAME_STATE_END 2 -#define GAME_STATE_INIT 3 -#include +#define GAME_STATE_RUNNING 1 +#define GAME_STATE_END 2 +#define GAME_STATE_INIT 3 +#define GAME_STATE_INACTIVE 4 +#define GAME_STATE_PLAY_WITH_ME 5 + +#define LIGHT_STATE_BAGGROUND 1 +#define LIGHT_STATE_PLAY 2 +#define LIGHT_STATE_WITH 3 +#define LIGHT_STATE_ME 4 +#define LIGHT_STATE_END 5 + uint8_t dataPin = 2; uint8_t clockPin = 4; @@ -89,10 +99,12 @@ void toggleLed(byte x, byte y, byte type); byte getPlayerMovement(byte playerId); void endGame(); void attentionLight(); +void attentionLight1(); +unsigned long delay_light = millis(); +byte lightState = LIGHT_STATE_BAGGROUND; +boolean delay_light_high = true; -#include - static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; } @@ -128,6 +140,8 @@ static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { void setup() { + ledcAttachPin(PIN_PIEZO , CHANNEL_PIEZO); + ledcSetup(CHANNEL_PIEZO, 10000, 16); pinMode(PIN_JOYSTICK_1_Y, INPUT); pinMode(PIN_JOYSTICK_1_BUTTON, INPUT_PULLUP); pinMode(PIN_JOYSTICK_2_Y, INPUT); @@ -147,6 +161,8 @@ void loop() switch(gameState) { case GAME_STATE_INIT: + lightState = LIGHT_STATE_BAGGROUND; + delay_light_high = true; initGame(); break; case GAME_STATE_RUNNING: @@ -157,14 +173,34 @@ void loop() if (isButtonPressed()) { gameState = GAME_STATE_INIT; } - else if (millis() - lastDrawUpdate > 30000) + else if (millis() - lastDrawUpdate > 3000 && millis() - lastDrawUpdate < 30000 && millis() - delay_light >= 500 && false == delay_light_high || millis() - lastDrawUpdate > 15000 && millis() - lastDrawUpdate < 30000 && millis() - delay_light >= 500 && true == delay_light_high ) { - resetLEDs(); - pointsPlayerBlue = 0; - pointsPlayerRed = 0; - attentionLight(); + gameState = GAME_STATE_INACTIVE; } + + else if (millis() - lastDrawUpdate > 30000 && millis() - delay_light >= 1500) + { + gameState = GAME_STATE_PLAY_WITH_ME; + } + break; + case GAME_STATE_INACTIVE: + + resetLEDs(); + pointsPlayerBlue = 0; + pointsPlayerRed = 0; + attentionLight1(); + gameState = GAME_STATE_END; + break; + + case GAME_STATE_PLAY_WITH_ME: + + resetLEDs(); + pointsPlayerBlue = 0; + pointsPlayerRed = 0; + attentionLight(); + gameState = GAME_STATE_END; + break; } } @@ -484,54 +520,139 @@ void attentionLight() int with [46] = {13,14,15,16,17,18,22,36,42,58,57,56,55,54,53,73,74,75,76,77,78,93,106,114,126,133,115,116,117,118,113,154,155,156,157,158,153,163,176,183,193,195,196,197,198,194}; int me [30] = {53,54,55,56,57,58,65,75,85,93,94,95,96,97,98,113,114,115,116,117,118,126,138,146,121,133,144,124,135,141}; - - - for (int i = 0; i< 200; i++) + switch (lightState) { - pixels.setPixelColor(i,Color(0,5,5)); - } - - pixels.show(); - delay(1000); - - for (int i = 0; i<44; i++) - { - pixels.setPixelColor(play[i],Color(255,0,0)); - } - - pixels.show(); - delay(1500); - - for (int i = 0; i< 200; i++) - { - pixels.setPixelColor(i,Color(5,5,5)); - } - - - for (int i = 0; i<46; i++) - { - pixels.setPixelColor(with[i],Color(255,0,0)); - } - - pixels.show(); - delay(1500); - + case LIGHT_STATE_BAGGROUND: for (int i = 0; i< 200; i++) - { - pixels.setPixelColor(i,Color(5,5,5)); - } + { + pixels.setPixelColor(i,Color(0,5,5)); + } + + pixels.show(); + lightState = LIGHT_STATE_PLAY; + delay_light = millis(); + + break; + + case LIGHT_STATE_PLAY: + + for (int i = 0; i<44; i++) + { + pixels.setPixelColor(play[i],Color(255,0,0)); + } + + pixels.show(); + lightState = LIGHT_STATE_WITH; + delay_light = millis(); + break; + + case LIGHT_STATE_WITH: + + for (int i = 0; i< 200; i++) + { + pixels.setPixelColor(i,Color(5,5,5)); + } - for (int i = 0; i<30; i++) - { - pixels.setPixelColor(me[i],Color(255,0,0)); + for (int i = 0; i<46; i++) + { + pixels.setPixelColor(with[i],Color(255,0,0)); + } + + pixels.show(); + lightState = LIGHT_STATE_ME; + delay_light = millis(); + break; + + case LIGHT_STATE_ME: + + for (int i = 0; i< 200; i++) + { + pixels.setPixelColor(i,Color(5,5,5)); + } + + + for (int i = 0; i<30; i++) + { + pixels.setPixelColor(me[i],Color(255,0,0)); + } + + pixels.show(); + + lightState = LIGHT_STATE_END; + delay_light = millis(); + + break; + + case LIGHT_STATE_END: + + lastDrawUpdate = millis(); + resetLEDs(); + lightState = LIGHT_STATE_BAGGROUND; + + default: + + lastDrawUpdate = millis(); + lightState = LIGHT_STATE_BAGGROUND; + break; } - pixels.show(); - delay(1500); - lastDrawUpdate = millis(); - resetLEDs(); + + + + + + + + + + + + + + } +void attentionLight1() +{ + byte r = random(1,255); + byte g = random(1,255); + byte b = random(1,255); + byte n = random(199); + + pixels.setPixelColor(n,r,g,b); + byte r1 = random(1,255); + byte g1 = random(1,255); + byte b1 = random(1,255); + byte n1 = random(199); + + pixels.setPixelColor(n1,r1,g1,b1); + pixels.show(); + delay_light = millis(); + delay_light_high = false; + + +} + +bool is_waiting_for_ms(unsigned long wait_time_ms) +{ + static bool is_waiting = false; + static unsigned long start_ms = 0; + static unsigned long static_wait_time_ms = 0; + if (false == is_waiting) + { + start_ms = millis(); + is_waiting = true; + static_wait_time_ms = wait_time_ms; + } + else + { + if (millis() - static_wait_time_ms >= start_ms ) + { + is_waiting = false; + } + } + return is_waiting; +} \ No newline at end of file