prep for additional bottom line displays

This commit is contained in:
Matt Walsh
2025-05-20 16:28:56 -05:00
parent 1a5548d135
commit 4cdced3659
4 changed files with 99 additions and 8 deletions

View File

@@ -5,10 +5,14 @@ import { currentDisplay } from './navigation.mjs';
// constants
const degree = String.fromCharCode(176);
const SCROLL_SPEED = 75; // pixels/second
const DEFAULT_UPDATE = 8; // 0.5s ticks
// local variables
let interval;
let screenIndex = 0;
let sinceLastUpdate = 0;
let nextUpdate = DEFAULT_UPDATE;
// start drawing conditions
// reset starts from the first item in the text scroll list
@@ -17,7 +21,7 @@ const start = () => {
// set up the interval if needed
if (!interval) {
interval = setInterval(incrementInterval, 4000);
interval = setInterval(incrementInterval, 500);
}
// draw the data
@@ -29,14 +33,24 @@ const stop = (reset) => {
};
// increment interval, roll over
const incrementInterval = () => {
// forcing is used when drawScreen receives an invalid screen and needs to request the next one in line
const incrementInterval = (force) => {
if (!force) {
// test for elapsed time (0.5s ticks);
sinceLastUpdate += 1;
if (sinceLastUpdate < nextUpdate) return;
}
// reset flags
sinceLastUpdate = 0;
nextUpdate = DEFAULT_UPDATE;
// test current screen
const display = currentDisplay();
if (!display?.okToDrawCurrentConditions) {
stop(display?.elemId === 'progress');
return;
}
screenIndex = (screenIndex + 1) % (screens.length);
screenIndex = (screenIndex + 1) % (lastScreen);
// draw new text
drawScreen();
};
@@ -48,7 +62,22 @@ const drawScreen = async () => {
// nothing to do if there's no data yet
if (!data) return;
drawCondition(screens[screenIndex](data));
const thisScreen = screens[screenIndex](data);
if (typeof thisScreen === 'string') {
// only a string
drawCondition(thisScreen);
} else if (typeof thisScreen === 'object') {
// an object was provided with additional parameters
switch (thisScreen.type) {
case 'scroll':
drawScrollCondition(thisScreen);
break;
default: drawCondition(thisScreen);
}
} else {
// can't identify screen, get another one
incrementInterval(true);
}
};
// the "screens" are stored in an array for easy addition and removal
@@ -71,7 +100,7 @@ const screens = [
(data) => `Humidity: ${data.Humidity}% Dewpoint: ${data.DewPoint}${degree}${data.TemperatureUnit}`,
// barometric pressure
(data) => `Barometric Pressure: ${data.Pressure} ${data.PressureUnit} ${data.PressureDirection}`,
(data) => `Barometric Pressure: ${data.Pressure} ${data.PressureDirection}`,
// wind
(data) => {
@@ -102,3 +131,56 @@ const drawCondition = (text) => {
document.addEventListener('DOMContentLoaded', () => {
start();
});
// store the original number of screens
const originalScreens = screens.length;
let lastScreen = originalScreens;
// reset the number of screens
const reset = () => {
lastScreen = originalScreens;
};
// add screen
const addScreen = (screen) => {
screens.push(screen);
lastScreen += 1;
};
const drawScrollCondition = (screen) => {
// create the scroll element
const scrollElement = document.createElement('div');
scrollElement.classList.add('scroll-area');
scrollElement.innerHTML = screen.text;
// add it to the page to get the width
document.querySelector('.weather-display .scroll .fixed').innerHTML = scrollElement.outerHTML;
// grab the width
const { scrollWidth, clientWidth } = document.querySelector('.weather-display .scroll .fixed .scroll-area');
// calculate the scroll distance and set a minimum scroll
const scrollDistance = Math.max(scrollWidth - clientWidth, 0);
// calculate the scroll time
const scrollTime = scrollDistance / SCROLL_SPEED;
// calculate a new minimum on-screen time +1.0s at start and end
nextUpdate = Math.round(Math.ceil(scrollTime / 0.5) + 4);
// update the element transition and set initial left position
scrollElement.style.left = '0px';
scrollElement.style.transition = `left linear ${scrollTime.toFixed(1)}s`;
elemForEach('.weather-display .scroll .fixed', (elem) => {
elem.innerHTML = '';
elem.append(scrollElement.cloneNode(true));
});
// start the scroll after a short delay
setTimeout(() => {
// change the left position to trigger the scroll
elemForEach('.weather-display .scroll .fixed .scroll-area', (elem) => {
elem.style.left = `-${scrollDistance.toFixed(0)}px`;
});
}, 1000);
};
window.CurrentWeatherScroll = {
addScreen,
reset,
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -113,7 +113,7 @@
.scroll {
@include u.text-shadow(3px, 1.5px);
width: 640px;
width: calc(640px - 2 * 30px);
height: 70px;
overflow: hidden;
margin-top: 10px;
@@ -122,6 +122,15 @@
font-family: 'Star4000';
font-size: 24pt;
margin-left: 55px;
overflow: hidden;
.scroll-area {
text-wrap: nowrap;
position: relative;
// the following added by js code as it is dependent on the content of the element
// transition: left (x)s;
// left: calc((elem width) - 640px);
}
}
}
}