Skip to content

Instantly share code, notes, and snippets.

@gesellix
Last active August 29, 2015 14:20
Show Gist options
  • Save gesellix/9f1c429f4b566c5052f4 to your computer and use it in GitHub Desktop.
Save gesellix/9f1c429f4b566c5052f4 to your computer and use it in GitHub Desktop.
Espruino Pico
SPI2.setup({baud:3200000,mosi:B15});
var setColor = function(arr){
SPI2.send4bit(arr, 0b0001, 0b0011);
};
setColor([255,0,0, 0,255,0, 0,0,255]);
/*
var ledCount = 3;
var colors = new Uint8ClampedArray(ledCount*3);
setInterval(function(){
var ix = 0;
for (var i=0;i<ledCount;i++) {
colors[ix++] = Math.random()*255;
colors[ix++] = Math.random()*255;
colors[ix++] = Math.random()*255;
}
setColor(colors);
}, 500);
*/
A5.write(0); // GND
A7.write(1); // VCC
//A6.write(1);
var spi = new SPI();
spi.setup({ sck:B1, mosi:B10 });
var updateDisplay = function(displayText){
g.clear();
g.drawString(displayText,0,0);
g.drawLine(0,10,84,10);
g.flip();
//setTimeout("A6.write(0);console.log('done');", 500);
//console.log('g: '+Object.keys(g));
};
var displayText = "Hello";
var g = require("PCD8544").connect(spi,B13,B14,B15, function() {
console.log('hello world...');
updateDisplay("Hello");
});
var btnCount = 0;
var onBtn = function(){
console.log('btn pressed');
btnCount++;
updateDisplay("count: "+btnCount);
};
setWatch(onBtn, BTN, {repeat: true, edge: "rising", debounce: 50});
pinMode(B3,"input_pulldown"); // enable the pull-down resistor
var on = false;
var shakes = 0;
function shaken() {
if (on) return; // if we haven't timed out, ignore this
// otherwise, turn the light on
on = true;
digitalWrite(LED1, on);
// count shakes
shakes++;
console.log("Shaken "+shakes+" times");
updateDisplay("shaken "+shakes+" times");
// now set a timeout to turn it off after 200ms = 1/5 second
setTimeout(function() {
on = false;
digitalWrite(LED1, on);
}, 200);
}
// Now 'watch' B3 for shakes
setWatch(shaken, B3, {repeat:true, edge:"rising"});
A5.write(0); // GND
A7.write(1); // VCC
//A6.write(1);
var initialised = false;
var spi;
var g;
var draw = function(usage) {
if (!initialised || usage===undefined) {
return;
}
usage = parseFloat(usage.toString().replace(",",".")); // deal with numbers in the format '1,23'
usage = usage % 100;
g.clear();
g.drawString("CPU Usage: "+usage+"%");
// draw 'needle'
var ang = Math.PI*usage/100;
g.drawLine(42,47,42-28*Math.cos(ang), 47-Math.sin(ang)*28);
// draw labels
for (var i=0;i<=100;i+=20) {
ang = Math.PI*i/100;
g.drawString(i, 42-36*Math.cos(ang)-(g.stringWidth(i)/2), 40-Math.sin(ang)*30);
}
// draw markings
for (i=0;i<=100;i+=5) {
ang = Math.PI*i/100;
g.setPixel(42-30*Math.cos(ang), 47-Math.sin(ang)*30);
}
g.flip();
};
var initSPI = function(){
console.log('initSPI...');
spi = new SPI();
spi.setup({ sck:B1, mosi:B10 });
};
var initGraphics = function(){
console.log('initGraphics...');
g = require("PCD8544").connect(spi,B13,B14,B15, function() {
initialised = true;
console.log('and now call "draw(42);"');
// updateDisplay("Hello");
draw(10);
});
};
var updateDisplay = function(displayText){
g.clear();
g.drawString(displayText,0,0);
g.drawLine(0,10,84,10);
g.flip();
//setTimeout("A6.write(0);console.log('done');", 500);
//console.log('g: '+Object.keys(g));
};
initSPI();
initGraphics();
var btnCount = 90;
var onBtn = function(){
console.log('btn pressed ' + btnCount + ' times.');
btnCount++;
draw(btnCount);
};
setWatch(onBtn, BTN, {repeat: true, edge: "rising", debounce: 50});
function onInit_() {
initSPI();
initGraphics();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment