Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
ezeio2:scriptref:start [2019-09-02 20:49] – andreh | ezeio2:scriptref:start [2021-09-22 17:55] – andreh | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Script reference ===== | ===== Script reference ===== | ||
- | {{indexmenu_n> | + | {{indexmenu_n> |
=== ezeio script programming - the PAWN language === | === ezeio script programming - the PAWN language === | ||
Line 31: | Line 31: | ||
The estimated requirements for a script is displayed when compiling. | The estimated requirements for a script is displayed when compiling. | ||
- | === Programming | + | === Programming |
- | Although the user script runs in a sandboxed runtime engine, the recommended programming pattern is similar to cooperative multitasking. | + | Although the user script runs in a sandboxed runtime engine, the recommended programming pattern is similar to [[https:// |
- | + | ||
- | This means that you should avoid long-running loops, and instead make use of blocking calls or system callbacks provided in the function library. | + | |
- | + | ||
- | The following calls are blocking: | + | |
- | + | ||
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
- | | '' | + | |
+ | This means that you should avoid long-running loops, and instead make use of system callbacks provided in the function library. | ||
The following system callback functions are defined: | The following system callback functions are defined: | ||
| '' | | '' | ||
- | | '' | + | | '' |
| '' | | '' | ||
| '' | | '' | ||
| '' | | '' | ||
- | | '' | ||
+ | === Some examples === | ||
+ | |||
+ | The program below will print out " | ||
+ | |||
+ | <code javascript> | ||
+ | main() | ||
+ | { | ||
+ | PDebug(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The following example adds the values of field 1 and field 2 together, and writes the result to field 3. | ||
+ | |||
+ | <code javascript> | ||
+ | // This example adds the value of field 1 and 2 and writes the sum to field 3 | ||
+ | // The value of field 3 is updated every 500ms (twice per second) | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | SetTickInterval(500); | ||
+ | } | ||
+ | |||
+ | @Tick(uptime) | ||
+ | { | ||
+ | new f1, f2; // declare local variables | ||
+ | | ||
+ | f1 = GetField(1); | ||
+ | f2 = GetField(2); | ||
+ | | ||
+ | SetField(3, f1+f2); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The example below illustrates the use of a global variable, the use of the main() function to initialize the values, and the @Tick(uptime) call. The program will use field number 1, and count from 100 down to 50, and then starting over. | ||
+ | |||
+ | <code javascript> | ||
+ | // A simple demo program, counting down from 100 to 50, and then starts over | ||
+ | |||
+ | new myCounter; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | SetTickInterval(1000); | ||
+ | myCounter = 100; // Initialize the counter with value 100 | ||
+ | } | ||
+ | |||
+ | @Tick(uptime) | ||
+ | { | ||
+ | myCounter = myCounter - 1; // Count down the counter with 1 | ||
+ | | ||
+ | if(myCounter <= 50) { // If we reached 50 (or lower) | ||
+ | myCounter = 100 // ..then set it back to 100 | ||
+ | } | ||
+ | | ||
+ | SetField(1, myCounter); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The example below will run a 3-speed fan based on the input from a temperature sensor on field 1. | ||
+ | Output 1 is used for low speed, output 2 is medium speed, and output 3 is high speed. | ||
+ | The speed is selected based on how much the temperature exceeds a given setpoint (here set to 20.5 degrees C) | ||
+ | |||
+ | <code javascript> | ||
+ | main() | ||
+ | { | ||
+ | SetTickInterval(5000); | ||
+ | } | ||
+ | |||
+ | @Tick(uptime) | ||
+ | { | ||
+ | new Float:temp, Float:diff; // Declare " | ||
+ | | ||
+ | temp = GetFieldFloat(1); | ||
+ | diff = temp - 20.5; // 20.5 is our setpoint. diff is the differece | ||
+ | | ||
+ | if(diff > 0.0) // Over setpoint? | ||
+ | SetOutput(1, | ||
+ | else | ||
+ | SetOutput(1, | ||
+ | | ||
+ | if(diff > 2.0) // 2 degrees or more over setpoint? | ||
+ | SetOutput(2, | ||
+ | else | ||
+ | SetOutput(2, | ||
+ | |||
+ | if(diff > 6.0) // 6 degrees or more over setpoint? | ||
+ | SetOutput(3, | ||
+ | else | ||
+ | SetOutput(3, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === State machines === | ||
+ | |||
+ | A common programming pattern in control applications is to use state machines. PAWN and the ezeio implements strong support for state machines. The following is a typical pattern showing the startup sequence of an engine. Note that there are three @Tick handlers; one for each state. Also note the " | ||
+ | |||
+ | <code javascript> | ||
+ | |||
+ | new count = 0; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | SetTickInterval(100); | ||
+ | state WAITING; | ||
+ | } | ||
+ | | ||
+ | @Tick(uptime) < | ||
+ | { | ||
+ | if( GetField(1) < 100 ) // condition to start up the process | ||
+ | state IGNITION; | ||
+ | } | ||
+ | | ||
+ | // ***** the IGNITION state | ||
+ | |||
+ | entry() < | ||
+ | { | ||
+ | | ||
+ | count = 0; | ||
+ | } | ||
+ | | ||
+ | @Tick(uptime) < | ||
+ | { | ||
+ | if( GetField(2) > 100 ) // Did the engine start? | ||
+ | state RUNNING; | ||
+ | |||
+ | | ||
+ | state WAITING; | ||
+ | } | ||
+ | | ||
+ | exit() < | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | // ***** the RUNNING state | ||
+ | |||
+ | @Tick(uptime) < | ||
+ | { | ||
+ | if( GetField(2) < 100 ) // Check if the engine stopped | ||
+ | state WAITING; | ||
+ | } | ||
+ | | ||
+ | </ | ||