import { PrismaClient, Stanza } from "@prisma/client"; import LCD from "raspberrypi-liquid-crystal"; const prisma = new PrismaClient(); const TICK = 250; const WAIT = 10; const lcd = new LCD(1, 0x27, 16, 2); lcd.beginSync(); lcd.clearSync(); 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); const randomStanza = await prisma.stanza.findMany({ skip: skip, take: 1, }); return randomStanza[0].text as string; }; const tag = " Black Portal 4856 E Davison, Detroit "; 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 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); }