Skip to content

Commit af2db12

Browse files
committed
feat: Support setting LED brightness
close #1
1 parent e12eec6 commit af2db12

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

src/led-element.stories.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { storiesOf } from '@storybook/polymer';
2-
import { withKnobs, boolean, text } from '@storybook/addon-knobs';
2+
import { withKnobs, boolean, text, number } from '@storybook/addon-knobs';
33
import { html } from 'lit-html';
44
import './led-element';
55

6+
const brightnessOptions = {
7+
range: true,
8+
min: 0,
9+
max: 1.0,
10+
step: 0.05
11+
};
12+
613
storiesOf('LED', module)
714
.addDecorator(withKnobs)
815
.add(
916
'Red',
1017
() =>
1118
html`
12-
<wokwi-led color="red" .value=${boolean('value', false)}></wokwi-led>
19+
<wokwi-led
20+
color="red"
21+
.value=${boolean('value', false)}
22+
.brightness=${number('brightness', 1.0, brightnessOptions)}
23+
></wokwi-led>
1324
`
1425
)
1526
.add(
@@ -20,41 +31,74 @@ storiesOf('LED', module)
2031
color="red"
2132
.value=${boolean('value', false)}
2233
label="${text('label', '12')}"
34+
.brightness=${number('brightness', 1.0, brightnessOptions)}
2335
></wokwi-led>
2436
`
2537
)
2638
.add(
2739
'Green',
2840
() =>
2941
html`
30-
<wokwi-led color="green" .value=${boolean('value', false)}></wokwi-led>
42+
<wokwi-led
43+
color="green"
44+
.value=${boolean('value', false)}
45+
.brightness=${number('brightness', 1.0, brightnessOptions)}
46+
></wokwi-led>
3147
`
3248
)
3349
.add(
3450
'Yellow',
3551
() =>
3652
html`
37-
<wokwi-led color="yellow" .value=${boolean('value', false)}></wokwi-led>
53+
<wokwi-led
54+
color="yellow"
55+
.value=${boolean('value', false)}
56+
.brightness=${number('brightness', 1.0, brightnessOptions)}
57+
></wokwi-led>
3858
`
3959
)
4060
.add(
4161
'Blue',
4262
() =>
4363
html`
44-
<wokwi-led color="blue" .value=${boolean('value', false)}></wokwi-led>
64+
<wokwi-led
65+
color="blue"
66+
.value=${boolean('value', false)}
67+
.brightness=${number('brightness', 1.0, brightnessOptions)}
68+
></wokwi-led>
4569
`
4670
)
4771
.add(
4872
'Orange',
4973
() =>
5074
html`
51-
<wokwi-led color="orange" .value=${boolean('value', false)}></wokwi-led>
75+
<wokwi-led
76+
color="orange"
77+
.value=${boolean('value', false)}
78+
.brightness=${number('brightness', 1.0, brightnessOptions)}
79+
></wokwi-led>
5280
`
5381
)
5482
.add(
5583
'White',
5684
() =>
5785
html`
58-
<wokwi-led color="white" .value=${boolean('value', false)}></wokwi-led>
86+
<wokwi-led
87+
color="white"
88+
.value=${boolean('value', false)}
89+
.brightness=${number('brightness', 1.0, brightnessOptions)}
90+
></wokwi-led>
91+
`
92+
)
93+
.add(
94+
'Brightness Levels',
95+
() =>
96+
html`
97+
<wokwi-led color="red" label="0" .value=${true} brightness="0"></wokwi-led>
98+
<wokwi-led color="red" label="10%" .value=${true} brightness="0.1"></wokwi-led>
99+
<wokwi-led color="red" label="25%" .value=${true} brightness="0.25"></wokwi-led>
100+
<wokwi-led color="red" label="50%" .value=${true} brightness="0.5"></wokwi-led>
101+
<wokwi-led color="red" label="75%" .value=${true} brightness="0.75"></wokwi-led>
102+
<wokwi-led color="red" label="100%" .value=${true}></wokwi-led>
59103
`
60104
);

src/led-element.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const lightColors: { [key: string]: string } = {
1212
@customElement('wokwi-led')
1313
export class LEDElement extends LitElement {
1414
@property() value = false;
15+
@property() brightness = 1.0;
1516
@property() color = 'red';
1617
@property() lightColor: string | null = null;
1718
@property() label = '';
@@ -42,6 +43,7 @@ export class LEDElement extends LitElement {
4243
render() {
4344
const { color, lightColor } = this;
4445
const lightColorActual = lightColor || lightColors[color] || '#ff8080';
46+
const opacity = this.brightness ? 0.3 + this.brightness / 0.7 : 0;
4547
return html`
4648
<div class="led-container">
4749
<svg
@@ -117,7 +119,7 @@ export class LEDElement extends LitElement {
117119
<g
118120
class="light"
119121
xmlns="http://www.w3.org/2000/svg"
120-
style="display: ${this.value ? '' : 'none'}"
122+
style="display: ${this.value ? '' : 'none'}; opacity: ${opacity}"
121123
>
122124
<ellipse
123125
cx="8"

0 commit comments

Comments
 (0)