Start Back Next End
  
ZBasic System Library
ZBasic Microcontrollers
1
ZBasic for the ESP8266
Introduction
The ZBasic Compiler and IDE are capable of producing applications for ESP8266 devices.  Because the
ESP8266 is different in many respects from the Atmel AVR devices (the original targets of ZBasic) there
are necessarily some differences in how ZBasic applications must be structured and in the set of ZBasic
System Library routines that are available.  There are several new ZBasic System Library routines that
are specific to the ESP8266, some routines that that different capabilities and limitations, and some
routines that are not available at all for the ESP8266.  More details on this topic are found later in this
document.
Also, the intrinsic data type Double is available for the ESP8266, providing 64-bit floating point in
addition to Single which provides 32-bit floating point.  Both data types may be used in a ZBasic
application and automatic conversions are performed when assigning one type to the other (but with loss
of precision when Double is converted to Single).  One caveat is that a variable of one of the two
floating point data types cannot be passed by reference to a subroutine that is expecting the other data
type.  The workaround would be to introduce an interim variable of the expected type, assign the value to
it and then pass it to the subroutine.
ZBasic for AVR devices is capable of producing either single-task or multi-task applications.  For these
devices, the ZBasic Main() routine is usually structured as some initialization code (which may invoke
additional tasks) followed by an infinite loop that performs the intended function of the application.  In
contrast, a ZBasic application for the ESP8266 is single-task only and consists of an optional initialization
subroutine and a Main() routine that performs a small amount of work and then returns.  The "operating
system" of the ESP8266 repeatedly invokes the Main() routine in addition to servicing the WiFi interface
and performing other system functions.  It is recommended that each pass through the Main() routine
occupy no more than 30 milliseconds or so.  Consuming more time that that may interfere with WiFi
processing and, further, if too much time is consumed (on the order of seconds) the ESP8266 watchdog
timer will trigger a device reset.  If an activity to be performed requires more than about 30mS, it can
either be broken up into smaller units or the System Library routine Yield() may be called occasionally
to allow the OS to perform its necessary functions.
With that brief introduction in mind, here is a simple "Hello world" ZBasic application for the ESP8266.
Option ConsoleSpeed 230400
Option UserPostInit myPostInit
Const pin as Byte = A.2
Dim state as Integer
Dim pinState as Byte
Sub Main()
   If (state = 0) Then
       pinState = pinState Xor 1
       Call PutPin(pin, pinState)
       Debug.Print "Hello, world!"
   End If
   state = state + 1
   If (state >= 1000) Then
       state = 0
   End If
   Call Sleep(1)
End Sub
Sub myPostInit()
   Call PutPin(pin, pinState)
   Debug.Print
End Sub
Previous page Top Next page