Skip to content

Instantly share code, notes, and snippets.

@Templarian
Created July 23, 2026 02:45
Show Gist options
  • Select an option

  • Save Templarian/10b4cb1e2a0f5d15526e32922508493f to your computer and use it in GitHub Desktop.

Select an option

Save Templarian/10b4cb1e2a0f5d15526e32922508493f to your computer and use it in GitHub Desktop.
Moddable JS SDK Joycon
/** Joycon Library **/
/** modules/drivers/sensors/joycon/joycon.js **/
class Joycon {
#x;
#y;
#xMin;
#xMax ;
#xCenter;
#yMin;
#yMax;
#yCenter;
#deadzone;
constructor(options) {
this.#x = new options.x.io(options.x);
this.#y = new options.y.io(options.y);
this.#xMin = 150;
this.#xMax = 800;
this.#xCenter = 500;
this.#yMin = 150;
this.#yMin = 800;
this.#yCenter = 500;
this.#deadzone = 50;
}
configure(options) {
if (options.xMin !== undefined)
this.#xMin = options.xMin;
if (options.xMax !== undefined)
this.#xMax = options.xMax;
if (options.xCenter !== undefined)
this.#xCenter = options.xCenter;
if (options.yMin !== undefined)
this.#yMin = options.yMin;
if (options.yMax !== undefined)
this.#yMax = options.yMax;
if (options.yCenter !== undefined)
this.#yCenter = options.yCenter;
if (options.deadzone !== undefined)
this.#deadzone = options.deadzone;
}
close() {
this.#x.close();
this.#y.close();
}
sample() {
const xRaw = this.#x.read(),
yRaw = this.#y.read();
// Calculate normalized x and y
// xMin | 50 | 0 to -1 | 50 | center | 50 | 0 to 1 | 50 | xMax
// xMinCenter ^ ^ xMaxCenter
const xMinCenter = this.#xCenter - this.#deadzone,
xMaxCenter = this.#xCenter + this.#deadzone,
yMinCenter = this.#yCenter - this.#deadzone,
yMaxCenter = this.#yCenter + this.#deadzone,
xMinZone = this.#xMin + this.#deadzone,
xMaxZone = this.#xMax - this.#deadzone,
yMinZone = this.#yMin + this.#deadzone,
yMaxZone = this.#yMax - this.#deadzone;
let x = 0, y = 0;
if (xRaw <= xMinZone)
x = -1;
else if (xRaw >= xMaxZone)
x = 1;
else if (xRaw >= xMinCenter
&& xRaw <= xMaxCenter)
x = 0;
else if (xRaw < xMinCenter)
x = -1 * (1 + ((xMinZone - xRaw) / (xMinCenter - xMinZone)));
else
x = 1 - ((xMaxZone - xRaw) / (xMaxZone - xMaxCenter));
if (yRaw <= yMinZone)
y = -1;
else if (yRaw >= yMaxZone)
y = 1;
else if (yRaw >= yMinCenter
&& yRaw <= yMaxCenter)
y = 0;
else if (yRaw < yMinCenter)
y = -1 * (1 + ((yMinZone - yRaw) / (yMinCenter - yMinZone)));
else
y = 1 - ((yMaxZone - yRaw) / (yMaxZone - yMaxCenter));
// x, y = -1 to 1, center 0
// xRaw, yRaw = 0 to 1023
return { x, y, xRaw, yRaw };
}
}
export default Joycon;
{
"modules": {
"embedded:sensor/Joycon": "$(MODDABLE)/modules/drivers/sensors/joycon/joycon"
},
"preload": [
"embedded:sensor/Joycon"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment