Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 22, 2023
1 parent a0b6095 commit e267243
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 169 deletions.
8 changes: 0 additions & 8 deletions lib/Domain/ITargetGui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
class ITargetGui {
public:
enum TARGET { One = 0, Two, Three, Four, Five };

enum TARGET{
One = 0,
Two,
Three,
Four,
Five
};

virtual ~ITargetGui() {}

Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/TargetHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class TargetHost : public ITargetHost {

public:
Target targets[5];
ITargetGui *gui;
Game *game;
ITargetGui *gui;

TargetHost(Game *game, ITargetGui *gui);

Expand Down
20 changes: 11 additions & 9 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ upload_command = avrdude -vv -b57600 -pm328p -c avrisp -Pcom9 $UPLOAD_FLAGS -U f
debug_test = native/test_Target
build_src_filter = ${env.src_filter} -<TargetApp.cpp> -<GunApp.cpp>

# [env:native_debug]
# lib_deps =
# ${env.lib_deps}
# platform=native
# build_type = debug
# debug_build_flags = -Og -ggdb3 -g3 -DNATIVE -UAVR
# test_ignore = cross/*
# debug_test = noarch/test_gun
# build_src_filter = ${env.src_filter} -<TargetApp.cpp> -<GunApp.cpp>
[env:native_debug]
lib_deps =
${env.lib_deps}
SerialCommands=https://github.com/arcadien/Arduino-SerialCommands
fakeit=https://github.com/FabioBatSilva/ArduinoFake.git
platform=native
build_type = debug
debug_build_flags = -Og -ggdb3 -g3 -DNATIVE -UAVR
test_ignore = cross/*
debug_test = native/test_gun
build_src_filter = ${env.src_filter} -<TargetApp.cpp> -<GunApp.cpp>

[env:target]
lib_deps =
Expand Down
156 changes: 5 additions & 151 deletions src/TargetApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,164 +15,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <Arduino.h>
#include <BTEGui.hpp>
#include <Game.hpp>

#include <SerialCommands.h>

#define TARGET_A_PIN A0
#define TARGET_Z_PIN A1
#define TARGET_E_PIN A2
#define TARGET_R_PIN A3
#define TARGET_T_PIN A4

#define LED_PIN 9

char serial_command_buffer_[32];
SerialCommands serial_commands_(&Serial, serial_command_buffer_,
sizeof(serial_command_buffer_), "|", " ");
void cmd_unrecognized(SerialCommands *sender, const char *cmd);
void cmd_resetTargets(SerialCommands *sender);
void cmd_setThreshold(SerialCommands *sender);
void cmd_nextRound(SerialCommands *sender);
void cmd_changePlayer(SerialCommands *sender);

SerialCommand cmd_nextRound_("N", &cmd_nextRound);
SerialCommand cmd_resetTargets_("R", &cmd_resetTargets);
SerialCommand cmd_setThreshold_("T", &cmd_setThreshold);
SerialCommand cmd_changePlayer_("P", &cmd_changePlayer);

void serialPrintInfo(uint16_t value);
void ledOn();
void ledOff();

// laser detection threshold
uint16_t threshold;

// ambient light ADC value
uint16_t reference;
#include <TargetHost.hpp>

BTEGui gui;
Game game(&gui);
TargetHost host(&game, &gui);

void setup() {

pinMode(LED_PIN, OUTPUT);
ledOff();

pinMode(TARGET_A_PIN, INPUT);
pinMode(TARGET_Z_PIN, INPUT);
pinMode(TARGET_E_PIN, INPUT);
pinMode(TARGET_R_PIN, INPUT);
pinMode(TARGET_T_PIN, INPUT);

serial_commands_.SetDefaultHandler(cmd_unrecognized);
serial_commands_.AddCommand(&cmd_resetTargets_);
serial_commands_.AddCommand(&cmd_setThreshold_);
serial_commands_.AddCommand(&cmd_nextRound_);
serial_commands_.AddCommand(&cmd_changePlayer_);

analogRead(TARGET_A_PIN);
analogRead(TARGET_Z_PIN);
analogRead(TARGET_E_PIN);
analogRead(TARGET_R_PIN);
analogRead(TARGET_T_PIN);

reference = 0;

reference += analogRead(TARGET_A_PIN);
reference += analogRead(TARGET_Z_PIN);
reference += analogRead(TARGET_E_PIN);
reference += analogRead(TARGET_R_PIN);
reference += analogRead(TARGET_T_PIN);
reference /= 5;

// initial value sent by GUI at startup
threshold = 500;

Serial.begin(115200);

host.setup();
game.reset();
serial_commands_.ReadSerial();
}

static void _recordHit() {
game.recordSucceededShoot();
ledOn();
delay(100);
ledOff();
}

static void _checkHit(uint16_t value, ITargetGui::TARGET target) {
if (value > threshold) {
if (!gui.isTargetHit(target)) {
gui.hitTarget(target);
_recordHit();
}
}
}

void loop() {

uint16_t value1 = analogRead(A0);
uint16_t value2 = analogRead(A1);
uint16_t value3 = analogRead(A2);
uint16_t value4 = analogRead(A3);
uint16_t value5 = analogRead(A4);

_checkHit(value1, ITargetGui::TARGET::One);
_checkHit(value2, ITargetGui::TARGET::Two);
_checkHit(value3, ITargetGui::TARGET::Three);
_checkHit(value4, ITargetGui::TARGET::Four);
_checkHit(value5, ITargetGui::TARGET::Five);

serial_commands_.ReadSerial();
delay(5);
}

void serialPrintInfo(uint16_t value) {
if (value > 0) {
Serial.print(F("HV: "));
Serial.println(value);
}
}
void cmd_unrecognized(SerialCommands *sender, const char *cmd) {
sender->GetSerial()->print("Unrecognized command [");
sender->GetSerial()->print(cmd);
sender->GetSerial()->println("]");
}
void cmd_setThreshold(SerialCommands *sender) {
uint16_t value = atoi(sender->Next());
if (value > 0 && value != threshold) {
threshold = value;
Serial.print(F("VThresh: "));
Serial.println(threshold);
Serial.print(F("Vref: "));
Serial.println(reference);
}
}
void cmd_changePlayer(SerialCommands *sender) {
uint8_t playerId = (uint8_t)atoi(sender->Next());
game.changeCurrentPlayerTo(playerId);
}
void cmd_resetTargets(SerialCommands *sender) {

(void)sender;
game.reset();
serialPrintInfo(0);
ledOn();
delay(200);
ledOff();
delay(200);
ledOn();
delay(200);
ledOff();
}
void cmd_nextRound(SerialCommands *sender) {
gui.resetTargets();
game.nextRound();
gui.setCurrentPlayer(game.currentPlayer->id);
}

void ledOff() { digitalWrite(LED_PIN, HIGH); }
void ledOn() { digitalWrite(LED_PIN, LOW); }
void loop() { host.loop(); }

0 comments on commit e267243

Please sign in to comment.