Ok, just dug it out:
var exports = {}; exports.list = function(g, items) { var options = items[""]; if (!(options instanceof Object)) options = {}; if (options.selected === undefined) options.selected = 0; if (!options.fontHeight) options.fontHeight = 6; var y = 0|options.y; if (options.title) y += options.fontHeight+2; var menuItems = Object.keys(items).slice(1); // remove first item var l = { draw : function() { g.clear(); g.setColor(-1); if (options.title) { g.drawString(options.title,(g.getWidth()-g.stringWidth(options.title))/2,y-options.fontHeight-2); g.drawLine(0,y-2,g.getWidth(),y-2); } var rows = 0|Math.min((g.getHeight()-y) / options.fontHeight,menuItems.length); var idx = E.clip(options.selected-rows/2,0,menuItems.length-rows); var iy = y; while (rows--) { if (idx==options.selected) { g.fillRect(0,iy,g.getWidth()-1,iy+options.fontHeight-1); g.setColor(0); } g.drawString(menuItems[idx++],0,iy); g.setColor(-1); iy += options.fontHeight; } if (g.flip) g.flip(); }, select : function() { var item = items[menuItems[options.selected]]; if ("function" == typeof item) item(); }, move : function(dir) { options.selected = 0|Math.clip(options.selected+dir,0,menuItems.length-1); l.draw(); } }; l.draw(); return l; }; var menu = exports; // would be a require if this were a library A5.write(0); // GND A7.write(1); // VCC // Setup SPI var spi = new SPI(); spi.setup({ sck:B1, mosi:B10 }); var g,m; var mainmenu = { "" : { "title" : "-- Menu --" }, "Light On" : function() { LED1.set(); }, "Light Off" : function() { LED1.reset(); }, "Submenu" : function() { m=menu.list(g, submenu); }, }; var submenu = { "" : { "title" : "-- SubMenu --" }, "One" : undefined, "Two" : undefined, "< Back" : function() { m=menu.list(g, mainmenu); }, }; function onInit() { g = require("PCD8544").connect(spi,B13,B14,B15, function() { m = menu.list(g, mainmenu); }); } // Now just call things when buttons are pressed // m.move(1); moves down // m.move(-1); moves up // m.select(); selects item onInit();
Anyone think this would be worth turning into a 'proper' module?
@Gordon started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ok, just dug it out:
Anyone think this would be worth turning into a 'proper' module?