Initial commit

This commit is contained in:
Mikko Ahlroth 2019-10-17 00:11:52 +03:00
commit 0e6a5b0ac6
11 changed files with 221 additions and 0 deletions

10
.editorconfig Normal file
View file

@ -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

5
.vscode/arduino.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"sketch": "nopeustesti.ino",
"board": "arduino:avr:micro",
"port": "/dev/cu.usbmodem14101"
}

37
.vscode/c_cpp_properties.json vendored Normal file
View file

@ -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
}

17
nopeustesti.ino Normal file
View file

@ -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();
}

37
src/config.hpp Normal file
View file

@ -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<uint16_t>(newDelay);
};
/**
* Maximum amount of beeps you can be "behind" before the game is stopped.
*/
const uint8_t MAX_WAITING = 10;

20
src/debugger.cpp Normal file
View file

@ -0,0 +1,20 @@
#include "debugger.hpp"
#include <Arduino.h>
#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());
}
}

16
src/debugger.hpp Normal file
View file

@ -0,0 +1,16 @@
#include <Arduino.h>
#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);
};

34
src/game.cpp Normal file
View file

@ -0,0 +1,34 @@
#include "game.hpp"
#include <Arduino.h>
#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);
}

24
src/game.hpp Normal file
View file

@ -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();
};

12
src/randomiser.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "randomiser.hpp"
#include <Arduino.h>
#include "config.hpp"
Randomiser::Randomiser(uint32_t seed) {
randomSeed(seed);
}
int32_t Randomiser::get() {
return random(0, BUTTONS);
}

9
src/randomiser.hpp Normal file
View file

@ -0,0 +1,9 @@
#include <Arduino.h>
#pragma once
class Randomiser {
public:
Randomiser(uint32_t seed);
int32_t get();
};