Skip to content

Commit fcc30da

Browse files
committed
feat(lcd1602): add text property
allow getting/setting the text on the display
1 parent f2b3a19 commit fcc30da

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/lcd1602-element.stories.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { html } from 'lit-html';
44
import { fontA02 } from './lcd1602-font-a02';
55
import './lcd1602-element';
66

7-
const encode = (s: string) => new Uint8Array(s.split('').map((c) => c.charCodeAt(0)));
87
const helloWorld = 'Hello, World!';
98
const symbols = '\x10 I \x9d Symbols! \x11\xab \x14\x18\x17\x1e \x91\x98\x96 \x93\x97\xa9 \xbb';
109

@@ -15,7 +14,7 @@ storiesOf('LCD1602', module)
1514
'Hello, World!',
1615
() => html`
1716
<wokwi-lcd1602
18-
.characters="${encode(text('value', helloWorld))}"
17+
.text="${text('value', helloWorld)}"
1918
.cursor=${boolean('cursor', false)}
2019
.blink=${boolean('blink', false)}
2120
cursorX=${number('cursorX', 0, { min: 0, max: 15 })}
@@ -31,30 +30,20 @@ storiesOf('LCD1602', module)
3130
color="white"
3231
background="blue"
3332
blink="true"
34-
.characters="${encode(helloWorld)}"
33+
.text="${helloWorld}"
3534
></wokwi-lcd1602>
3635
`
3736
)
3837
.add(
3938
'Blinking cursor',
4039
() => html`
41-
<wokwi-lcd1602
42-
.characters="${encode(helloWorld)}"
43-
blink="true"
44-
cursorX="7"
45-
cursorY="1"
46-
></wokwi-lcd1602>
40+
<wokwi-lcd1602 .text="${helloWorld}" blink="true" cursorX="7" cursorY="1"></wokwi-lcd1602>
4741
`
4842
)
4943
.add(
5044
'Cursor',
5145
() => html`
52-
<wokwi-lcd1602
53-
.characters="${encode(helloWorld)}"
54-
cursor="true"
55-
cursorX="7"
56-
cursorY="1"
57-
></wokwi-lcd1602>
46+
<wokwi-lcd1602 .text="${helloWorld}" cursor="true" cursorX="7" cursorY="1"></wokwi-lcd1602>
5847
`
5948
)
6049
.add(
@@ -72,7 +61,7 @@ storiesOf('LCD1602', module)
7261
() =>
7362
html`
7463
<wokwi-lcd1602
75-
.characters="${encode(text('value', symbols))}"
64+
.text="${text('value', symbols)}"
7665
.font=${fontA02}
7766
.cursor=${boolean('cursor', false)}
7867
.blink=${boolean('blink', false)}
@@ -83,21 +72,10 @@ storiesOf('LCD1602', module)
8372
)
8473
.add(
8574
'I²C pins',
86-
() =>
87-
html`
88-
<wokwi-lcd1602
89-
.characters="${encode('I only need 4 pins!')}"
90-
pins="i2c"
91-
></wokwi-lcd1602>
92-
`
75+
() => html` <wokwi-lcd1602 text="I only need 4 pins!" pins="i2c"></wokwi-lcd1602> `
9376
)
9477
.add(
9578
'No pins',
9679
() =>
97-
html`
98-
<wokwi-lcd1602
99-
.characters="${encode('Look ma! I got no pins')}"
100-
pins="none"
101-
></wokwi-lcd1602>
102-
`
80+
html` <wokwi-lcd1602 text="Look ma! I got no pins" pins="none"></wokwi-lcd1602> `
10381
);

src/lcd1602-element.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export class LCD1602Element extends LitElement {
2626
@property() backlight = true;
2727
@property() pins: 'full' | 'i2c' | 'none' = 'full';
2828

29+
@property()
30+
get text() {
31+
return Array.from(this.characters)
32+
.map((c) => String.fromCharCode(c))
33+
.join('');
34+
}
35+
36+
set text(value: string) {
37+
this.characters = new Uint8Array(value.split('').map((char) => char.charCodeAt(0)));
38+
}
39+
2940
static get styles() {
3041
return css`
3142
.cursor-blink {

0 commit comments

Comments
 (0)