Skip to content

Instantly share code, notes, and snippets.

@MatthaeusHarris
Created December 1, 2014 06:55
Show Gist options
  • Save MatthaeusHarris/ac89aa5c110ac301a292 to your computer and use it in GitHub Desktop.
Save MatthaeusHarris/ac89aa5c110ac301a292 to your computer and use it in GitHub Desktop.
RTC Clock POC
var LCD = {};
LCD.connect = function(/*=SPI*/_spi, /*=PIN*/_dc, /*=PIN*/_ce, /*=PIN*/_rst, callback) {
var LCD = Graphics.createArrayBuffer(84,48,1,{vertical_byte:true});
var spi = _spi;
var dc = _dc;
var ce = _ce;
var rst = _rst;
setTimeout(function() {
digitalWrite(dc,0); // cmd
digitalPulse(rst, 0, 10); // pulse reset low
setTimeout(function() {
spi.send(
[0x21, // fnset extended
0x80 | 0x3F, // setvop (experiment with 2nd val to get the right contrast)
0x14, // setbias 4
0x04 | 0x02, // temp control
0x20, // fnset normal
0x08 | 0x04], ce); // dispctl normal
if (callback!==undefined) callback();
}, 100);
}, 100);
LCD.flip = function () {
digitalWrite(dc,0); // cmd
spi.send([0x40,0x80],ce); // X + Y addr (0,0)
digitalWrite(dc,1); // data
spi.send(this.buffer,ce);
};
LCD.setContrast = function(c) { // c between 0 and 1. 0.5 is default
digitalWrite(dc,0); // cmd
spi.send(
[0x21, // fnset extended
0x80 | Math.clip(c*0x7f,0,0x7f), // setvop
0x20, // fnset normal
0x08 | 0x04], ce); // dispctl normal
};
return LCD;
};
var rtcDateMath = {
rtcToDate: function(rtcArray) {
var seconds = 0,
minutes = 1,
hours = 2,
day = 3,
date = 4,
month = 5,
year = 6;
var workingArray = [
[],
[],
[],
[],
[],
[],
[]
];
var finalArray = [0,0,0,0,0,0,0];
// console.log("Convert BCD into working arrays");
// Convert BCD into working arrays
for (var i = 0; i < 7; i++) {
for (var j = 0; j < 8; j++) {
workingArray[i][j] = rtcArray[i] & 1<<j;
}
}
// console.log(workingArray);
// console.log("Convert BCD");
// Convert BCD
for (i = 0; i < 7; i++) {
for (j = 0; j <= 3; j++) {
finalArray[i] += workingArray[i][j];
}
if (i !== hours) {
for (j = 4; j <= 7; j++) {
finalArray[i] += workingArray[i][j] / 16 * 10;
}
}
}
// console.log("Handle hours specially");
// Hours need to be handled specially.
finalArray[hours] += workingArray[hours][4] / 16 * 10;
if (workingArray[hours][6] === 0) {
// 24-hour mode
finalArray[hours] += workingArray[hours][5] / 16 * 10;
} else {
// 12-hour mode
finalArray[hours] += workingArray[hours][5] / 16 * 12;
}
// console.log("Fix year");
// Fix year, nasty hack way
if (finalArray[year] < 70) {
finalArray[year] += 2000;
} else {
finalArray[year] += 1900;
}
// console.log(finalArray);
// console.log("Convert to strings");
// Convert to strings
var doublize = [seconds, minutes];
var stringArray = finalArray.map(function(i,j) {
if (i-10<0 && doublize.indexOf(j) !== -1) {
return "0"+i;
} else {
return ""+i;
}
});
// console.log("Assemble output string");
var outputString =
stringArray[year] + '-' + stringArray[month] + '-' +
stringArray[date] + 'T' + stringArray[hours] + ':' +
stringArray[minutes] + ':' + stringArray[seconds] + '';
// console.log("done!");
return outputString;
}
};
var testRTCMath = function() {
I2C1.writeTo(104,0);
var c = I2C1.readFrom(104,7);
return rtcDateMath.rtcToDate(c);
};
var clockInterval;
var rawData = [];
var startClock = function() {
clockInterval = setInterval(function() {
I2C1.writeTo(104,0);
var readArray = I2C1.readFrom(104,7);
var changed = false;
for (var i = 0; i < 7; i++) {
if (rawData[i] !== readArray[i]) {
changed = true;
}
}
if (changed) {
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var date = new Date(rtcDateMath.rtcToDate(readArray));
var minutes = date.getMinutes();
minutes = (minutes-9>0)?minutes:"0"+minutes;
var seconds = date.getSeconds();
seconds = (seconds-9>0)?seconds:"0"+seconds;
var s = date.getHours() + ":" + minutes + ":" + seconds;
var d = date.getTime() / 1000;
g.clear();
g.setFontVector(16);
g.drawString(s,0,0);
g.setFontVector(10);
s = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
g.drawString(s,0,20);
for (var i = 31; i >=0; i--) {
for (var j = 38; j < 45; j++) {
g.setPixel((31-i)+20, j, d&1<<i?1:0);
}
}
g.drawRect(18,36,53,47);
g.flip();
rawData = readArray;
}
}, 100);
};
var stopClock = function() {
clearInterval(clockInterval);
};
var g;
var onInit = function() {
I2C1.setup({scl:B6,sda:B7});
var ledState = false;
var throbInterval = setInterval(function() {
ledState = !ledState;
digitalWrite(LED3, ledState);
}, 250);
setTimeout(function() {
clearInterval(throbInterval);
digitalWrite(LED3, 0);
SPI1.setup({ sck:A5, mosi:A7 });
g = LCD.connect(SPI1,B0,B1,B10, function() {
g.setContrast(0.45);
g.clear();
g.drawString("Clock Initialzing",0,0);
g.drawLine(0,10,84,10);
g.flip();
analogWrite(A3, 0.5, {freq: 120});
g.setFontVector(18);
startClock();
});
},2000);
};
save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment