Skip to content

Instantly share code, notes, and snippets.

@jhalbrecht
Created February 9, 2015 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhalbrecht/07757c2501494a3e943f to your computer and use it in GitHub Desktop.
Save jhalbrecht/07757c2501494a3e943f to your computer and use it in GitHub Desktop.
til311 demo in bonescript
/*
Jeff Albrecht February 9, 2015
An experiment to display digits on a Texas Instruments TIL311. And practice my .js
http://www.ti.com/lit/ds/symlink/til311.pdf
Not implemented in this til311 experiment
left and right decimal points
configurable blank leading zeros
PWM blanking input for brightness control
Notes / stuff to remember.
jha handy pins http://kilobaser.com/blog/2014-07-15-beaglebone-black-gpios
oscon 2012 slides see 18 of 24
*/
var b = require('bonescript');
var latchA = "P8_46";
var latchB = "P8_42";
var latchC = "P8_38";
var latchD = "P8_34";
var strobeA = "P8_45";
var strobeB = "P8_41";
var strobeC = "P8_37";
var strobeD = "P8_33";
var til311pins = [latchA, latchB, latchC, latchD, strobeA, strobeB, strobeC, strobeD]; // order important. strobe lines calculated with offset of 4 in code
initPins();
// count and display from 0 to 9999
for (num = 0; num < 9999; num++) {
// setInterval(displayNumber(num), 1000); // odd this did not pause a second between digits display.
displayNumber(num);
sleep(1000);
}
// display a number on the four til311 elements. Pad with preceding 0s
function displayNumber(number) {
// console.log("number ", number);
var arrayNumber = ("" + number).split("");
// console.log("arrayNumber ", arrayNumber);
var pad = 4 - arrayNumber.length;
for (i = 0; i < pad; i++) {
arrayNumber.splice(0, 0, '0');
}
var i = 0;
for (digit in arrayNumber) {
displayDigits(arrayNumber[digit], i++);
}
}
// display a single digit on a single element where number to be displayed on place element position 0...3
function displayDigits(number, place) {
// console.log("number ", number, "place ", place);
b.digitalWrite(til311pins[place + 4], b.LOW); // strobe line for segment
var bNumber = parseInt(number).toString(2);
// console.log("bNumber ", bNumber);
var digits = ("" + bNumber).split("");
// console.log("digits ", digits);
// console.log("length of digits array ", digits.length);
if (digits.length < 4) { // left pad with '0' for an array length of exactly 4
var pad = 4 - digits.length;
// console.log("pad ", pad);
for (i = 0; i < pad; i++) {
digits.splice(0, 0, '0');
}
// console.log("digits ", digits);
}
// kludge! The bits are in a disadvantageous order for the for in loop. Swap the order of the array.
var digitsFix = new Array(4);
for (j = 3, k = 0; j >= 0; j--) {
digitsFix[j] = digits[k++];
}
for (digit in digitsFix) {
// console.log("charToState ", charToState(digits[digit]))
b.digitalWrite(til311pins[digit], charToState(digitsFix[digit]));
}
b.digitalWrite(til311pins[place + 4], b.HIGH); // inhibit strobe
}
function initPins() {
for (var j in til311pins) {
b.pinMode(til311pins[j], b.OUTPUT);
b.digitalWrite(til311pins[j], b.HIGH);
}
}
// I should experiment with just using 1 or 0 in the line that calls this.
function charToState(bit) {
// console.log("bit ", bit);
if (bit !== '1') {
return b.LOW;
}
else {
return b.HIGH;
}
}
// blocking delay
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment