📄 bombilla.html
字号:
<center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>call settimer0(10); </pre> </code> </td></tr> </table> </center> This will start firing Timer0 at 1Hz, or once per second. Hit <b>Inject</b>. You should see the LEDs start ticking.</p> <p> <table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3> <tr bgcolor="#e0e0ff"> <td width="100%"><nobr> <b> <font face="arial,helvetica">TinyScript</font> </b></td> </tr> </table> </p> <p>TinyScript programs have two sections. The first is the variable declaration region. In the Timer handler above, this was the <code>private counter;</code> statement. All variables must be declared before any statements. There are three kinds of variables: <code>private</code>, which only that handler can access, <code>shared</code>, which all handlers can access, and <code>buffer</code> variables, which are shared. For example, this program declares a shared variable, a private variable, a buffer, and turns on the red LED: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>shared data;private value;buffer cache;call led(1); ! Turn on red led </pre> </code> </td></tr> </table> </center> Comments can be entered with <code>!</code>, which comments out everything until the end of line.</p> <p>Private and shared valuables can be either sensor readings, generated by primitives such as <code>temp</code> or <code>light</code>, or integers. Sensor readings are immutable; trying to modify them will result in a run-time error. You also cannot compare with with anything except sensor values of the same type (e.g., light with light). For example: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>private cond;shared value;cond = call rand() & 1;if (cond) then value = call light();else value = 20;end ifvalue = value / 2; ! This will fail if value is a light reading </pre> </code> </td></tr> </table> </center> If you need to modify sensor readings, say, for averaging, then you can cast them to integers with the <code>cast</code> primitive. This is a version of the above program that will execute without a run-time error: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>private cond;shared value;cond = call rand() & 1;if (cond) then value = call cast(call light());else value = 20;end ifvalue = value / 2; ! Now it works </pre> </code> </td></tr> </table> </center> </p> <p>Buffers are indexed with <code>[]</code>; by default, they have ten elements. An empty index implies the tail of the buffer; <code>call cast(buf[]);</code> will return an integer value of the end of the buffer, while <code>buf[] = 0;</code> will append to a buffer.</p> <p>Buffers are typed. An empty buffer has no type. The <code>bclear</code> primitive empties a buffer. A buffer takes the type of the first element put into it (integer, light reading, etc.), and a run-time error occurs if any other type is put into it. Here is a program that sends a light reading over a tree-based ad-hoc collection routing protocol: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>buffer buf;call bclear(buf);buf[] = call light();call send(buf); </pre> </code> </td></tr> </table> </center> </p> <p>TinyScript supports basic arithmetic operations, such as addition (<code>+</code>), multiplication (<code>*</code>), division (<code>/</code>), modulus (<code>%</code>), as well as boolean operations such as and (<code>AND</code>) as well as or (<code>OR</code>), as well as their binary (logical) equivalents (<code>&</code>, <code>|</code>).</p> <p>Primitives are invoked with the <code>call</code> keyword (e.g., <code>call led(1);</code>). If the primitive has a return value (check its documentation), it must be stored. For example, <code>rand()</code> returns a random value. <code>call random();</code> will cause a compile error, while <code>rval = call random();</code> will not.</p> <p>Control structures include <code>if ... then</code> as shown above, as well as loops. For example, to put ten successive light values into a buffer: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>buffer buf;private i;call bclear(buf);for i=0 to 9 buf[] = call light();next icall send(buf); </pre> </code> </td></tr> </table> </center> </p> <p>There are a limited number of buffers and variables. By default, there are 16 shared variables, eight private variables (per handler), and eight buffers.</p> <p>A full <A HREF="../mate-manual.pdf">reference</A> for TinyScript can be found in the standard documentation directory. This tutorial only covers a few major points.</p> <p> <table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3> <tr bgcolor="#e0e0ff"> <td width="100%"><nobr> <b> <font face="arial,helvetica">Errors</font> </b></td> </tr> </table></p> <p>When Maté encounters an error in a program (e.g. trying to add a light and temperture value), it enters an error state. In this error state, it stops running capsules. Instead, it periodically toggles all of the LEDs (all of them blinking on and off is an indication something has gone wrong) and sends a packet over the UART. The packet is a MatéErrorMsg and contains information on the cause of the error.</p> <p> <table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3> <tr bgcolor="#e0e0ff"> <td width="100%"><nobr> <b> <font face="arial,helvetica">A Simple Program</font> </b></td> </tr> </table></p> <p>Here is a simple program, where each mote will periodically (one every ten seconds) sample its temperature and light sensors, but those values into a buffer, and route those to a base station. Note that, for this to work, there must be a routing base station (a node with ID 0).</p> <p>If Timer0 is running from the earlier part of the tutorial, stop it with the Once handler by <code>call settimer0(0);</code>. Then, install this as Timer0: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>buffer buf;private counter;counter = counter + 1;if (counter >= 10) then call bclear(buf); buf[] = call cast(call light()); buf[] = call cast(call temp()); counter = 0; call send(buf);end if </pre> </code> </td></tr> </table> </center> </p> followed by this as Once: <center> <table border=0 hspace=4 cellspacing=2 width="80%" cellpadding=3> <tr bgcolor="#e0e0e0"> <td width="100%"> <code> <pre>call settimer0(10); </pre> </code> </td></tr> </table> </center> Alternatively, instead of maintaining the counter value, you could <code>call settimer0(100);</code>.</p> <table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3> <tr bgcolor="#e0e0ff"> <td width="100%"><nobr> <b> <font face="arial,helvetica">Data Consistency</font> </b></td> </tr> </table></p> <p>By default, multiple Maté handlers can run concurrently; the VM interleaves their execution at instruction-level granularity. When there are shared variables (and buffers), this can lead to possible race conditions. Using installation-time program analysis, Maté ensures race-free, deadlock-free execution of handlers. If two handlers can safely run concurrently (they share no variables), then the scheduler lets them do so.</p> <p>This can fail due to handler installation. Notably, as handlers are installed immediately upon reception, a context can be in the midst of a computation when it is halted due to new code being installed.</p> <p>This means that to ensure perfect data consistency, the values of shared variables cannot be transferred from one version of a handler to the next. For example, before installing a new version of the clock handler, one could install a clock handler that clears out the shared variables, restoring them to a consistent state.</p> <p>We are currently working on a handler installation mechanism that will preserve execution atomicity; this understandably becomes difficult in the presence of buggy handlers that do not halt.</p> <table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3> <tr bgcolor="#e0e0ff"> <td width="100%"><nobr> <b> <font face="arial,helvetica">Further Use </font> </b></td> </tr> </table></p> <p>This document just touches on Maté and some simple programs that you can write with the example Bombilla VM. Maté allows you to build customized VMs, with user-specified event handlers and primitives. For example, you can build a scripting environment that receives and sends packets, reacts to target detection, or acts as a data aggregation engine. For more information, please refer to the <A HREF="../mate-manual.pdf">Maté manual</A></p> <p>Maté is in active development. If you find bugs, please enter them in the <A HREF="http://sourceforge.net/tracker/?group_id=28656&atid=393934">TinyOS bug tracker</A> on Sourceforge. Feature requests and questions should be sent to the standard TinyOS users list, <code>tinyos-users@millennium.berkeley.edu</code>. You can sign up <A HREF="http://webs.cs.berkeley.edu/tos/support.html#lists">here</A>. <p><hr><b><a href="index.html"> Tutorial Index</a></b></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -