Graphics how to set callbacks to C- functions

Posted on
  • Looks like on each call of graphicsGetFromVar, graphicsSetCallbacks is called.
    So its called very often.
    My expectation was to set callbacks (fillRect, setPixel) only once during initialisation.
    This would add an option to write my own drivers in C, which only set fillRect and setPixel.

  • Yes, graphicsSetCallbacks gets called for each draw call - usually it's reasonably fast though as it's just a few assignments?

    If you're thinking of doing your own build then you could look at what Bangle.js does as that uses graphicsInternal which avoids the graphicsSetCallbacks as far as I remember

  • Hmm, I tried to figure out, how to use graphicsInternal, .....
    and found only something in lcd_st7789-8bit where setPixel is not set again.

    Next try was to add JSGRAPHICSTYPE_OTHERS in JsGraphicsType (graphics.h)
    and an if-statement in graphicsSetCallbacks(graphics.c) to skip xxxSetCallbacks.
    In my own driver, this worked fine, but in all functions from jswrap_graphics(for example setPixel) I've got runtime error. Looks to me like setCallback is necessary (?)

    My actual solution for my problem is to add a weak function for JSGRAPHICSTYPE_OTHERS, which can be overroled in my driver. My driver for ILI9341 now is much faster compared to JS-solution.

    See following sources.
    If this is of interest for Espruino, I would create a pull request.


    typedef enum {
      JSGRAPHICSTYPE_ARRAYBUFFER, ///< Write everything into an ArrayBuffer
      JSGRAPHICSTYPE_JS,          ///< Call JavaScript when we want to write something
      JSGRAPHICSTYPE_FSMC,        ///< FSMC (or fake FSMC) ILI9325 16bit-wide LCDs
      JSGRAPHICSTYPE_SDL,         ///< SDL graphics library for linux
      JSGRAPHICSTYPE_SPILCD,      ///< SPI LCD library
      JSGRAPHICSTYPE_ST7789_8BIT, ///< ST7789 in 8 bit mode
      JSGRAPHICSTYPE_MEMLCD,      ///< Memory LCD
      JSGRAPHICSTYPE_LCD_SPI_UNBUF, ///< LCD SPI unbuffered 16 bit driver
      JSGRAPHICSTYPE_OTHERS  ///< Driver that support callbacks only
    } JsGraphicsType;
    bool graphicsSetCallbacksOthers(JsGraphics *gfx);


    /// Set up the callbacks for this graphics instance (usually done by graphicsGetFromVar)
    __attribute__((weak))bool graphicsSetCallbacksOthers(JsGraphics *gfx){
      return false;
    bool graphicsSetCallbacks(JsGraphics *gfx) {
      if(gfx->data.type >= JSGRAPHICSTYPE_OTHERS){
    	return graphicsSetCallbacksOthers(gfx);

    Last not least in myOwnDriver.c:

    bool graphicsSetCallbacksOthers(JsGraphics *gfx){
      gfx->setPixel = myOwnCallbackSetPixel;
      gfx->fillRect = myOwnCallbackFillRect;
      return true;
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Graphics how to set callbacks to C- functions

Posted by Avatar for JumJum @JumJum