-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
971 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.