Transmitter and receiver

Posted on
  • Playing around with REMOTE CONTROL SOCKETS .
    Wired transmitter and receiver, and its sends and receives the bits on pressing BTN but I trying to understand the whole code:
    In this function

    function sigOff(e) {
      var d = e.time-t;
      t = e.time;
      if (d>0.0001 && d<0.001)
        n = (n<<1) | ((d>=0.0004)?1:0);
      else
        n=0;
    }
    

    what is the meaning of bold code
    n = (n<<1) | ((d>=0.0004)?1:0);
    I mean what doing "<<" sign and "|" one (it's not the same as "||" in JavaScript?)

    Can you please add a link for reference for those signs in JS (or not JS)?

    EDIT:
    Found it here but still little confused to understand the sigOn() and sigOff() functions. Can someone please add step-by-step explanations for those functions or add new ones but with simple code?

  • 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...

  • It helps a lot. Thank you very much.
    Just to test my self I attaching table with 5 pulses for example (may be it will help others too)

    So if understood right, it doesn't matter how much time the signal was in "0" state (if it was less than 5ms)?

    PS: steps in e.time are the same 10 steps as in table, I think it not looks good on the image.


    1 Attachment

    • receiver.jpg
  • Now I trying to understand the transmitter functionality.
    I would be very thankful, If you (or some one else) can add a little explanation for those functions, especially in few moments:
    in sendCommand() function there is a line

    var bits = (CODE & ~0b11111) | command;
    

    what we trying to put in "bits"? If I understand correctly, this part

    (CODE & ~0b11111)
    

    always returns our "CODE" with five right zeros, what for?

    In setWatch() :

    socketOn = !socketOn;
    

    we switch between 0 and 1, what for? Is there some reason for?

    Thank you in advance.

  • For the receiver, for the remote control I used, the time the signal was 0 doesn't matter so it isn't measured (it's the opposite of the time the signal is 1). If you were using a different brand of remote control socket then it could be different...

    For:

    var bits = (CODE & ~0b11111) | command;
    

    You're correct - it makes the last 5 bits of the code zero, and then it adds in the command. This is because the last 5 bits contain the command code. They could have just been zeroed when CODE was set, but I wanted to make it so you could just copy and paste the code you got in order to make it work.

    socketOn = !socketOn; is just so that when you press the button on Espruino, it toggles the socket between turning on and turning off. If you had two buttons you could assign one to sendMultiple(0b11110); and one to sendMultiple(0b01110);...

  • Got it, thank you.

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

Transmitter and receiver

Posted by Avatar for Pumych @Pumych

Actions