You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • If the voltage really does drop below zero, your best bet is to 'bias' it up so the average voltage is at the point at which Espruino's input flips between a 0 and a 1:

                  3.3v
                   |
                  _|_
                 |   |
                 |   |  10k Ohm
                 |   |
                 |___|
                   |
    IN    | |      |
    ------| |------|------------ Espruino Pin
          | |      |
                  _|_
        10uF     |   |
                 |   |  10k Ohm
                 |   |
                 |___|
                   |
                   |
                  GND
    

    Those values are just total guesses, but they should work. If you have some kind of oscillocope it'd be a massive help so you can see what's going on. If that doesn't work, you may have to resort to using a separate comparator.

    At that point you should be able to use setWatch like you suggest though. Just 2 potential issues:

    • In setWatch( BMCman.tick, A0, {repeat:true, edge:'both'} ) ;, the this value won't be correct when you call BMCman.tick - you actually need BMCman.tick.bind(BMCman) to force it.

    • Looking at that document, the time code can be about 2.4kHz - which is right on the limit of what Espruino can handle in normal JS, so you'll have to be super careful to get your code to run quickly... Possibly resorting to using espruino.com/Compilation for the watch handler.

    Something like the following might be a good start though:

    function start() {
      var d,c = "";
      setWatch( function(e) {d=e.time-e.lastTime>0.00075;if(d||e.sta­te)c+=1^d;}, A0, {repeat:true, edge:'both'} ) ;
      setInterval(function() {
        var sync = c.indexOf("0011111111111101");
        // ideally you'd use 'sync' for re-syncing here
        console.log(c);
        c="";
      }, 500);
    }
    

    The watch handler is pretty cryptic because it's trying to be fast to execute, but:

    • Using local variables to try and keep the speed up
    • d is true if it's a long pulse
    • if it's a short pulse and it's low, we ignore it (so we don't get 2 bits for the double-transition)
    • otherwise we negate d (short pulses are 1, long are 0) and stick it on the end of the string c
    • every so often we check for the sync bits - we don't do it that often because we want the bits to be handled first.
About

Avatar for Gordon @Gordon started