Skip to content

Commit 0d3ba5f

Browse files
committed
Update the demo to fetch JavaScript from the server and run it.
1 parent 70f1411 commit 0d3ba5f

File tree

3 files changed

+542
-175
lines changed

3 files changed

+542
-175
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// FFI Imports
2+
// Each function imported from the host environment needs to be assigned to a
3+
// global like this and identified by a constant that the resolver in the C/C++
4+
// code will understand.
5+
// These constants are defined in the `Exports` enumeration.
6+
7+
8+
var FFINumber = 1;
9+
10+
/**
11+
* Log function, writes all arguments to the UART.
12+
*/
13+
export const print = vmImport(FFINumber++);
14+
15+
/**
16+
* led_on(index).
17+
*
18+
* Turns on the LED at the specified index.
19+
*/
20+
export const led_on = vmImport(FFINumber++);
21+
22+
/**
23+
* led_off(index).
24+
*
25+
* Turns off the LED at the specified index.
26+
*/
27+
export const led_off = vmImport(FFINumber++);
28+
29+
/**
30+
* buttons_read().
31+
*
32+
* Reads the value of all of the buttons, returning a 4-bit value indicating
33+
* the states of all of them.
34+
*/
35+
export const buttons_read = vmImport(FFINumber++);
36+
37+
/**
38+
* switches_read().
39+
*
40+
* Reads the value of all of the switches, returning a 4-bit value indicating
41+
* the states of all of them.
42+
*/
43+
export const switches_read = vmImport(FFINumber++);
44+
45+
46+
export const mqtt_publish = vmImport(FFINumber++);
47+
export const mqtt_subscribe = vmImport(FFINumber++);
48+
49+
/**
50+
* led_set(index, state).
51+
*
52+
* Turns the LED at the specified index on or off depending on whether state is
53+
* true or false.
54+
*/
55+
export function led_set(index, state)
56+
{
57+
if (state)
58+
{
59+
led_on(index);
60+
}
61+
else
62+
{
63+
led_off(index);
64+
}
65+
}
66+
67+
/**
68+
* button_read(index).
69+
*
70+
* Reads the value of the button at the specified index.
71+
*/
72+
export function button_read(index)
73+
{
74+
return (buttons_read() & (1 << index)) !== 0;
75+
}
76+
77+
78+
/**
79+
* switch_read(index).
80+
*
81+
* Reads the value of the switch at the specified index.
82+
*/
83+
export function switch_read(index)
84+
{
85+
return (switches_read() & (1 << index)) !== 0;
86+
}
87+

0 commit comments

Comments
 (0)