Skip to content

Commit 9247b5a

Browse files
committed
first commit
0 parents  commit 9247b5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2389
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
*.lss
3+
*.d
4+
*.o

.vscode/c_cpp_properties.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/libs/**",
7+
"${workspaceFolder}/include/**"
8+
],
9+
"defines": [
10+
"_DEBUG",
11+
"UNICODE",
12+
"_UNICODE",
13+
"__AVR_ATmega328P__",
14+
"ARDUINO=10816",
15+
"ARDUINO_AVR_NANO",
16+
"ARDUINO_ARCH_AVR",
17+
"F_CPU=16000000L",
18+
"BAUD=9600L",
19+
"ADC_CHANNEL=7"
20+
],
21+
"compilerPath": "C:\\msys64\\mingw64\\bin\\avr-gcc.exe",
22+
"intelliSenseMode": "gcc-x86",
23+
"browse": {
24+
"path": [
25+
//"${workspaceFolder}"
26+
]
27+
},
28+
"configurationProvider":"ms-vscode.cmake-tools"
29+
}
30+
],
31+
"version": 4
32+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"C_Cpp.clang_format_style": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0, DerivePointerAlignment: false, PointerAlignment: Left, MaxEmptyLinesToKeep: 2 }",
3+
}

Makefile

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# $^ names of all prerequisites
2+
# $< name of first prerequisite
3+
# $@ name of target of the rule
4+
5+
# ** Build for Arduino NANO **
6+
# > make build MCU=NANO
7+
# > make flash COMPORT=COM5
8+
9+
# ** Build for Arduino UNO **
10+
# > make build MCU=UNO
11+
# > make flash COMPORT=COM5
12+
13+
# ** Clean Build **
14+
# > make clean
15+
16+
17+
COMPORT ?= COM8
18+
MCU ?= NANO
19+
20+
PROJDIR := $(realpath $(CURDIR)/..)
21+
MAIN_APP = DuckHunter
22+
MAIN_HEX = $(BUILD_DIR)/$(MAIN_APP).hex
23+
MAIN_ELF = $(BUILD_DIR)/$(MAIN_APP).elf
24+
ATMEGA328P_LIB = $(BUILD_DIR)/libs/libatmega328p.a
25+
MMCU_TYPE = atmega328p
26+
BAUDRATE ?= 57600
27+
28+
29+
ifeq ($(MCU), NANO)
30+
ADC_CHANNEL = -DADC_CHANNEL=7
31+
BAUDRATE = 57600
32+
endif
33+
34+
ifeq ($(MCU), UNO)
35+
ADC_CHANNEL = -DADC_CHANNEL=0
36+
BAUDRATE ?= 115200
37+
endif
38+
39+
ifeq ($(MCU), MINI)
40+
ADC_CHANNEL = -DADC_CHANNEL=7
41+
BAUDRATE = 57600
42+
endif
43+
44+
45+
CXX = avr-g++
46+
CC = avr-gcc
47+
LINKER = avr-g++
48+
OBJCOPY = avr-objcopy
49+
OBJDUMP = avr-objdump
50+
AVRDUDE = avrdude
51+
AVR_SIZE = avr-size
52+
53+
# Enable automatic removal of unused code
54+
FLAGS = -ffunction-sections -fdata-sections -flto
55+
56+
# Options for avr-g++
57+
CPPFLAGS = -c -Os -fno-exceptions -fshort-enums -std=gnu++14
58+
CPPFLAGS += $(ADC_CHANNEL) -D__AVR_ATmega328P__ -DF_CPU=16000000L
59+
CPPFLAGS += -Wall
60+
CPPFLAGS += -Wextra -Wpedantic
61+
CPPFLAGS += -Werror
62+
CPPFLAGS += -fmessage-length=0 -Wno-sign-compare -Wno-unused-parameter
63+
CPPFLAGS += -Wno-unused-variable -Wno-unused-but-set-variable
64+
CPPFLAGS += -mmcu=$(MMCU_TYPE) -o
65+
66+
# ---------------- Library Options ----------------
67+
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
68+
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt -lm
69+
# If this is left blank, then it will use the Standard printf version.
70+
PRINTF_LIB =
71+
# PRINTF_LIB = $(PRINTF_LIB_MIN)
72+
# PRINTF_LIB = $(PRINTF_LIB_FLOAT)
73+
74+
# Linking options for avr-gcc
75+
LFLAGS = -Os -flto -fshort-enums
76+
LFLAGS += -Wl,--gc-sections -Wl,-Map=$(BUILD_DIR)/$(MAIN_APP).map
77+
LFLAGS += -mmcu=$(MMCU_TYPE)
78+
LFLAGS += $(PRINTF_LIB)
79+
80+
# Options for HEX file generation
81+
HFLAGS = -j .text -j .data -R .eeprom -O ihex
82+
83+
# Options for avrdude to burn the hex file
84+
AVRDUDE_VERBOSE = -v -v
85+
AVRDUDE_NO_VERIFY = -V
86+
DUDEFLAGS += -c arduino
87+
DUDEFLAGS += -p m328p -P $(COMPORT)
88+
DUDEFLAGS += $(AVRDUDE_VERBOSE) $(AVRDUDE_NO_VERIFY)
89+
DUDEFLAGS += -b $(BAUDRATE)
90+
91+
# Add libraries
92+
LIB_BASE_DIR = libs
93+
LIB_DIRS = $(LIB_BASE_DIR)/I2C $(LIB_BASE_DIR)/LCD_I2C_Driver $(LIB_BASE_DIR)/Timer
94+
LIB_DIRS += $(LIB_BASE_DIR)/TM1637 $(LIB_BASE_DIR)/ShiftRegister
95+
LIB_DIRS += $(LIB_BASE_DIR)/UART $(LIB_BASE_DIR)/ADC
96+
LIB_INCLUDES = $(foreach dir,$(LIB_DIRS),$(addprefix -I, $(dir)))
97+
98+
INCLUDE_DIRS = include include/Components
99+
SOURCE_DIRS = src src/Components
100+
SOURCE_DIR = src
101+
BUILD_DIR = build
102+
103+
INCLUDES = $(foreach dir,$(INCLUDE_DIRS),$(addprefix -I, $(dir)))
104+
105+
BUILD_DIRS = $(subst $(SOURCE_DIR),$(BUILD_DIR)/$(SOURCE_DIR),$(SOURCE_DIRS))
106+
BUILD_DIRS += $(subst $(LIB_BASE_DIR),$(BUILD_DIR)/$(LIB_BASE_DIR),$(LIB_DIRS))
107+
108+
# Source files needed for building the application
109+
SRC_FILES = $(foreach dir,$(SOURCE_DIRS),$(wildcard $(dir)/*.cpp))
110+
OBJ_FILES = $(subst $(SOURCE_DIR),$(BUILD_DIR)/$(SOURCE_DIR),$(SRC_FILES:.cpp=.o))
111+
112+
LIB_SRC_FILES = $(foreach dir,$(LIB_DIRS),$(wildcard $(dir)/*.cpp))
113+
LIB_OBJ_FILES = $(subst $(LIB_BASE_DIR),$(BUILD_DIR)/$(LIB_BASE_DIR),$(LIB_SRC_FILES:.cpp=.o))
114+
115+
116+
all: INFOTEXT build flash
117+
118+
build: INFOTEXT $(MAIN_ELF) list
119+
120+
flash: $(MAIN_HEX)
121+
@echo flashing $@ file
122+
$(AVRDUDE) $(DUDEFLAGS) -D -U flash:w:$(MAIN_HEX):i
123+
124+
list: $(MAIN_ELF)
125+
$(OBJDUMP) -h -S -z $< > $(BUILD_DIR)/list.lss
126+
$(OBJDUMP) -h $< > $(BUILD_DIR)/sections_summary.txt
127+
$(OBJDUMP) -D $< > $(BUILD_DIR)/disassembly_all.d
128+
# $(OBJDUMP) -d $< > $(BUILD_DIR)/disassembly_text_section.d
129+
130+
lib: $(ATMEGA328P_LIB)
131+
@echo Testing Library $<
132+
avr-ar -t $<
133+
134+
$(ATMEGA328P_LIB): $(LIB_OBJ_FILES)
135+
@echo Building... $@
136+
avr-ar rcs $(ATMEGA328P_LIB) $(LIB_OBJ_FILES)
137+
138+
# Rule for Building .hex flashable file
139+
$(MAIN_HEX): $(MAIN_ELF)
140+
@echo Generating flashable $(MAIN_HEX) file
141+
$(OBJCOPY) $(HFLAGS) $< $(MAIN_HEX)
142+
143+
# Rule for Building .elf file
144+
$(MAIN_ELF): $(OBJ_FILES) $(ATMEGA328P_LIB)
145+
@echo Linking $@ file
146+
$(LINKER) $(LFLAGS) $^ --output $@
147+
$(AVR_SIZE) -A $(MAIN_ELF)
148+
149+
# Rule for Compiling .cpp files with header file
150+
$(BUILD_DIR)/%.o: %.cpp %.h
151+
@echo Building... $@ from $^
152+
$(CXX) $(INCLUDES) $(LIB_INCLUDES) $(FLAGS) $(CPPFLAGS) $@ $<
153+
154+
# Rule for Compiling .cpp files without header file
155+
$(BUILD_DIR)/%.o: %.cpp
156+
@echo Building... $@ from $^
157+
$(CXX) $(INCLUDES) $(LIB_INCLUDES) $(FLAGS) $(CPPFLAGS) $@ $<
158+
159+
160+
.PHONY: clean INFOTEXT
161+
162+
# Create Build Folders
163+
$(foreach dir, $(BUILD_DIRS), $(shell mkdir -p $(dir) 2>/dev/null))
164+
165+
clean:
166+
rm -rf $(BUILD_DIR)/*.elf
167+
rm -rf $(BUILD_DIR)/*.hex
168+
rm -rf $(OBJ_FILES)
169+
rm -rf $(LIB_OBJ_FILES)
170+
rm -rf $(BUILD_DIR)/.dep/*
171+
rm -rf $(BUILD_DIR)/libs/*.a
172+
rm -rf $(BUILD_DIR)
173+
174+
175+
INFOTEXT:
176+
@echo ====== BOARD: $(MCU) =======
177+
@echo ProjectDir:: $(PROJDIR)
178+
@echo CURRENT PROJECT: $(CURDIR)
179+
@echo Source Files: $(SRC_FILES)
180+
@echo Object Files: $(OBJ_FILES)
181+
@echo Lib Object Files: $(LIB_OBJ_FILES)
182+
@echo Build Dirs: $(BUILD_DIRS)
183+
@echo Include Folders: $(INCLUDES)

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Duck Hunter
2+
3+
Low cost electronics game project for children using AVR bare metal programming in C++.
4+
5+
## Build Instructions
6+
7+
### Build for Arduino Nano (ATmega328P)
8+
```shell
9+
make build
10+
```
11+
12+
### Flash
13+
```shell
14+
make flash COMPORT=COM8
15+
```
16+
17+
### Clean build
18+
```shell
19+
make clean
20+
```
21+
22+
## Features
23+
24+
+ HD44780 16x2 LCD Modul Display an alien hunting a duck
25+
+ TM1637 4 Digit 7-Segment Display to display the game score
26+
+ Lots of blinking LEDs
27+
+ Potentiometer to control move direction of the hunting alien
28+
29+
30+
## Schematics
31+
32+
[DuckHunter.pdf](docs/DuckHunter.pdf)
33+
34+
![DuckHunter.png](docs/DuckHunter.png?raw=true)
35+
36+
# Pictures
37+
38+
![DSC01115_2.jpg](docs/pics/DSC01115_2.jpg)
39+
![DSC01116_2.jpg](docs/pics/DSC01116_2.jpg)
40+
![DSC01118_2.jpg](docs/pics/DSC01118_2.jpg)
41+
![DSC01119_2.jpg](docs/pics/DSC01119_2.jpg)

docs/DuckHunter.pdf

94.8 KB
Binary file not shown.

docs/DuckHunter.png

384 KB
Loading

docs/pics/DSC01115_2.jpg

522 KB
Loading

docs/pics/DSC01116_2.jpg

270 KB
Loading

docs/pics/DSC01118_2.jpg

263 KB
Loading

docs/pics/DSC01119_2.jpg

259 KB
Loading

include/Components/Alien.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#ifndef ALIEN_H_
3+
#define ALIEN_H_
4+
5+
#include "Entity.h"
6+
#include "GameStates.h"
7+
#include "GraphicsComponent.h"
8+
#define INIT_SPEED (50U)
9+
10+
typedef enum {
11+
PAUSED = 0,
12+
MOVING_LEFT = 1,
13+
MOVING_RIGHT = 2
14+
} move_dir_t;
15+
16+
class Alien : public Entity {
17+
public:
18+
uint8_t spd_ticks_per_step = INIT_SPEED; // ticks needed to make one step
19+
20+
Alien();
21+
void update() override;
22+
void setState(move_dir_t move_direction);
23+
move_dir_t getState();
24+
25+
private:
26+
move_dir_t st_move_direction{PAUSED};
27+
28+
};
29+
30+
#endif

include/Components/Duck.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#ifndef DUCK_H_
3+
#define DUCK_H_
4+
5+
#include <stdint.h>
6+
7+
#include "Entity.h"
8+
9+
class Duck : public Entity {
10+
public:
11+
Duck();
12+
void update() override;
13+
};
14+
15+
16+
#endif

include/Components/Entity.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef ENTITY_H_
2+
#define ENTITY_H_
3+
4+
#include <stdint.h>
5+
6+
typedef struct _Position {
7+
uint8_t row; // 0 or 1
8+
uint8_t column; // 0...15
9+
} Position;
10+
11+
bool operator==(Position lhs, Position rhs);
12+
13+
class Entity {
14+
public:
15+
bool render_needed = true;
16+
uint8_t figure_id = 0x20; // empty figure
17+
18+
Entity();
19+
virtual ~Entity() {}
20+
virtual void update() = 0;
21+
void setPosition(uint8_t _row, uint8_t _column);
22+
void setPosition(Position _position);
23+
void incrementPosition();
24+
void decrementPosition();
25+
Position getPosition() { return position; };
26+
Position getPrevPosition() { return prev_position; };
27+
28+
private:
29+
Position position{0, 5};
30+
Position prev_position{0, 5};
31+
};
32+
33+
#endif

include/Components/Game.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef GAME_H_
2+
#define GAME_H_
3+
4+
#include <stdint.h>
5+
6+
#include "Entity.h"
7+
#include "GraphicsComponent.h"
8+
#include "InputComponent.h"
9+
#include "PhysicsComponent.h"
10+
11+
class Game {
12+
public:
13+
static const uint8_t ALIEN_ID;
14+
static const uint8_t DUCK_ID;
15+
16+
Game(InputComponent* _input,
17+
GraphicsComponent* _graphics,
18+
PhysicsComponent* _physics);
19+
void init();
20+
void update();
21+
void increment_score();
22+
23+
private:
24+
InputComponent* input;
25+
GraphicsComponent* graphics;
26+
PhysicsComponent* physics;
27+
uint8_t score{0};
28+
static const uint8_t NUM_ENTITIES = 2;
29+
Entity* entities[NUM_ENTITIES];
30+
};
31+
32+
33+
#endif

0 commit comments

Comments
 (0)