B9X C

B9X C Language Reference

B9X C is a high level language that utilizes pretty much the same syntax as C.
This language is case sensitive. You can use this documentation page as a reference for understanding the functions. B9X C operates as, what technically is called, a finite state machine. This is nothing more than a while(1) {;} loop that you define. All the included functions return immediately. This means there are no blocking functions. The only exception is wait(). By definition, wait(); is defined to block intentionally for the number of milliseconds you provide.

The best way to get started is to view the included examples and try them out on our module. Below is a download link to a zip file that contains nothing more than text files. This file contains numerous application written exclusively for B9X C. You simply upload the ‘config.txt’ and the ‘main.c’ text files, for each example to our module, to try them out.

To try out the examples, you need to purchase our module. Please go to the ‘Shop’ section found on this site or go to B9X Developer Module to purchase.

WiFi Network Setup For Static IP

These functions are defined to work with most any WiFi router.
The below values are only examples and can be added to the config.txt file. This file
can then be uploaded to the module for setting a static IP.
Do a module reset after uploading. All fields must be included.

Add To config.txt:
[DEVICENAME][B9X_CDEV40]
[LOCALHOST][192.168.5.40]
[SUBNET][255.255.255.0]
[GATEWAY][192.168.5.90]
[DNS1][192.168.5.90]
[DNS2][8.8.8.8]
[LOCALPORT][24000]

Variables:

Three types of variables are supported. String variables, double precision floating point, and limited support for
arrays of double precision floating point.

There are no declarations required for double precision floating point.
i=3.14; is an example of assigning a value to double.

Arrays of double precision floating point variables must be declared before they are used and
must begin with the ‘@’ character. Up to 20 arrays can be created.

To declare an array:
new @arrayVar[]; // creates an array named @arrayVar and assigns all its 128 values to 0.0

To delete an array:
delete @arrayVar[]; // deletes an array named @arrayVar

To assign a value to an array element:
@arrayVar[0]=3.14; // arrays can only be indexed from 0 to 127

Strings must be declared before they are used.
$var=””; is an example of declaring the string variable $var. All string variables must
begin with the ‘$’ character.

Two types can represent strings:
1) quoted string -> “in quotes”
2) string variable -> $str

Math operators for double precision floating point:

Assignment operators: =, +=, -=, *=, /=
Conditional expressions: ? :
Logical operators: !, &&, ||
Comparison operators: ==, !=, <, <=, >, >=
Binary arithmetic operators: +, -, *, /
Modulus operator: %
Exponentiation: **
Unary arithmetic operators: +, –
Parentheses ( )

Native Clauses And Equivalents:

Examples:

1)
statement;
statement;
statement;

2)
do while(expression) {
statements;
}

3)
while(expression) {
statements;
}

4)
if(expression) {
statements;
}

5)
if(expression) {
statements;
}
else {
statements;
}

6) Equivalant for() clause
// example
for(i=0;i<10;i++) {
statements;
}

// equivalent
i=0; // example -> for(i=0;…;…) {;}

while(i<10) { // example -> for(…;i<10;…) {;}
statements;
statements;

    i+=1; // example -> for(...;...;i++) {;}

}

7) Equivalent switch/case clause

// example
switch(n) {
case 1:
statements;
break;

    case 2:
        statements;
        break;

    case 3:
        statements;
        break;

    default:
        statements;
        break;

}

// equivalent
if(n==1) {
statements;
}
if(n==2) {
statements;
}
if(n==3) {
statements;
}
else {
statements; // default statements
}

Math Functions

/==========================================================/
double fabs(double value);
/==========================================================/

This function returns the absolute value of the double ‘value’

/==========================================================/
double log(double value);
/==========================================================/

This function returns the natural logarithm (base e) of the double ‘value’

/==========================================================/
double modf(double value);
/==========================================================/

This function returns the decimal part of the double ‘value’.
Example: 3.14 will return 0.14

/==========================================================/
double log10(double value);
/==========================================================/

This function returns the base-10 logarithm of the double ‘value’.

/==========================================================/
double exp(double value);
/==========================================================/

This function returns the Euler’s number (e ≈ 2.71828) raised
to the power of the double ‘value’.

/==========================================================/
double sin(double value);
/==========================================================/

This function returns the sine of an angle (in radians) of
the double ‘value’.

/==========================================================/
double cos(double value);
/==========================================================/

This function returns the cosine of an angle (in radians) of
the double ‘value’.

/==========================================================/
double tan(double value);
/==========================================================/

This function returns the tangent of an angle of an angle (in radians) of
the double ‘value’.

/==========================================================/
double asin(double value);
/==========================================================/

This function returns the arc sine (inverse sine) of an angle of
the double ‘value’ and returns the result in radians. The input must be in the range -1 to 1.

/==========================================================/
double acos(double value);
/==========================================================/

This function returns the arc cosine (inverse cosine) of an angle of
the double ‘value’ and returns the result in radians. The input must be in the range -1 to 1.

/==========================================================/
double atan(double value);
/==========================================================/

This function returns the arc tangent/inverse tangent of the
double ‘value’ and returns the result in radians.

/==========================================================/
double cosh(double value);
/==========================================================/
This function returns the hyperbolic cosine of an angle of
the double ‘value’ in radians.

/==========================================================/
double sinh(double value);
/==========================================================/

This function returns the hyperbolic sine of an angle of
the double ‘value’.

/==========================================================/
double sqrt(double value);
/==========================================================/

This function returns the square root of a the double number ‘value’.

/==========================================================/
double round(double value);
/==========================================================/

This function is used to round a floating-point number to the
nearest integer. Only the first number after the decimal point
is considered.

/==========================================================/
double random(double lowest, double highest);
/==========================================================/

This function is used to provide a random floating-point number
in the range ‘lowest’ to ‘highest’.

String Functions

/==========================================================/
stringvar get_nth_field(double index, stringvar $fields);
/==========================================================/

This function returns the nth field in a series of square bracket delimited string data.
Example:
$var=””;
$fields=”[one][two][three][four]”;
$var=get_nth_field(3, $fields);

$var would now equals "three"

/==========================================================/
stringvar get_nth_field(double index, stringvar $fields);
/==========================================================/

This function returns the nth field in a series of less than/greater than bracket delimited string data.
Example:
$var=””;
$fields=””;
$var=get_nth_field_lg(3, $fields);

$var would now equals "three"

/==========================================================/
stringvar ftoa(double value);
/==========================================================/

This function returns the string equivalent of the provided double ‘value’.

Example:
n=3.14;
$var=ftoa(n);

$var would now equal "3.14"

/==========================================================/
double strcmp(stringvar $str1, stringvar $str2);
/==========================================================/

This function compares two string variables to see if they are equivalent. This function is case sensitive.

Example:
$var1="hello";
$var2="hello";
n=strcmp($var1, $var2);

n would now equal 0.0 meaning the strings are equivalent. If they were not equal it would return 1.0.

/==========================================================/
double strcat(stringvar $str1, stringvar $str2);
/==========================================================/

This function concatenates string variable $str2 onto $str1.
Example:
$str1=””;
$str2=””;
$str1=”hello “;
$str2=”world!”;
strcat($str1, $str2);

$str1 would now equal "hello world!"

/==========================================================/
double strcat(stringvar $str, stringquote “…”);
/==========================================================/

This function concatenates stringquote onto $str1.
Example:
$str=””;
$str=”hello “;
strcat($str, “world!”);

$str1 would now equal "hello world!"

/==========================================================/
double strsave(stringquote “…”, stringvar $str);
/==========================================================/

This function saves a string variable named stringquote “…” with
a value of $str into flash memory.

Example:
    $str="";
    $str="3.14";
    strsave("VAR1", $str);

    VAR1 will now equal "3.14" and saved into flash memory.

/==========================================================/
double strget(stringvar $str1, stringvar $str2);
/==========================================================/

This function retrieves a string variable named $str1
from flash memory.

Example:
    $str1="";
    $str2="";
    $str1="VAR1";
    strget($str1, $str2);

    $str2 will now equal "3.14" that was previously saved using strsave.

/==========================================================/
double strget(stringquote “…”, stringvar $str);
/==========================================================/

This function retrieves a string variable named stringquote “…”,
from flash memory, and places it into $str.

Example:
    $str="";
    strget("VAR1", $str);

    $str will now equal "3.14" that was previously saved using strsave.

/==========================================================/
double strtof(stringvar $str);
/==========================================================/

This function will take the string variable $str and return its value as a double.
Example:
$str=””;
value=0;
$str=”3.14″;
value=strtof($str);

    The double variable will now be equal to 3.14 .

/==========================================================/
stringvar encrypt_decrypt(double encrypt_decrypt, stringvar $text);
/==========================================================/

This function encrypts/decrypts the text found in $text. It uses the key
contained in the config.sys file. The encryption type is AES 256 bits.

Set encrypt_decrypt to 0 to encrypt the
text and 1 to decrypt the text. A maximun of 112 characters of text can
can be encrypted.

Example:
[KEY][0123456789ABCDEF0123456789ABCDEF]

Note: This key must be exactly 32 bytes long.

/==========================================================/

Date And Time Functions

These functions are defined to work with most any NTP Time Server.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions.
Do a module reset after uploading.

Add To config.txt:
[TIMESERVER][pool.ntp.org]
[TIMEZONE][EST5EDT,M3.2.0/2,M11.1.0]

/==========================================================/
double get_month(double reserved);
/==========================================================/

Returns the current month.

/==========================================================/
double get_day(double reserved);
/==========================================================/

Returns the current day.

/==========================================================/
double get_year(double reserved);
/==========================================================/

Returns the current year.

/==========================================================/
double get_hour(double reserved);
/==========================================================/

Returns the current hour.

/==========================================================/
double get_minute(double reserved);
/==========================================================/

Returns the current minute.

/==========================================================/
double get_second(double reserved);
/==========================================================/

Returns the current minute.

/==========================================================/
stringvar get_date_str();
/==========================================================/

Returns a properly formatted string, for text to speech, of the current date.

/==========================================================/
stringvar get_time_str();
/==========================================================/

Returns a properly formatted string, for text to speech, of the current time.

/==========================================================/
stringvar get_dow_str();
/==========================================================/

Returns a properly formatted string, for text to speech, of the current weekday.

Misc.

/==========================================================/
call function(stringvar $str);
/==========================================================/

This routine calls the function with the name that is stored in $str. A function
is simply a text file with extension .c . This text file must contain code and
a return statement. Functions are uploaded, to your device, using most any browser.
If main.c or any function.c file calls a function it must exist on your devices drive.
config.txt .

Example:
$str=””;
$str=”sayDateTime”;
call function($str);

This example will run the code that is in the sayDateTime.c file. It will return to the calling
code when it encounters a ‘return;’ statement.

/==========================================================/
call function(stringquote “…”);
/==========================================================/

This routine calls the function with the name you provide in quotes. A function
is simply a text file with extension ‘.c’. This text file must contain code and
a return statement. Functions are uploaded, to your device, using most any browser.
If ‘main.c’ or any ‘somefunction.c’ file calls a function it must exist on your devices drive.
config.txt.

Example:
call function(“sayDateTime”);

This example will run the code that is in the sayDateTime.c file. It will return to the calling
code when it encounters a ‘return;’ statement.

/==========================================================/
return;
/==========================================================/

This statement returns from a function to the code that called it. This statement should
never be used in ‘main.c’.

/==========================================================/
double os_function(double osfunction);
/==========================================================/

This functions executes the os funtion osfunction. Currently, the only available
funtion has a value of ‘0’ and reboots your device.

Example:
os_function(0); reboots your device.

/==========================================================/
double printf(stringquote “…”);
/==========================================================/

This function prints the quoted string and crlf on the console output. The console
output is the USB serial port connected to UART0. The printf function is most commonly used
to help in debugging your application.

Example:
printf(“Hello World!!!”);

/==========================================================/
double printf(stringvar $str);
/==========================================================/

This function prints the string, contained in $str, and crlf on the console output. The console
output is the USB serial port connected to UART0. The printf function is most commonly used
to help in debugging your application.

Example:
$str=””;
$str=”Hello World!!!”;
printf($str);

/==========================================================/
double get_ticks(double units);
/==========================================================/

This functions returns the time since the processor started.
Units can be ‘0’, ‘1’, ‘2’, ‘3’.

‘0’ returns seconds
‘1’ returns milliseconds
‘2’ returns microseconds

/==========================================================/
double wait(double milliseconds);
/==========================================================/

This function suspends your app for for the milliseconds you provide.
This function is useful for timed events and allowing internal background tasks
to do their processing. A wait of 10 milliseconds is required in the main loop
to prevent watchdog timeout.

Example:
wait(1000); // waits 1 second.

ADC Functions

ADC functions are used to convert analog values to digital values
that your program can use.On boot, GPIO1, GPIO2, GPIO3, GPIO4
are configured as ADC0_0, ADC_1, ADC_2, ADC_3 respectively.

/==========================================================/
double read_raw_adc(double adcNumber);
/==========================================================/

Returns the raw analog value of adcNumber. adcNumber can be
0, 1, 2, or 3. This number is in the range of 0 to 4095.
0 is 0 volts and 4095 is 3.3 volts. You can find
the voltage with the formula volts=(analog value)/4095.

/==========================================================/
double read_cal_adc(double adcNumber);
/==========================================================/

Returns the calibrated analog value of adcNumber. adcNumber can be
0, 1, 2, or 3. This number is in the range of 0 to 3300 millivolts.
0 is 0 volts and 3300 is 3.3 volts. You can find
the voltage with the formula volts=(analog value)/1000.

/==========================================================/

Input And Output

/==========================================================/
double make_output(double GPIONumber);
/==========================================================/

This function sets the provided GPIONumber to an output. You can
set the output with set_high(GPIONumber) and set_low(GPIONumber);

/==========================================================/
double make_input(double GPIONumber);
/==========================================================/

This function sets the provided GPIONumber to an input. You can
get the input with get_input(GPIONumber).
It will return a 0 or a 1.

/==========================================================/
double set_pullup(double GPIONumber);
/==========================================================/

This function connects the internal pull up resistor to high or
3.3 volts. The get_input(GPIONumber) will return a 1 if
left floating or when connected to 3.3 volts.

/==========================================================/
double set_pulldown(double GPIONumber);
/==========================================================/

This function connects the internal pull down resistor to low or
0 volts. The get_input(GPIONumber) will return a 0 if
left floating or when connected to ground.

/==========================================================/
double get_input(double GPIONumber);
/==========================================================/

This function gets the logic level of the provided GPIONumber.
It will return either a 0 for 0 volts or a 1 for 3.3 volts.

/==========================================================/
double set_high(double GPIONumber);
/==========================================================/

This function sets the logic level of the provided GPIONumber to
1 to output 3.3 volts.

/==========================================================/
double set_low(double GPIONumber);
/==========================================================/

This function sets the logic level of the provided GPIONumber to
0 to output 0 volts.

/==========================================================/

MQTT Functions

These functions are for communicating to and from a MQTT server.
MQTT stands for Message Queuing Telemetry Transport. A variety of
useful information is available on the Internet.

The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions. The values
you provide are the parameters of the MQTT server to connect to on
startup. Do a module reset after uploading. Your device will connect
to and maintain a connection with the server upon device reset or
power up.

[MQTT_URI][mqtt://192.168.1.130:1883] this value sets the URI of the server
[MQTT_USERNAME][myusername] this value sets the server’s username
[MQTT_PASSWORD][mypassword] this value sets the server’s password

/==========================================================/
stringvar mqtt_get_message();
/==========================================================/

This function gets any messages that came from the server for
the topics you have subscribed to. The messages contain 4 fields in square bracketed
delimited format. The fields are:

[command][message id][topic][payload]

You can parse these fields using the get_nth_field function.

The possible received commands are:

“DATA” – data was received that was published from a remote client
“CONNECT” – your device has successfully connected to the server
“DISCONNECT” – your device has disconnected from the server
“SUBSCRIBED” – your device has successfully subscribed to a topic
“UNSUBSCRIBED” – your device has successfully unsubscribed from a topic
“PUBLISHED” – your device has successfully published a data payload to a topic
“ERROR” – an error has occurred

Depending on the command, all the 4 received fields may not be populated.

/==========================================================/
double mqtt_subscribe(stringvar $topic);
/==========================================================/

This function subscribes to the provided $topic. Any messages,
from remote clients that publish to that topic, will be received
by your device when mqtt_get_message is called.

/==========================================================/
double mqtt_unsubscribe(stringvar $topic);
/==========================================================/

This function unsubscribes to the provided $topic. Any messages,
from remote clients that publish to that topic, will NOT be received,
by your device, when mqtt_get_message is called.

/==========================================================/
double mqtt_publish(stringvar $topic, stringvar $payload);
/==========================================================/

This function posts the data provided in $payload to the provided $topic.
Any remote clients, that are subscribed to that topic, will
receive this payload.

/==========================================================/

LED Strip:

These functions are defined to work with the WS2812b led strip.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions.
Do a module reset after uploading.

Add To config.txt:
[STRIP_LED][46] the data pin of the LED(s)
[MAX_LEDS][300] number of LEDs connected

This sets the data pin to GPIO46 on a led strip that can have up to 300 leds.

/==========================================================/
double led_set_color(double ledNumber, double redColor, double greenColor, double blueColor);
/==========================================================/

This function sets the color of a specific LED specified by ledNumber.
The ledNumber is from 0 to [MAX_LEDS]-1. The redColor, greenColor,
and blueColor are in the range of 0 to 255. The higher the value,
the brighter is that color. Be careful not to set these values too high
because they can be too bright.

The led_set_color does not actually change the color of all LEDs. You
must call led_all_refresh to update all LED(s) to this setting.

Example:
A red color with value of 10 appears to be a normal red color.

/==========================================================/
double led_all_off();
/==========================================================/

This function turns all LED(s) off. The update
will appear instantly across all LEDs.

/==========================================================/
double led_all_refresh();
/==========================================================/

This function updates all LEDs previously set with led_set_color. The update
will appear instantly across all LEDs.

/==========================================================/

Text To Speech

These functions are defined to work with the SYN6988 TTS Module.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions. The values
you provide are the GPIO numbers that connect to that pin on the
SYN6988. Do a module reset after uploading.

Add To config.txt:
[SYN6988_BUSY][10]
[SYN6988_RXD][11]
[SYN6988_TXD][12]

/==========================================================/
double is_speak_busy(double reserved);
/==========================================================/

This function returns the state of the busy pin. This
function returns immediately. When text is spoken, it won’t finish
right away. You can determine when its done when this function
returns a zero.

/==========================================================/
double speak(stringquote “…”);
/==========================================================/

This function speaks the provided quoted string. You can determine,
when it is done by calling the is_speak_busy function.

/==========================================================/
double speak(stringvar $text);
/==========================================================/

This function speaks the provided string variable $text. You can determine,
when it is done by calling the is_speak_busy function.

/==========================================================/

Air Quality Monitoring Functions

These functions are defined to work with the PMS5003 Air Quality Sensor.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions. The values
you provide are the GPIO numbers that connect to that pin on the
PMS5003. Do a module reset after uploading.

Add To config.txt:
[PMS5003_RXD][18]
[PMS5003_TXD][5]

/==========================================================/
double get_air_quality(double register);
/==========================================================/

This function gets the air quality values from the PMS5003 sensor.
There are 12 registers that provide different air quality units.
Registers are numbered from 0 to 11.Please refer to the PMS5003
datasheet for their definitions.

/==========================================================/

Temperature Functions

These functions are defined to work with the DS18b20 Temperature Sensor.
The below value, the data pin GPIO, must be added to the config.txt file
and this file must be uploaded to module before using these functions.
Do a module reset after uploading.

Add To config.txt:
[ONEWIRE_BUS][21]

/==========================================================/
double get_temperature(double sensorNumber);
/==========================================================/

This function returns the temperature reading of a DS18b20 sensor. Up
to 2 sensors can be connected to the data pin. The sensorNumber is either
0 or 1. The temperature returned is in centigrade, You can easily convert
to farenheit using the formula of: ‘F=(9*C)/5 +32.0;’

/==========================================================/

Motion Functions

These functions are defined to work with the RD-03 RF motion sensor.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions. The values
you provide are the GPIO numbers that connect to that pin on the
RD-03. Do a module reset after uploading.

Add To config.txt:
[RD03_TXD][13]
[RD03_RXD][14]

/==========================================================/
double get_motion(double inches_or_cm);
/==========================================================/

This function returns the distance from the sensor where motion is
detected. Providing 0 will return centimeters and 1 will return inches.

/==========================================================/
Display Functions


These functions are defined to work with the SSD1306 Display.
The below values must be added to the config.txt file and this file
must be uploaded to module before using these functions.
Do a module reset after uploading.

Add To config.txt:
[SSD1306_SDA][16]
[SSD1306_SCL][17]

/==========================================================/
double display_text(stringquote “…”);
/==========================================================/

This function will display text on the ssd1306 display. Be sure
to use the square display and not the thin rectangular display.
With this the first line is typically yellow and the remaining lines
are blue.There are 4 lines to the display. A ‘~’ character forces
the text before the ‘~’ to be displayed.

Example:
if stringquote is “Hello~World~”, the display will show
‘Hello’ on the first line and ‘World’ on the second.

/==========================================================/
double display_text(stringvar $text);
/==========================================================/

This function will display text on the ssd1306 display. Be sure
to use the square display and not the thin rectangular display.
With this the first line is typically yellow and the remaining lines
are blue.There are 4 lines to the display. A ‘~’ character forces
the text before the ‘~’ to be displayed.

Example:
if $text=”Hello~World~Today~”, the display will show
‘Hello’ on the first line and ‘World’ on the second, and
‘Today’ on the third.

/==========================================================/

The information provided here by B9X Electronics (‘we,’ ‘us,’ or ‘our’), in this document,
is for general informational purposes only. All information in this document
is provided in good faith, however, we make no representation or warranty of any kind,
express or implied, regarding the accuracy, adequacy, validity, reliability, availability,
or completeness of any information in this document. Under no circumstance shall we have any liability
to you for any loss or damage of any kind incurred as a result of the use of this document
or reliance on any information provided in this document.”