Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New implementation of Gun application #16

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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
run: pio run -e native
Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
# Five In A Row

Set of two applications targetting Atmel/Microchip [ATMega328P](https://www.microchip.com/en-us/product/atmega328p), implementing shooting game.
Set of applications targetting Atmel/Microchip [ATMega328P](https://www.microchip.com/en-us/product/atmega328p), implementing shooting games.

Using a *gun*, users tries to hit *targets*.
Using a _gun_, users tries to hit _targets_ managed by a _target host_.
The game is up to four players: yellow, green, red and blue.
There are 5 targets.

### Game mode

#### Biathlon like
Each player turn gives 5 shots. Player tries to hit each target. Total number of shoots is counted, as well as successfull hits.
Various games will be implemented.

#### Simple shooter

Each player shoot until the five targets are hit. Number of shoots is counted on the gun. Successful hits are counted by the target host and sent to the remote application.

## Functional details
### target

The **target** application manages player points and turns. It communicates with an Android application implemented on top of [Bluetooth Electronics](https://www.keuwl.com/apps/bluetoothelectronics/) application. It manages the game logic and player points.
### target

The **target host** application manages player points and turns. It communicates with an Android application implemented on top of [Bluetooth Electronics](https://www.keuwl.com/apps/bluetoothelectronics/) application. It manages the game logic and player points. Various games and modes can be implemented for the host. Each of these could have a specific remote UI.

### Gun

The **Gun** application manages the gun. Using two buttons, it allows:

| Press | Button 1 | Button 2 |
|-------|------------------|--------------------------------|
| short | shoot | Begin new round (give 5 shots) |
| long | power off | continuous laser (calibration) |
| Press | Button 1 | Button 2 |
| ----- | -------- | ------------------------------ |
| short | shoot | reset shoot count |
| long | N/A | continuous laser (calibration) |

Gun application manages following outputs:
* Laser
* vibrator
* 128x32 I²C display

- Laser
- vibrator
- 128x32 I²C display

## Architecture details

Some parts of the projet, implementing logic, can be used on both native and target platform.
All that code is represented in the "domain" package below.

Both **target** and **Gun** application are run both native and cross environment. The current
target hardware for both is the [ATMega328p](https://www.microchip.com/en-us/product/atmega328p) chip from Atmel/Microchip.
Current target hardware for all applications is the [ATMega328p](https://www.microchip.com/en-us/product/atmega328p) chip from Atmel/Microchip.
To facilitate testing and extension, user interface (Gui) and hardware abstraction (HAL) are provided using interfaces. Implementation exists for native and cross (i.e. testing) environments.

Note: The *BTEGui* implementation can be used in native environment. Based on plain string exchange, if it may be difficult to interpret.
Note: The _BTEGui_ implementation can be used in native environment. Based on plain string exchange, if it may be difficult to interpret.

![Architecture overview](architecture.png)
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.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();
}
};
2 changes: 1 addition & 1 deletion lib/Domain/BTEGui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BTEGui : public ITargetGui {
BTEGui() : targetState(0) {}
#else
std::ostream &out;
BTEGui(std::ostream &out) : targetState(0), out(out) {}
explicit BTEGui(std::ostream &out) : targetState(0), out(out) {}
#endif

void setCurrentPlayer(uint8_t playerId) override;
Expand Down
Loading