B4R Question Returning a String from inline C code

demasi

Active Member
Licensed User
Longtime User
Hello,

I need to use methods setTime and getTimeStr from the Real Time Clock DS1307 in a project.
I could not find a library for this clock for B4R, so I tried to use inline C to access these methods.
I have no knowledge of C, but I can understand the code, and they are very simple. The problem is with strings, I can't return a string from C code. When I compile I receive an error about converting char to string.
Any advice?

this is my code:
\
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim time As String 
    Dim timer1 As Timer 
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    timer1.Initialize("timer1_Tick",1000) 
    Log("AppStart")
    timer1.Enabled=True 
End Sub

Sub timer1_tick
    RunNative("getTimeStr", Null)
    Log("Time: ",time)
End Sub


#if C
#include <DS1307.h>   // DS1307 RTC Clock library

DS1307 rtc(5, 4);
   
// Get Time string
char*  getTimeStr(B4R::Object* unused) {  
    char* time = rtc.getTimeStr();
      b4r_main::_time = time;
}
#End if

b4r_main.cpp:17: error: cannot convert 'char*' to 'B4R::B4RString*' in assignment
b4r_main::_time = time;
 

demasi

Active Member
Licensed User
Longtime User
Thank you.
I tried this lib, but it not worked for me, maybe because wiring errors, or because my RTC is a DS1307 and most probably because I´m using a NodeMCU, with only few pins available.
Actually, I had success with inline code, I will post here my solution later. I changed the original arduino lib and created there 3 new methods to return hours, minutes, seconds as int. The setTime works well with no problems.
But I dtill want to know how tro return a string from inline C. Could you please post an example?
I noted that the RTC DS1307 lib can return a Time TYPE. It would be nice an example of this too.
Thank you again.
 
Upvote 0
Top