From 998db18615375f8f02731a5e2db8495633e550e1 Mon Sep 17 00:00:00 2001 From: Aurelien Labrosse Date: Tue, 21 Nov 2023 13:02:49 +0100 Subject: [PATCH] wip --- lib/Domain/Gun/Atmega328pHal.cpp | 22 +++--------------- lib/Domain/Gun/Button.cpp | 21 +++++++++++++++++ lib/Domain/Gun/Button.hpp | 32 ++++++++++++++++++++++++++ lib/Domain/Gun/Gun.cpp | 26 ++------------------- lib/Domain/Gun/Gun.hpp | 39 ++++++++------------------------ lib/Domain/Gun/Trigger.cpp | 28 +++++++++++++++++++++++ lib/Domain/Gun/Trigger.hpp | 29 ++++++++++++++++++++++++ src/GunApp.cpp | 34 ++++++++++++++++++++++++---- 8 files changed, 153 insertions(+), 78 deletions(-) create mode 100644 lib/Domain/Gun/Button.cpp create mode 100644 lib/Domain/Gun/Button.hpp create mode 100644 lib/Domain/Gun/Trigger.cpp create mode 100644 lib/Domain/Gun/Trigger.hpp diff --git a/lib/Domain/Gun/Atmega328pHal.cpp b/lib/Domain/Gun/Atmega328pHal.cpp index 32334d6..02594a5 100644 --- a/lib/Domain/Gun/Atmega328pHal.cpp +++ b/lib/Domain/Gun/Atmega328pHal.cpp @@ -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); @@ -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 @@ -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); diff --git a/lib/Domain/Gun/Button.cpp b/lib/Domain/Gun/Button.cpp new file mode 100644 index 0000000..651f8e6 --- /dev/null +++ b/lib/Domain/Gun/Button.cpp @@ -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 . + */ +#include +#include + +void Button::onShortPress() { gun->shootCount = 0; } +void Button::onLongPress() { gun->toggleCalibrationMode(); } \ No newline at end of file diff --git a/lib/Domain/Gun/Button.hpp b/lib/Domain/Gun/Button.hpp new file mode 100644 index 0000000..0782c52 --- /dev/null +++ b/lib/Domain/Gun/Button.hpp @@ -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 . + */ +#pragma once +#include + +class Gun; + +class Button : public Contactor { + + Gun *gun; + +public: + Button() : Contactor() {} + + void onShortPress() override; + void onLongPress() override; + void setGun(Gun *gun) { gun = gun; } +}; \ No newline at end of file diff --git a/lib/Domain/Gun/Gun.cpp b/lib/Domain/Gun/Gun.cpp index 4ee97f0..f4672aa 100644 --- a/lib/Domain/Gun/Gun.cpp +++ b/lib/Domain/Gun/Gun.cpp @@ -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); @@ -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); -} +} \ No newline at end of file diff --git a/lib/Domain/Gun/Gun.hpp b/lib/Domain/Gun/Gun.hpp index bf6b41e..48751b0 100644 --- a/lib/Domain/Gun/Gun.hpp +++ b/lib/Domain/Gun/Gun.hpp @@ -17,43 +17,21 @@ #pragma once #include +#include #include #include - -#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 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; @@ -65,11 +43,12 @@ class Gun { trigger.setGun(this); } - void decreaseCycleCount() { + void decreaseShootCycleCount() { if (shootCycleCountdown > 0) { shootCycleCountdown--; } } + void toggleCalibrationMode() { calibrationMode = !calibrationMode; } void loop(); diff --git a/lib/Domain/Gun/Trigger.cpp b/lib/Domain/Gun/Trigger.cpp new file mode 100644 index 0000000..418701f --- /dev/null +++ b/lib/Domain/Gun/Trigger.cpp @@ -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 . + */ +#include +#include + +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); +} \ No newline at end of file diff --git a/lib/Domain/Gun/Trigger.hpp b/lib/Domain/Gun/Trigger.hpp new file mode 100644 index 0000000..b2c2ecf --- /dev/null +++ b/lib/Domain/Gun/Trigger.hpp @@ -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 . + */ +#pragma once +#include + +class Gun; + +class Trigger : public Contactor { + Gun *gun; + +public: + Trigger() : Contactor() {} + void setGun(Gun *gun) { gun = gun; } + void onDown(long now) override; +}; \ No newline at end of file diff --git a/src/GunApp.cpp b/src/GunApp.cpp index 992c7bd..3f2c4ab 100644 --- a/src/GunApp.cpp +++ b/src/GunApp.cpp @@ -15,18 +15,42 @@ * along with this program. If not, see . */ -#include - -#include - #include #include +#include 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();