diff --git a/build/screen.js b/build/screen.js index c6e96be..6719dc9 100644 --- a/build/screen.js +++ b/build/screen.js @@ -1,22 +1,12 @@ import { PrismaClient } from "@prisma/client"; -const prisma = new PrismaClient(); import LCD from "raspberrypi-liquid-crystal"; -const timer = (ms) => new Promise((res) => setTimeout(res, ms)); +const prisma = new PrismaClient(); +const TICK = 250; +const WAIT = 10; const lcd = new LCD(1, 0x27, 16, 2); -const tag = " Black Portal 1234 E Davison, Detroit Michigan"; lcd.beginSync(); lcd.clearSync(); -let tagCount = 0; -setInterval(() => { - lcd.printLineSync(1, " "); - setTimeout(() => { - lcd.printLineSync(1, tag.substring(tagCount, 16 + tagCount)); - }, 50); - tagCount++; - if (tagCount > tag.length) { - tagCount = 0; - } -}, 300); +const timer = (ms) => new Promise((res) => setTimeout(res, ms)); const getStanza = async () => { const stanzaCount = await prisma.stanza.count(); const skip = Math.floor(Math.random() * stanzaCount); @@ -26,27 +16,37 @@ const getStanza = async () => { }); return randomStanza[0].text; }; -let stanza = await getStanza(); -while (true) { - let outerInterval; - let innerInterval; - if (stanza) { - let characterLocation = 0; - outerInterval = setInterval(async () => { - lcd.printLineSync(0, " "); - innerInterval = setTimeout(() => { - lcd.printLineSync(0, `${16 - characterLocation > 0 - ? Array(16 - characterLocation).join(" ") - : ""}${stanza}`.substring(characterLocation, 16 + characterLocation)); - }, 50); - characterLocation++; - if (characterLocation > stanza.length) { - stanza = await getStanza(); - characterLocation = 0; - } - }, 500); +const tag = " Black Portal 1234 E Davison, Detroit Michigan "; +let tagCharacterLocation = 0; +setInterval(() => { + lcd.printLineSync(1, " "); + setTimeout(() => { + lcd.printLineSync(1, tag.substring(tagCharacterLocation, 16 + tagCharacterLocation)); + }, WAIT); + tagCharacterLocation++; + if (tagCharacterLocation > tag.length) { + tagCharacterLocation = 0; } - await timer((16 + stanza.length) * 550); - clearInterval(outerInterval); - clearInterval(innerInterval); +}, TICK); +while (true) { + let stanza = await getStanza(); + let stanzaOuterInterval; + let stanzaInnerInterval; + let stanzaCharacterLocation = 0; + stanzaOuterInterval = setInterval(async () => { + lcd.printLineSync(0, " "); + stanzaInnerInterval = setTimeout(() => { + lcd.printLineSync(0, `${16 - stanzaCharacterLocation > 0 + ? Array(16 - stanzaCharacterLocation).join(" ") + : ""}${stanza}`.substring(stanzaCharacterLocation, 16 + stanzaCharacterLocation)); + }, WAIT); + stanzaCharacterLocation++; + if (stanzaCharacterLocation > stanza.length) { + stanza = await getStanza(); + stanzaCharacterLocation = 0; + } + }, TICK); + await timer((16 + stanza.length) * (WAIT + TICK)); + clearInterval(stanzaOuterInterval); + clearInterval(stanzaInnerInterval); } diff --git a/screen.ts b/screen.ts index 1a9edae..fd92b96 100644 --- a/screen.ts +++ b/screen.ts @@ -1,29 +1,15 @@ import { PrismaClient, Stanza } from "@prisma/client"; -const prisma = new PrismaClient(); import LCD from "raspberrypi-liquid-crystal"; +const prisma = new PrismaClient(); -const timer = (ms: number) => new Promise((res) => setTimeout(res, ms)); +const TICK = 250; +const WAIT = 10; const lcd = new LCD(1, 0x27, 16, 2); -const tag = " Black Portal 1234 E Davison, Detroit Michigan"; lcd.beginSync(); lcd.clearSync(); -let tagCount = 0; - -setInterval(() => { - lcd.printLineSync(1, " "); - setTimeout(() => { - lcd.printLineSync(1, tag.substring(tagCount, 16 + tagCount)); - }, 50); - - tagCount++; - - if (tagCount > tag.length) { - tagCount = 0; - } -}, 300); - +const timer = (ms: number) => new Promise((res) => setTimeout(res, ms)); const getStanza = async () => { const stanzaCount = await prisma.stanza.count(); const skip = Math.floor(Math.random() * stanzaCount); @@ -36,34 +22,54 @@ const getStanza = async () => { return randomStanza[0].text as string; }; -let stanza = await getStanza(); +const tag = + " Black Portal 1234 E Davison, Detroit Michigan "; +let tagCharacterLocation = 0; +setInterval(() => { + lcd.printLineSync(1, " "); + setTimeout(() => { + lcd.printLineSync( + 1, + tag.substring(tagCharacterLocation, 16 + tagCharacterLocation) + ); + }, WAIT); + + tagCharacterLocation++; + if (tagCharacterLocation > tag.length) { + tagCharacterLocation = 0; + } +}, TICK); while (true) { - let outerInterval; - let innerInterval; - if (stanza) { - let characterLocation = 0; - outerInterval = setInterval(async () => { - lcd.printLineSync(0, " "); - innerInterval = setTimeout(() => { - lcd.printLineSync( - 0, - `${ - 16 - characterLocation > 0 - ? Array(16 - characterLocation).join(" ") - : "" - }${stanza}`.substring(characterLocation, 16 + characterLocation) - ); - }, 50); - characterLocation++; + let stanza = await getStanza(); - if (characterLocation > stanza.length) { - stanza = await getStanza(); - characterLocation = 0; - } - }, 500); - } - await timer((16 + stanza.length) * 550); - clearInterval(outerInterval); - clearInterval(innerInterval); + let stanzaOuterInterval; + let stanzaInnerInterval; + let stanzaCharacterLocation = 0; + stanzaOuterInterval = setInterval(async () => { + lcd.printLineSync(0, " "); + stanzaInnerInterval = setTimeout(() => { + lcd.printLineSync( + 0, + `${ + 16 - stanzaCharacterLocation > 0 + ? Array(16 - stanzaCharacterLocation).join(" ") + : "" + }${stanza}`.substring( + stanzaCharacterLocation, + 16 + stanzaCharacterLocation + ) + ); + }, WAIT); + stanzaCharacterLocation++; + + if (stanzaCharacterLocation > stanza.length) { + stanza = await getStanza(); + stanzaCharacterLocation = 0; + } + }, TICK); + + await timer((16 + stanza.length) * (WAIT + TICK)); + clearInterval(stanzaOuterInterval); + clearInterval(stanzaInnerInterval); }