-
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
5 changed files
with
336 additions
and
188 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
/* | ||
* | ||
* 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/Gun.hpp> | ||
|
||
#define TICKS_BETWEEN_BATTERY_UI_UPDATE 100 | ||
|
||
Gun::Gun(IGunHal *hal, IGunUi *ui) { | ||
this->hal = hal; | ||
this->ui = ui; | ||
calibrationMode = false; | ||
shootCount = 0; | ||
|
||
// display at first loop | ||
shootCycleCountdown = 0; | ||
batteryDisplayCycleCountdown = 0; | ||
|
||
button.setGun(this); | ||
trigger.setGun(this); | ||
} | ||
|
||
void Gun::toggleCalibrationMode() { | ||
calibrationMode = !calibrationMode; | ||
if (calibrationMode) { | ||
hal->laserOn(); | ||
hal->vibrationOff(); | ||
ui->displayCalibration(); | ||
} else { | ||
hal->laserOff(); | ||
ui->clearCalibration(); | ||
ui->displayShootCount(shootCount); | ||
batteryDisplayCycleCountdown = 0; | ||
} | ||
} | ||
|
||
void Gun::resetShoots() { | ||
shootCount = 0; | ||
ui->displayShootCount(shootCount); | ||
} | ||
|
||
void Gun::activateShoot() { | ||
if (!calibrationMode && shootCycleCountdown == 0) { | ||
shootCount += 1; | ||
hal->laserOn(); | ||
hal->vibrationOn(); | ||
|
||
// add 1 to allow easy detection of last tick | ||
shootCycleCountdown = Gun::SHOOT_DURATION_TICKS + 1; | ||
} | ||
} | ||
|
||
void Gun::loop(void) { | ||
|
||
// 10ms per loop thanks to timer2 | ||
millisSinceStart += 10; | ||
|
||
trigger.processPendingEvent(millisSinceStart); | ||
button.processPendingEvent(millisSinceStart); | ||
|
||
button.checkForLongPress(millisSinceStart); | ||
|
||
if (shootCycleCountdown > 0) { | ||
shootCycleCountdown--; | ||
if (shootCycleCountdown == 1) { | ||
hal->laserOff(); | ||
hal->vibrationOff(); | ||
ui->displayShootCount(shootCount); | ||
shootCycleCountdown--; | ||
} | ||
} | ||
|
||
if (batteryDisplayCycleCountdown > 0) { | ||
batteryDisplayCycleCountdown--; | ||
} else { | ||
ui->displayBatteryStatus(hal->getBatteryVoltageMv(), | ||
hal->getBatteryVoltagePercent()); | ||
batteryDisplayCycleCountdown = TICKS_BETWEEN_BATTERY_UI_UPDATE; | ||
} | ||
} | ||
|
||
void Gun::setup(void) { | ||
ui->displaySplash(2000); | ||
// mV is not used | ||
ui->displayBatteryStatus(0, hal->getBatteryVoltagePercent()); | ||
ui->displayShootCount(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,65 @@ | ||
/* | ||
* | ||
* 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> | ||
#include <Gun/Button.hpp> | ||
#include <Gun/IGunHal.hpp> | ||
#include <Gun/IGunUi.hpp> | ||
#include <Gun/Trigger.hpp> | ||
|
||
class Gun { | ||
|
||
uint8_t shootCycleCountdown; | ||
uint16_t batteryDisplayCycleCountdown; | ||
long millisSinceStart = 0; | ||
bool calibrationMode; | ||
|
||
public: | ||
/* 50 ms */ | ||
static const uint8_t SHOOT_DURATION_TICKS = 5; | ||
|
||
IGunHal *hal; | ||
IGunUi *ui; | ||
Button button; | ||
Trigger trigger; | ||
uint16_t shootCount; | ||
|
||
Gun(IGunHal *hal, IGunUi *ui); | ||
|
||
/** | ||
* Reset shoot count and redisplay | ||
*/ | ||
void resetShoots(); | ||
|
||
/** | ||
* Activate laser and vibrator | ||
*/ | ||
void activateShoot(); | ||
|
||
void toggleCalibrationMode(); | ||
|
||
/** | ||
* @brief Gun loop shall be called at least each 10ms, | ||
* but interrupt shall trigger loop() execution in | ||
* a shorter delay. | ||
* | ||
*/ | ||
void loop(); | ||
|
||
void setup(); | ||
}; |
Oops, something went wrong.