Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadien committed Nov 21, 2023
1 parent 10baa8f commit 998db18
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 78 deletions.
22 changes: 3 additions & 19 deletions lib/Domain/Gun/Atmega328pHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@

ISR(TIMER2_COMPA_vect) {}

volatile Contactor::Event pendingTriggerEvent;
void triggerInterruptHandler() {
if (bit_is_set(PIND, PD2)) {
pendingTriggerEvent = Contactor::Event::Released;
} else {
pendingTriggerEvent = Contactor::Event::Pressed;
}
}

volatile Contactor::Event pendingButtonEvent;
void buttonInterruptHandler() {
if (bit_is_set(PIND, PD3)) {
pendingButtonEvent = Contactor::Event::Released;
} else {
pendingButtonEvent = Contactor::Event::Pressed;
}
}

Atmega328pHal::Atmega328pHal() {

pinMode(VIBRATOR_PIN, OUTPUT);
Expand All @@ -66,8 +48,8 @@ Atmega328pHal::Atmega328pHal() {
void Atmega328pHal::setupHeartbeat() {
TCCR2A = (1U << WGM21) | (0U << WGM20); // set Timer2 in CTC mode
TCCR2B = (1U << CS22) | (1U << CS21) | (1U << CS20); // 1/1024 prescaler
TIMSK2 = (1U << OCIE2A); // enable compare Interrupt
ASSR &= ~(1U << AS2);
TIMSK2 = (1U << OCIE2A); // enable TIMER2 compare Interrupt
TCNT2 = 0U;

// set the output-compare register based on the desired tick frequency
Expand Down Expand Up @@ -107,12 +89,14 @@ void Atmega328pHal::sleep() {

void Atmega328pHal::setGun(Gun *gun) { gun = gun; }

extern void buttonInterruptHandler();
void Atmega328pHal::configureInputCallbacks() {

pinMode(BUTTON2_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON2_PIN), buttonInterruptHandler,
CHANGE);

extern void triggerInterruptHandler();
pinMode(TRIGGER_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), triggerInterruptHandler,
CHANGE);
Expand Down
21 changes: 21 additions & 0 deletions lib/Domain/Gun/Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
*
* 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 <Gun/Button.hpp>
#include <Gun/Gun.hpp>

void Button::onShortPress() { gun->shootCount = 0; }
void Button::onLongPress() { gun->toggleCalibrationMode(); }
32 changes: 32 additions & 0 deletions lib/Domain/Gun/Button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* 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 <Contactor.hpp>

class Gun;

class Button : public Contactor {

Gun *gun;

public:
Button() : Contactor() {}

void onShortPress() override;
void onLongPress() override;
void setGun(Gun *gun) { gun = gun; }
};
26 changes: 2 additions & 24 deletions lib/Domain/Gun/Gun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ void Gun::loop(void) {

now += 10; // 10ms per loop thanks to timer2

decreaseCycleCount();

//trigger.pendingEvent = hal->pendingTriggerEvent;
//button.pendingEvent = hal->pendingButtonEvent;
//pendingTriggerEvent = Contactor::Event::NoEvent;
//pendingButtonEvent = Contactor::Event::NoEvent;
decreaseShootCycleCount();

trigger.processPendingEvent(now);
button.processPendingEvent(now);
Expand Down Expand Up @@ -72,21 +67,4 @@ void Gun::setup(void) {
hal->getBatteryVoltagePercent());

ui->displayShootCount(0);
}

// Button implementation

void Button::onShortPress() { gun->shootCount = 0; }
void Button::onLongPress() { gun->toggleCalibrationMode(); }

// Trigger implementation

void Trigger::onDown(long now) {
if (gun->shootCycleCountdown == 0) {
gun->shootCount += 1;
gun->hal->laserOn();
gun->hal->vibrationOn();
gun->shootCycleCountdown = SHOOT_DURATION_TICKS;
}
Contactor::onDown(now);
}
}
39 changes: 9 additions & 30 deletions lib/Domain/Gun/Gun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,21 @@
#pragma once

#include <Contactor.hpp>
#include <Gun/Button.hpp>
#include <Gun/IGunHal.hpp>
#include <Gun/IGunUi.hpp>

#define SHOOT_DURATION_TICKS 5 /* 50 ms */

// forward declaration
class Gun;

class Button : public Contactor {

Gun *gun;

public:
Button() : Contactor() {}

void onShortPress() override;
void onLongPress() override;
void setGun(Gun *gun) { gun = gun; }
};

class Trigger : public Contactor {
Gun *gun;

public:
Trigger() : Contactor() {}
void setGun(Gun *gun) { gun = gun; }
void onDown(long now) override;
};
#include <Gun/Trigger.hpp>

class Gun {

Button button;
Trigger trigger;

public:
/* 50 ms */
static const uint8_t SHOOT_DURATION_TICKS = 5;

IGunHal *hal;
IGunUi *ui;
Button button;
Trigger trigger;
bool calibrationMode;
uint16_t shootCount;
uint8_t shootCycleCountdown;
Expand All @@ -65,11 +43,12 @@ class Gun {
trigger.setGun(this);
}

void decreaseCycleCount() {
void decreaseShootCycleCount() {
if (shootCycleCountdown > 0) {
shootCycleCountdown--;
}
}

void toggleCalibrationMode() { calibrationMode = !calibrationMode; }

void loop();
Expand Down
28 changes: 28 additions & 0 deletions lib/Domain/Gun/Trigger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* 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 <Gun/Trigger.hpp>
#include <Gun/Gun.hpp>

void Trigger::onDown(long now) {
if (gun->shootCycleCountdown == 0) {
gun->shootCount += 1;
gun->hal->laserOn();
gun->hal->vibrationOn();
gun->shootCycleCountdown = Gun::SHOOT_DURATION_TICKS;
}
Contactor::onDown(now);
}
29 changes: 29 additions & 0 deletions lib/Domain/Gun/Trigger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* 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 <Contactor.hpp>

class Gun;

class Trigger : public Contactor {
Gun *gun;

public:
Trigger() : Contactor() {}
void setGun(Gun *gun) { gun = gun; }
void onDown(long now) override;
};
34 changes: 29 additions & 5 deletions src/GunApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,42 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <Gun/SSD1306Ui.hpp>

#include <LowPower.h>

#include <Gun/Atmega328pHal.hpp>
#include <Gun/Gun.hpp>
#include <Gun/SSD1306Ui.hpp>

SSD1306Ui ui;
Atmega328pHal hal;
Gun gun(&hal, &ui);

void loop(void) { gun.loop(); }
volatile Contactor::Event pendingTriggerEvent;
void triggerInterruptHandler() {
if (bit_is_set(PIND, PD2)) {
pendingTriggerEvent = Contactor::Event::Released;
} else {
pendingTriggerEvent = Contactor::Event::Pressed;
}
}

volatile Contactor::Event pendingButtonEvent;
void buttonInterruptHandler() {
if (bit_is_set(PIND, PD3)) {
pendingButtonEvent = Contactor::Event::Released;
} else {
pendingButtonEvent = Contactor::Event::Pressed;
}
}

void loop(void) {

// wire interrupt-based events with main code
gun.trigger.pendingEvent = pendingTriggerEvent;
gun.button.pendingEvent = pendingButtonEvent;
pendingTriggerEvent = Contactor::Event::NoEvent;
pendingButtonEvent = Contactor::Event::NoEvent;

gun.loop();
}

void setup(void) {
hal.setupHeartbeat();
Expand Down

0 comments on commit 998db18

Please sign in to comment.