From a8a7222e032da446550e030a00befe1db160b4d9 Mon Sep 17 00:00:00 2001 From: jenoack Date: Tue, 8 Feb 2022 18:55:18 +0100 Subject: [PATCH] initial version --- VSCode/Matrix/Ping Pong/.gitignore | 5 + .../Matrix/Ping Pong/.vscode/extensions.json | 7 + VSCode/Matrix/Ping Pong/include/README | 39 +++ VSCode/Matrix/Ping Pong/lib/README | 46 +++ VSCode/Matrix/Ping Pong/platformio.ini | 18 ++ VSCode/Matrix/Ping Pong/src/main.cpp | 167 ++++++++++ VSCode/Matrix/Ping Pong/test/README | 11 + VSCode/Pong/.gitignore | 5 + VSCode/Pong/.vscode/extensions.json | 7 + VSCode/Pong/include/README | 39 +++ VSCode/Pong/lib/README | 46 +++ VSCode/Pong/platformio.ini | 15 + VSCode/Pong/src/main.cpp | 302 ++++++++++++++++++ VSCode/Pong/test/README | 11 + 14 files changed, 718 insertions(+) create mode 100755 VSCode/Matrix/Ping Pong/.gitignore create mode 100755 VSCode/Matrix/Ping Pong/.vscode/extensions.json create mode 100755 VSCode/Matrix/Ping Pong/include/README create mode 100755 VSCode/Matrix/Ping Pong/lib/README create mode 100755 VSCode/Matrix/Ping Pong/platformio.ini create mode 100755 VSCode/Matrix/Ping Pong/src/main.cpp create mode 100755 VSCode/Matrix/Ping Pong/test/README create mode 100755 VSCode/Pong/.gitignore create mode 100755 VSCode/Pong/.vscode/extensions.json create mode 100755 VSCode/Pong/include/README create mode 100755 VSCode/Pong/lib/README create mode 100755 VSCode/Pong/platformio.ini create mode 100755 VSCode/Pong/src/main.cpp create mode 100755 VSCode/Pong/test/README diff --git a/VSCode/Matrix/Ping Pong/.gitignore b/VSCode/Matrix/Ping Pong/.gitignore new file mode 100755 index 0000000..89cc49c --- /dev/null +++ b/VSCode/Matrix/Ping Pong/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/VSCode/Matrix/Ping Pong/.vscode/extensions.json b/VSCode/Matrix/Ping Pong/.vscode/extensions.json new file mode 100755 index 0000000..0f0d740 --- /dev/null +++ b/VSCode/Matrix/Ping Pong/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/VSCode/Matrix/Ping Pong/include/README b/VSCode/Matrix/Ping Pong/include/README new file mode 100755 index 0000000..194dcd4 --- /dev/null +++ b/VSCode/Matrix/Ping Pong/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/VSCode/Matrix/Ping Pong/lib/README b/VSCode/Matrix/Ping Pong/lib/README new file mode 100755 index 0000000..6debab1 --- /dev/null +++ b/VSCode/Matrix/Ping Pong/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/VSCode/Matrix/Ping Pong/platformio.ini b/VSCode/Matrix/Ping Pong/platformio.ini new file mode 100755 index 0000000..0877fc4 --- /dev/null +++ b/VSCode/Matrix/Ping Pong/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega328new] +platform = atmelavr +board = nanoatmega328 +framework = arduino +lib_deps = + adafruit/Adafruit NeoMatrix@^1.2.0 + adafruit/Adafruit NeoPixel@^1.10.4 + adafruit/Adafruit WS2801 Library@^1.1.1 diff --git a/VSCode/Matrix/Ping Pong/src/main.cpp b/VSCode/Matrix/Ping Pong/src/main.cpp new file mode 100755 index 0000000..0e0a0cc --- /dev/null +++ b/VSCode/Matrix/Ping Pong/src/main.cpp @@ -0,0 +1,167 @@ +#include "SPI.h" +#include "Adafruit_WS2801.h" +#include +#include +#include + + +int x = 0; +int y = 0; +/***************************************************************************** +Example sketch for driving Adafruit WS2801 pixels! + + + Designed specifically to work with the Adafruit RGB Pixels! + 12mm Bullet shape ----> https://www.adafruit.com/products/322 + 12mm Flat shape ----> https://www.adafruit.com/products/738 + 36mm Square shape ----> https://www.adafruit.com/products/683 + + These pixels use SPI to transmit the color data, and have built in + high speed PWM drivers for 24 bit color per pixel + 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by David Kavanagh (dkavanagh@gmail.com). + BSD license, all text above must be included in any redistribution + +*****************************************************************************/ + +// Choose which 2 pins you will use for output. +// Can be any valid output pins. +// The colors of the wires may be totally different so +// BE SURE TO CHECK YOUR PIXELS TO SEE WHICH WIRES TO USE! +uint8_t dataPin = 6; // Yellow wire on Adafruit Pixels +uint8_t clockPin = 3; // Green wire on Adafruit Pixels + +// Don't forget to connect the ground wire to Arduino ground, +// and the +5V wire to a +5V supply + +// Set the first variable to the NUMBER of pixels in a row and +// the second value to number of pixels in a column. +Adafruit_WS2801 strip = Adafruit_WS2801((uint16_t)200, dataPin, clockPin); + + +/* Helper functions */ + +// Create a 24 bit color value from R,G,B +uint32_t Color(byte r, byte g, byte b) +{ + uint32_t c; + c = r; + c <<= 8; + c |= g; + c <<= 8; + c |= b; + return c; +} + +uint32_t Wheel(byte WheelPos) +{ + if (WheelPos < 85) { + return Color(WheelPos * 3, 255 - WheelPos * 3, 0); + } else if (WheelPos < 170) { + WheelPos -= 85; + return Color(255 - WheelPos * 3, 0, WheelPos * 3); + } else { + WheelPos -= 170; + return Color(0, WheelPos * 3, 255 - WheelPos * 3); + } +} + + +void drawX(uint8_t w, uint8_t h, uint8_t wait) { + uint16_t x, y; + for (x=0; x THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/VSCode/Pong/platformio.ini b/VSCode/Pong/platformio.ini new file mode 100755 index 0000000..4464928 --- /dev/null +++ b/VSCode/Pong/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +lib_deps = adafruit/Adafruit NeoPixel@^1.10.4 diff --git a/VSCode/Pong/src/main.cpp b/VSCode/Pong/src/main.cpp new file mode 100755 index 0000000..95fc243 --- /dev/null +++ b/VSCode/Pong/src/main.cpp @@ -0,0 +1,302 @@ +#include + +/** + * Player 1: x = 0 + * Player 2: x = X_MAX-1 + */ + +#define PIN_LED_MATRIX 2 +#define PIN_JOYSTICK_1_BUTTON 6 +#define PIN_JOYSTICK_2_BUTTON 7 +#define PIN_PIEZO 11 +#define PIN_JOYSTICK_1_Y A0 +#define PIN_JOYSTICK_2_Y A1 + +#define JOYSTICK_OFFSET_MIN 200 +#define JOYSTICK_OFFSET_MAX 700 + +#define DEBOUNCE_TIME 300 // in ms + +#define X_MAX 10 +#define Y_MAX 20 + +#define GAME_DELAY 80 // in ms +#define BALL_DELAY_MAX 350 // in ms +#define BALL_DELAY_MIN 50 // in ms +#define BALL_DELAY_STEP 5 // in ms + +#define PLAYER_AMOUNT 2 +#define PLAYER_1 0 +#define PLAYER_2 1 + +#define PADDLE_WIDTH 3 + +#define PADDLE_MOVE_NONE 0 +#define PADDLE_MOVE_UP 1 +#define PADDLE_MOVE_DOWN 2 + +#define LED_TYPE_OFF 1 +#define LED_TYPE_PADDLE 2 +#define LED_TYPE_BALL 3 +#define LED_TYPE_BALL_RED 4 + +#define TONE_PLAYER 1 +#define TONE_WALL 2 +#define TONE_BUZZ 3 + +#define GAME_STATE_RUNNING 1 +#define GAME_STATE_END 2 +#define GAME_STATE_INIT 3 + +#include + +struct Coords { + byte x; + byte y; +}; + +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(X_MAX * Y_MAX, PIN_LED_MATRIX, NEO_GRB + NEO_KHZ800); +bool buttonPressed = false; +byte gameState; +byte joystickPins[PLAYER_AMOUNT] = {PIN_JOYSTICK_1_Y, PIN_JOYSTICK_2_Y}; +Coords paddles[PLAYER_AMOUNT][PADDLE_WIDTH]; +Coords ball; +int ballMovement[2]; +unsigned int ballDelay; +unsigned long lastDrawUpdate = 0; +unsigned long lastBallUpdate = 0; +unsigned long lastButtonClick = 0; + +void setup() +{ + pinMode(PIN_JOYSTICK_1_Y, INPUT); + pinMode(PIN_JOYSTICK_1_BUTTON, INPUT_PULLUP); + pinMode(PIN_JOYSTICK_2_Y, INPUT); + pinMode(PIN_JOYSTICK_2_BUTTON, INPUT_PULLUP); + pixels.begin(); + resetLEDs(); + gameState = GAME_STATE_END; +} + +void loop() +{ + switch(gameState) { + case GAME_STATE_INIT: + initGame(); + break; + case GAME_STATE_RUNNING: + updateBall(); + updateGame(); + break; + case GAME_STATE_END: + if (isButtonPressed()) { + gameState = GAME_STATE_INIT; + } + break; + } +} + +void initGame() +{ + resetLEDs(); + lastButtonClick = millis(); + + ball.x = 1; + ball.y = (Y_MAX/2) - (PADDLE_WIDTH/2) + 1; + ballMovement[0] = 1; + ballMovement[1] = -1; + ballDelay = BALL_DELAY_MAX; + + for(byte i=0; i BALL_DELAY_MIN) { + ballDelay -= BALL_DELAY_STEP; + } + } + + ball.x += ballMovement[0]; + ball.y += ballMovement[1]; + + if (ball.x <=0 || ball.x >= X_MAX-1) { + endGame(); + return; + } + + if (ball.y <= 0 || ball.y >= Y_MAX-1) { + ballMovement[1] *= -1; + playTone(TONE_WALL); + } + + toggleLed(ball.x, ball.y, LED_TYPE_BALL); + pixels.show(); +} + +void endGame() +{ + gameState = GAME_STATE_END; + toggleLed(ball.x, ball.y, LED_TYPE_BALL_RED); + pixels.show(); + playTone(TONE_BUZZ); +} + +void updateGame() +{ + if ((millis() - lastDrawUpdate) < GAME_DELAY) { + return; + } + lastDrawUpdate = millis(); + + // turn off paddle LEDs + for(byte p=0; p 0) { + for(byte i=0; i JOYSTICK_OFFSET_MAX) { + if (playerId == PLAYER_2) { + return PADDLE_MOVE_UP; + } else { + return PADDLE_MOVE_DOWN; + } + } + return PADDLE_MOVE_NONE; +} + +bool isButtonPressed() +{ + if ((millis() - lastButtonClick) < DEBOUNCE_TIME) { + return false; + } + if (digitalRead(PIN_JOYSTICK_1_BUTTON) == LOW) { + lastButtonClick = millis(); + return true; + } + if (digitalRead(PIN_JOYSTICK_2_BUTTON) == LOW) { + lastButtonClick = millis(); + return true; + } + return false; +} + +void resetLEDs() +{ + for(byte i=0; i