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 83a63c8 commit b30a55a
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 247 deletions.
119 changes: 119 additions & 0 deletions lib/Domain/Gun/Atmega328pHal.cpp
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);
}
63 changes: 14 additions & 49 deletions lib/Domain/Gun/Atmega328pHal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,35 @@
#pragma once

#include <Gun/IGunHal.hpp>
#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 MIN_BAT_VOLTAGE 3000
#define MAX_BAT_VOLTAGE 4120

ISR(TIMER2_COMPA_vect) {}

class Atmega328pHal : public IGunHal {
Gun *gun;

public:
~Atmega328pHal() {}

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 setupHeartbeat() override {
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 laserOn() override { PORTB |= (1 << PB2); }
void laserOff() override { PORTB &= ~(1 << PB2); }
void vibrationOn() override { PORTD |= (1 << PD6); }
void vibrationOff() override { PORTD &= ~(1 << PD6); }
void setupHeartbeat() override;

uint16_t getBatteryVoltageMv() override {
void setGun(Gun *gun) override;
void laserOn() override;
void laserOff() override;
void vibrationOn() override;
void vibrationOff() override;

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;
uint16_t getBatteryVoltageMv() override;

return battMv;
};
uint8_t getBatteryVoltagePercent() override;
bool isCharging() override;

uint8_t getBatteryVoltagePercent() override {
return map(getBatteryVoltageMv(), MIN_BAT_VOLTAGE, MAX_BAT_VOLTAGE, 0, 100);
}
void configureInputCallbacks() override;

virtual bool isCharging() override {
return (digitalRead(CHARGING_STATE_PIN) == HIGH);
}
void sleep() override;
};
18 changes: 0 additions & 18 deletions lib/Domain/Gun/Button.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions lib/Domain/Gun/Button.hpp

This file was deleted.

Loading

0 comments on commit b30a55a

Please sign in to comment.