Skip to content

Commit f3c60bd

Browse files
committed
wiimote: add example to read wiimote data
1 parent 87aaa55 commit f3c60bd

2 files changed

Lines changed: 228 additions & 0 deletions

File tree

devices/wiimote/Makefile

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#---------------------------------------------------------------------------------
2+
# Clear the implicit built in rules
3+
#---------------------------------------------------------------------------------
4+
.SUFFIXES:
5+
#---------------------------------------------------------------------------------
6+
ifeq ($(strip $(DEVKITPPC)),)
7+
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
8+
endif
9+
10+
include $(DEVKITPPC)/wii_rules
11+
12+
#---------------------------------------------------------------------------------
13+
# TARGET is the name of the output
14+
# BUILD is the directory where object files & intermediate files will be placed
15+
# SOURCES is a list of directories containing source code
16+
# INCLUDES is a list of directories containing extra header files
17+
#---------------------------------------------------------------------------------
18+
TARGET := $(notdir $(CURDIR))
19+
BUILD := build
20+
SOURCES := source
21+
DATA := data
22+
INCLUDES :=
23+
24+
#---------------------------------------------------------------------------------
25+
# options for code generation
26+
#---------------------------------------------------------------------------------
27+
28+
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
29+
CXXFLAGS = $(CFLAGS)
30+
31+
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
32+
33+
#---------------------------------------------------------------------------------
34+
# any extra libraries we wish to link with the project
35+
#---------------------------------------------------------------------------------
36+
LIBS := -lwiiuse -lbte -logc -lm
37+
38+
#---------------------------------------------------------------------------------
39+
# list of directories containing libraries, this must be the top level containing
40+
# include and lib
41+
#---------------------------------------------------------------------------------
42+
LIBDIRS :=
43+
44+
#---------------------------------------------------------------------------------
45+
# no real need to edit anything past this point unless you need to add additional
46+
# rules for different file extensions
47+
#---------------------------------------------------------------------------------
48+
ifneq ($(BUILD),$(notdir $(CURDIR)))
49+
#---------------------------------------------------------------------------------
50+
51+
export OUTPUT := $(CURDIR)/$(TARGET)
52+
53+
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
54+
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
55+
56+
export DEPSDIR := $(CURDIR)/$(BUILD)
57+
58+
#---------------------------------------------------------------------------------
59+
# automatically build a list of object files for our project
60+
#---------------------------------------------------------------------------------
61+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
62+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
63+
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
64+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
65+
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
66+
67+
#---------------------------------------------------------------------------------
68+
# use CXX for linking C++ projects, CC for standard C
69+
#---------------------------------------------------------------------------------
70+
ifeq ($(strip $(CPPFILES)),)
71+
export LD := $(CC)
72+
else
73+
export LD := $(CXX)
74+
endif
75+
76+
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
77+
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
78+
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
79+
80+
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
81+
82+
#---------------------------------------------------------------------------------
83+
# build a list of include paths
84+
#---------------------------------------------------------------------------------
85+
export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
86+
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
87+
-I$(CURDIR)/$(BUILD) \
88+
-I$(LIBOGC_INC)
89+
90+
#---------------------------------------------------------------------------------
91+
# build a list of library paths
92+
#---------------------------------------------------------------------------------
93+
export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
94+
95+
export OUTPUT := $(CURDIR)/$(TARGET)
96+
.PHONY: $(BUILD) clean
97+
98+
#---------------------------------------------------------------------------------
99+
$(BUILD):
100+
@[ -d $@ ] || mkdir -p $@
101+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
102+
103+
#---------------------------------------------------------------------------------
104+
clean:
105+
@echo clean ...
106+
@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
107+
108+
#---------------------------------------------------------------------------------
109+
run:
110+
wiiload $(TARGET).dol
111+
112+
113+
#---------------------------------------------------------------------------------
114+
else
115+
116+
DEPENDS := $(OFILES:.o=.d)
117+
118+
#---------------------------------------------------------------------------------
119+
# main targets
120+
#---------------------------------------------------------------------------------
121+
$(OUTPUT).dol: $(OUTPUT).elf
122+
$(OUTPUT).elf: $(OFILES)
123+
124+
$(OFILES_SOURCES) : $(HFILES)
125+
126+
#---------------------------------------------------------------------------------
127+
# This rule links in binary data with the .jpg extension
128+
#---------------------------------------------------------------------------------
129+
%.jpg.o %_jpg.h : %.jpg
130+
#---------------------------------------------------------------------------------
131+
@echo $(notdir $<)
132+
$(bin2o)
133+
134+
-include $(DEPENDS)
135+
136+
#---------------------------------------------------------------------------------
137+
endif
138+
#---------------------------------------------------------------------------------

devices/wiimote/source/wiimote.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <gccore.h>
4+
#include <wiiuse/wpad.h>
5+
6+
static void *xfb = NULL;
7+
static GXRModeObj *rmode = NULL;
8+
9+
//---------------------------------------------------------------------------------
10+
int main(int argc, char **argv) {
11+
//---------------------------------------------------------------------------------
12+
13+
// Initialise the video system
14+
VIDEO_Init();
15+
16+
// Obtain the preferred video mode from the system
17+
// This will correspond to the settings in the Wii menu
18+
rmode = VIDEO_GetPreferredMode(NULL);
19+
20+
// Allocate memory for the display in the uncached region
21+
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
22+
23+
// Initialise the console, required for printf
24+
CON_Init(xfb, 20, 20, rmode->fbWidth-20, rmode->xfbHeight-20, rmode->fbWidth*VI_DISPLAY_PIX_SZ);
25+
//SYS_STDIO_Report(true);
26+
27+
// Set up the video registers with the chosen mode
28+
VIDEO_Configure(rmode);
29+
30+
// Tell the video hardware where our display memory is
31+
VIDEO_SetNextFramebuffer(xfb);
32+
33+
// Clear the framebuffer
34+
VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
35+
36+
// Make the display visible
37+
VIDEO_SetBlack(false);
38+
39+
// Flush the video register changes to the hardware
40+
VIDEO_Flush();
41+
42+
// Wait for Video setup to complete
43+
VIDEO_WaitVSync();
44+
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
45+
46+
// This function initialises the attached controllers
47+
WPAD_Init();
48+
49+
//tell wpad to output the IR data
50+
WPAD_SetDataFormat(WPAD_CHAN_ALL, WPAD_FMT_BTNS_ACC_IR);
51+
52+
//set console starting position to the second row, to make space for tv's offsets
53+
printf("\x1b[2;0H");
54+
55+
printf("Hello World!\n");
56+
printf("Connect a wiimote by pressing a button\n");
57+
printf("Or press the red sync button on the wii together with the wiimote!\n");
58+
59+
while(1)
60+
{
61+
//reset console location to 5th row
62+
printf("\x1b[5;0H");
63+
64+
// Call WPAD_ScanPads each loop, this reads the latest controller states
65+
WPAD_ScanPads();
66+
67+
for (int i = 0; i <= 3; i++)
68+
{
69+
//clear line and print the wiimote's data
70+
printf("\33[2K\r");
71+
WPADData* data = WPAD_Data(i);
72+
if(data->data_present)
73+
printf("wiimote %d: x -> %f y-> %f angle -> %f\n", i, data->ir.x, data->ir.y, data->ir.angle);
74+
else
75+
printf("wiimote %d: Disconnected\n", i);
76+
}
77+
78+
// WPAD_ButtonsDown tells us which buttons were pressed in this loop
79+
// this is a "one shot" state which will not fire again until the button has been released
80+
u32 pressed = WPAD_ButtonsDown(0) | WPAD_ButtonsDown(1) | WPAD_ButtonsDown(2) | WPAD_ButtonsDown(3);
81+
82+
// We return to the launcher application via exit
83+
if ( pressed & WPAD_BUTTON_HOME ) exit(0);
84+
85+
// Wait for the next frame
86+
VIDEO_WaitVSync();
87+
}
88+
89+
return 0;
90+
}

0 commit comments

Comments
 (0)