B4J Programming Press on the image to return to the main documentation page.

Core

List of types:

Bit
Keywords
Pin
Serial
Stream
String
Timer

Bit


Events:

None

Members:


  And (X As UInt, Y As UInt) As UInt

  Clear (B As Byte, Bit As Byte) As Byte

  Get (B As Byte, Bit As Byte) As Byte

  HighByte (N As UInt) As Byte

  LowByte (N As UInt) As Byte

  Not (N As UInt) As UInt

  Or (X As UInt, Y As UInt) As UInt

  ParseInt (Value As String, Radix As Byte) As Int

  Set (B As Byte, Bit As Byte) As Byte

  ShiftLeft (N As UInt, Shift As Byte) As UInt

  ShiftRight (N As UInt, Shift As Byte) As UInt

  ToBinaryString (N As UInt) As String

  Xor (X As UInt, Y As UInt) As UInt

Members description:

And (X As UInt, Y As UInt) As UInt
Returns the bitwise AND of the two values.
Clear (B As Byte, Bit As Byte) As Byte
Returns a byte based on the given number, with the specified bit cleared.
Get (B As Byte, Bit As Byte) As Byte
Returns the value of the specified bit in the given number.
HighByte (N As UInt) As Byte
Returns the high byte value of the given number.
LowByte (N As UInt) As Byte
Returns the low byte value of the given number.
Not (N As UInt) As UInt
Returns the bitwise complement of the given value.
Or (X As UInt, Y As UInt) As UInt
Returns the bitwise OR of the two values.
ParseInt (Value As String, Radix As Byte) As Int
Parses Value as an integer using the specified radix. Returns 0 if the value cannot be parsed.
Radix - Should be between 2 to 36.
Set (B As Byte, Bit As Byte) As Byte
Returns a byte based on the given number, with the specified bit set.
Example:
Dim b As Byte = 0
b = Bit.Set(b, 0)
Log(b) '1
ShiftLeft (N As UInt, Shift As Byte) As UInt
Shifts N left.
Shift - Number of positions to shift.
ShiftRight (N As UInt, Shift As Byte) As UInt
Shifts N right.
Shift - Number of positions to shift.
ToBinaryString (N As UInt) As String
Returns a string representation of N in base 2.
Xor (X As UInt, Y As UInt) As UInt
Returns the bitwise XOR of the two values.

Keywords


Events:

None

Members:


  Abs (Number As Double) As Double

  ACos (Value As Double) As Double

  ACosD (Value As Double) As Double

  AddLooper (SubName As SubVoidVoid) As void

  Array As void

  Asc (Character As String) As Byte

  ASin (Value As Double) As Double

  ASinD (Value As Double) As Double

  ATan (Value As Double) As Double

  ATan2 (Y As Double, X As Double) As Double

  ATan2D (Y As Double, X As Double) As Double

  ATanD (Value As Double) As Double

  AvailableRAM As ULong

  CallSubPlus (SubName As SubVoidByte, DelayMs As ULong, Tag As Byte) As void

  cE As Double

  Ceil (Number As Double) As Double

  Constrain (Value As Double, MinValue As Double, MaxValue As Double) As Double

  Continue As void

  Cos (Radians As Double) As Double

  CosD (Degrees As Double) As Double

  cPI As Double

  CRLF As StringLiteral

  Delay (DelayMs As ULong) As void

  DelayMicroseconds (Delay As UInt) As void

  Dim As void

  Exit As void

  False As Boolean

  Floor (Number As Double) As Double

  For As void

  If As void

  IsNumber (Text As String) As Boolean

  JoinBytes (ArraysOfObjects() As Object) As Byte()

  JoinStrings (Strings() As String) As String

  Log (OneOrMoreMessages As Object) As void

  Logarithm (Number As Double, Base As Double) As Double

  MapRange (Value As Long, FromLow As Long, FromHigh As Long, ToLow As Long, ToHigh As Long) As Long

  Max (Number1 As Double, Number2 As Double) As Double

  Micros As ULong

  Millis As ULong

  Min (Number1 As Double, Number2 As Double) As Double

  Not (Value As Boolean) As Boolean

  Null As Object

  NumberFormat (Number As Double, MinimumIntegers As Byte, MaximumFractions As Byte) As String

  Power (Base As Double, Exponent As Double) As Double

  QUOTE As StringLiteral

  Return As void

  Rnd (Mininum As Long, Maximum As Long) As Long

  RndSeed (Seed As ULong) As void

  Round (Number As Double) As Long

  RunNative (Method As FunctionObjectObject, Arg As Object) As Object

  Select As void

  Sender As Object

  Sin (Radians As Double) As Double

  SinD (Degrees As Double) As Double

  SizeOf (Object As Object) As UInt

  Sqrt (Value As Double) As Double

  StackBufferUsage As UInt

  Sub As void

  TAB As StringLiteral

  Tan (Radians As Double) As Double

  TanD (Degrees As Double) As Double

  True As Boolean

  Type As void

  Until As void

  While As void

Members description:

Abs (Number As Double) As Double
Returns the absolute value.
ACos (Value As Double) As Double
Returns the angle measured with radians.
ACosD (Value As Double) As Double
Returns the angle measured with degrees.
AddLooper (SubName As SubVoidVoid) As void
Adds a looper sub. Looper is similar to a timer with the lowest possible interval.
There could be any number of looper subs.
Example:
AddLooper("Looper1")
'...
Sub Looper1

End Sub
Array As void
Creates a single dimension array of the specified type, or Object if the type is not specified.
The syntax is: Array As type (list of values).
Example:
Dim Days() As String = Array As String("Sunday", "Monday", ...)
Asc (Character As String) As Byte
Returns the byte (ASCII) value of the first character in the string.
ASin (Value As Double) As Double
Returns the angle measured with radians.
ASinD (Value As Double) As Double
Returns the angle measured with degrees.
ATan (Value As Double) As Double
Returns the angle measured with radians.
ATan2 (Y As Double, X As Double) As Double
Returns the angle measured with radians.
ATan2D (Y As Double, X As Double) As Double
Returns the angle measured with degrees.
ATanD (Value As Double) As Double
Returns the angle measured with degrees.
AvailableRAM As ULong
Returns the available memory.
CallSubPlus (SubName As SubVoidByte, DelayMs As ULong, Tag As Byte) As void
Runs the given sub after the specified time elapses. Note that this call does not block the thread.
SubName - Sub name as literal string.
DelayMs - Delay in milliseconds.
Tag - Value that will be passed to the called sub. This is useful when multiple CallSubPlus call the same sub.
Example:
CallSubPlus("DoSomething", 1000, 5)
'...

Sub DoSomething(Tag as Byte)

End Sub
cE As Double
e (natural logarithm base) constant.
Ceil (Number As Double) As Double
Returns the smallest double that is greater or equal to the specified number and is equal to an integer.
Constrain (Value As Double, MinValue As Double, MaxValue As Double) As Double
Equivalent to Min(MaxValue, Max(Value, MinValue))
Continue As void
Stops executing the current iteration and continues with the next one.
Cos (Radians As Double) As Double
Calculates the trigonometric cosine function. Angle measured in radians.
CosD (Degrees As Double) As Double
Calculates the trigonometric cosine function. Angle measured in degrees.
cPI As Double
PI constant.
CRLF As StringLiteral
New line character. Value of Chr(13) and Chr(10). Note that in other B4X products the value is Chr(10).
Delay (DelayMs As ULong) As void
Pauses the execution for the specified delay measured in milliseconds.
Note that in most cases it is better to use a Timer or CallSubPlus instead.
DelayMicroseconds (Delay As UInt) As void
Pauses the execution for the specified delay measured in microseconds.
Dim As void
Declares a variable.
Syntax:
Declare a single variable:
Dim variable name [As type] [= expression]
The default type is String.

Declare multiple variables. All variables will be of the specified type.
Dim variable1 [= expression], variable2 [= expression], ..., [As type]
Note that the shorthand syntax only applies to Dim keyword.
Example:Dim a = 1, b = 2, c = 3 As Int

Declare an array:
Dim variable(Rank1, Rank2, ...) [As type]
Example:Dim Days(7) As String
The actual rank can be omitted for zero length arrays.
Exit As void
Exits the most inner loop.
Note that Exit inside a Select block will exit the Select block.
False As Boolean
Floor (Number As Double) As Double
Returns the largest double that is smaller or equal to the specified number and is equal to an integer.
For As void
Syntax:
For variable = value1 To value2 [Step interval]
...
Next
If the iterator variable was not declared before it will be of type Int.

Or:
For Each variable As type In collection
...
Next
Examples:
For i = 1 To 10
Log(i) 'Will print 1 to 10 (inclusive).
Next
For Each n As Int In Numbers 'an array
Sum = Sum + n
Next

Note that the loop limits will only be calculated once before the first iteration.
If As void
Single line:
If condition Then true-statement [Else false-statement]
Multiline:
If condition Then
statement
Else If condition Then
statement
...
Else
statement
End If
IsNumber (Text As String) As Boolean
Tests whether the given string can be safely parsed as a number.
JoinBytes (ArraysOfObjects() As Object) As Byte()
Concatenates the arrays of bytes to a single array.
Think carefully before using this method. In most cases there are better solutions that require less memory (calling Write multiple times for example).
Example:
Dim b() as byte = JoinBytes(Array("abc".GetBytes, Array as Byte(13, 10)))
JoinStrings (Strings() As String) As String
Concatenates the strings to a single string.
Think carefully before using this method. In most cases there are better solutions that require less memory.
Example:
Dim s As String = JoinStrings(Array As String("Pi = ", cPI))
Log (OneOrMoreMessages As Object) As void
Logs one or more messages.
This is a special method. It can accept any number of parameters.
The data is sent through the Serial object.
Each parameter can be a String, number or an array of bytes (which will be treated as a string).
Example:
Log("x = ", x)
Logarithm (Number As Double, Base As Double) As Double
MapRange (Value As Long, FromLow As Long, FromHigh As Long, ToLow As Long, ToHigh As Long) As Long
Maps the value from the "from" range to the "to" range.

Example:
Dim v as int = MapRange(p.AnalogRead, 0, 1023, 0, 255)
Max (Number1 As Double, Number2 As Double) As Double
Returns the larger number between the two numbers.
Micros As ULong
Returns the number of microseconds since the last restart.
Note that this value overflows (goes back to zero) after approximately 70 minutes.
Millis As ULong
Returns the number of milliseconds since the last restart.
Min (Number1 As Double, Number2 As Double) As Double
Returns the smaller number between the two numbers.
Not (Value As Boolean) As Boolean
Inverts the value of the given boolean.
Null As Object
NumberFormat (Number As Double, MinimumIntegers As Byte, MaximumFractions As Byte) As String
Converts the number to string with the specified minimum number of integers and maximum number of fractions.
Power (Base As Double, Exponent As Double) As Double
Returns the Base value raised to the Exponent power.
QUOTE As StringLiteral
Quote character.
Return As void
Returns from the current sub and optionally returns the given value.
Note that only primitives, numbers, strings and array of primitives can be returned.
Syntax: Return [value]
Rnd (Mininum As Long, Maximum As Long) As Long
Returns a random number between Min (inclusive) and Max (exclusive).
RndSeed (Seed As ULong) As void
Sets that random seed value.
Example based on the analog value of a floating pin:
dim p as Pin
p.Initialize(0, p.MODE_INPUT)
RndSeed(p.AnalogRead)
Round (Number As Double) As Long
Returns the whole number closest to the given number.
RunNative (Method As FunctionObjectObject, Arg As Object) As Object
Runs an inline C function.
Example:
RunNative("testc", Null)

#If C
void testc(B4R::Object* o) {
  Serial.println(b4r_main::_i); //print a global variable named i
}
#End If
Select As void
Compares a single value to multiple values.
Example:
Dim value As Int = 7
Select value
  Case 1
    Log("One")
  Case 2, 4, 6, 8
    Log("Even")
  Case 3, 5, 7, 9
    Log("Odd larger than one")
  Case Else
    Log("Larger than 9")
End Select
Sender As Object
Returns the object that raised the event (if it was set).
Sin (Radians As Double) As Double
Calculates the trigonometric sine function. Angle measured in radians.
SinD (Degrees As Double) As Double
Calculates the trigonometric sine function. Angle measured in degrees.
SizeOf (Object As Object) As UInt
Returns the size (number of bytes) of the given object. The object type must be known during compilation.
Sqrt (Value As Double) As Double
Returns the positive square root.
StackBufferUsage As UInt
Returns the usage of the stack memory buffer (set with #StackMemoryBuffer attribute).
Sub As void
Declares a sub with the parameters and return type.
Syntax: Sub name [(list of parameters)] [As return-type]
Parameters include name and type.
The lengths of arrays dimensions should not be included.
Example:
Sub MySub (FirstName As String, LastName As String, Age As Int, OtherValues() As Double) As Boolean
...
End Sub

In this example OtherValues is a single dimension array.
The return type declaration is different than other declarations as the array parenthesis follow the type and not
the name (which does not exist in this case).
TAB As StringLiteral
Tab character.
Tan (Radians As Double) As Double
Calculates the trigonometric tangent function. Angle measured in radians.
TanD (Degrees As Double) As Double
Calculates the trigonometric tangent function. Angle measured in degrees.
True As Boolean
Type As void
Declares a structure.
Can only be used inside Process_Globals sub.
Syntax:
Type type-name (field1, field2, ...)
Fields include name and type.
Example:
Type MyType (Name As String, Value As Int)
Dim a, b As MyType
a.Value = 123
Until As void
Loops until the condition is true.
Syntax:
Do Until condition
...
Loop
While As void
Loops while the condition is true.
Syntax:
Do While condition
...
Loop

Pin


Events:

StateChanged (State As Boolean)

Members:


  A0 As Byte

  A1 As Byte

  A2 As Byte

  A3 As Byte

  A4 As Byte

  A5 As Byte

  A6 As Byte

  A7 As Byte

  AddListener (StateChangedSub As SubVoidBool) As void

  AnalogRead As UInt

  AnalogWrite (Value As UInt) As void

  DigitalRead As Boolean

  DigitalWrite (Value As Boolean) As void

  Initialize (Pin As Byte, Mode As Byte) As void

  Mode As Byte [write only]

  MODE_INPUT As Byte

  MODE_INPUT_PULLUP As Byte

  MODE_OUTPUT As Byte

  PinNumber As Byte

Members description:

A0 As Byte
A1 As Byte
A2 As Byte
A3 As Byte
A4 As Byte
A5 As Byte
A6 As Byte
A7 As Byte
AddListener (StateChangedSub As SubVoidBool) As void
Adds a listener. The StateChanged event will be raised when the pin's state changes.
AnalogRead As UInt
Reads the pin value and returns a value between 0 to 1023.
AnalogWrite (Value As UInt) As void
Writes the given analog value. Value should be between 0 to 255.
This can only be used with pins that support PMW waves.
DigitalRead As Boolean
Reads the pin value and returns true or false.
DigitalWrite (Value As Boolean) As void
Writes the given value.
Initialize (Pin As Byte, Mode As Byte) As void
Initializes the pin and sets the mode.
Mode As Byte [write only]
Sets the pin's mode (one of the MODE constants).
MODE_INPUT As Byte
Input mode.
MODE_INPUT_PULLUP As Byte
Input mode with internal pull up resistor.
MODE_OUTPUT As Byte
Output mode.
PinNumber As Byte
Returns the pin's number.

Serial


Events:

None

Members:


  Close As void

  Initialize (BaudRate As ULong) As void

  Stream As Stream [read only]

Members description:

Close As void
Closes the serial port.
Initialize (BaudRate As ULong) As void
Initializes the serial object and sets the baudrate. Uses the default 8N1 configuration.
The baudrate should match the setting in the IDE if the serial will be used for logging.
Stream As Stream [read only]
Returns the internal stream. This can be used together with AsyncStreams to read or write from the serial.

Stream


Events:

None

Members:


  BytesAvailable As Int

  Flush As void

  ReadBytes (Buffer() As Byte, StartOffset As UInt, Length As UInt) As UInt

  WriteBytes (Buffer() As Byte, StartOffset As UInt, Length As UInt) As UInt

Members description:

BytesAvailable As Int
Returns the number of bytes that are available for reading.
Flush As void
Flushes the stream.
ReadBytes (Buffer() As Byte, StartOffset As UInt, Length As UInt) As UInt
Reads data from the stream and stores it in the buffer.
Will attempt to read the specified number of bytes.
Returns the number of bytes read.
Buffer - Data will be written to this array.
StartOffset - Index of the first byte in the array.
Length - Number of bytes to read.
WriteBytes (Buffer() As Byte, StartOffset As UInt, Length As UInt) As UInt
Writes the data to the stream. Returns the number of bytes written.
All the bytes will be written to the stream if possible.
Buffer - Data that will be written.
StartOffset - Index of the first byte.
Length - Number of bytes that will be written.

String


Events:

None

Members:


  GetBytes As Byte()

  Length As Int [read only]

Members description:

GetBytes As Byte()
Returns the string content as an array of bytes.
Note that the array and string share the same memory.
Length As Int [read only]
Returns the string length.

Timer


Events:

Tick

Members:


  Enabled As Boolean

  Initialize (TickSub As SubVoidVoid, Interval As ULong) As void

  Interval As ULong

Members description:

Enabled As Boolean
Enables or disables the timer.
Initialize (TickSub As SubVoidVoid, Interval As ULong) As void
Initializes the timer. Note that you must set Enabled to True for the timer to start running.
TickSub - The sub that will handle the tick event.
Interval - Interval measured in milliseconds.
Interval As ULong
Gets or sets the timer interval, measured in milliseconds.
Top