Skip to content

Commit ac8f5e8

Browse files
committed
fix(lcd1602): don't show cursor out of boundries
1 parent 7f7a061 commit ac8f5e8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/lcd1602-element.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { fontA00 } from './lcd1602-font-a00';
33

44
export type ICursorType = 'off' | 'blink' | 'underline';
55

6+
const ROWS = 2;
7+
const COLS = 16;
8+
69
const charXSpacing = 3.55;
710
const charYSpacing = 5.95;
811

@@ -50,8 +53,8 @@ export class LCD1602Element extends LitElement {
5053
const ySpacing = 0.7;
5154
const result = [];
5255
for (let i = 0; i < characters.length; i++) {
53-
const charX = (i % 16) * charXSpacing;
54-
const charY = Math.floor(i / 16) * charYSpacing;
56+
const charX = (i % COLS) * charXSpacing;
57+
const charY = Math.floor(i / COLS) * charYSpacing;
5558

5659
for (let py = 0; py < 8; py++) {
5760
const row = this.font[characters[i] * 8 + py];
@@ -70,6 +73,10 @@ export class LCD1602Element extends LitElement {
7073
renderCursor() {
7174
const xOffset = 12.45 + this.cursorX * charXSpacing;
7275
const yOffset = 12.55 + this.cursorY * charYSpacing;
76+
if (this.cursorX < 0 || this.cursorX >= COLS || this.cursorY < 0 || this.cursorY >= ROWS) {
77+
return null;
78+
}
79+
7380
switch (this.cursor) {
7481
case 'blink':
7582
return svg`

0 commit comments

Comments
 (0)