• @Ollie, thanks! That looks a lot smaller! If you're trying to get Espruino memory usage down, you might find that:

    function onData(data) {
    			if((data.charCodeAt(0) >> 4) === 3) {
    				var cmd = data.charCodeAt(0);
    				var var_len = data.charCodeAt(2) << 8 | data.charCodeAt(3);
    				var msg = {
    					topic: data.substr(4, var_len),
    					message: data.substr(4+var_len, (data.charCodeAt(1))-var_len),
    					dup: (cmd & 0b00001000) >> 3,
    					qos: (cmd & 0b00000110) >> 1,
    					retain: cmd & 0b00000001
    				};
    				this.emit('message', msg);
    			}
    		}
    
    MQTT.prototype.connect = function(){
    	var onConnected = function() {
    		client.write(mq.mqttConnect(getSerial())­);
    		mq.emit("connected");
    		client.on('data', onData.bind(mq));
    // ...
    

    actually uses even less memory on Espruino at the moment (it won't have two copies of the function text in memory).

    @tve I'm just trying to make sure that things are as useful to as many people as possible. So few people want to compile their own firmwares, doing stuff in JS lets them choose how they use their memory.

    Also the past few years have shown that JS code actually gets many more improvements and tweaks than the C code does - and from my point of view it's good to do things that make it easier for others to contribute.

    But if you do add MQTT in C code, feel free to stick it back in the Espruino repo (as long as the licence for the stuff you're adding is compatible with MPLv2). As you say, it could be useful for people that want to build it in.

    I was going to see whether I can insert a C-level sockets API so protocols in C could go through the Espruino sockets layer and thereby be target independent.

    While that'd be cool, you might find that you have a lot of trouble. Both CC3000 and WIZnet have C-ish socket APIs (each slightly different, but with send/recv/etc), that conflict so they can't even be compiled into the same binary. I'd imagine adding another socket API on top might cause similar problems - on CC3k, WIZnet and Linux.

About

Avatar for Gordon @Gordon started