Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ jobs:
run: pip install --upgrade platformio

- name: Test project in native environment
run: pio test -e native
run: pio test -vv -e native

- name: Build console application
- name: Build native console application
run: pio run -e native

- name: Build 'target' cross application
run: pio run -e target

- name: Build 'gun' cross application
- name: Build cross Gun application
run: pio run -e gun

- name: Build cross Target application
run: pio run -e target

- name: Archive test console application
uses: actions/upload-artifact@v3
with:
Expand Down
103 changes: 103 additions & 0 deletions lib/Cross/Gun/Atmega328pHal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
*
* 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/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 BUTTON_PIN 3
#define TRIGGER_PIN 2

#define MIN_BAT_VOLTAGE 3000
#define MAX_BAT_VOLTAGE 4120

Atmega328pHal::Atmega328pHal() {
pinMode(VIBRATOR_PIN, OUTPUT);
pinMode(LASER_PIN, OUTPUT);
pinMode(TRIGGER_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

bool Atmega328pHal::triggerIsUp() { return bit_is_set(PIND, PD2); }
bool Atmega328pHal::buttonIsUp() { return bit_is_clear(PIND, PD3); }

/*
* 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
TIMSK2 = (1U << OCIE2A); // enable compare Interrupt
ASSR &= ~(1U << AS2);
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_OFF, TIMER0_OFF, SPI_OFF,
USART0_OFF, TWI_OFF);
}

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

extern void buttonInterruptHandler();
extern void triggerInterruptHandler();

void Atmega328pHal::configureInputCallbacks() {
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonInterruptHandler,
CHANGE);
attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), triggerInterruptHandler,
CHANGE);
}
49 changes: 49 additions & 0 deletions lib/Cross/Gun/Atmega328pHal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* 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 <Gun/IGunHal.hpp>

class Atmega328pHal : public IGunHal {
Gun *gun;

public:
~Atmega328pHal() {}

Atmega328pHal();

/*
* 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;

void setGun(Gun *gun) override;
void laserOn() override;
void laserOff() override;
void vibrationOn() override;
void vibrationOff() override;
bool triggerIsUp() override;
bool buttonIsUp() override;
uint16_t getBatteryVoltageMv() override;
uint8_t getBatteryVoltagePercent() override;
bool isCharging() override;
void configureInputCallbacks() override;
void sleep() override;
};
108 changes: 108 additions & 0 deletions lib/Cross/Gun/SSD1306Ui.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
*
* 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 <Gun/IGunUi.hpp>

#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeMonoOblique9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// 0x3D for 128x64, 0x3C for 128x32
#define SCREEN_ADDRESS 0x3C

static Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

/**
* @brief UI on a SSD1306 OLED display wired over I²C bus
*
* Used https://rickkas7.github.io/DisplayGenerator/index.html for prototyping
*/
class SSD1306Ui : public IGunUi {
public:
SSD1306Ui() {}

void setup() override {
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
for (;;)
; // Don't proceed, loop forever
}
}

void displaySplash(uint16_t timeoutMs) override {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setFont(&FreeMonoOblique9pt7b);
display.setCursor(9, 11);
display.println("5 in a row");
display.setCursor(4, 27);
display.println(" # # # # #");
display.display();
delay(timeoutMs);
display.clearDisplay();
display.display();
}

void displayBatteryStatus(uint16_t mv, uint8_t percent) override {
display.setFont(NULL);
display.fillRect(100, 0, 25, 9, 0); // clear
display.setCursor(100, 2);
display.print(percent);
display.println("%");
}

void displayChargingStatus(bool isCharging) override {
if (isCharging) {
display.setFont(NULL);
display.setCursor(80, 15);
display.println("charging");
} else {
display.fillRect(80, 12, 47, 11, 0); // clear
}
}

void displayShootCount(uint16_t count) override {
display.fillRect(11, 0, 68, 30, 0); // clear
display.setFont(&FreeMonoBold18pt7b);
if (count > 999) {
count = 0;
}
if (count < 10) {
display.setCursor(15, 25);
} else {
display.setCursor(10, 25);
}
display.print(count);
display.display();
}

void clearCalibration() override {
display.fillRect(2, 10, 126, 22, 0); // clear
}

void displayCalibration() override {
display.clearDisplay();
display.setFont(&FreeMonoOblique9pt7b);
display.setCursor(2, 25);
display.print("Calibration");
display.display();
}
};
50 changes: 50 additions & 0 deletions lib/Domain/Contactor/Contactor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* 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/Contactor.hpp>

void Contactor::checkForLongPress(long now) {
if ((downStartTime > 0) && ((now - downStartTime) >= LONG_PRESS)) {
downStartTime = 0;
onLongPress();
inALongPress = true;
}
}
void Contactor::onDown(long now) {
currentState = State::Down;
downStartTime = now;
}

void Contactor::onUp(long now) {
currentState = State::Up;
checkForLongPress(now);
if (!inALongPress) {
onShortPress();
}
inALongPress = false;
downStartTime = 0;
}

void Contactor::processPendingEvent(long now) {
Event toProcess = pendingEvent;
pendingEvent = Event::NoEvent;
if (toProcess == Event::Released) {
onUp(now);
} else if (toProcess == Event::Pressed) {
onDown(now);
}
}
53 changes: 53 additions & 0 deletions lib/Domain/Contactor/Contactor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* 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

/**
* Designed to manage hardware buttons and their different behaviour:
* - short press
* - long press
*/
class Contactor {

static const int LONG_PRESS = 2000;
long downStartTime = 0;
bool inALongPress = false;

public:
enum Event { NoEvent, Pressed, Released };
enum State { Up, Down };

/**
* @brief Event can be detected in an interrupt, and processed later
*
*/
Event pendingEvent;

State currentState;

Contactor() : pendingEvent(Event::NoEvent), currentState(State::Up) {}

virtual void checkForLongPress(long now);
virtual void onDown(long now);
virtual void onUp(long now);
virtual void processPendingEvent(long now);

virtual ~Contactor() {}

virtual void onShortPress(){};
virtual void onLongPress(){};
};
Loading