commit 0e6a5b0ac620b324af1f929dfe7727da14370c1b Author: Mikko Ahlroth Date: Thu Oct 17 00:11:52 2019 +0300 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7535c46 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true + +[*.{cpp,hpp,c,h,ino}] +charset = utf-8 +indent_style = space +indent_size = 2 diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..40ed396 --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,5 @@ +{ + "sketch": "nopeustesti.ino", + "board": "arduino:avr:micro", + "port": "/dev/cu.usbmodem14101" +} diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..74231d7 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,37 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino", + "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/micro", + "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include", + "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/7.3.0/include" + ], + "browse": { + "path": [ + "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino", + "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/micro", + "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include", + "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/7.3.0/include" + ] + }, + "forcedInclude": [], + "macFrameworkPath": [ + "/System/Library/Frameworks", + "/Library/Frameworks" + ], + "intelliSenseMode": "clang-x64", + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "compilerArgs": [ + "-Werror -Wall -Wextra -pedantic" + ], + "defines": [ + "USBCON" + ] + } + ], + "version": 4 +} diff --git a/nopeustesti.ino b/nopeustesti.ino new file mode 100644 index 0000000..a65e193 --- /dev/null +++ b/nopeustesti.ino @@ -0,0 +1,17 @@ +#include "src/debugger.hpp" +#include "src/game.hpp" +#include "src/randomiser.hpp" + +Debugger* debugger; +Randomiser* randomiser; +Game* game; + +void setup() { + debugger = new Debugger(9600); + randomiser = new Randomiser(analogRead(0)); + game = new Game(debugger, randomiser); +} + +void loop() { + game->loop(); +} diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 0000000..4fb0be8 --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,37 @@ +/** + * Set to true to enable debug prints (via serial console) and false to disable. + */ +const bool DEBUG = true; + +/** + * Amount of buttons in the game. + */ +const uint8_t BUTTONS = 4; + +/** + * Delay at start of game between beeps, in milliseconds. + */ +const uint16_t DELAY_START = 570; + +/** + * Function to decrement the given delay. Should return the new smaller delay. + */ +const auto DELAY_DECREMENT = [](uint16_t delay) -> uint16_t { + double newDelay; + if (delay > 399) { + newDelay = delay * 0.993; + } else if (delay > 326) { + newDelay = delay * 0.996; + } else if (delay > 192) { + newDelay = delay * 0.9985; + } else { + newDelay = delay - 1; + } + + return static_cast(newDelay); +}; + +/** + * Maximum amount of beeps you can be "behind" before the game is stopped. + */ +const uint8_t MAX_WAITING = 10; diff --git a/src/debugger.cpp b/src/debugger.cpp new file mode 100644 index 0000000..55190a3 --- /dev/null +++ b/src/debugger.cpp @@ -0,0 +1,20 @@ +#include "debugger.hpp" + +#include +#include "config.hpp" + +Debugger::Debugger(uint16_t port) : serialPort(port) { + Serial.begin(serialPort); +} + +void Debugger::print(String msg, bool newline) { + if (DEBUG) { + auto msgCopy = String(msg); + + if (newline) { + msgCopy.concat("\r\n"); + } + + Serial.write(msgCopy.c_str()); + } +} diff --git a/src/debugger.hpp b/src/debugger.hpp new file mode 100644 index 0000000..cc74e13 --- /dev/null +++ b/src/debugger.hpp @@ -0,0 +1,16 @@ +#include + +#pragma once + +/** + * Debugger class providing an interface to the serial port. To disable the debug calls entirely, + * use the DEBUG configuration variable. + */ +class Debugger { + uint16_t serialPort; + + public: + Debugger(uint16_t port); + + void print(String msg, bool newline = true); +}; diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..e6e7511 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,34 @@ +#include "game.hpp" + +#include +#include "config.hpp" +#include "debugger.hpp" +#include "randomiser.hpp" + +Game::Game(Debugger* debugger, Randomiser* randomiser) : debugger(debugger), + randomiser(randomiser), + state(State::MENU), + currentDelay(DELAY_START), + currentScore(0), + waitingBeeps({}), + waitingBeepsAmount(0) { + debugger->print("Initialising new game..."); +} + +void Game::loop() { + ++currentScore; + + auto msg = String("Delay: "); + msg.concat(currentDelay); + debugger->print(msg); + + msg = String("Score: "); + msg.concat(currentScore); + debugger->print(msg); + + msg = String(randomiser->get()); + debugger->print(msg); + + delay(currentDelay); + currentDelay = DELAY_DECREMENT(currentDelay); +} diff --git a/src/game.hpp b/src/game.hpp new file mode 100644 index 0000000..0daf0d2 --- /dev/null +++ b/src/game.hpp @@ -0,0 +1,24 @@ +#include "config.hpp" +#include "debugger.hpp" +#include "randomiser.hpp" + +enum State { + MENU, + GAME, + SCORE +}; + +class Game { + Debugger* debugger; + Randomiser* randomiser; + State state; + uint16_t currentDelay; + uint16_t currentScore; + uint8_t waitingBeeps[MAX_WAITING]; + uint8_t waitingBeepsAmount; + + public: + Game(Debugger* debugger, Randomiser* randomiser); + + void loop(); +}; diff --git a/src/randomiser.cpp b/src/randomiser.cpp new file mode 100644 index 0000000..fbdb7b4 --- /dev/null +++ b/src/randomiser.cpp @@ -0,0 +1,12 @@ +#include "randomiser.hpp" + +#include +#include "config.hpp" + +Randomiser::Randomiser(uint32_t seed) { + randomSeed(seed); +} + +int32_t Randomiser::get() { + return random(0, BUTTONS); +} diff --git a/src/randomiser.hpp b/src/randomiser.hpp new file mode 100644 index 0000000..23848cd --- /dev/null +++ b/src/randomiser.hpp @@ -0,0 +1,9 @@ +#include + +#pragma once + +class Randomiser { + public: + Randomiser(uint32_t seed); + int32_t get(); +};