LiquidCrystal_I2C problem (C Development)

Hi. Am used ESP32 with C implementation. I have a problem of used LiquidCrystal_I2C Lib from https://github.com/JagoST7/LiquidCrystal_I2C

What I did is Copy the whole thing to my project folder…

demo-c /
-include
-src
-mjs_fs

And I modify my mos.yml By add

includes:

  • include

config_schema:

  • [“i2c.enable”, “b”, true, {title: “Enable I2C”}]
  • [“i2c.sda_gpio”, “i”, 21, {title: “GPIO to use for SDA”}]
  • [“i2c.scl_gpio”, “i”, 22, {title: “GPIO to use for SCL”}]

libs:

And inside main.c I declare

#include "LiquidCrystal_I2C.h"

I got compiler error not sure what wrong. Please help me to set “Hello World” on LCD in C

/demo-c/esp32/build_contexts/build_ctx_382560242/include/LiquidCrystal_I2C.h:60:1: error: unknown type name ‘class’
class LiquidCrystal_I2C {
^~~~~
CC /data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_382560242/build/objs/newlib/poll.o
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_382560242/include/LiquidCrystal_I2C.h:60:25: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘{’ token
class LiquidCrystal_I2C {
^

You don’t need to copy the library in your source tree, just add it to your mos.yml:

- location: https://github.com/JagoST7/LiquidCrystal_I2C

Since this is a C++ library, you can’t use it in a C file. Rename main.c to main.cpp.

Also don’t redefine the i2c settings:

  - ["i2c.enable", true]
  - ["i2c.sda_gpio", 21]
  - ["i2c.scl_gpio", 22]

Hi. Thanks you for your reply. I Did what you suggest

  1. I add to main.cpp

#include “LiquidCrystal_I2C.h” Compiler OK

  1. Then I add

LiquidCrystal_I2C lcd(0x27, 16, 2); Compiler Fail

/demo-c/esp32/build_contexts/build_ctx_468798003/src/main.cpp:9:34: error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int)’
LiquidCrystal_I2C lcd(0x27, 16, 2);
^
In file included from /data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/src/main.cpp:7:
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:62:5: note: candidate: ‘LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t)’
LiquidCrystal_I2C(uint8_t addr = DEFAULT_LCD_ADDRESS);
^~~~~~~~~~~~~~~~~
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:62:5: note: candidate expects 1 argument, 3 provided
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:60:7: note: candidate: ‘constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)’
class LiquidCrystal_I2C {
^~~~~~~~~~~~~~~~~
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:60:7: note: candidate expects 1 argument, 3 provided
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:60:7: note: candidate: ‘constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(LiquidCrystal_I2C&&)’
/data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/deps/LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:60:7: note: candidate expects 1 argument, 3 provided
CC /data/fwbuild-volumes/latest/apps/demo-c/esp32/build_contexts/build_ctx_468798003/build/objs/bt/host/bluedroid/bta/ar/bta_ar.o
make[1]: *** [main.cpp.o] Error 1

This is not related to Mongoose OS, but to C/C++ programming.

The error is very clear:

error: no matching function for call to ‘LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int)’
...
LiquidCrystal_I2C/include/LiquidCrystal_I2C.h:62:5: note: candidate: ‘LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t)’
LiquidCrystal_I2C(uint8_t addr = DEFAULT_LCD_ADDRESS);

Yes… My bad! Now change to

LiquidCrystal_I2C lcd(0x27);

But when I put

lcd.begin(4,20);
lcd.print(“hello”);

I got core dump after mos flash.

Here is the message

[Aug 20 19:30:34.175] mgos_provision_stat:120 Max state: 0
[Aug 20 19:30:34.177] mgos_rpc_channel_ua:313 0x3ffdf478 UART0
[Aug 20 19:30:34.321] Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] Core  1 register dump:
[Aug 20 19:30:34.321] PC      : 0x400d7f71  PS      : 0x00060630  A0      : 0x800d83bc  A1      : 0x3ffc55c0  
[Aug 20 19:30:34.321] A2      : 0x00000000  A3      : 0x00000027  A4      : 0x3ffc5600  A5      : 0x00000001  
[Aug 20 19:30:34.321] A6      : 0x00000001  A7      : 0x00000000  A8      : 0x800827ec  A9      : 0x3ffc55d0  
[Aug 20 19:30:34.321] A10     : 0x0000c350  A11     : 0x00000000  A12     : 0x001cf937  A13     : 0x3ffc9888  
[Aug 20 19:30:34.321] A14     : 0x00000000  A15     : 0x20200000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
[Aug 20 19:30:34.321] EXCVADDR: 0x00000038  LBEG    : 0x400814ed  LEND    : 0x400814f5  LCOUNT  : 0x00000027  
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] Backtrace:0x400d7f6e 0x400d83b9 0x400d8535 0x400d77b0 0x400d33af 0x400d86fb 0x4008282c
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] ELF file SHA256: dc1e4c7235005104
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] 
[Aug 20 19:30:34.321] --- BEGIN CORE DUMP ---
[Aug 20 19:30:34.321] mos: catching core dump
[Aug 20 19:30:37.147] .......read err EOF

I suppose you declare a global LiquidCrystal_I2C object. At that time the i2c bus is not initialized and is NULL, hence the core dump. The library does not check the validity of the return value of myi2c.

Use a pointer to the LiquidCrystal_I2C at global scope:

LiquidCrystal_I2C *lcd{nullptr};

and create the object in mgos_app_init:

enum mgos_app_init_result mgos_app_init(void) {
  lcd = new LiquidCrystal_I2C(0x27);
  lcd->begin(4, 20);
  lcd->print("hello");

  return MGOS_APP_INIT_SUCCESS;
}

Oh… thanks you so much.