-
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
13 changed files
with
225 additions
and
247 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* | ||
* 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 <Contactor.hpp> | ||
#include <Gun/Atmega328pHal.hpp> | ||
#include <LowPower.h> | ||
#include <avr/io.h> | ||
|
||
#define BSP_TICKS_PER_SEC 100 | ||
#define F_CPU 16000000L | ||
|
||
#define LASER_PIN 10 | ||
#define VIBRATOR_PIN 6 | ||
#define BATTERY_VOLTAGE_PIN A3 | ||
#define CHARGING_STATE_PIN A2 | ||
#define BUTTON2_PIN 3 | ||
#define TRIGGER_PIN 2 | ||
|
||
#define MIN_BAT_VOLTAGE 3000 | ||
#define MAX_BAT_VOLTAGE 4120 | ||
|
||
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); | ||
pinMode(LASER_PIN, OUTPUT); | ||
} | ||
/* | ||
* the 'loop' method shall be called each 10ms | ||
* | ||
* Configure timer2 so that an interrupt occurs each 10ms | ||
* and wake up the MCU if it is in sleep mode. | ||
*/ | ||
void Atmega328pHal::setupHeartbeat() { | ||
TCCR2A = (1U << WGM21) | (0U << WGM20); // set Timer2 in CTC mode | ||
TCCR2B = (1U << CS22) | (1U << CS21) | (1U << CS20); // 1/1024 prescaler | ||
ASSR &= ~(1U << AS2); | ||
TIMSK2 = (1U << OCIE2A); // enable TIMER2 compare Interrupt | ||
TCNT2 = 0U; | ||
|
||
// set the output-compare register based on the desired tick frequency | ||
OCR2A = (F_CPU / BSP_TICKS_PER_SEC / 1024U) - 1U; | ||
} | ||
|
||
void Atmega328pHal::laserOn() { PORTB |= (1 << PB2); } | ||
void Atmega328pHal::laserOff() { PORTB &= ~(1 << PB2); } | ||
void Atmega328pHal::vibrationOn() { PORTD |= (1 << PD6); } | ||
void Atmega328pHal::vibrationOff() { PORTD &= ~(1 << PD6); } | ||
|
||
uint16_t Atmega328pHal::getBatteryVoltageMv() { | ||
|
||
analogRead(BATTERY_VOLTAGE_PIN); | ||
float rawBatt = analogRead(BATTERY_VOLTAGE_PIN); | ||
rawBatt += analogRead(BATTERY_VOLTAGE_PIN); | ||
rawBatt += analogRead(BATTERY_VOLTAGE_PIN); | ||
rawBatt += analogRead(BATTERY_VOLTAGE_PIN); | ||
rawBatt /= 4; | ||
uint16_t battMv = (5000 / 1023.f) * rawBatt; | ||
|
||
return battMv; | ||
}; | ||
|
||
uint8_t Atmega328pHal::getBatteryVoltagePercent() { | ||
return map(getBatteryVoltageMv(), MIN_BAT_VOLTAGE, MAX_BAT_VOLTAGE, 0, 100); | ||
} | ||
|
||
bool Atmega328pHal::isCharging() { | ||
return (digitalRead(CHARGING_STATE_PIN) == HIGH); | ||
} | ||
|
||
void Atmega328pHal::sleep() { | ||
LowPower.idle(SLEEP_15MS, ADC_OFF, TIMER2_ON, TIMER1_ON, TIMER0_ON, SPI_OFF, | ||
USART0_OFF, TWI_OFF); | ||
} | ||
|
||
void Atmega328pHal::setGun(Gun *gun) { gun = gun; } | ||
|
||
void Atmega328pHal::configureInputCallbacks() { | ||
|
||
pinMode(BUTTON2_PIN, INPUT_PULLUP); | ||
attachInterrupt(digitalPinToInterrupt(BUTTON2_PIN), buttonInterruptHandler, | ||
CHANGE); | ||
|
||
pinMode(TRIGGER_PIN, INPUT_PULLUP); | ||
attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), triggerInterruptHandler, | ||
CHANGE); | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.