Silicon Labs BGSCRIPT User manual

BGSCRIPT SCRIPTING LANGUAGE
DEVELOPER GUIDE Wednesday, 2 December 2020 Version 34.
1 Version History ________ ________________________________________________________________ 5
2 Introduction ___________________________________________________________________________ 6
3 What is BGScript? ______________________________________________________________________ 7
3.1 BGScript Scripting Language ________________________________________________________ 7
3.2 BGScript vs. BGAPI ________________________________________________________________ 8
4 BGScript Syntax _______________________________________________________________________ 9
4.1 Comments _______________________________________________________________________ 9
4.2 Variables and Values _______________________________________________________________ 9
4.2.1 Values ____________________________________________________________________ 9
4.2.2 Variables __________________________________________________________________ 9
4.2.3 Global Variables ___________________________________________________________ 11
4.2.4 Constant Values ___________________________________________________________ 11
4.2.5 Buffers ___________________________________________________________________ 11
4.2.6 Strings ___________________________________________________________________ 12
4.2.7 Constant Strings ___________________________________________________________ 12
4.3 Expressions _____________________________________________________________________ 14
4.4 Commands ______________________________________________________________________ 15
4.4.1 event <event_name> (<event_parameters>) _____________________________________ 15
4.4.2 if <expression> then [else] end if _______________________________________________ 15
4.4.3 while <expression> end while _________________________________________________ 16
4.4.4 call <command name>(<command parameters>..)[(response parameters)] _____________ 16
4.4.5 let <variable> = <expression> _________________________________________________ 16
4.4.6 return ____________________________________________________________________ 17
4.4.7 sfloat(mantissa, exponent) ___________________________________________________ 17
4.4.8 float(mantissa, exponent) ____________________________________________________ 18
4.4.9 memcpy(destination, source, length) ___________________________________________ 18
4.4.10 memcmp(buffer1, buffer2, length) _____________________________________________ 18
4.4.11 memset(buffer, value, length) ________________________________________________ 19
4.5 Procedures ______________________________________________________________________ 20
4.6 Using multiple script files ___________________________________________________________ 21
4.6.1 import ___________________________________________________________________ 21
4.6.2 export ___________________________________________________________________ 21
5 BGScript Limitations ___________________________________________________________________ 23
5.1 32-bit resolution __________________________________________________________________ 23
5.2 Declaration required before use ______________________________________________________ 23
5.3 Reading internal temperature meter disabled IO interrupts _________________________________ 23
5.4 Writing data to an endpoint, which is not read ___________________________________________ 23
5.5 No interrupts on Port 2 _____________________________________________________________ 23
5.6 Performance _____________________________________________________________________ 23
5.7 RAM ___________________________________________________________________________ 23
5.8 Flash ___________________________________________________________________________ 24
5.9 Stack ___________________________________________________________________________ 24
5.9.1 Interface drivers ____________________________________________________________ 24
5.10 Debugging _____________________________________________________________________ 24
6 Example BGscripts ____________________________________________________________________ 25
6.1 Basics __________________________________________________________________________ 25
6.1.1 Catching system start-up _____________________________________________________ 25
6.1.2 Catching Bluetooth connection event ___________________________________________ 26
6.1.3 Catching Bluetooth disconnection event _________________________________________ 27
6.2 Hardware interfaces _______________________________________________________________ 28
6.2.1 ADC _____________________________________________________________________ 28
6.2.2 I2C ______________________________________________________________________ 30
6.2.3 GPIO ____________________________________________________________________ 31
6.2.4 SPI ______________________________________________________________________ 33
6.2.5 Generating PWM signals ____________________________________________________ 35
6.3 Timers __________________________________________________________________________ 36
6.3.1 Continuous timer generated interrupt ____________________________________________ 36
6.3.2 Single timer generated interrupt ________________________________________________ 37
Silicon Labs Page of 3 55
6.4 USB and UART endpoints __________________________________________________________ 38
6.4.1 UART endpoint ____________________________________________________________ 38
6.4.2 USB endpoint _____________________________________________________________ 39
6.5 Attribute Protocol (ATT) ____________________________________________________________ 40
6.5.1 Catching attribute write event _________________________________________________ 40
6.6 Generic Attribute Profile (GATT) ______________________________________________________ 41
6.6.1 Changing device name ______________________________________________________ 41
6.6.2 Writing to local GATT database _______________________________________________ 42
6.7 PS store ________________________________________________________________________ 43
6.7.1 Writing a PS keys __________________________________________________________ 43
6.7.2 Reading a PS keys _________________________________________________________ 44
6.8 Flash ___________________________________________________________________________ 45
6.8.1 Erasing, Reading and Writing Flash ____________________________________________ 45
6.9 Advanced scripting examples ________________________________________________________ 47
6.9.1 Catching IO events and exposing them in GATT __________________________________ 47
6.10 Bluegiga Development Kit Specific Examples __________________________________________ 48
6.10.1 Display initialization ________________________________________________________ 48
6.10.2 FindMe demo ____________________________________________________________ 49
6.10.3 Temperature and battery readings to display ____________________________________ 50
6.11 BGScript tricks __________________________________________________________________ 52
6.11.1 HEX to ASCII _____________________________________________________________ 52
6.11.2 UINT to ASCII ____________________________________________________________ 52
7 BGScript editors ______________________________________________________________________ 54
7.1 Notepad ++ ______________________________________________________________________ 54
7.1.1 Syntax highlight for BGScript _________________________________________________ 54
Silicon Labs Page of 4 55
Version History
Bluetooth Low Energy Software 1.3.0 compatible document version. The limitation for the maximum size of all DIM variables is removed.
Bluetooth Low Energy Software 1.3.1 compatible document version. Editorial changes and Examples section updated.
Silicon Labs Page of 5 55

Introduction

This document briefly describes the Bluegiga BGScript programming language for Bluegiga Energy Products. It explains what the BGScript programming language is, what its benefits are, how it may be used, and what its limitations are. The document also contains multiple examples of BGScript code and some API methods, and how it can be used to perform various tasks such as detecting receiving and transmitting data, and managing hardware interfaces like UART, USB, SPI, and I2C.
Bluetooth
Bluetooth
connections,
Low
Silicon Labs Page of 6 55

What is BGScript?

3.1 BGScript Scripting Language
Bluegiga BGScript is a simple BASIC-style programming language that allows end-user applications to be embedded to the Bluegiga fully standalone size, cost and power consumption reductions. Although being a simple and easy-to-learn programming language BGScript does provide features and functions to create fairly complex and powerful applications and it provides the necessary APIs for managing Bluetooth connections, security, data transfer and various hardware interfaces such as UART, USB, SPI, I2C, GPIO, PWM and ADC.
BGScript is fully event based programming language and code execution is started when events such as system start-up,
BGScript applications are developed with Bluegiga's free-of-charge BGScript applications are executed in the BGScript Virtual Machine (VM) that is part of the Bluegiga Bluetooth Low Energy software. The Bluetooth Low Energy SDK comes with all the necessary tools for code editing and compilation and also the needed tools for installing the complied firmware binary to the Bluegiga Bluetooth Low Energy modules. Multiple example applications and code snipplets are also available for Bluegiga implementing applications like thermometers, heart rate transmitters, medical sensors and iBeacons just to mention a few.
The illustration below describes the Bluegiga Bluetooth Low Energy software, API and how BGScript VM and applications interface to it.
Bluetooth
Bluetooth
Bluetooth
Low Energy devices without the need of an external MCU and this enables further
connection, I/O interrupt etc. occur.
Low Energy modules. The benefit of using BGScript is that one can create
Bluetooth
Low Energy SDK and the
Silicon Labs Page of 7 55
Silicon Labs Page of 8 55
Silicon Labs Page of 9 55
Silicon Labs Page of 10 55
Silicon Labs Page of 11 55
Silicon Labs Page of 12 55
Silicon Labs Page of 13 55
Silicon Labs Page of 14 55
Silicon Labs Page of 15 55
Silicon Labs Page of 16 55
Silicon Labs Page of 17 55
Silicon Labs Page of 18 55
Loading...
+ 37 hidden pages