🔢 Adds math function to convert to uint8
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import nipplejs from 'nipplejs';
|
||||
import { onMount } from 'svelte';
|
||||
import { throttler } from '$lib/utilities';
|
||||
import { throttler, toUint8 } from '$lib/utilities';
|
||||
import socketService from '$lib/services/socket-service';
|
||||
import { emulateModel, input, outControllerData } from '$lib/store';
|
||||
|
||||
@@ -67,12 +67,12 @@
|
||||
|
||||
const updateData = () => {
|
||||
data[0] = 0;
|
||||
data[1] = $input.left.x * 127 + 128;
|
||||
data[2] = $input.left.y * 127 + 128;
|
||||
data[3] = $input.right.x * 127 + 128;
|
||||
data[4] = $input.right.y * 127 + 128;
|
||||
data[5] = $input.height;
|
||||
data[6] = $input.speed;
|
||||
data[1] = toUint8($input.left.x, -1, 1);
|
||||
data[2] = toUint8($input.left.y, -1, 1);
|
||||
data[3] = toUint8($input.right.x, -1, 1);
|
||||
data[4] = toUint8($input.right.y, -1, 1);
|
||||
data[5] = toUint8($input.height, 0, 100);;
|
||||
data[6] = toUint8($input.speed, 0, 100);
|
||||
|
||||
outControllerData.set(data);
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export const toUint8 = (number:number, min:number, max:number) => {
|
||||
number = Math.max(min, Math.min(max, number));
|
||||
let scaled = ((number - min) / (max - min)) * 255;
|
||||
return Math.round(scaled) & 0xFF;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { toUint8 } from '../../src/lib/utilities';
|
||||
|
||||
describe('toUint8', () => {
|
||||
it('min interval value should get 0', () => {
|
||||
expect(toUint8(-1, -1, 1)).toBe(0);
|
||||
});
|
||||
|
||||
it('middle interval value should get 128', () => {
|
||||
expect(toUint8(0, -1, 1)).toBe(128);
|
||||
});
|
||||
|
||||
it('max interval value should get 255', () => {
|
||||
expect(toUint8(1, -1, 1)).toBe(255);
|
||||
});
|
||||
|
||||
it('min value should be clamped', () => {
|
||||
expect(toUint8(-2, -1, 1)).toBe(0);
|
||||
});
|
||||
|
||||
it('max value should be clamped', () => {
|
||||
expect(toUint8(2, -1, 1)).toBe(255);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user