You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Basically what's happening is rather than storing the data in an array or a string, it's being stored in a number (as binary). There are probably some tutorials about it online...

    While it could be done another way, it's faster and more efficient this way - and it's nearer to what the normal socket's receiver and transmitter is doing internally.

    The line:

    n = (n<<1) | ((d>=0.0004)?1:0);
    

    could be rewritten as:

    n = n*2; // the <<
    if (d>=0.0004)
      n = n + 1;
    

    Note that replacing the | with a + only works because we're sure that the lowest bit of the number is 0, because we just multiplied it by 2.

    What it's doing is turning the data it is receiving into a number, one bit at a time.

    So what sigOff does is:

    • It's called when a pulse from the receiver ends
    • It works out the amount of time that the pulse was on for (and stores it in d)
    • If the pulse is a 'valid' length (it could have just been random noise) then we work out if it was a 1 or a 0 (with d>=0.0004) and store that in n
    • If the pulse wasn't valid, we clear n

    And in sigOn:

    • It's called when a pulse from the receiver starts
    • It works out the amount of time that the pulse was off for (and stores it in d)
    • If the pulse was off for enough time (>5ms) and we had data that we'd collected from sigOff being called a few times, then it prints that data. The toString(2) displays the number as a binary number.

    Hope that helps... Let me know if you have any other questions...

About

Avatar for Gordon @Gordon started