Skip to content

Commit d66e039

Browse files
committed
feat(led-bar-graph): add element wokwi#104
1 parent 662860d commit d66e039

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ export { KY040Element } from './ky-040-element';
4444
export { PhotoresistorSensorElement } from './photoresistor-sensor-element';
4545
export { RGBLedElement } from './rgb-led-element';
4646
export { ILI9341Element } from './ili9341-element';
47+
export { LedBarGraphElement } from './led-bar-graph-element';

src/led-bar-graph-element.stories.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { html } from 'lit-html';
2+
import './led-bar-graph-element';
3+
4+
export default {
5+
title: 'Led Bar Graph',
6+
component: 'wokwi-led-bar-graph',
7+
argTypes: {
8+
color: { control: { type: 'color' } },
9+
values: 'string',
10+
},
11+
args: {
12+
values: '[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]',
13+
color: 'red',
14+
},
15+
};
16+
17+
const Template = ({ color, values }) =>
18+
html`<wokwi-led-bar-graph values=${values} color=${color}></wokwi-led-bar-graph>`;
19+
20+
export const Red = Template.bind({});
21+
Red.args = { color: 'red' };
22+
23+
export const Green = Template.bind({});
24+
Green.args = { color: 'lime' };
25+
26+
export const Off = Template.bind({});
27+
Off.args = { color: 'lime', values: '[]' };

src/led-bar-graph-element.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { customElement, html, LitElement, property, svg } from 'lit-element';
2+
import { ElementPin } from './pin';
3+
import { mmToPix } from './utils/units';
4+
5+
const segments = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
6+
const mm = mmToPix;
7+
const anodeX = 1.27 * mm;
8+
const cathodeX = 8.83 * mm;
9+
10+
@customElement('wokwi-led-bar-graph')
11+
export class LedBarGraphElement extends LitElement {
12+
/** The color of a lit segment */
13+
@property() color = 'red';
14+
15+
/** The color of an unlit segment */
16+
@property() offColor = '#444';
17+
18+
readonly pinInfo: ElementPin[] = [
19+
{ name: 'A1', x: anodeX, y: 1.27 * mm, number: 1, description: 'Anode 1', signals: [] },
20+
{ name: 'A2', x: anodeX, y: 3.81 * mm, number: 2, description: 'Anode 2', signals: [] },
21+
{ name: 'A3', x: anodeX, y: 6.35 * mm, number: 3, description: 'Anode 3', signals: [] },
22+
{ name: 'A4', x: anodeX, y: 8.89 * mm, number: 4, description: 'Anode 4', signals: [] },
23+
{ name: 'A5', x: anodeX, y: 11.43 * mm, number: 5, description: 'Anode 5', signals: [] },
24+
{ name: 'A6', x: anodeX, y: 13.97 * mm, number: 6, description: 'Anode 6', signals: [] },
25+
{ name: 'A7', x: anodeX, y: 16.51 * mm, number: 7, description: 'Anode 7', signals: [] },
26+
{ name: 'A8', x: anodeX, y: 19.05 * mm, number: 8, description: 'Anode 8', signals: [] },
27+
{ name: 'A9', x: anodeX, y: 21.59 * mm, number: 9, description: 'Anode 9', signals: [] },
28+
{ name: 'A10', x: anodeX, y: 24.13 * mm, number: 10, description: 'Anode 10', signals: [] },
29+
{ name: 'C1', x: cathodeX, y: 1.27 * mm, number: 20, description: 'Cathode 1', signals: [] },
30+
{ name: 'C2', x: cathodeX, y: 3.81 * mm, number: 19, description: 'Cathode 2', signals: [] },
31+
{ name: 'C3', x: cathodeX, y: 6.35 * mm, number: 18, description: 'Cathode 3', signals: [] },
32+
{ name: 'C4', x: cathodeX, y: 8.89 * mm, number: 17, description: 'Cathode 4', signals: [] },
33+
{ name: 'C5', x: cathodeX, y: 11.43 * mm, number: 16, description: 'Cathode 5', signals: [] },
34+
{ name: 'C6', x: cathodeX, y: 13.97 * mm, number: 15, description: 'Cathode 6', signals: [] },
35+
{ name: 'C7', x: cathodeX, y: 16.51 * mm, number: 14, description: 'Cathode 7', signals: [] },
36+
{ name: 'C8', x: cathodeX, y: 19.05 * mm, number: 13, description: 'Cathode 8', signals: [] },
37+
{ name: 'C9', x: cathodeX, y: 21.59 * mm, number: 12, description: 'Cathode 9', signals: [] },
38+
{ name: 'C10', x: cathodeX, y: 24.13 * mm, number: 11, description: 'Cathode 10', signals: [] },
39+
];
40+
41+
/**
42+
* The values for the individual segments: 1 for a lit segment, and 0 for
43+
* an unlit segment.
44+
*/
45+
@property({ type: Array }) values: number[] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
46+
47+
render() {
48+
const { values, color, offColor } = this;
49+
return html`
50+
<svg
51+
width="10.1mm"
52+
height="25.5mm"
53+
version="1.1"
54+
viewBox="0 0 10.1 25.5"
55+
xmlns="http://www.w3.org/2000/svg"
56+
>
57+
<pattern id="pin-pattern" height="2.54" width="10.1" patternUnits="userSpaceOnUse">
58+
<circle cx="1.27" cy="1.27" r="0.5" fill="#aaa" />
59+
<circle cx="8.83" cy="1.27" r="0.5" fill="#aaa" />
60+
</pattern>
61+
<path d="m1.4 0h8.75v25.5h-10.1v-24.2z" />
62+
<rect width="10.1" height="25.4" fill="url(#pin-pattern)" />
63+
${segments.map(
64+
(index) =>
65+
svg`<rect x="2.5" y="${0.4 + index * 2.54}" width="5" height="1.74" fill="${
66+
values[index] ? color : offColor
67+
}"/>`
68+
)}
69+
</svg>
70+
`;
71+
}
72+
}

src/react-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { KY040Element } from './ky-040-element';
4343
import { PhotoresistorSensorElement } from './photoresistor-sensor-element';
4444
import { RGBLedElement } from './rgb-led-element';
4545
import { ILI9341Element } from './ili9341-element';
46+
import { LedBarGraphElement } from './led-bar-graph-element';
4647

4748
type WokwiElement<T> = Partial<T> & React.ClassAttributes<T>;
4849

@@ -91,6 +92,7 @@ declare global {
9192
'wokwi-photoresistor-sensor': WokwiElement<PhotoresistorSensorElement>;
9293
'wokwi-rgb-led': WokwiElement<RGBLedElement>;
9394
'wokwi-ili9341': WokwiElement<ILI9341Element>;
95+
'wokwi-led-bar-graph': WokwiElement<LedBarGraphElement>;
9496
}
9597
}
9698
}

0 commit comments

Comments
 (0)