Skip to content

Instantly share code, notes, and snippets.

@ISNIT0
Last active April 20, 2024 13:58
Show Gist options
  • Save ISNIT0/bda42b7db0a84aa534b66de770d35bb3 to your computer and use it in GitHub Desktop.
Save ISNIT0/bda42b7db0a84aa534b66de770d35bb3 to your computer and use it in GitHub Desktop.
import {
HorizontalAlignment,
VerticalAlignment,
Font,
LayoutUtils,
LedMatrix,
GpioMapping,
} from "rpi-led-matrix";
const wait = (t: number) => new Promise((ok) => setTimeout(ok, t));
const TFL_API_URL =
"https://api.tfl.gov.uk/Line/london-overground/Arrivals/910GCLAPTON?direction=inbound";
async function run() {
console.log("Refetching");
const arrivals = await fetchArrivals();
const now = Date.now();
const text = arrivals
.slice(0, 2)
.map((arrival: any, i: number) => {
const minCount = Math.floor(
(arrival.expectedArrival.valueOf() - now) / 60000
);
return ` Liverpool St. ${minCount} min`;
})
.join(" ");
await printText(text);
}
const _run = () => {
run().catch((err) => console.error(err));
};
setInterval(_run, 1000 * 60);
_run();
async function fetchArrivals() {
const response = await fetch(TFL_API_URL);
const data = (await response.json()) as any;
const arrivals = data.map((arrival: any) => ({
destinationName: arrival.destinationName,
expectedArrival: new Date(arrival.expectedArrival),
}));
const headingSouth = arrivals
.filter(
(arrival: any) =>
arrival.destinationName === "London Liverpool Street Rail Station"
)
.sort((a: any, b: any) => (a.expectedArrival - b.expectedArrival ? 1 : -1));
return headingSouth;
}
async function printText(text: string) {
const runtimeOptions = LedMatrix.defaultRuntimeOptions();
let alignmentH: HorizontalAlignment = HorizontalAlignment.Left;
let alignmentV: VerticalAlignment = VerticalAlignment.Top;
const path = `${process.cwd()}/HaxorMedium-10.bdf`;
const font = new Font("HaxorMedium-10", path);
try {
const matrix = new LedMatrix(
{
...LedMatrix.defaultMatrixOptions(),
rows: 64,
cols: 64,
chainLength: 2,
rowAddressType: 0,
hardwareMapping: GpioMapping.AdafruitHat,
disableHardwarePulsing: true,
},
runtimeOptions
);
matrix.brightness(100);
matrix.fgColor(0xffa500);
/* matrix
.clear() // clear the display
.brightness(100) // set the panel brightness to 100%
.fgColor(0x0000ff) // set the active color to blue
.fill() // color the entire diplay blue
.fgColor(0xffff00) // set the active color to yellow
// draw a yellow circle around the display
.drawCircle(
matrix.width() / 2,
matrix.height() / 2,
matrix.width() / 2 - 1
)
// draw a yellow rectangle
.drawRect(
matrix.width() / 4,
matrix.height() / 4,
matrix.width() / 2,
matrix.height() / 2
)
// sets the active color to red
.fgColor({ r: 255, g: 0, b: 0 })
// draw two diagonal red lines connecting the corners
.drawLine(0, 0, matrix.width(), matrix.height())
.drawLine(matrix.width() - 1, 0, 0, matrix.height() - 1);
*/
// const text = "Hello Satur-DIY!";
matrix.clear();
matrix.sync();
const fgColor = matrix.fgColor();
matrix.fgColor(matrix.bgColor()).fill().fgColor(fgColor);
matrix.font(font);
// const font = fonts[matrix.font()];
const lines = LayoutUtils.textToLines(font, matrix.width(), text);
const glyphs = LayoutUtils.linesToMappedGlyphs(
lines,
font.height(),
matrix.width(),
matrix.height(),
alignmentH,
alignmentV
);
for (const glyph of glyphs) {
matrix.drawText(glyph.char, glyph.x, glyph.y);
matrix.sync();
// await wait(150 * Math.random() + 20);
}
matrix.sync();
} catch (error) {
console.error(`${__filename} caught: `, error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment