Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Dec 8, 2023
1 parent 1cbc09e commit ddd6091
Show file tree
Hide file tree
Showing 29 changed files with 971 additions and 294 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ jobs:
- name: Test project in native environment
run: pio test -vv -e native

- name: Build console application
- name: Build native console application
run: pio run -e native

- name: Build 'target' cross application
run: pio run -e target

- name: Build 'gun' cross application
- name: Build cross Gun application
run: pio run -e gun

- name: Build cross Target application
run: pio run -e target

- name: Archive test console application
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <Contactor.hpp>
#include <Contactor/Contactor.hpp>

void Contactor::checkForLongPress(long now) {
if ((downStartTime > 0) && ((now - downStartTime) >= LONG_PRESS)) {
Expand Down
File renamed without changes.
13 changes: 1 addition & 12 deletions lib/Domain/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
// 5 rounds per player
static const uint8_t TOTAL_ROUNDS = 20;

Game::Game(ITargetGui *gui) {
this->gui = gui;
Game::Game() {
currentRound = 0;
currentPlayer = &players[0];
}

void Game::recordSucceededShoot() {
currentPlayer->recordSucceededShoot();
gui->displayPlayerInfo(*currentPlayer);
}

/**
Expand All @@ -45,9 +43,7 @@ void Game::nextRound() {
nextPlayerId = playerId + 1;
}
currentPlayer = &players[nextPlayerId];

currentPlayer->startRound();
gui->displayPlayerInfo(*currentPlayer);
}

bool Game::isFinished() {
Expand All @@ -60,21 +56,14 @@ void Game::changeCurrentPlayerTo(uint8_t playerIndex) {
currentPlayer->endRound();
currentPlayer = &players[playerIndex];
currentPlayer->startRound();
gui->setCurrentPlayer(playerIndex);
gui->resetTargets();
gui->displayPlayerInfo(*currentPlayer);
}
}

void Game::reset() {
gui->restart();
for (Player &player : players) {
player.reset();
gui->displayPlayerInfo(player);
}
currentRound = 0;
currentPlayer = &players[0];
currentPlayer->startRound();
gui->displayPlayerInfo(*currentPlayer);
gui->setCurrentPlayer(0);
}
6 changes: 2 additions & 4 deletions lib/Domain/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
#pragma once

#include <ITargetGui.hpp>
#include <Player.hpp>
#include <Target/ITargetUi.hpp>

#include <stdint.h>

Expand All @@ -26,8 +26,6 @@
*/
class Game {

ITargetGui *gui;

public:
static const uint8_t PLAYER_COUNT = 4;

Expand All @@ -43,7 +41,7 @@ class Game {
uint8_t currentRound;
Player *currentPlayer;

explicit Game(ITargetGui *);
Game();

void recordSucceededShoot();

Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Gun/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Contactor.hpp>
#include <Contactor/Contactor.hpp>

class Gun;

Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Gun/Gun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
#pragma once

#include <Contactor.hpp>
#include <Contactor/Contactor.hpp>
#include <Gun/Button.hpp>
#include <Gun/IGunHal.hpp>
#include <Gun/IGunUi.hpp>
Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Gun/Trigger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Contactor.hpp>
#include <Contactor/Contactor.hpp>

class Gun;

Expand Down
44 changes: 44 additions & 0 deletions lib/Domain/Target/ITarget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/*
*
* Copyright (c) 2023 Aurélien Labrosse
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include <stdint.h>

/**
* This interface suggests an implemetation following the state pattern.
* `update()` behaviour can change according to current ITarget state.
*
*/
class ITarget {

public:

enum State { Activated, Calibrating, Ready, Hit, Error };
enum Event { NoEvent, Calibrate, Calibrated, Shoot, Reset };

virtual ~ITarget() {}

virtual void post(ITarget::Event event) = 0;

virtual ITarget::State getState() = 0;

/**
* System heartbeat callback
*/
virtual void update() = 0;
};
36 changes: 36 additions & 0 deletions lib/Domain/Target/ITargetUi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* Copyright (c) 2023 Aurélien Labrosse
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include <Target/ITarget.hpp>
#include <stdint.h>

class ITargetUi {
public:
virtual ~ITargetUi() {}

// map ITarget states

virtual void onHit() = 0;
virtual void onCalibrating() = 0;
virtual void onReady() = 0;
virtual void onError() = 0;

virtual void log(const char *) = 0;

virtual void log(uint8_t value) = 0;
};
92 changes: 92 additions & 0 deletions lib/Domain/Target/LDRTarget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* Copyright (c) 2023 Aurélien Labrosse
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <Target/LDRTarget.hpp>

LDRTarget::LDRTarget(uint8_t luminosityPin)
: ambiantLuminosity(0), luminosityPin(luminosityPin), state(Ready),
threshold(0) {

state = ITarget::State::Activated;
}

void LDRTarget::post(ITarget::Event event) { nextEvent = event; }

void LDRTarget::update() {

if (nextEvent != NoEvent) {

switch (state) {

case ITarget::State::Activated: {
if (nextEvent == ITarget::Event::Calibrate) {
onCalibrate();
}
}

case ITarget::State::Calibrating: {
if (nextEvent == ITarget::Event::Calibrated) {
onReady();
}
}

case ITarget::State::Ready: {
if (nextEvent == ITarget::Event::Shoot) {
onShoot();
}
}

case ITarget::State::Hit: {
if (nextEvent == ITarget::Event::Reset) {
onReady();
}
}
}

if (nextEvent == ITarget::Event::Error) {
onError();
}

// consumed or ignored
nextEvent = NoEvent;
} else {
if (getLuminosity() > (ambiantLuminosity + threshold)) {
post(ITarget::Event::Shoot);
}
}
}

ITarget::State LDRTarget::getState() { return state; }

uint16_t LDRTarget::getLuminosity() { return analogRead(luminosityPin); }

void LDRTarget::onError() { state = ITarget::State::Error; }
void LDRTarget::onReady() { state = ITarget::State::Ready; }
void LDRTarget::onShoot() { state = ITarget::State::Hit; }

void LDRTarget::onCalibrate() {
state = ITarget::State::Calibrating;
getLuminosity();
ambiantLuminosity = getLuminosity();
ambiantLuminosity += getLuminosity();
ambiantLuminosity += getLuminosity();
ambiantLuminosity += getLuminosity();
ambiantLuminosity /= 4;
post(ITarget::Event::Calibrated);
}

void LDRTarget::setThreshold(uint8_t threshold) { this->threshold = threshold; }
57 changes: 57 additions & 0 deletions lib/Domain/Target/LDRTarget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

/*
*
* Copyright (c) 2023 Aurélien Labrosse
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Target/ITarget.hpp>

// forward declaration
class TargetHost;

class LDRTarget : public ITarget {

uint16_t ambiantLuminosity;
uint8_t luminosityPin;
ITarget::State state;
ITarget::Event nextEvent;
uint8_t threshold;

uint16_t getLuminosity();
void onReady();
void onError();
void onShoot();
void onCalibrate();

enum State { All, Activated, Calibrating, Ready, Hit, Failure };
enum Event { NoEvent, Calibrate, Calibrated, Shoot, Reset, Error };

public:
LDRTarget(uint8_t luminosityPin);

ITarget::State getState() override;

void post(ITarget::Event event) override;

void update() override;

/**
* @brief Set the difference with ambiant luminosity level that triggers a hit
* condition
*
* @param threshold
*/
void setThreshold(uint8_t threshold);
};
Loading

0 comments on commit ddd6091

Please sign in to comment.