feat: basic control of LEDs
This commit is contained in:
1
src/app.css
Normal file
1
src/app.css
Normal file
@@ -0,0 +1 @@
|
||||
@import 'tailwindcss';
|
||||
13
src/app.d.ts
vendored
Normal file
13
src/app.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||
// for information about these interfaces
|
||||
declare global {
|
||||
namespace App {
|
||||
// interface Error {}
|
||||
// interface Locals {}
|
||||
// interface PageData {}
|
||||
// interface PageState {}
|
||||
// interface Platform {}
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
12
src/app.html
Normal file
12
src/app.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%sveltekit.assets%/favicon.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
||||
32
src/lib/index.ts
Normal file
32
src/lib/index.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// place files you want to import through the `$lib` alias in this folder.
|
||||
|
||||
type WLEDAPI = {
|
||||
seg: Array<{ id: string, col: Array<[number, number, number]> }>
|
||||
}
|
||||
|
||||
const getAddr = (addr: string) => `http://${addr}/json/state`
|
||||
|
||||
const buildReq = (hex: string): WLEDAPI => {
|
||||
// hex is not validated.
|
||||
|
||||
const hexValues = parseInt(hex.slice(1), 16);
|
||||
|
||||
const r = (hexValues >> 16) & 255;
|
||||
const g = (hexValues >> 8) & 255;
|
||||
const b = hexValues & 255;
|
||||
|
||||
return {
|
||||
seg: [{
|
||||
id: "0",
|
||||
col: [[r, g, b]],
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
export const createLedClient = (addr: string) => {
|
||||
return {
|
||||
async setColor(hex: string) {
|
||||
await fetch(getAddr(addr), { method: "POST", body: JSON.stringify(buildReq(hex)) });
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/routes/+layout.svelte
Normal file
9
src/routes/+layout.svelte
Normal file
@@ -0,0 +1,9 @@
|
||||
<script lang="ts">
|
||||
import "../app.css";
|
||||
|
||||
let { children } = $props();
|
||||
</script>
|
||||
|
||||
<main class="p-4 flex flex-col items-center">
|
||||
{@render children()}
|
||||
</main>
|
||||
19
src/routes/+page.server.ts
Normal file
19
src/routes/+page.server.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createLedClient } from "$lib";
|
||||
import { fail, type Actions } from "@sveltejs/kit";
|
||||
|
||||
const client = createLedClient("192.168.1.215");
|
||||
|
||||
export const actions: Actions = {
|
||||
async default({ request }) {
|
||||
const data = await request.formData();
|
||||
|
||||
const color = data.get("color");
|
||||
if (color == null) {
|
||||
return fail(400, { error: "Color was not provided" });
|
||||
}
|
||||
|
||||
// TODO: validate actual HEX
|
||||
|
||||
await client.setColor(color.toString());
|
||||
}
|
||||
}
|
||||
6
src/routes/+page.svelte
Normal file
6
src/routes/+page.svelte
Normal file
@@ -0,0 +1,6 @@
|
||||
<form method="POST">
|
||||
<h1>Twitch Lights</h1>
|
||||
<p class="">Pick a colour for the lights</p>
|
||||
<input name="color" defaultValue="#880000" type="color" />
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
Reference in New Issue
Block a user