[sensehatsim] Add support for scrolling text

This commit is contained in:
Nick O'Leary
2020-06-18 00:44:17 +01:00
parent 696a99eaa3
commit 98d5df965c
5 changed files with 212 additions and 124 deletions

View File

@@ -25,6 +25,9 @@
box-shadow: 2px 2px 1px #99cc99;
}
#display.low_brightness .led {
filter: brightness(0.6);
}
.led {
background-color: #000;
border: 1px solid #eee;
@@ -162,6 +165,9 @@
</div>
<script>
var fontData = {" ":[0,0,0,0,0,0,0,0],"+":[0,4,4,31,4,4,0,0],"-":[0,0,0,31,0,0,0,0],"*":[0,4,21,14,21,4,0,0],"/":[0,16,8,4,2,1,0,0],"!":[4,0,4,4,4,4,4,0],"\"":[0,0,0,0,10,10,10,0],"#":[10,10,31,10,31,10,10,0],"$":[4,30,5,14,20,15,4,0],">":[8,4,2,1,2,4,8,0],"<":[2,4,8,16,8,4,2,0],"0":[14,17,25,21,19,17,14,0],"1":[14,4,4,4,4,12,4,0],"2":[31,8,4,2,1,17,14,0],"3":[14,17,1,2,4,2,31,0],"4":[2,2,31,18,10,6,2,0],"5":[14,17,1,1,30,16,31,0],"6":[14,17,17,30,16,8,6,0],"7":[4,4,4,4,2,1,31,0],"8":[14,17,17,14,17,17,14,0],"9":[12,2,1,15,17,17,14,0],".":[12,12,0,0,0,0,0,0],"=":[0,0,31,0,31,0,0,0],")":[8,4,2,2,2,4,8,0],"(":[2,4,8,8,8,4,2,0],"A":[17,17,17,31,17,17,14,0],"B":[30,17,17,30,17,17,30,0],"C":[14,17,16,16,16,17,14,0],"D":[28,18,17,17,17,18,28,0],"E":[31,16,16,30,16,16,31,0],"F":[16,16,16,30,16,16,31,0],"G":[15,17,17,23,16,17,14,0],"H":[17,17,17,31,17,17,17,0],"I":[14,4,4,4,4,4,14,0],"J":[12,18,2,2,2,2,7,0],"K":[17,18,20,24,20,18,17,0],"L":[31,16,16,16,16,16,16,0],"M":[17,17,17,21,21,27,17,0],"N":[17,17,19,21,25,17,17,0],"O":[14,17,17,17,17,17,14,0],"P":[16,16,16,30,17,17,30,0],"Q":[13,18,21,17,17,17,14,0],"R":[17,18,20,30,17,17,30,0],"S":[30,1,1,14,16,16,15,0],"T":[4,4,4,4,4,4,31,0],"U":[14,17,17,17,17,17,17,0],"V":[4,10,17,17,17,17,17,0],"W":[17,27,21,21,17,17,17,0],"X":[17,17,10,4,10,17,17,0],"Y":[4,4,4,4,10,17,17,0],"Z":[31,16,8,4,2,1,31,0],"a":[15,17,15,1,14,0,0,0],"b":[14,17,17,25,22,16,16,0],"c":[14,17,16,16,14,0,0,0],"d":[15,17,17,19,13,1,1,0],"e":[14,16,31,17,14,0,0,0],"f":[4,4,4,14,4,5,2,0],"g":[14,1,15,17,15,0,0,0],"h":[17,17,17,25,22,16,16,0],"i":[14,4,4,4,12,0,4,0],"j":[12,18,2,2,2,0,2,0],"k":[9,10,12,10,9,8,8,0],"l":[14,4,4,4,4,4,12,0],"m":[21,21,21,21,26,0,0,0],"n":[17,17,17,25,22,0,0,0],"o":[14,17,17,17,14,0,0,0],"p":[16,16,30,17,30,0,0,0],"q":[1,1,15,17,15,0,0,0],"r":[8,8,8,12,11,0,0,0],"s":[30,1,14,16,15,0,0,0],"t":[2,5,4,4,14,4,0,0],"u":[13,19,17,17,17,0,0,0],"v":[4,10,17,17,17,0,0,0],"w":[10,21,21,17,17,0,0,0],"x":[19,12,4,6,25,0,0,0],"y":[24,4,6,9,17,0,0,0],"z":[31,8,4,2,31,0,0,0],"?":[4,0,4,2,1,17,14,0],",":[8,4,12,0,0,0,0,0],";":[8,4,12,0,12,12,0,0],":":[0,12,12,0,12,12,0,0],"|":[4,4,4,4,4,4,4,0],"@":[14,21,21,13,1,17,14,0],"%":[3,19,8,4,2,25,24,0],"[":[14,8,8,8,8,8,14,0],"&":[13,18,21,8,20,18,12,0],"_":[31,0,0,0,0,0,0,0],"'":[0,0,0,0,8,4,12,0],"]":[14,2,2,2,2,2,14,0],"\\":[0,1,2,4,8,16,0,0],"~":[0,0,0,22,13,0,0,0]};
var temperature = 29;
var humidity = 80;
var pressure = 1000;
@@ -256,8 +262,10 @@ function connect() {
var location = document.location.toString().replace(/^http/,"ws")+"/ws";
ws = new WebSocket(location);
ws.onopen = function() {
console.log("Connected to Node-RED")
}
ws.onclose = function() {
console.log("Lost connection, reconnecting in 5 seconds")
setTimeout(connect,5000);
}
ws.onmessage = function(msg) {
@@ -265,102 +273,180 @@ function connect() {
var data = msg.data.substring(1);
var parts;
var x,y,t;
if (command === 'P') {
parts = data.split(",");
for (var i=0;i<parts.length;i+=5) {
x = parts[i];
y = parts[i+1];
if (flipH) { x = 7-x; }
if (flipV) { y = 7-y; }
if (rotation === 180) {
x = 7-x;
y = 7-y;
} else if (rotation === 90) {
if (y<4 && x<4) { t = y; y = x; x = 7-t; }
else if (y<4 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x<4) { t=y; y = x; x = 7-t; }
} else if (rotation === 270) {
if (y<4 && x<4) { t = x; x = y; y = 7-t; }
else if (y<4 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x<4) { t=x; x = y; y = 7-t; }
}
cells[y][x].style.backgroundColor = "rgb("+parts[i+2]+","+parts[i+3]+","+parts[i+4]+")"
}
} else if (command === 'Y') {
if (command === 'Y') {
parts = data.split(",");
updateTemperature(Number(parts[0]));
updateHumidity(Number(parts[1]));
updatePressure(Number(parts[2]));
} else if (command === 'R') {
var delta = Number(data) - rotation;
if (delta < 0) {
delta += 360;
} else if (command === 'D') {
if (data === '0') {
document.getElementById("display").classList.add("low_brightness");
} else if (data === '1') {
document.getElementById("display").classList.remove("low_brightness");
}
if (delta === 90) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
} else {
clearTimeout(scrollMessageTimeout);
if (command === 'P') {
parts = data.split(",");
for (var i=0;i<parts.length;i+=5) {
x = parts[i];
y = parts[i+1];
if (flipH) { x = 7-x; }
if (flipV) { y = 7-y; }
if (rotation === 180) {
x = 7-x;
y = 7-y;
} else if (rotation === 90) {
if (y<4 && x<4) { t = y; y = x; x = 7-t; }
else if (y<4 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x<4) { t=y; y = x; x = 7-t; }
} else if (rotation === 270) {
if (y<4 && x<4) { t = x; x = y; y = 7-t; }
else if (y<4 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x<4) { t=x; x = y; y = 7-t; }
}
cells[y][x].style.backgroundColor = "rgb("+parts[i+2]+","+parts[i+3]+","+parts[i+4]+")"
}
} else if (delta === 270) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
} else if (command === 'R') {
var delta = Number(data) - rotation;
if (delta < 0) {
delta += 360;
}
} else if (delta === 180) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][7-x].style.backgroundColor;
cells[7-y][7-x].style.backgroundColor = t;
t = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
}
rotation = Number(data);
//data = 0,90,180,270
} else if (command === 'F') {
if (data === 'H') {
flipH = !flipH;
for (y=0;y<8;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else {
flipV = !flipV;
for (x=0;x<8;x++) {
if (delta === 90) {
for (y=0;y<4;y++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else if (delta === 270) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
}
} else if (delta === 180) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][7-x].style.backgroundColor;
cells[7-y][7-x].style.backgroundColor = t;
t = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
}
rotation = Number(data);
//data = 0,90,180,270
} else if (command === 'F') {
if (data === 'H') {
flipH = !flipH;
for (y=0;y<8;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else {
flipV = !flipV;
for (x=0;x<8;x++) {
for (y=0;y<4;y++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
}
}
} else if (command === 'T'){
var splitAt = data.indexOf(":");
var colors = data.substring(0,splitAt);
var message = data.substring(splitAt+1);
var colorParts = colors.split(",");
var foreground = "rgb("+colorParts[0]+","+colorParts[1]+","+colorParts[2]+")";
var background = "rgb("+colorParts[3]+","+colorParts[4]+","+colorParts[5]+")";
if (message.length === 1) {
drawCharAt(message[0],0,foreground,background)
} else {
currentMessage = {
foreground: foreground,
background: background,
text: " "+message +" ",
speed: colors.length === 7?colors[6]:0.1,
pos: 0,
subpos: 0
}
stepMessage();
}
}
}
}
}
function drawCharAt(char,cx,foreground,background) {
var charData = fontData[char]||fontData["?"];
for (var y=0;y<8;y++) {
var line = charData[y].toString(2);
while(line.length < 5) {
line = "0"+line;
}
for (var j=0;j<6;j++) {
var x = cx + j;
if (x >= 0 && x < 8) {
if (j === 5) {
cells[7-y][x].style.backgroundColor = background;
} else {
if (line[j] === "1") {
cells[7-y][x].style.backgroundColor = foreground;
} else {
cells[7-y][x].style.backgroundColor = background;
}
}
}
}
}
}
var currentMessage;
var scrollMessageTimeout;
function stepMessage() {
var x = 0;
var subpos = currentMessage.subpos;
var colCount = 0;
for (var i=currentMessage.pos;i<Math.min(currentMessage.pos+3,currentMessage.text.length);i++) {
var char = currentMessage.text[i];
drawCharAt(char,x-subpos,currentMessage.foreground,currentMessage.background)
x += 5-subpos;
subpos = 0;
}
currentMessage.subpos++;
if (currentMessage.subpos === 5) {
currentMessage.subpos = 0;
currentMessage.pos++;
}
if (currentMessage.pos < currentMessage.text.length) {
scrollMessageTimeout = setTimeout(stepMessage,currentMessage.speed*1000);
}
}
connect();
</script>
</body>