Skip to content

Commit eb3b78b

Browse files
committed
feat(lcd1602): blink+underline cursor
support for display blinking + underline cursor together
1 parent 7a7d79c commit eb3b78b

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export { LEDElement } from './led-element';
22
export { PushbuttonElement } from './pushbutton-element';
33
export { ResistorElement } from './resistor-element';
44
export { SevenSegmentElement } from './7segment-element';
5-
export { LCD1602Element, ICursorType } from './lcd1602-element';
5+
export { LCD1602Element } from './lcd1602-element';
66
export { fontA00 } from './lcd1602-font-a00';
77
export { fontA02 } from './lcd1602-font-a02';

src/lcd1602-element.stories.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { withKnobs, text, select, number, boolean } from '@storybook/addon-knobs';
1+
import { withKnobs, text, number, boolean } from '@storybook/addon-knobs';
22
import { storiesOf } from '@storybook/web-components';
33
import { html } from 'lit-html';
44
import { fontA02 } from './lcd1602-font-a02';
@@ -15,7 +15,8 @@ storiesOf('LCD1602', module)
1515
() => html`
1616
<wokwi-lcd1602
1717
.characters="${encode(text('value', helloWorld))}"
18-
cursor=${select('cursor', ['off', 'blink', 'underline'], 'off')}
18+
.cursor=${boolean('cursor', false)}
19+
.blink=${boolean('blink', false)}
1920
cursorX=${number('cursorX', 0, { min: 0, max: 15 })}
2021
cursorY=${number('cursorY', 0, { min: 0, max: 1 })}
2122
.backlight=${boolean('backlight', true)}
@@ -28,8 +29,8 @@ storiesOf('LCD1602', module)
2829
<wokwi-lcd1602
2930
color="white"
3031
background="blue"
32+
blink="true"
3133
.characters="${encode(helloWorld)}"
32-
cursor="blink"
3334
></wokwi-lcd1602>
3435
`
3536
)
@@ -38,18 +39,18 @@ storiesOf('LCD1602', module)
3839
() => html`
3940
<wokwi-lcd1602
4041
.characters="${encode(helloWorld)}"
41-
cursor="blink"
42+
blink="true"
4243
cursorX="7"
4344
cursorY="1"
4445
></wokwi-lcd1602>
4546
`
4647
)
4748
.add(
48-
'Underline cursor',
49+
'Cursor',
4950
() => html`
5051
<wokwi-lcd1602
5152
.characters="${encode(helloWorld)}"
52-
cursor="underline"
53+
cursor="true"
5354
cursorX="7"
5455
cursorY="1"
5556
></wokwi-lcd1602>
@@ -73,7 +74,8 @@ storiesOf('LCD1602', module)
7374
<wokwi-lcd1602
7475
.characters="${encode(text('value', symbols))}"
7576
.font=${fontA02}
76-
cursor=${select('cursor', ['off', 'blink', 'underline'], 'off')}
77+
.cursor=${boolean('cursor', false)}
78+
.blink=${boolean('blink', false)}
7779
cursorX=${number('cursorX', 0, { min: 0, max: 15 })}
7880
cursorY=${number('cursorY', 0, { min: 0, max: 1 })}
7981
></wokwi-lcd1602>

src/lcd1602-element.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { customElement, html, LitElement, property, css, svg } from 'lit-element';
22
import { fontA00 } from './lcd1602-font-a00';
33

4-
export type ICursorType = 'off' | 'blink' | 'underline';
5-
64
const ROWS = 2;
75
const COLS = 16;
86

@@ -20,7 +18,8 @@ export class LCD1602Element extends LitElement {
2018
@property() background = 'green';
2119
@property({ type: Array }) characters: number[] | Uint8Array = new Uint8Array(32);
2220
@property() font = fontA00;
23-
@property() cursor: ICursorType = 'off';
21+
@property() cursor = false;
22+
@property() blink = false;
2423
@property() cursorX = 0;
2524
@property() cursorY = 0;
2625
@property() backlight = true;
@@ -77,9 +76,10 @@ export class LCD1602Element extends LitElement {
7776
return null;
7877
}
7978

80-
switch (this.cursor) {
81-
case 'blink':
82-
return svg`
79+
const result = [];
80+
81+
if (this.blink) {
82+
result.push(svg`
8383
<rect x="${xOffset}" y="${yOffset}" width="2.95" height="5.55" fill="${this.color}">
8484
<animate
8585
attributeName="opacity"
@@ -89,16 +89,17 @@ export class LCD1602Element extends LitElement {
8989
repeatCount="indefinite"
9090
/>
9191
</rect>
92-
`;
93-
94-
case 'underline': {
95-
const y = yOffset + 0.7 * 7;
96-
return svg`<rect x="${xOffset}" y="${y}" width="2.95" height="0.65" fill="${this.color}" />`;
97-
}
92+
`);
93+
}
9894

99-
default:
100-
return null;
95+
if (this.cursor) {
96+
const y = yOffset + 0.7 * 7;
97+
result.push(
98+
svg`<rect x="${xOffset}" y="${y}" width="2.95" height="0.65" fill="${this.color}" />`
99+
);
101100
}
101+
102+
return result;
102103
}
103104

104105
render() {

0 commit comments

Comments
 (0)