You are reading a single comment by @dwallersv and its replies. Click here to read the full conversation.
  • Thanks for the feedback, guys.

    Ran into problems. I have successfully split the GUI code into 4 separate modules: Widget, Button, CheckBox, and Slider. Widget must always be included (require("Widget")), and must come first.

    I clearly didn't understand scoping and namespace particulars, however. Simply cutting up the source and adding exports lines didn't do the trick. The scope the module is loaded (and run) in is not the same scope as the main console prompt. After looking at other modules, it looks like I have to call a function exported and set everything up there.

    So, as a first-pass, I just took the entire "class" for one of these objects, and wrapped it in an init() function that's exported. Constructor function, prototype properties and functions, etc. -- literally indented the entire thing and put "exports.init = function() { ... }" around it. Then, I use it all this way in my demo code:

    require("Widget").init();
    require("Button").init();
    require("CheckBox").init();
    require("Slider").init();
    
    var w;
    var sld;
    function start() {
    	LCD.clear();
    	new Button({
    		style: 'flat',
    		area: [58,5,100,50],
    		text: 'LED1 on',
    		color: new Uint8Array([1, 0, 0, 102]),
    		border: 5,
    		textColor: new Uint8Array([0,200,200,200]),
    		onPress: function() {
    			LED1.write(!LED1.read());
    			this.text = LED1.read() ? "LED1 off" : "LED1 on";
    		},
    	});
    	new Button({
    		area: [163,5,100,50],
    		text: 'LED2 on',
    		color: new Uint8Array([1, 0, 128, 153]),
    		onPress: function() {
    			LED2.write(!LED2.read());
    			this.text = LED2.read() ? "LED2 off" : "LED2 on";
    		},
    	});
    	new CheckBox({
    		area: [70,70,150,30],
    		text: 'Backlight',
    		textColor: new Uint8Array([0,255,255,0])
    	});
    	sld = new Slider(
    	{
    		area: [10,189,300,50],
    		min: 0,
    		max: 24,
    		value: 0,
    		range: [0, 1],
    		showSlide: false,
    		onPress: function() {
    			SPI2.send8bit(Uint8Array(this.pixels.buf­fer, (this.value>>>0)*3, 72), 0b00000011, 0b00011111);
    		},
    		onHold: function() {
    			SPI2.send8bit(Uint8Array(this.pixels.buf­fer, (this.value>>>0)*3, 72), 0b00000011, 0b00011111);
    		},
    		draw: function() {
    			strips = Math.ceil(this.area[2] / 8);
    			for (i = 0; i < strips; i++) {
    				hue = i * (this.range[1] - this.range[0]) / strips + this.range[0];
    				LCD.setColor2(Uint8Array.prototype.HSBto­RGB([1, hue * 255, 255, 255]));
    				LCD.fillRect(
    					this.area[0] + i * 8,
    					this.area[1],
    					this.area[0] + (i + 1) * 8 - 1,
    					this.area[1] + this.area[3]
    				);
    			}
    			LCD.setColor2(this.borderColor);
    			LCD.drawRect(
    				this.area[0],
    				this.area[1],
    				this.area[0] + this.area[2],
    				this.area[1] + this.area[3]
    			);
    		}
    	});
    	sld.pixels = new Uint8Array(144);
    	for (i=0; i<48; i++) {
    		c = (new Uint8Array([1, (i%24)*255/24, 255, 25])).HSBtoRGB();
    		sld.pixels[i*3] = c[2];
    		sld.pixels[i*3+1] = c[1];
    		sld.pixels[i*3+2] = c[3];
    	}
    	SPI2.setup({baud:6400000, mosi:B15});
    	
    	new Slider({
    		color: new Uint8Array([0,100,100,100]),
    		slideColor: new Uint8Array([0,200,200,200]),
    		area: [60,120,200,25],
    		min: 0,
    		max: 1,
    		value: LCD.brt,
    		onPress: function() { LCD.setBrt(this.value); },
    		onHold: function() { LCD.setBrt(this.value); }
    	});
    }
    

    Not sure if this is the right way to do this (I'm really trying to do what #include does in C). What I found is that it seems to consume twice the memory space just getting the stuff into the system than if the code in those require statements is just literally there rather than being loaded through the module system. This is before even executing the start() function from the demo code, which creates the objects.

    I don't get it.

About

Avatar for dwallersv @dwallersv started