How would I get pin for jshSetOutputValue ?

Posted on
  • In jshardware.c is a function, jshSetOutputValue, used for waveforms (and others ?) to set analog output.
    To set the output, only a JshPinFunction is given.
    This needs to be translated to a pin.
    What is the best way to do that ?

  • Is this for ESP32?

    Personally I'd just use JshPinFunction to store the pin number... So you have DAC_CH1,2,etc - just set the channel to be the pin number.

    As-is you can get up to 16 pins like that. If that's not enough, perhaps consider using JSH_TIMER1 for pins 0..15 and JSH_TIMER2 for pins 16..31. You should just be able to do that in jshAnalogOutput.

    However I can't believe that ESP32 actually has 32 DACs? Surely it's PWM?

    Usually what happens is jshardware.js (jshAnalogOutput) works out what PWM device to use on what pin, and sets it up. Then jshSetOutputValue updates the chosen device.

  • Would it be an option to have func and pin in UtilTimerTaskBuffer instead of a union ?
    And to set pin, always not only for UET_IS_BUFFER_READ_EVENT, in jstStartSignal ?
    As far as I can see, jshSetOutputValue is only supported for Espruino Board (and ESP32 ;-))

  • ESP32 supports PWM for all(at least most) pins. So we have the choice between more than 16 pins.
    Since pin is available in jstStartSignal, I would prefer to store it in buffer, instead of recalculating on each call.

  • Ok, so it's not DAC you want, just PWM?

    Would it be an option to have func and pin in UtilTimerTaskBuffer instead of a union ?

    Not really - it's just more memory usage, and it's taking code that works for all platforms and then adding loads of platform-specific definitions to it.

    Just do something like:

    JshPinFunction pinToPinFunction(Pin x) {
      return JSH_TIMER1 + ((x>>4)<<JSH_SHIFT_TYPE) + ((x&15)<<JSH_SHIFT_INFO);
    Pin pinFunctionToPin(JshPinFunction x) {
      return (((x-JSH_TIMER1)>>JSH_SHIFT_TYPE)<<4) + (x>>JSH_SHIFT_INFO);
  • As well as pwm there are 2 8 bit DAC available

  • Ok, then that's probably ok - you have the stuff I posted above for PWM pins, and then you have each of the DAC channels done with DAC_CH1&2.

    You leave jshAnalogOutput to set the pins themselves up. The idea is that jshSetOutputValue is fast since it runs in an IRQ - you don't want it having to configure pin states each time a value is written.

  • @JumJum

    There is also the sigma - delta -API :­api/sigmadelta.html

    This might be easier to implement Rather than the led API:­api/ledc.html

  • Oh, this really looks nice. And has less restrictions for freqency, compared to ledc.
    Saw this too late. Already finished a ledc driven solution :-(
    May we we should switch to sigmadelta soon ......

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

How would I get pin for jshSetOutputValue ?

Posted by Avatar for JumJum @JumJum