How to imitate Serial?

Posted on
  • Hello all,

    I'm working on a HBS(Home bus system) with mitsumi MM1192 chip (http://pdf.datasheetcatalog.com/datashee­t_pdf/mitsumi-electric/MM1192XD.pdf)

    I can run this chip with Serial(19200,{tx:A2,parity='even'});

    It works like Serial but there is some differences about HBS protocol.

    in serial protocol 0x80 looks like this

    1 0 0 0 0 0 0 0
    -_ _ _ _ _ _ _
    

    but in home bus it is like (Each bits frequency is 52 micro seconds)

    1    0   0   0   0   0   0   0
    -_   _-  _-  _-  _-  _-  _-  _-
    

    Thats why I have created a function to convert hex to home bus binary array.

    And I'm trying to push this data to mitsumi MM1192 with digitalWrite.

    Please don't care the hextohomebus function, may be it is wrong, i didn't test it.

    function hextohomebus(data) {
        ret = [];
        var newBinary = "";
        for (x = 0; x < data.length; x++) {
            var binary = ("0000000" + data[x].toString(2)).slice(-8);
            newBinary = newBinary + "01";
            for (i = 0; i < binary.length; i++) {
                if (binary[i] == "0") {
                    newBinary = newBinary + "01";
                }
                if (binary[i] == "1") {
                    newBinary = newBinary + "10";
                }
            }
            var oneCount= 0;
            for (g = 0; g < binary.length; g++) {
                if (binary[g] == "1") {
                    oneCount = oneCount + 1;
                }
            }
            var parityBit = "0";
            if (oneCount % 2) {
                parityBit = "10";
            } else {
                parityBit = "01";
            }
            newBinary = newBinary + parityBit + "10";
        }
        return newBinary;
    }
    
    
    function doSend(data, i) {
        digitalWrite(A2,data[i]=="0"?0:1);
    }
    
    function send(data) {
      setInterval(function(){
        i=i+1;
        doSend(data,i);
      },0.052);
    }
    
    
    setWatch(function(e) {
      console.log("Button pressed"); send(hextohomebus([0x80,0x00,0x10,0x52,0­x80,0x00,0x10,0x12,0x10,0x00,0x10,0x32,0­x30,0x00,0x20,0x62]));
    }, BTN1, { repeat: true, edge: 'rising' });
    

    But here I'm getting a memory error, probably I'm doing something wrong.

    My question is

    1- What I am doing wrong?
    2- Is there a way to ride homebus with espruino?

    Thanks

  • What Espruino device are you running this on? I guess maybe converting the data to the newBinary string in one go is causing it to use a lot of memory, since each bit turns into 2 characters.

    While what you're doing seems possible, I think using setInterval isn't really going to work for you, since the JS execution speed may not be fast enough, and any extra code that runs will delay execution.

    However, you do have some other options:

    • You can pass an array into digitalPulse - it should work fine but creating the array will be a bit fiddly since you need to specify the time between pulses, not the actual pulse values
    • Use SPI with the right baud rate (but just the MOSI - output - pin). You just need to ensure that newBinary is a Uint8Array of binary data rather than a string (so effectively 4 bits of actual homebus data go to 1 byte of data in newBinary).

    Hardware SPI probably won't run at exactly the right baud rate (depending on the board you're using) but software SPI should be fine

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to imitate Serial?

Posted by Avatar for kervanaslan @kervanaslan

Actions