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

Add support for M5Stack Cardputer #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 1 addition & 2 deletions doomgeneric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LIBS+=-lm -lc -lX11
OBJDIR=build
OUTPUT=doomgeneric

SRC_DOOM = dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_xlib.o
SRC_DOOM = dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_xlib.o doomgeneric_m5card.o
OBJS += $(addprefix $(OBJDIR)/, $(SRC_DOOM))

all: $(OUTPUT)
Expand Down Expand Up @@ -51,4 +51,3 @@ $(OBJDIR)/%.o: %.c

print:
@echo OBJS: $(OBJS)

125 changes: 125 additions & 0 deletions doomgeneric/doomgeneric_m5card.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "doomkeys.h"
#include "m_argv.h"
#include "doomgeneric.h"

#include <M5Unified.h>
#include <M5Cardputer.h>
#include <M5GFX.h>

#define KEYQUEUE_SIZE 16

static unsigned short s_KeyQueue[KEYQUEUE_SIZE];
static unsigned int s_KeyQueueWriteIndex = 0;
static unsigned int s_KeyQueueReadIndex = 0;

static unsigned char convertToDoomKey(unsigned int key)
{
switch (key)
{
case M5Cardputer::Key::Enter:
return KEY_ENTER;
case M5Cardputer::Key::Escape:
return KEY_ESCAPE;
case M5Cardputer::Key::Left:
return KEY_LEFTARROW;
case M5Cardputer::Key::Right:
return KEY_RIGHTARROW;
case M5Cardputer::Key::Up:
return KEY_UPARROW;
case M5Cardputer::Key::Down:
return KEY_DOWNARROW;
case M5Cardputer::Key::Ctrl:
return KEY_FIRE;
case M5Cardputer::Key::Space:
return KEY_USE;
case M5Cardputer::Key::Shift:
return KEY_RSHIFT;
default:
return tolower(key);
}
}

static void addKeyToQueue(int pressed, unsigned int keyCode)
{
unsigned char key = convertToDoomKey(keyCode);
unsigned short keyData = (pressed << 8) | key;

s_KeyQueue[s_KeyQueueWriteIndex] = keyData;
s_KeyQueueWriteIndex++;
s_KeyQueueWriteIndex %= KEYQUEUE_SIZE;
}

static void handleKeyInput()
{
M5Cardputer::KeyEvent e;
while (M5Cardputer::pollEvent(e))
{
if (e.type == M5Cardputer::KeyEvent::KeyDown)
{
addKeyToQueue(1, e.key);
}
else if (e.type == M5Cardputer::KeyEvent::KeyUp)
{
addKeyToQueue(0, e.key);
}
}
}

void DG_Init()
{
M5.begin();
M5Cardputer::begin();
M5GFX::begin();
}

void DG_DrawFrame()
{
M5GFX::drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, DG_ScreenBuffer);
handleKeyInput();
}

void DG_SleepMs(uint32_t ms)
{
delay(ms);
}

uint32_t DG_GetTicksMs()
{
return millis();
}

int DG_GetKey(int *pressed, unsigned char *doomKey)
{
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex)
{
return 0;
}
else
{
unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex];
s_KeyQueueReadIndex++;
s_KeyQueueReadIndex %= KEYQUEUE_SIZE;

*pressed = keyData >> 8;
*doomKey = keyData & 0xFF;

return 1;
}
}

void DG_SetWindowTitle(const char *title)
{
// No window title to set for M5Stack Cardputer
}

int main(int argc, char **argv)
{
doomgeneric_Create(argc, argv);

for (;;)
{
doomgeneric_Tick();
}

return 0;
}
114 changes: 64 additions & 50 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// #include "doomgeneric.h"
#include "../doomgeneric/doomgeneric.h"
#include "../doomgeneric/m_argv.h"
#include "../doomgeneric/doomgeneric.h"
#include <M5Cardputer.h>
#include <M5Unified.h>
#include <M5GFX.h>
Expand All @@ -10,27 +12,70 @@ static unsigned short s_KeyQueue[KEYQUEUE_SIZE];
static unsigned int s_KeyQueueWriteIndex = 0;
static unsigned int s_KeyQueueReadIndex = 0;

#ifdef __cplusplus
extern "C"
static unsigned char convertToDoomKey(unsigned int key)
{
switch (key)
{
case M5Cardputer::Key::Enter:
return KEY_ENTER;
case M5Cardputer::Key::Escape:
return KEY_ESCAPE;
case M5Cardputer::Key::Left:
return KEY_LEFTARROW;
case M5Cardputer::Key::Right:
return KEY_RIGHTARROW;
case M5Cardputer::Key::Up:
return KEY_UPARROW;
case M5Cardputer::Key::Down:
return KEY_DOWNARROW;
case M5Cardputer::Key::Ctrl:
return KEY_FIRE;
case M5Cardputer::Key::Space:
return KEY_USE;
case M5Cardputer::Key::Shift:
return KEY_RSHIFT;
default:
return tolower(key);
}
}

static void addKeyToQueue(int pressed, unsigned int keyCode)
{
#endif
unsigned char key = convertToDoomKey(keyCode);
unsigned short keyData = (pressed << 8) | key;

void doomgeneric_Create(int argc, char **argv);
void doomgeneric_Tick();
s_KeyQueue[s_KeyQueueWriteIndex] = keyData;
s_KeyQueueWriteIndex++;
s_KeyQueueWriteIndex %= KEYQUEUE_SIZE;
}

#ifdef __cplusplus
static void handleKeyInput()
{
M5Cardputer::KeyEvent e;
while (M5Cardputer::pollEvent(e))
{
if (e.type == M5Cardputer::KeyEvent::KeyDown)
{
addKeyToQueue(1, e.key);
}
else if (e.type == M5Cardputer::KeyEvent::KeyUp)
{
addKeyToQueue(0, e.key);
}
}
}
#endif

void DG_Init()
{
M5.begin();
// Initialize M5Stack Cardputer
M5Cardputer::begin();
M5GFX::begin();
}

void DG_DrawFrame()
{
M5.Lcd.drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, (uint16_t *)DG_ScreenBuffer);
M5GFX::drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, DG_ScreenBuffer);
handleKeyInput();
}

void DG_SleepMs(uint32_t ms)
Expand All @@ -43,34 +88,10 @@ uint32_t DG_GetTicksMs()
return millis();
}

// int DG_GetKey(int* pressed, unsigned char* key) {
// *pressed = 0;
// *key = 0;

// if(M5.BtnA.wasPressed()) {
// *pressed = 1;
// *key = KEY_ENTER;
// return 1;
// }
// else if(M5.BtnB.wasPressed()) {
// *pressed = 1;
// *key = KEY_FIRE;
// return 1;
// }
// else if(M5.BtnC.wasPressed()) {
// *pressed = 1;
// *key = KEY_USE;
// return 1;
// }

// return 0;
// }

int DG_GetKey(int *pressed, unsigned char *doomKey)
{
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex)
{
// key queue is empty
return 0;
}
else
Expand All @@ -84,28 +105,21 @@ int DG_GetKey(int *pressed, unsigned char *doomKey)

return 1;
}

return 0;
}

void setup()
void DG_SetWindowTitle(const char *title)
{
doomgeneric_Create(0, nullptr);
// No window title to set for M5Stack Cardputer
}

void loop()
int main(int argc, char **argv)
{
doomgeneric_Tick();
}

// int main(int argc, char **argv)
// {
// doomgeneric_Create(argc, argv);
doomgeneric_Create(argc, argv);

// while (1)
// {
// doomgeneric_Tick();
// }
for (;;)
{
doomgeneric_Tick();
}

// return 0;
// }
return 0;
}
Loading