Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Not able get example-arduino-adafruit-ssd1306-c-1.18 working

I am using Mongoose-os 1.18, OLED 128x64 with SPI and ESP32
Trying for long time to get the C example running but i still fail.

When i try example-arduino-adafruit-ssd1306-js-1.18 with modified settings to match my configuration, anything works fine.
Have also tried to place some debug messages in the code, successful create Adafruit_SSD1306 d2 object, also show_num function
is entered, but display stays black.

As you can see, only minimal changes are done to the code which are verified to work with the Javascript Example

Here my configurations:

Hope anyone can help me with that.

mos.yml

author: mongoose-os
description: Adafruit SSD1306 usage example (C/C++)
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

sources:
  - src
filesystem:
  - fs
libs:
    # common mgos libs
  - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/i2c
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/spi

    # libs necessary for the current app
  - origin: https://github.com/mongoose-os-libs/arduino-adafruit-ssd1306

config_schema:
  - ["i2c.enable", false]
  - ["spi.enable", true]

tags:
  - c
  - arduino
  - hw

manifest_version: 2017-05-18

Main.cpp

/*
 * Copyright (c) 2014-2017 Cesanta Software Limited
 * All rights reserved
 */

#include <Arduino.h>
#include <Adafruit_SSD1306.h>

#include "common/cs_dbg.h"
#include "mgos_app.h"
#include "mgos_timers.h"

Adafruit_SSD1306 *d1 = nullptr, *d2 = nullptr;

static void timer_cb(void *arg);

void setup(void) {
  // I2C
  d1 = new Adafruit_SSD1306(4 /* RST GPIO */, Adafruit_SSD1306::RES_128_64);

  if (d1 != nullptr) {
    d1->begin(SSD1306_SWITCHCAPVCC, 0x3D, true /* reset */);
    d1->display();
  }

  // SPI.
  // On ESP8266 SPI and I2C cannot be enabled at the same time by default
  // because their pins overlap.
  // You will need to disable I2C and enable SPI:
  // mos config-set i2c.enable=false spi.enable=true
  d2 = new Adafruit_SSD1306(32 /* DC */, 25 /* RST */, 33 /* CS */,
                            Adafruit_SSD1306::RES_128_64);

  if (d2 != nullptr) {
    d2->begin(SSD1306_SWITCHCAPVCC, 0 /* unused */, true /* reset */);
    d2->display();
  }

  mgos_set_timer(1000 /* ms */, true /* repeat */, timer_cb, NULL);
}

static void show_num(Adafruit_SSD1306 *d, const char *s, int i) {
  d->clearDisplay();
  d->setTextSize(2);
  d->setTextColor(WHITE);
  d->setCursor(d->width() / 4, d->height() / 4);
  d->printf("%s%d", s, i);
  d->display();
}

static void timer_cb(void *arg) {
  static int i = 0, j = 0;
  if (d1 != nullptr) show_num(d1, "i = ", i);
  if (d2 != nullptr) show_num(d2, "j = ", j);
  LOG(LL_INFO, ("i = %d, j = %d", i, j));
  i++;
  j++;
  (void) arg;
}

#if 0
void loop(void) {
  /* For now, do not use delay() inside loop, use timers instead. */
}
#endif

Comments

  • jonnyjonny Austria

    Thanks to jordansissel who found the solution and reported the issue

    /*
     * Copyright (c) 2014-2017 Cesanta Software Limited
     * All rights reserved
     */
    
    #include <Arduino.h>
    #include <Adafruit_SSD1306.h>
    
    #include "common/cs_dbg.h"
    #include "mgos_app.h"
    #include "mgos_timers.h"
    
    Adafruit_SSD1306 *d1 = nullptr, *d2 = nullptr;
    
    static void timer_cb(void *arg);
    
    enum mgos_app_init_result mgos_app_init(void) {
      // I2C
      d1 = new Adafruit_SSD1306(4 /* RST GPIO */, Adafruit_SSD1306::RES_128_64);
    
      if (d1 != nullptr) {
        d1->begin(SSD1306_SWITCHCAPVCC, 0x3D, true /* reset */);
        d1->display();
      }
    
      // SPI.
      // On ESP8266 SPI and I2C cannot be enabled at the same time by default
      // because their pins overlap.
      // You will need to disable I2C and enable SPI:
      // mos config-set i2c.enable=false spi.enable=true
      d2 = new Adafruit_SSD1306(32 /* DC */, 25 /* RST */, 33 /* CS */,
                                Adafruit_SSD1306::RES_128_64);
    
      if (d2 != nullptr) {
        d2->begin(SSD1306_SWITCHCAPVCC, 0 /* unused */, true /* reset */);
        d2->display();
      }
    
      mgos_set_timer(1000 /* ms */, true /* repeat */, timer_cb, NULL);
        return MGOS_APP_INIT_SUCCESS;
    }
    
    static void show_num(Adafruit_SSD1306 *d, const char *s, int i) {
      d->clearDisplay();
      d->setTextSize(2);
      d->setTextColor(WHITE);
      d->setCursor(d->width() / 4, d->height() / 4);
      d->printf("%s%d", s, i);
      d->display();
    }
    
    static void timer_cb(void *arg) {
      static int i = 0, j = 0;
      if (d1 != nullptr) show_num(d1, "i = ", i);
      if (d2 != nullptr) show_num(d2, "j = ", j);
      LOG(LL_INFO, ("i = %d, j = %d", i, j));
      i++;
      j++;
      (void) arg;
    }
    
    #if 0
    void loop(void) {
      /* For now, do not use delay() inside loop, use timers instead. */
    }
    #endif
    
Sign In or Register to comment.