-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleds_ctrl.tsx
168 lines (152 loc) · 5.54 KB
/
leds_ctrl.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { useState, useEffect, useContext, useRef } from 'react';
import './leds_ctrl.css';
import { AllLeds } from './all_led_ctrl';
import { unwrap, unwrap_num, rgb_to_hex, hex_to_rgb } from '../utils/utils';
import { ColorPickrWrapper } from '../utils/pickr_adapter';
import { BrightnessContext } from '../contexts/brightness_context';
import { post_brightness, post_leds } from '../utils/api_calls';
import { OnlineContext } from '../contexts/online_context';
import { build_single_promise_queue, ignore_task_results, log_task_results } from '../utils/task_queues';
function LedCtrl() {
const [color, set_color] = useState({ red: 0, green: 0, blue: 0 });
const [brightness, set_brightness] = useState(255);
const [delay_duration, set_delay_duration] = useState("");
const [fade_duration, set_fade_duration] = useState(100);
const { brightness: brightness_context } = useContext(BrightnessContext);
useEffect(_ => {
if (brightness_context !== null && brightness !== brightness_context) {
set_brightness(brightness_context);
}
}, [brightness_context]);
function push_brightness(new_brightness) {
set_brightness(new_brightness);
const data = { brightness: new_brightness };
if (delay_duration !== "") {
data['delay_duration'] = delay_duration;
}
if (fade_duration !== "") {
data['fade_duration'] = fade_duration;
}
post_brightness(data);
}
const post_leds_queue = useRef(build_single_promise_queue());
const { online } = useContext(OnlineContext);
function push_color_state(new_color) {
if (!online) {
return;
}
const data = {
red: Math.round(new_color.red),
green: Math.round(new_color.green),
blue: Math.round(new_color.blue),
};
const res = post_leds_queue.current(() => {
if (delay_duration !== "") {
data['delay_duration'] = delay_duration;
}
if (fade_duration !== "") {
data['fade_duration'] = fade_duration;
}
return post_leds({
...data,
});
});
ignore_task_results(res);
}
function pick_color(new_color) {
set_color(new_color);
push_color_state(new_color);
}
const int_color = {
red: Math.round(color.red),
green: Math.round(color.green),
blue: Math.round(color.blue),
};
function set_red(red) {
pick_color({ ...int_color , red });
}
function set_green(green) {
pick_color({ ...int_color , green });
}
function set_blue(blue) {
pick_color({ ...int_color , blue });
}
function set_delay(value) {
if (value == null || value === "") {
set_delay_duration("");
} else {
set_delay_duration(Math.max(0, Math.round(Number(value))));
}
}
function set_fade(value) {
if (value == null || value === "") {
set_fade_duration("");
} else {
set_fade_duration(Math.max(0, Math.round(Number(value))));
}
}
function set_display_color(hex_color) {
const rgb_color = hex_to_rgb(hex_color);
if (rgb_color == null) {
return;
}
pick_color(rgb_color);
}
const { red, green, blue } = int_color;
const hex_value = rgb_to_hex(int_color);
return (
<>
<div className="container-column">
<h1 className="section-title">Lights</h1>
<div className="container-row input-row">
<div>
{hex_value.toUpperCase()}
<ColorPickrWrapper color={color} set_color={pick_color} />
</div>
<div className="container-column led-form flex-grow-1">
<div className="container-row input-row">
<label htmlFor="red_slider">
Red: {red} (0x{red.toString(16).toUpperCase()})
<br />
<input type="range" min="0" max="255" name="red_slider" id="red_slider" value={red} onChange={unwrap_num(set_red)} />
</label>
<label htmlFor="green">
Green: {green} (0x{green.toString(16).toUpperCase()})
<br />
<input type="range" min="0" max="255" name="green" id="green" value={green} onChange={unwrap_num(set_green)} />
</label>
<label htmlFor="blue">
Blue: {blue} (0x{blue.toString(16).toUpperCase()})
<br />
<input type="range" min="0" max="255" name="blue" id="blue" value={blue} onChange={unwrap_num(set_blue)} />
</label>
</div>
<label htmlFor="brightness">
Brightness: {brightness} ({Math.round(brightness / 255 * 100)}%)
<br />
<input type="range" min="0" max="255" name="brightness" id="brightness" value={brightness} onChange={unwrap_num(push_brightness)} />
</label>
<div className="container-row input-row">
<label htmlFor="led_delay_duration">
Delay:
<input type="number" placeholder="0" name="led_delay_duration" id="led_delay_duration" value={delay_duration} onChange={unwrap(set_delay)}/>
ms
</label>
<label htmlFor="led_fade_duration">
Fade:
<input type="number" placeholder="0" name="led_fade_duration" id="led_fade_duration" value={fade_duration} onChange={unwrap(set_fade)}/>
ms
</label>
</div>
<div>
Native Pickr:
<br/>
<input type="color" name="color_display" id="color_display" value={hex_value} onChange={unwrap(set_display_color)}/>
</div>
</div>
</div>
</div>
</>
);
}
export { LedCtrl };