Skip to content

Commit

Permalink
Add second controller support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSant0s committed Feb 6, 2024
1 parent 144b7fd commit b0aa764
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bin/pads.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const l3 = new Image("pads/l3.png", VRAM);
const r3 = new Image("pads/r3.png", VRAM);

var rumble = false;
var pad = Pads.get();
var pad = Pads.get(0);

Screen.display(() => {
pad.update();
Expand Down
10 changes: 7 additions & 3 deletions bin/playground.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// {"name": "Playground", "author": "Daniel Santos", "version": "06022024", "file": "playground.js"}

let v1 = new Vector2(15.0f, 15.0f);
let v2 = new Vector2(30.0f, 30.0f);
let v1 = Vector2.new(15.0f, 15.0f);
let v2 = Vector2.new(30.0f, 30.0f);

Screen.log(`Vectors distance is ${v1.dist(v2)}`);
Screen.log(`Vectors distance is ${v1.dist(v2)}`)
Screen.log(`Vectors add is ${Vector2.add(v1, v2).toString()}`);
Screen.log(`Vectors sub is ${Vector2.sub(v1, v2).toString()}`);
Screen.log(`Vectors mul is ${Vector2.mul(v1, v2).toString()}`);
Screen.log(`Vectors div is ${Vector2.div(v1, v2).toString()}`);

System.sleep(99999);
3 changes: 2 additions & 1 deletion src/ath_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ static int qjs_handle_fh(JSContext *ctx, FILE *f, const char *filename, const ch
"globalThis.System = System;\n"
"globalThis.Archive = Archive;\n"
"globalThis.IOP = IOP;\n"
"globalThis.Vector2 = Vector2.Vector2;\n";
"globalThis.Vector2 = Vector2;\n";


rc = qjs_eval_buf(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE);
if (rc != 0) { return retval; }
Expand Down
29 changes: 28 additions & 1 deletion src/ath_pads.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ static JSValue athena_gettype(JSContext *ctx, JSValue this_val, int argc, JSValu
return JS_NewInt32(ctx, mode);
}

static JSValue athena_getstate(JSContext *ctx, JSValue this_val, int argc, JSValueConst *argv){
if (argc != 0 && argc != 1) return JS_ThrowSyntaxError(ctx, "wrong number of arguments");
int port = 0;
if (argc == 1){
JS_ToInt32(ctx, &port, argv[0]);
if (port > 1) return JS_ThrowSyntaxError(ctx, "wrong port number.");
}
int mode = padGetState(port, 0);
return JS_NewInt32(ctx, mode);
}

static bool pad1_initialized = false;

static JSValue athena_getpad(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv){
int port = 0;

Expand All @@ -48,6 +61,10 @@ static JSValue athena_getpad(JSContext *ctx, JSValueConst this_val, int argc, JS
return JS_EXCEPTION;
if (argc == 1){
JS_ToInt32(ctx, &port, argv[0]);
if (port == 1 && !pad1_initialized) {
initializePad(1, 0);
pad1_initialized = true;
}
if (port > 1) return JS_ThrowSyntaxError(ctx, "wrong port number.");
}

Expand Down Expand Up @@ -362,9 +379,11 @@ static JSValue js_pad_set_prop(JSContext *ctx, JSValueConst this_val, JSValue va
static const JSCFunctionListEntry module_funcs[] = {
JS_CFUNC_DEF("get", 1, athena_getpad),
JS_CFUNC_DEF("getType", 1, athena_gettype),
JS_CFUNC_DEF("getState", 1, athena_getstate),
JS_CFUNC_DEF("getPressure", 2, athena_getpressure),
JS_CFUNC_DEF("rumble", 3, athena_rumble),
JS_CFUNC_DEF("setLED", 4, athena_set_led),

JS_PROP_INT32_DEF("SELECT", PAD_SELECT, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("START", PAD_START, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("UP", PAD_UP, JS_PROP_CONFIGURABLE ),
Expand All @@ -381,9 +400,17 @@ static const JSCFunctionListEntry module_funcs[] = {
JS_PROP_INT32_DEF("R2", PAD_R2, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("L3", PAD_L3, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("R3", PAD_R3, JS_PROP_CONFIGURABLE ),

JS_PROP_INT32_DEF("DIGITAL", PAD_TYPE_DIGITAL, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("ANALOG", PAD_TYPE_ANALOG, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("DUALSHOCK", PAD_TYPE_DUALSHOCK, JS_PROP_CONFIGURABLE )
JS_PROP_INT32_DEF("DUALSHOCK", PAD_TYPE_DUALSHOCK, JS_PROP_CONFIGURABLE ),

JS_PROP_INT32_DEF("STATE_DISCONN", PAD_STATE_DISCONN, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("STATE_FINDPAD", PAD_STATE_FINDPAD, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("STATE_FINDCTP1", PAD_STATE_FINDCTP1, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("STATE_EXECCMD", PAD_STATE_EXECCMD, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("STATE_STABLE ", PAD_STATE_STABLE, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("STATE_ERROR ", PAD_STATE_ERROR, JS_PROP_CONFIGURABLE ),
};

static const JSCFunctionListEntry js_pad_proto_funcs[] = {
Expand Down
172 changes: 154 additions & 18 deletions src/ath_vector.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ath_env.h"
#include <math.h>
#include <stdio.h>

typedef struct {
float x;
Expand All @@ -21,8 +22,12 @@ static JSValue js_vector2_ctor(JSContext *ctx,
{
Vector2 *s;
JSValue obj = JS_UNDEFINED;
JSValue proto;

obj = JS_NewObjectClass(ctx, js_vector2_class_id);
if (JS_IsException(obj))
goto fail;


s = js_mallocz(ctx, sizeof(*s));
if (!s)
return JS_EXCEPTION;
Expand All @@ -32,13 +37,7 @@ static JSValue js_vector2_ctor(JSContext *ctx,
goto fail;
/* using new_target to get the prototype is necessary when the
class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if (JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_vector2_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(obj))
goto fail;

JS_SetOpaque(obj, s);
return obj;
fail:
Expand Down Expand Up @@ -122,6 +121,146 @@ static JSValue js_vector2_distancesqr(JSContext *ctx, JSValueConst this_val,
return JS_NewFloat32(ctx, ((v1->x - v2->x) * (v1->x - v2->x) + (v1->y - v2->y) * (v1->y - v2->y)));
}

static JSValue js_vector2_tostring(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
char str[32];
Vector2 *v = JS_GetOpaque2(ctx, this_val, js_vector2_class_id);

if (!v)
return JS_EXCEPTION;

sprintf(str, "{x:%g, y:%g}", v->x, v->y);
return JS_NewString(ctx, str);
}

static JSValue js_vector2_add(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
Vector2 *s;
JSValue obj = JS_UNDEFINED;

Vector2 *v1 = JS_GetOpaque2(ctx, argv[0], js_vector2_class_id);
Vector2 *v2 = JS_GetOpaque2(ctx, argv[1], js_vector2_class_id);
if (!v1 || !v2)
return JS_EXCEPTION;

obj = JS_NewObjectClass(ctx, js_vector2_class_id);
if (JS_IsException(obj))
goto fail;

s = js_mallocz(ctx, sizeof(*s));
if (!s)
return JS_EXCEPTION;

s->x = v1->x + v2->x;
s->y = v1->y + v2->y;

JS_SetOpaque(obj, s);
return obj;
fail:
js_free(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}

static JSValue js_vector2_sub(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
Vector2 *s;
JSValue obj = JS_UNDEFINED;

Vector2 *v1 = JS_GetOpaque2(ctx, argv[0], js_vector2_class_id);
Vector2 *v2 = JS_GetOpaque2(ctx, argv[1], js_vector2_class_id);
if (!v1 || !v2)
return JS_EXCEPTION;

obj = JS_NewObjectClass(ctx, js_vector2_class_id);
if (JS_IsException(obj))
goto fail;

s = js_mallocz(ctx, sizeof(*s));
if (!s)
return JS_EXCEPTION;

s->x = v1->x - v2->x;
s->y = v1->y - v2->y;

JS_SetOpaque(obj, s);
return obj;
fail:
js_free(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}


static JSValue js_vector2_mul(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
Vector2 *s;
JSValue obj = JS_UNDEFINED;

Vector2 *v1 = JS_GetOpaque2(ctx, argv[0], js_vector2_class_id);
Vector2 *v2 = JS_GetOpaque2(ctx, argv[1], js_vector2_class_id);
if (!v1 || !v2)
return JS_EXCEPTION;

obj = JS_NewObjectClass(ctx, js_vector2_class_id);
if (JS_IsException(obj))
goto fail;

s = js_mallocz(ctx, sizeof(*s));
if (!s)
return JS_EXCEPTION;

s->x = v1->x * v2->x;
s->y = v1->y * v2->y;

JS_SetOpaque(obj, s);
return obj;
fail:
js_free(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}


static JSValue js_vector2_div(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
Vector2 *s;
JSValue obj = JS_UNDEFINED;

Vector2 *v1 = JS_GetOpaque2(ctx, argv[0], js_vector2_class_id);
Vector2 *v2 = JS_GetOpaque2(ctx, argv[1], js_vector2_class_id);
if (!v1 || !v2)
return JS_EXCEPTION;

obj = JS_NewObjectClass(ctx, js_vector2_class_id);
if (JS_IsException(obj))
goto fail;

s = js_mallocz(ctx, sizeof(*s));
if (!s)
return JS_EXCEPTION;

s->x = v1->x / v2->x;
s->y = v1->y / v2->y;

JS_SetOpaque(obj, s);
return obj;
fail:
js_free(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}


static const JSCFunctionListEntry js_vector2_funcs[] = {
JS_CFUNC_DEF("new", 2, js_vector2_ctor),
JS_CFUNC_DEF("add", 2, js_vector2_add),
JS_CFUNC_DEF("sub", 2, js_vector2_sub),
JS_CFUNC_DEF("mul", 2, js_vector2_mul),
JS_CFUNC_DEF("div", 2, js_vector2_div),
};

static JSClassDef js_vector2_class = {
"Vector2",
.finalizer = js_vector2_finalizer,
Expand All @@ -135,6 +274,8 @@ static const JSCFunctionListEntry js_vector2_proto_funcs[] = {
JS_CFUNC_DEF("cross", 1, js_vector2_crossproduct),
JS_CFUNC_DEF("dist", 1, js_vector2_distance),
JS_CFUNC_DEF("distsqr", 1, js_vector2_distancesqr),

JS_CFUNC_DEF("toString", 0, js_vector2_tostring),
};

static int js_vector2_init(JSContext *ctx, JSModuleDef *m)
Expand All @@ -148,21 +289,16 @@ static int js_vector2_init(JSContext *ctx, JSModuleDef *m)
vector2_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, vector2_proto, js_vector2_proto_funcs, countof(js_vector2_proto_funcs));

vector2_class = JS_NewCFunction2(ctx, js_vector2_ctor, "Vector2", 2, JS_CFUNC_constructor, 0);
//vector2_class = JS_NewCFunction2(ctx, js_vector2_ctor, "xy", 2, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, vector2_class, vector2_proto);
//JS_SetConstructor(ctx, vector2_class, vector2_proto);
JS_SetClassProto(ctx, js_vector2_class_id, vector2_proto);

JS_SetModuleExport(ctx, m, "Vector2", vector2_class);
return 0;
//JS_SetModuleExport(ctx, m, "Vector2", vector2_class);
return JS_SetModuleExportList(ctx, m, js_vector2_funcs, countof(js_vector2_funcs));
}

JSModuleDef *athena_vector_init(JSContext *ctx)
{
JSModuleDef *m;
m = JS_NewCModule(ctx, "Vector2", js_vector2_init);
if (!m)
return NULL;
JS_AddModuleExport(ctx, m, "Vector2");
return m;
return athena_push_module(ctx, js_vector2_init, js_vector2_funcs, countof(js_vector2_funcs), "Vector2");
}
10 changes: 8 additions & 2 deletions src/pad.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include "include/pad.h"
#include "include/dbgprintf.h"

static char padBuf[256] __attribute__((aligned(64)));
static char pad0Buf[256] __attribute__((aligned(64)));
static char pad1Buf[256] __attribute__((aligned(64)));

static char actAlign[6];
static int actuators;
Expand Down Expand Up @@ -178,7 +179,7 @@ void pad_init()
dbgprintf("PortMax: %d\n", padGetPortMax());
dbgprintf("SlotMax: %d\n", padGetSlotMax(port));

if((ret = padPortOpen(port, slot, padBuf)) == 0) {
if((ret = padPortOpen(0, 0, pad0Buf)) == 0) {
dbgprintf("padOpenPort failed: %d\n", ret);
SleepThread();
}
Expand All @@ -187,5 +188,10 @@ void pad_init()
dbgprintf("pad initalization failed!\n");
SleepThread();
}

if((ret = padPortOpen(1, 0, pad1Buf)) == 0) {
dbgprintf("padOpenPort failed: %d\n", ret);
SleepThread();
}
}

0 comments on commit b0aa764

Please sign in to comment.