B4R Question InLine and Strings

newbie

Member
Licensed User
Longtime User
Hi,

to send text to my display via InLine-code
i need a global string-var (other var are for the inline-code unknown)

But how can i in my Sub assign a text ("abc") or another String to this String-var ?????
I have try something, but every time i get a compiler-error like

Cannot set a Const variable (global non-primitive variables are always constants)

any suggestions ? Thanks.
 

newbie

Member
Licensed User
Longtime User
Sorry then i don't understand it right.
i use runnative without parameter and use the var in the inline-void.
How can i do this otherwise
Please a little example (see the sub "ToDisplayC" and the called TftPrint in Inline)

Here is the complete Modul
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Private IsDark As Boolean
   Private Zeile As Int
   Private Spalte As Int
   Private Color As Int
   
   'Private Text As String = "abcdef"  ' was shown on the Display !!
   Private Text As String  ' compiler accept this but no possibility to assign other strings to this Var
   
   Public VGA_BLACK      As Int = 0x0000
   Public VGA_WHITE      As Int = 0xFFFF
   Public VGA_RED        As Int = 0xF800
   Public VGA_GREEN      As Int = 0x0400
   Public VGA_BLUE      As Int = 0x001F
   Public VGA_SILVER      As Int = 0xC618
   Public VGA_GRAY      As Int = 0x8410
   Public VGA_DarkGRAY    As Int = 0xAD55  
   Public VGA_DimGRAY    As Int = 0x6B4D   
   Public VGA_MAROON      As Int = 0x8000
   Public VGA_YELLOW      As Int = 0xFFE0
   Public VGA_OLIVE      As Int = 0x8400
   Public VGA_LIME      As Int = 0x07E0
   Public VGA_AQUA      As Int = 0x07FF
   Public VGA_TEAL      As Int = 0x0410
   Public VGA_NAVY      As Int = 0x0010
   Public VGA_FUCHSIA      As Int = 0xF81F
   Public VGA_PURPLE      As Int = 0x8010
   Public VGA_TRANSPARENT    As Int = 0xFFFFFFFF
End Sub
   
'Private Text As String  'on this place unknown for the inline-code  
   
Sub InitDisplay
   RunNative("TftInit", Null)
   IsDark = False
End Sub

Sub LowDisplay
   RunNative("TftDark", Null)
   IsDark = True
End Sub

Sub ToDisplay(xZeile As Int, xSpalte As Int, xText As String)
   If IsDark Then
     Color = VGA_DimGRAY
     RunNative("TftSetBackColor", Null)   
   Else
     Color = VGA_WHITE
     RunNative("TftSetBackColor", Null)
   End If
     
   ' here i need to assign the text to display
   ' -----------------------------------------
   'Text = xText  ' not possible for const
   ' -----------------------------------------
   Spalte = 16 * (xSpalte-1)
   Zeile = 16 * (xZeile-1)
   RunNative("TftPrint", Null)
End Sub

Sub ToDisplayC(xZeile As Int, xSpalte As Int, xText As String,xColor As Int)
   Color = xColor
   Spalte = 16 * (xSpalte-1)
   Zeile = 16 * (xZeile-1)
  ' here i need to assign the text to display
   ' -----------------------------------------
   'Text = xText  ' not possible for const
   ' -----------------------------------------
   
   RunNative("TftSetBackColor", Null)
   RunNative("TftPrint", Null)
   Color = VGA_WHITE
   RunNative("TftSetBackColor", Null)   
End Sub

#If C
#include <UTFT.h>

extern uint8_t BigFont[];
UTFT myDisplay(TFT01_22SP,4,3,7,6,5);

void TftInit(B4R::Object* o) {
   myDisplay.InitLCD();
     myDisplay.setFont(BigFont);
   myDisplay.fillScr(b4r_utft::_vga_white);
     myDisplay.setColor(b4r_utft::_vga_black);
     myDisplay.setBackColor(b4r_utft::_vga_white);
}

void TftDark(B4R::Object* o){
  myDisplay.InitLCD();
  myDisplay.setFont(BigFont);
  myDisplay.fillScr(b4r_utft::_vga_dimgray);
  myDisplay.setColor(b4r_utft::_vga_black);
  myDisplay.setBackColor(b4r_utft::_vga_dimgray);  
}

void TftSetBackColor(B4R::Object* o) {
   myDisplay.setBackColor(b4r_utft::_color);
}
//void TftSetBackColorHell(B4R::Object* o) {
//    myDisplay.setBackColor(255,255,255);
//}

//void TftSetBackColorDunkel(B4R::Object* o) {
//    myDisplay.setBackColor(50,50,50);
//}

void TftPrint(B4R::Object* o) {
   myDisplay.print(b4r_utft::_text->data,b4r_utft::_zeile,b4r_utft::_spalte,0);
}

#End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot simply assign global objects or strings. See this tutorial for more information: https://www.b4x.com/android/forum/threads/memory-variables-and-objects.65672/#content

You can however pass the string directly to the native method. This is simpler and doesn't require using the heap:
B4X:
RunNative("test", "abc")

#If C
void test(B4R::Object* msg) {
   const char* str = (const char*)msg->data.PointerField;
   ::Serial.println(str);
}
#end if
 
Upvote 0
Top