-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3c1d957
Showing
21 changed files
with
3,721 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Barva je jednoduchá trojice červené, zelené, a modré složky | ||
* - R: červená (rozsah 0-255) | ||
* - G: zelená (rozsah 0-255) | ||
* - B: modrá (rozsah 0-255) | ||
*/ | ||
interface Rgb { | ||
r: number; | ||
g: number; | ||
b: number; | ||
} | ||
|
||
|
||
/** | ||
* Alternativní způsob, jak vyjádřit barvu, je HSL: | ||
* - Hue: odstín (rozsah 0-360) | ||
* - Saturation: sytost barev (rozsah 0-1) | ||
* - Lightness: světlost (rozsah 0-1) | ||
*/ | ||
interface Hsl { | ||
h: number; | ||
s: number; | ||
l: number; | ||
} | ||
|
||
/** | ||
* Mezi jednotlivými reprezentacemi lze převádět | ||
* @param hsl {Number} Hue (0-360), Saturation (0-1), Lightness (0-1) | ||
* @returns {Rgb} Red (0-255), Green (0-255), Blue (0-255) | ||
*/ | ||
export function hsl_to_rbg( hsl: Hsl ) : Rgb { | ||
const chroma = ( 1 - Math.abs( 2 * hsl.l - 1 ) * hsl.s ); | ||
const hue = hsl.h / 60; | ||
const x = chroma * ( 1 - Math.abs( ( hue % 2 ) - 1 ) ); | ||
|
||
let color : Rgb = { r: 0, g: 0, b: 0 }; | ||
if( hue > 0 && hue < 1 ){ | ||
color = { r: chroma, g: x, b: 0 }; | ||
} else if( hue >= 1 && hue < 2 ){ | ||
color = { r: x, g: chroma, b: 0 }; | ||
} else if( hue >= 2 && hue < 3 ){ | ||
color = { r: 0, g: chroma, b: x }; | ||
} else if( hue >= 3 && hue < 4 ){ | ||
color = { r: 0, g: x, b: chroma }; | ||
} else if( hue >= 4 && hue < 5 ){ | ||
color = { r: x, g: 0, b: chroma }; | ||
} else { | ||
color = { r: chroma, g: 0, b: x }; | ||
} | ||
const correction = hsl.l - chroma / 2; | ||
color.r = ( color.r + correction ) * 255; | ||
color.g = ( color.g + correction ) * 255; | ||
color.b = ( color.b + correction ) * 255; | ||
|
||
return color; | ||
} | ||
|
||
/** | ||
* Funkce rainbow zafixuje sytost a světlost, a prochází barvami | ||
* @param hue (0-360) | ||
* @param brightness (0-100) - 50 je defaultní hodnota | ||
* @returns {Rgb} | ||
*/ | ||
export function rainbow( hue: number, brightness: number = 50) : Rgb { | ||
hue = Math.min( hue, 360 ); // Zajistíme, že zadaná hodnota není mimo rozsah | ||
// fix range to 0-100 | ||
let brightness_mapped = Math.min(Math.max(brightness, 0), 100); | ||
return hsl_to_rbg( { h: hue, s: 1, l: brightness_mapped / 100 } ); | ||
} | ||
|
||
/* Základní barvy pro LED pásky*/ | ||
export const red = rainbow( 0 ); | ||
export const orange = rainbow( 27 ); | ||
export const yellow = rainbow( 54 ); | ||
export const green = rainbow( 110 ); | ||
export const light_blue = rainbow( 177 ); | ||
export const blue = rainbow( 240 ); | ||
export const purple = rainbow( 285 ); | ||
export const pink = rainbow( 323 ); | ||
export const white : Rgb = { r: 100, g: 100, b: 100 }; | ||
export const off : Rgb = { r: 0, g: 0, b: 0 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { SmartLed, LED_WS2812 } from "smartled"; | ||
import * as colors from "./libs/colors.js" | ||
|
||
const ledStrip = new SmartLed(48, 1, LED_WS2812); | ||
|
||
// Set the first LED to red | ||
ledStrip.set(0, colors.red); | ||
ledStrip.show(); | ||
|
||
// Set custom HSL color | ||
ledStrip.set(0, colors.hsl_to_rbg({ h: 0, s: 1, l: 0.5 })); | ||
ledStrip.show(); | ||
|
||
// Set rainbow color | ||
ledStrip.set(0, colors.rainbow(0)); | ||
ledStrip.show(); | ||
|
||
// Set rainbow color with custom brightness | ||
ledStrip.set(0, colors.rainbow(0, 50)); | ||
ledStrip.show(); | ||
|
||
// Set off | ||
ledStrip.set(0, colors.off); | ||
ledStrip.show(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: Colors | ||
description: Library for handling colors | ||
files: | ||
- colors.ts | ||
examples: | ||
- | ||
name: Basic usage | ||
file: examples/basic-usage.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { stdout } from "stdio"; | ||
import { readline } from "./libs/readline.js"; | ||
|
||
//* řetězce | ||
async function echo() { | ||
stdout.write("Napiš nějaký text a stiskni enter.\n"); | ||
const reader = new readline(false); // vytvoří novou instanci třídy readline | ||
while (true) { // opakuje se donekonečna | ||
const line = await reader.read(); // přečte řádek z konzole | ||
stdout.write("Zadal jsi: " + line + "\n"); // vypíše řádek na konzoli | ||
stdout.write(`Druhá možnost výpisu: Zadal jsi: ${line}\n`); // vypíše řádek na konzoli | ||
|
||
if (line == "konec") { // pokud je řádek roven "konec" | ||
stdout.write("Ukončuji.\n"); // vypíše text na konzoli | ||
break; // ukončí cyklus | ||
} | ||
} | ||
reader.close(); // ukončí čtení z konzole | ||
} | ||
|
||
echo(); // zavolá funkci echo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: Readline | ||
description: Library for easily reading lines from standard input | ||
files: | ||
- readline.ts | ||
examples: | ||
- | ||
name: Basic usage | ||
file: examples/basic-usage.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { stdout, stdin } from "stdio"; | ||
|
||
/** | ||
* A class for reading standard input line by line. | ||
*/ | ||
|
||
export class readline { | ||
private buffer: string = ""; | ||
private promise: Promise<string> | null = null; | ||
private resolve: ((value: string) => void) | null = null; | ||
private reject: ((reason: any) => void) | null = null; | ||
private closed: boolean = false; | ||
private echo: boolean; | ||
|
||
private onGet(str: string) { | ||
if (this.echo) { | ||
stdout.write(str); | ||
} | ||
|
||
if (str == "\n") { | ||
if (this.resolve) { | ||
this.resolve(this.buffer); | ||
} | ||
|
||
this.buffer = ""; | ||
this.promise = null; | ||
this.resolve = null; | ||
this.reject = null; | ||
|
||
return; | ||
} | ||
|
||
this.buffer += str; | ||
|
||
if (!this.closed) { | ||
stdin.get() | ||
.then((data) => this.onGet(data)) | ||
.catch((reason) => { | ||
if (this.reject) { | ||
this.reject(reason); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
constructor(echo: boolean = false) { | ||
this.echo = echo; | ||
} | ||
|
||
public read(): Promise<string> { | ||
if (this.promise != null) { | ||
return Promise.reject("Already reading"); | ||
} | ||
|
||
this.promise = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
|
||
stdin.get() | ||
.then((data) => this.onGet(data)) | ||
.catch((reason) => { | ||
if (this.reject) { | ||
this.reject(reason); | ||
} | ||
}); | ||
|
||
return this.promise; | ||
} | ||
|
||
public close() { | ||
this.closed = true; | ||
|
||
if (this.reject) { | ||
this.reject("Stopped"); | ||
} | ||
|
||
this.buffer = ""; | ||
this.promise = null; | ||
this.resolve = null; | ||
this.reject = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Servo } from "./libs/servo.js" | ||
|
||
const SERVO_PIN = 35; | ||
const TIMER = 1; | ||
const CHANNEL = 3; | ||
const servo = new Servo(SERVO_PIN, TIMER, CHANNEL); | ||
|
||
servo.write(0); // 0° | ||
servo.write(512); // 90° | ||
servo.write(1023); // 180° |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Servo } from "./libs/servo.js" | ||
|
||
const SERVO_PIN = 35; | ||
const TIMER = 1; | ||
const CHANNEL_1 = 3; | ||
const CHANNEL_2 = 4; | ||
|
||
const servo_1 = new Servo(SERVO_PIN, TIMER, CHANNEL_1); | ||
const servo_2 = new Servo(SERVO_PIN, TIMER, CHANNEL_2); | ||
|
||
servo_1.write(0); // 0° | ||
servo_2.write(0); // 0° | ||
|
||
servo_1.write(512); // 90° | ||
servo_2.write(512); // 90° | ||
|
||
servo_1.write(1023); // 180° | ||
servo_2.write(1023); // 180° |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: Servo | ||
description: Library for controlling servos | ||
files: | ||
- servo.ts | ||
examples: | ||
- | ||
name: Basic usage | ||
file: examples/basic-usage.ts | ||
- | ||
name: Multiple servos | ||
file: examples/multi-servo.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Barva je jednoduchá trojice červené, zelené, a modré složky | ||
* - R: červená (rozsah 0-255) | ||
* - G: zelená (rozsah 0-255) | ||
* - B: modrá (rozsah 0-255) | ||
*/ | ||
interface Rgb { | ||
r: number; | ||
g: number; | ||
b: number; | ||
} | ||
|
||
|
||
/** | ||
* Alternativní způsob, jak vyjádřit barvu, je HSL: | ||
* - Hue: odstín (rozsah 0-360) | ||
* - Saturation: sytost barev (rozsah 0-1) | ||
* - Lightness: světlost (rozsah 0-1) | ||
*/ | ||
interface Hsl { | ||
h: number; | ||
s: number; | ||
l: number; | ||
} | ||
|
||
/** | ||
* Mezi jednotlivými reprezentacemi lze převádět | ||
* @param hsl {Number} Hue (0-360), Saturation (0-1), Lightness (0-1) | ||
* @returns {Rgb} Red (0-255), Green (0-255), Blue (0-255) | ||
*/ | ||
export function hsl_to_rbg( hsl: Hsl ) : Rgb { | ||
const chroma = ( 1 - Math.abs( 2 * hsl.l - 1 ) * hsl.s ); | ||
const hue = hsl.h / 60; | ||
const x = chroma * ( 1 - Math.abs( ( hue % 2 ) - 1 ) ); | ||
|
||
let color : Rgb = { r: 0, g: 0, b: 0 }; | ||
if( hue > 0 && hue < 1 ){ | ||
color = { r: chroma, g: x, b: 0 }; | ||
} else if( hue >= 1 && hue < 2 ){ | ||
color = { r: x, g: chroma, b: 0 }; | ||
} else if( hue >= 2 && hue < 3 ){ | ||
color = { r: 0, g: chroma, b: x }; | ||
} else if( hue >= 3 && hue < 4 ){ | ||
color = { r: 0, g: x, b: chroma }; | ||
} else if( hue >= 4 && hue < 5 ){ | ||
color = { r: x, g: 0, b: chroma }; | ||
} else { | ||
color = { r: chroma, g: 0, b: x }; | ||
} | ||
const correction = hsl.l - chroma / 2; | ||
color.r = ( color.r + correction ) * 255; | ||
color.g = ( color.g + correction ) * 255; | ||
color.b = ( color.b + correction ) * 255; | ||
|
||
return color; | ||
} | ||
|
||
/** | ||
* Funkce rainbow zafixuje sytost a světlost, a prochází barvami | ||
* @param hue (0-360) | ||
* @param brightness (0-100) - 50 je defaultní hodnota | ||
* @returns {Rgb} | ||
*/ | ||
export function rainbow( hue: number, brightness: number = 50) : Rgb { | ||
hue = Math.min( hue, 360 ); // Zajistíme, že zadaná hodnota není mimo rozsah | ||
// fix range to 0-100 | ||
let brightness_mapped = Math.min(Math.max(brightness, 0), 100); | ||
return hsl_to_rbg( { h: hue, s: 1, l: brightness_mapped / 100 } ); | ||
} | ||
|
||
/* Základní barvy pro LED pásky*/ | ||
export const red = rainbow( 0 ); | ||
export const orange = rainbow( 27 ); | ||
export const yellow = rainbow( 54 ); | ||
export const green = rainbow( 110 ); | ||
export const light_blue = rainbow( 177 ); | ||
export const blue = rainbow( 240 ); | ||
export const purple = rainbow( 285 ); | ||
export const pink = rainbow( 323 ); | ||
export const white : Rgb = { r: 100, g: 100, b: 100 }; | ||
export const off : Rgb = { r: 0, g: 0, b: 0 }; |
Oops, something went wrong.