Skip to content

Commit

Permalink
Daniel/wasm (#11)
Browse files Browse the repository at this point in the history
* wasm changes
* fixed up linux build
  • Loading branch information
Unreal-Dan authored Feb 5, 2023
1 parent 75425b3 commit 9def1a3
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "tinyNeoPixel.h"
6 changes: 5 additions & 1 deletion VortexTestingFramework/LinuxMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ int main(int argc, char *argv[])
{
TestFramework framework;
framework.init();
framework.run();
#ifndef WASM
while (framework.stillRunning()) {
framework.run();
}
#endif
return 0;
}
20 changes: 14 additions & 6 deletions VortexTestingFramework/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SUFFIXES:

# List all make targets which are not filenames
.PHONY: all tests clean
.PHONY: all tests clean wasm

# compiler tool definitions
CC=g++
Expand Down Expand Up @@ -77,7 +77,7 @@ TESTS=\

# target files
TARGETS=\
vortex_framework \
vortex \

# Default target for 'make' command
all: $(TARGETS)
Expand All @@ -86,11 +86,19 @@ all: $(TARGETS)
tests: $(TESTS)

# target for detect_gibberish
vortex_framework: $(DEPS)
vortex: $(DEPS)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)
@rm -f *.txt

valgrind: vortex_framework
vortex.html: $(DEPS)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)

wasm: CC=em++
wasm: DEFINES += -D WASM
wasm: INCLUDES += -I ../../emcc/emsdk/upstream/emscripten/cache/sysroot/include/
wasm: $(DEPS) vortex.html
tar -zcvf vortex.tar.gz vortex.html vortex.js vortex.wasm

valgrind: vortex
rm -f valg.out
valgrind \
-v \
Expand All @@ -99,7 +107,7 @@ valgrind: vortex_framework
--track-origins=yes \
--gen-suppressions=all \
--log-file=valg.out \
./vortex_framework
./vortex
cat valg.out

# catch-all make target to generate .o and .d files
Expand Down
92 changes: 80 additions & 12 deletions VortexTestingFramework/TestFrameworkLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include <ctime>

#include <stdio.h>

#include "TestFrameworkLinux.h"
#include "Arduino.h"

Expand All @@ -25,10 +27,47 @@

TestFramework *g_pTestFramework = nullptr;

FILE *m_logHandle = nullptr;

using namespace std;

#ifdef WASM // Web assembly glue
#include <emscripten/html5.h>
#include <emscripten.h>

#include <queue>

static queue<char> keyQueue;

static EM_BOOL key_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData)
{
switch (e->key[0]) {
case 'a':
case 's':
case 'd':
case 'q':
keyQueue.push(e->key[0]);
break;
default:
break;
}
return 0;
}

static void do_run()
{
g_pTestFramework->run();
}

static void wasm_init()
{
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, key_callback);
//emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, key_callback);
emscripten_set_main_loop(do_run, 0, true);
}
#endif // ifdef WASM

TestFramework::TestFramework() :
m_logHandle(NULL),
m_ledList(nullptr),
m_numLeds(0),
m_initialized(false),
Expand Down Expand Up @@ -71,28 +110,32 @@ bool TestFramework::init()
// do the arduino init/setup
arduino_setup();
m_initialized = true;

#ifdef WASM
wasm_init();
#endif

return true;
}

unsigned long do_release = 0;
void TestFramework::run()
{
while (m_initialized && m_keepGoing) {
VortexEngine::tick();
g_pButton->m_longClick = false;
g_pButton->m_isPressed = false;
g_pButton->m_shortClick = false;
g_pButton->m_holdDuration = 0;
if (!stillRunning()) {
printf("Cleaning up...\n");
VortexEngine::cleanup();
return;
}

VortexEngine::cleanup();
VortexEngine::tick();
}

void TestFramework::cleanup()
{
DEBUG_LOG("Quitting...");
m_keepGoing = false;
m_isPaused = false;
#ifdef WASM
emscripten_force_exit(0);
#endif
}

void TestFramework::arduino_setup()
Expand All @@ -114,12 +157,18 @@ void TestFramework::show()

void TestFramework::pressButton()
{
if (m_buttonPressed) {
return;
}
printf("Press\r\n");
m_buttonPressed = true;
}

void TestFramework::releaseButton()
{
if (!m_buttonPressed) {
return;
}
printf("Release\r\n");
m_buttonPressed = false;
}
Expand Down Expand Up @@ -150,15 +199,29 @@ void TestFramework::printlog(const char *file, const char *func, int line, const
va_list list2;
va_copy(list2, vlst);
vfprintf(stdout, strMsg.c_str(), vlst);
vfprintf(g_pTestFramework->m_logHandle, strMsg.c_str(), list2);
vfprintf(m_logHandle, strMsg.c_str(), list2);
}

void TestFramework::injectButtons()
{
int ch = getchar();
int ch = 0;
#ifndef WASM
ch = getchar();
#else
if (!keyQueue.size()) {
return;
}
ch = keyQueue.front();
keyQueue.pop();
#endif
if (ch == 0) {
return;
}
handleLetter(ch);
}

void TestFramework::handleLetter(char ch)
{
switch (ch) {
case 'a':
printf("short click\n");
Expand Down Expand Up @@ -196,3 +259,8 @@ void TestFramework::injectButtons()
break;
}
}

bool TestFramework::stillRunning() const
{
return (m_initialized && m_keepGoing);
}
9 changes: 4 additions & 5 deletions VortexTestingFramework/TestFrameworkLinux.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <stdio.h>

#include "VortexEngine.h"

Expand All @@ -8,8 +7,6 @@
#include "Colors/Colorset.h"
#include "Leds/LedTypes.h"

#include <vector>

typedef uint32_t HDC;
typedef uint32_t HINSTANCE;
typedef uint32_t HWND;
Expand All @@ -22,7 +19,6 @@ typedef uint32_t UINT;

typedef struct tagCRGB CRGB;


// paint callback type
typedef void (*paint_fn_t)(void *, HDC);

Expand Down Expand Up @@ -55,12 +51,15 @@ class TestFramework
// called by engine Buttons::check right after buttons are checked
void injectButtons();

void handleLetter(char ch);
bool stillRunning() const;

static void printlog(const char *file, const char *func, int line, const char *msg, va_list list);

private:
// these are in no particular order
//HANDLE m_loopThread;
FILE *m_logHandle;
//FILE *m_logHandle;
//RECT m_ledPos[LED_COUNT];
RGBColor *m_ledList;
uint32_t m_numLeds;
Expand Down
2 changes: 1 addition & 1 deletion VortexTestingFramework/VortexEngine
96 changes: 96 additions & 0 deletions VortexTestingFramework/minimal_wasm_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!doctypehtml>
<html lang=en-us>
<head>
<meta charset=utf-8>
<meta content="text/html; charset=utf-8"http-equiv=Content-Type>
<style>
body{
font-family:arial;
margin:0;
padding:none
}
.emscripten{
padding-right:0;
margin-left:auto;
margin-right:auto;
display:block
}
div.emscripten{
text-align:center
}
div.emscripten_border{
border:1px solid #000
}
canvas.emscripten{
border:0 none;
background-color:#000;
height:0
}
#emscripten_logo{
display:inline-block;
margin:0
}
#status{
display:inline-block;
vertical-align:top;
margin-top:30px;
margin-left:20px;
font-weight:700;
color:#787878
}
#controls{
display:inline-block;
float:right;
vertical-align:top;
margin-top:30px;
margin-right:20px
}
#output{
width:100%;
height:600px;
margin:0 auto;
margin-top:10px;
border-left:0;
border-right:0px;
padding-left:0;
padding-right:0;
display:block;
background-color:#000;
color:#fff;
font-family:'Lucida Console',Monaco,monospace;
outline:0
}
</style>
a = short press<br>
s = long press<br>
d = open ringmenu<br>
</head>
<body>
<canvas class=emscripten id=canvas></canvas>
<textarea id=output rows=8></textarea>
<script>
var Module = {
preRun: [],
postRun: [],
print: function() {
var e = document.getElementById("output");
return e && (e.value = ""),
function(t) {
arguments.length > 1 && (t = Array.prototype.slice.call(arguments).join(" ")), console.log(t), e && (e.value += t + "\n", e.scrollTop = e.scrollHeight)
}
}(),
canvas: function() {
var e = document.getElementById("canvas");
return e.addEventListener("webglcontextlost", (function(e) {
alert("WebGL context lost. You will need to reload the page."), e.preventDefault()
}), !1), e
}(),
totalDependencies: 0,
monitorRunDependencies: function(e) {
this.totalDependencies = Math.max(this.totalDependencies, e);
}
};
</script>
<script async src=vortex.js></script>
</body>
</html>

0 comments on commit 9def1a3

Please sign in to comment.