Skip to content

Commit

Permalink
Support color option in Switch.IRSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Dec 28, 2023
1 parent 2964d88 commit 0ad81bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-boxes-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Support `color` option in `Switch.IRSensor`
2 changes: 1 addition & 1 deletion packages/runtime/src/$.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export interface Init {

// irs.c
irsInit(): () => void;
irsSensorNew(image: ImageBitmap): IRSensor;
irsSensorNew(image: ImageBitmap, color: RGBA): IRSensor;
irsSensorStart(s: IRSensor): void;
irsSensorStop(s: IRSensor): void;
irsSensorUpdate(s: IRSensor): boolean;
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime/src/switch/irsensor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import colorRgba = require('color-rgba');
import { $ } from '../$';
import { ImageBitmap } from '../canvas/image-bitmap';
import { INTERNAL_SYMBOL } from '../internal';
Expand Down Expand Up @@ -28,6 +29,7 @@ export interface IRSensorInit {
* requested.
*/
frequency?: number;
color?: string;
}

/**
Expand Down Expand Up @@ -57,11 +59,15 @@ export class IRSensor extends Sensor {
init = true;
addEventListener('unload', $.irsInit());
}
const color = colorRgba(opts.color || 'green');
if (!color) {
throw new Error(`Invalid color specified: "${opts.color}"`);
}
// @ts-expect-error Internal constructor
super(INTERNAL_SYMBOL);
const image = $.imageNew(320, 240) as ImageBitmap;
Object.setPrototypeOf(image, ImageBitmap.prototype);
const self = $.irsSensorNew(image);
const self = $.irsSensorNew(image, color);
Object.setPrototypeOf(self, IRSensor.prototype);
_.set(self, {
activated: false,
Expand Down
31 changes: 30 additions & 1 deletion source/irs.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include "irs.h"
#include "image.h"
#include "canvas.h"

static JSClassID nx_ir_sensor_class_id;

struct RGBA
{
u8 r;
u8 g;
u8 b;
u8 a;
};

typedef struct
{
IrsIrCameraHandle irhandle;
IrsImageTransferProcessorConfig config;
struct RGBA color;
nx_image_t *image;
u8 *sensor_buf;
u32 sensor_buf_size;
Expand Down Expand Up @@ -60,6 +70,20 @@ static JSValue nx_irs_sensor_new(JSContext *ctx, JSValueConst this_val, int argc
return JS_EXCEPTION;
}

u32 r, g, b;
double a;
if (JS_ToUint32(ctx, &r, JS_GetPropertyUint32(ctx, argv[1], 0)) ||
JS_ToUint32(ctx, &g, JS_GetPropertyUint32(ctx, argv[1], 1)) ||
JS_ToUint32(ctx, &b, JS_GetPropertyUint32(ctx, argv[1], 2)) ||
JS_ToFloat64(ctx, &a, JS_GetPropertyUint32(ctx, argv[1], 3)))
{
return JS_EXCEPTION;
}
data->color.r = r;
data->color.g = g;
data->color.b = b;
data->color.a = a * 255;

// TODO: make configurable
HidNpadIdType id = HidNpadIdType_Handheld;

Expand Down Expand Up @@ -131,7 +155,12 @@ static JSValue nx_irs_sensor_update(JSContext *ctx, JSValueConst this_val, int a
{
// The IR image/camera is sideways with the joycon held flat.
u32 pos = y * data->image->width + x;
((u32 *)data->image->data)[pos] = BGRA8_MAXALPHA(/*ir_buffer[pos2]*/ 0, data->sensor_buf[pos], /*ir_buffer[pos2]*/ 0);
u8 alpha = (data->sensor_buf[pos] * data->color.a) / 255;
((u32 *)data->image->data)[pos] = BGRA8(
(data->color.r * alpha) / 255,
(data->color.g * alpha) / 255,
(data->color.b * alpha) / 255,
alpha);
}
}
cairo_surface_mark_dirty_rectangle(
Expand Down

0 comments on commit 0ad81bb

Please sign in to comment.