Skip to content

Instantly share code, notes, and snippets.

@edds
Created September 4, 2011 08:32
Show Gist options
  • Save edds/1192523 to your computer and use it in GitHub Desktop.
Save edds/1192523 to your computer and use it in GitHub Desktop.
Javscript LED Scroller implementation.
<!doctype html>
<title>LED Scroller</title>
<style>
div#scroller {
width: 720px;
background:#000;
border:3px solid #000;
overflow:hidden;
}
div#scroller div {
width: 4px;
height: 4px;
float: left;
margin: 1px;
background: #333;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
div#scroller div.on{
background: #f00;
}
</style>
<div id="scroller"></div>
<script src="./scroller.js"></script>
<script>
Scroller.init('scroller', 120,7);
Scroller.enqueue('Hello Edd. You Rock');
Scroller.start();
</script>
// Scroller.js
//
// returns Scroller for scrolling messages.
var Scroller = (function(){
var scroll = {},
led = [],
message = [],
messageOffset,
emptyLetters = 0,
letterSpaces;
scroll._messageQueue = [];
(function(){
// Lovingly borrowed from http://one.idontsmoke.co.uk/x/text/
// bitch stands for 'Basic Intergers and Typographic Characters displayed Horizontally'
var bitch = new Array();
bitch['a'] = ["00000","00000","01110","00001","01111","10001","01111"];
bitch['b'] = ["10000","10000","11110","10001","10001","10001","11110"];
bitch['c'] = ["00000","00000","01111","10000","10000","10000","01111"];
bitch['d'] = ["00001","00001","01111","10001","10001","10001","01111"];
bitch['e'] = ["00000","00000","01110","10001","11110","10000","01111"];
bitch['f'] = ["00111","00100","11110","01000","01000","01000","01000"];
bitch['g'] = ["00000","00000","01111","10001","01111","00001","01110"];
bitch['h'] = ["10000","10000","10110","11001","10001","10001","10001"];
bitch['i'] = ["01100","00000","01100","00100","00100","00100","01110"];
bitch['j'] = ["00110","00000","00110","00010","00010","10010","01100"];
bitch['k'] = ["10000","10000","10010","10100","11000","10100","10010"];
bitch['l'] = ["01100","00100","00100","00100","00100","00100","01110"];
bitch['m'] = ["00000","00000","11010","10101","10101","10101","10101"];
bitch['n'] = ["00000","00000","10110","11001","10001","10001","10001"];
bitch['o'] = ["00000","00000","01110","10001","10001","10001","01110"];
bitch['p'] = ["00000","00000","11110","10001","10001","11110","10000"];
bitch['q'] = ["00000","00000","01111","10001","10001","01111","00001"];
bitch['r'] = ["00000","00000","10111","11000","10000","10000","10000"];
bitch['s'] = ["00000","00000","01111","10000","01110","00001","11110"];
bitch['t'] = ["00100","00100","01111","00100","00100","00100","00011"];
bitch['u'] = ["00000","00000","10001","10001","10001","10011","01101"];
bitch['v'] = ["00000","00000","10001","10001","01010","01010","00100"];
bitch['w'] = ["00000","00000","10001","10101","10101","11011","10001"];
bitch['x'] = ["00000","00000","10001","01010","00100","01010","10001"];
bitch['y'] = ["00000","00000","10001","10001","01111","00001","01110"];
bitch['z'] = ["00000","00000","11111","00010","00100","01000","11111"];
bitch['A'] = ["01110","10001","10001","10001","11111","10001","10001"];
bitch['B'] = ["11110","10001","10001","11110","10001","10001","11110"];
bitch['C'] = ["01110","10001","10000","10000","10000","10001","01110"];
bitch['D'] = ["11110","10001","10001","10001","10001","10001","11110"];
bitch['E'] = ["11111","10000","10000","11110","10000","10000","11111"];
bitch['F'] = ["11111","10000","10000","11110","10000","10000","10000"];
bitch['G'] = ["01110","10001","10000","10011","10001","10001","01110"];
bitch['H'] = ["10001","10001","10001","11111","10001","10001","10001"];
bitch['I'] = ["01110","00100","00100","00100","00100","00100","01110"];
bitch['J'] = ["00111","00010","00010","00010","00010","10010","01100"];
bitch['K'] = ["10001","10010","10100","11000","10100","10010","10001"];
bitch['L'] = ["10000","10000","10000","10000","10000","10000","11111"];
bitch['M'] = ["10001","11011","10101","10101","10001","10001","10001"];
bitch['N'] = ["10001","10001","11001","10101","10011","10001","10001"];
bitch['O'] = ["01110","10001","10001","10001","10001","10001","01110"];
bitch['P'] = ["11110","10001","10001","11110","10000","10000","10000"];
bitch['Q'] = ["01110","10001","10001","10001","10101","10010","01101"];
bitch['R'] = ["11110","10001","10001","11110","10100","10010","10001"];
bitch['S'] = ["01110","10001","10000","01110","00001","10001","01110"];
bitch['T'] = ["11111","00100","00100","00100","00100","00100","00100"];
bitch['U'] = ["10001","10001","10001","10001","10001","10001","01110"];
bitch['V'] = ["10001","10001","10001","10001","01010","01010","00100"];
bitch['W'] = ["10001","10001","10001","10101","10101","11011","10001"];
bitch['X'] = ["10001","10001","01010","00100","01010","10001","10001"];
bitch['Y'] = ["10001","10001","10001","01010","00100","00100","00100"];
bitch['Z'] = ["11111","00001","00010","00100","01000","10000","11111"];
bitch['0'] = ["01110","10001","10011","10101","11001","10001","01110"];
bitch['1'] = ["00010","01110","00010","00010","00010","00010","00010"];
bitch['2'] = ["01110","10001","00001","00110","01000","10000","11111"];
bitch['3'] = ["01110","10001","00001","00110","00001","10001","01110"];
bitch['4'] = ["00010","00110","01010","10010","11111","00010","00010"];
bitch['5'] = ["11111","10000","11110","00001","00001","10001","01110"];
bitch['6'] = ["00110","01000","10000","11110","10001","10001","01110"];
bitch['7'] = ["11111","00001","00010","00010","00100","00100","00100"];
bitch['8'] = ["01110","10001","10001","01110","10001","10001","01110"];
bitch['9'] = ["01110","10001","10001","01111","00001","00010","01100"];
bitch["/"] = ["00001","00001","00010","00100","01000","10000","10000"];
bitch["\\"]= ["10000","10000","01000","00100","00010","00001","00001"];
bitch[":"] = ["00000","01100","01100","00000","01100","01100","00000"];
bitch["."] = ["00000","00000","00000","00000","00000","01100","01100"];
bitch[","] = ["00000","00000","00000","00000","00000","01100","00100"];
bitch[" "] = ["00000","00000","00000","00000","00000","00000","00000"];
bitch["("] = ["00010","00100","00100","00100","00100","00100","00010"];
bitch[")"] = ["01000","00100","00100","00100","00100","00100","01000"];
bitch["["] = ["00111","00100","00100","00100","00100","00100","00111"];
bitch["]"] = ["11100","00100","00100","00100","00100","00100","11100"];
bitch['<'] = ["00000","00010","00100","01000","00100","00010","00000"];
bitch['>'] = ["00000","01000","00100","00010","00100","01000","00000"];
bitch["-"] = ["00000","00000","00000","11111","00000","00000","00000"];
bitch["="] = ["00000","00000","11111","00000","11111","00000","00000"];
bitch["'"] = ["00110","00110","00010","00000","00000","00000","00000"];
bitch["`"] = ["01000","00100","00010","00000","00000","00000","00000"];
bitch['"'] = ["11011","11011","01001","00000","00000","00000","00000"];
scroll.bitch = bitch;
}());
scroll.enqueue = function(msg){
// assume the index is the length - 1. Risky I know.
return scroll._messageQueue.push(msg) - 1;
};
scroll.dequeue = function(index){
delete scroll._messageQueue[index];
}
scroll.clearQueue = function(){
scroll._messageQueue = [];
};
scroll.start = function(){
if(!scroll.intervalTimer){
scroll.appendMessages();
scroll.intervalTimer = window.setInterval(scroll.run, 50); // e3 to turn into miliseconds
}
};
scroll.stop = function(){
if(scroll.intervalTimer){
window.clearInterval(scroll.intervalTimer);
delete scroll.intervalTimer;
}
};
scroll.drawLetter = function(letter, xOffset){
var offset = (typeof xOffset !== 'undefined') ? xOffset : 0,
i, j, v, obj;
for(i = 0; i < 7; i++){
for(j = 0; j < 7; j++){
if( offset + i >= 0 && offset + i < scroll._cols ){
if(scroll.bitch[letter]){
v = (scroll.bitch[letter][j].charAt(i) === '1') ? 'on' : 'off';
obj = led[j][i+offset];
if(obj.value !== v){
obj.value = v;
obj.elm.setAttribute('class', v);
}
}
}
}
}
};
scroll.appendMessages = function(){
var messages = scroll._messageQueue.join(' .:. ');
if(messages.length){
message = message.concat(messages.split(''));
} else {
scroll.stop();
}
};
scroll.addBlank = function(){
if(message.length < letterSpaces){
message.push(' ');
emptyLetters++;
if(emptyLetters > letterSpaces){
scroll.appendMessages();
emptyLetters = 0;
}
}
}
scroll.run = function(){
if(messageOffset === 7){
messageOffset = 0;
message.shift();
scroll.addBlank();
}
var i, j;
for(i = -messageOffset, j = 0; i < scroll._cols; j++){
scroll.drawLetter(message[j], i);
i = i + 7;
}
messageOffset = messageOffset + 1;
};
scroll.init = function(elm, cols, rows){
var i, j, elm;
scroll._body = document.getElementById(elm);
scroll._rows = rows;
scroll._cols = cols;
letterSpaces = Math.ceil(cols / 7);
// Fill all available spaces with blanks
message = new Array(letterSpaces + 1).join(' ').split('');
messageOffset = 0;
for(i=0; i<rows; i++){
led[i] = [];
for(j=0; j<cols; j++){
elm = document.createElement('div');
elm.setAttribute('id', 'led-'+i+'-'+j);
scroll._body.appendChild(elm);
led[i][j] = {
value: 'off',
elm: elm
}
}
}
};
return scroll;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment