You are reading a single comment by @Stevie and its replies. Click here to read the full conversation.
  • Hi,

    here is a first prototype of the RNG as a C module:

    \#include "jswrap_rng.h"  // We need the declaration of the jswrap_rng_random function
    \#include "jsinteractive.h" // Pull in the jsiConsolePrint function
    
    // Let's define the JavaScript class that will contain our `random()` methods. We'll call it `RNG`
    /*JSON{
      "type" : "class",
      "class" : "RNG"
    }*/
    
    // Now, we define the `jswrap_rng_random` to be a `staticmethod` on the `RNG` class
    /*JSON{
      "type" : "staticmethod",
      "class" : "RNG",
      "name" : "random",
      "generate" : "jswrap_rng_random",
      "return" : ["JsVar","The random number or NAN, if it is not supported on the platform."]
    }*/
    
    // Now, we define the `jswrap_rng_random_long` to be a `staticmethod` on the `RNG` class
    /*JSON{
      "type" : "staticmethod",
      "class" : "RNG",
      "name" : "random_long",
      "generate" : "jswrap_rng_random_long",
      "return" : ["JsVar","The random number or NAN, if it is not supported on the platform."]
    }*/
    
    // TODO: We need a better way to pull this in! This will not work on all boards. And we need
    // to make the jshAnalogRead function non-static where it is defined.
    extern int jshAnalogRead(JsvPinInfoAnalog analog, bool fastConversion);
    
    static int get_bit() {
      jshDelayMicroseconds(10);
      int v = jshAnalogRead(JSH_ANALOG1 | JSH_ANALOG_CH17, false);
      // It seems the lower 4 bits are always zero.
      v >>= 4;
      v &= 1;
      return v;
    }
    
    static long long rng_random() {
    \#if defined(STM32F1) || defined(STM32F4)
      // enable sensor
      ADC_TempSensorVrefintCmd(ENABLE);
    
      long long sum = 0;
      int s;
      int v1 = get_bit();
      for (s = 0; s < 32; s++)
      {
        int v2 = get_bit();
        sum <<= 1;
        if (v1 != v2)
           sum++;
        v1 = v2;
      }
    
      // disable sensor
      ADC_TempSensorVrefintCmd(DISABLE);
      return sum;
    \#else
      return NAN;
    \#endif
    }
    
    JsVar *jswrap_rng_random_long() {
    \#if defined(STM32F1) || defined(STM32F4)
      long long v = rng_random();
      return jsvNewFromLongInteger(v);
    \#else
      return jsNewFromFloat(NAN);
    \#endif
    }
    
    JsVar *jswrap_rng_random() {
    \#if defined(STM32F1) || defined(STM32F4)
      long long v = rng_random();
      double value = ((double)(v)) / 4294967296.0;
      return jsvNewFromFloat(value);
    \#else
      return jsNewFromFloat(NAN);
    \#endif
    }
    
    

    Seems to work fine. But there are some flaws: Namely it pulls in some function from the hardware abstraction layer. I had to make the function non-static there. I am not sure what the best way would be to do this. Add the function to the hardware layer itself? Add a function which takes the analog reading as an int?

About

Avatar for Stevie @Stevie started