-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.lua
276 lines (238 loc) · 8.32 KB
/
builtin.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
LOW = 0
HIGH = 65535
-- This is set for each execution and represents a point in time when the program was started, as seconds from some point in time.
START = 123.45
-- This is set before handling events.
NOW = 123.45
-- This is set before handling events.
-- Contains the time of day in seconds since midnight.
-- This example value is 14:36:12.
TIME_OF_DAY = 14*60*60 + 36*60 + 12
-- now returns the time in seconds since the program epoch.
function now()
return NOW
end
-- These are provided by the runtime during setup
function set_priority(p) end
function set_slow_mode(slow_mode) end
function add_input_alias(alias) end
function add_output_alias(alias) end
function add_output_group(group) end
function add_event_subscription(alias, event_type, target_function_name) end
-- And these maybe during event handling too? (not yet)
function enable_tick() end
function disable_tick() end
-- Provided by the runtime:
-- Perlin noise functions, returning values in [-1,1]
function noise2d(x, y) return 0.0 end
function noise3d(x, y, z) return 0.0 end
function noise3d(x, y, z, t) return 0.0 end
-- Provided by the runtime:
-- Functions to access global, shared values.
_globals = {}
function get_global(key)
return _globals[key]
end
-- The deltas to global values are picked up before processing events, from all
-- loaded programs.
-- They are then aggregated and redistributed to all loaded programs, before
-- processing events.
-- This is probably expensive. Don't set globals too often, I guess...
-- If multiple programs update the same global during the same tick it's
-- unspecified which value will be propagated for the next tick.
-- In particular, this can lead to inconsistent state between programs.
_global_deltas = {}
function set_global(key, value)
assert(type(key) == "string", "global keys must be strings")
value_type = type(value)
assert(value_type == "string"
or value_type == "number"
or value_type == "integer"
or value_type == "boolean"
or value_type == "nil",
"global values must be of type string, number, integer, boolean, or nil")
_globals[key] = value
_global_deltas[key] = value
end
-- This is called by the runtime before events are processed.
function _update_globals(new_values)
_global_deltas = {}
for k,v in pairs(new_values) do
_globals[k] = v
end
end
PROGRAM_ENABLE_SIGNAL = 1
PROGRAM_DISABLE_SIGNAL = 2
PROGRAM_ENABLE_TOGGLE_SIGNAL = 3
_program_enable_deltas = {}
function program_enable(program_name)
assert(type(program_name) == "string", "program names must be strings")
_program_enable_deltas[program_name] = PROGRAM_ENABLE_SIGNAL
end
function program_disable(program_name)
assert(type(program_name) == "string", "program names must be strings")
_program_enable_deltas[program_name] = PROGRAM_DISABLE_SIGNAL
end
function program_enable_toggle(program_name)
assert(type(program_name) == "string", "program names must be strings")
_program_enable_deltas[program_name] = PROGRAM_ENABLE_TOGGLE_SIGNAL
end
function _get_program_enable_deltas()
deltas = _program_enable_deltas
_program_enable_deltas = {}
return deltas
end
-- clamp clamps x to [from, to]
function clamp(from, to, x)
if x < from then
return from
elseif x > to then
return to
end
return x
end
-- lerp interpolates linearly between from and to.
function lerp(from, to, x)
return from + (to - from) * x
end
-- map_range maps x from one range to another.
function map_range(a_lower, a_upper, b_lower, b_upper, x)
return b_lower + (x - a_lower) * (b_upper - b_lower) / (a_upper - a_lower)
end
-- map_to_value maps x to the 16-bit Submarine value range.
function map_to_value(from, to, x)
return math.floor(map_range(from, to, LOW, HIGH, x))
end
function map_from_value(from, to, x)
return map_range(LOW, HIGH, from, to, x)
end
function map_value_to_temperature(x)
return map_from_value(-40,80,x)
end
function map_value_to_relative_humidity(x)
return map_from_value(0,100,x)
end
function absolute_humidity(temperature, humidity)
return (6.112*math.exp((17.67*temperature)/(temperature+243.5))*humidity*2.1674)/(273.15+temperature)
end
-- These are provided by the runtime during setup.
input_alias_address = {}
output_alias_address = {}
group_addresses = {}
function input_alias_to_address(alias)
local addr = input_alias_address[alias]
if addr == nil then
error("unknown input alias: " .. alias)
end
return addr
end
function output_alias_to_address(alias)
local addr = output_alias_address[alias]
if addr == nil then
error("unknown output alias: " .. alias)
end
return addr
end
function group_to_addresses(group)
local addr = group_addresses[alias]
if addr == nil then
error("unknown group: " .. alias)
end
return addr
end
-- This is read by the runtime after each tick.
_output_values_by_address = {}
function set_alias(alias, value)
_output_values_by_address[output_alias_to_address(alias)] = value
end
function set_group(group, value)
for i, address in ipairs(group_to_addresses(group)) do
_output_values_by_address[address] = value
end
end
-- This is called by the runtime.
-- By calling tick() from within Lua and returning the table in just
-- one function call we avoid one trip through the C FFI.
function _tick(now)
-- This clears the previous tick's map.
-- It costs performance, but otherwise we cannot distinguish whether a program wrote a value
-- during this tick or some previous tick.
_output_values_by_address = {}
tick(now)
return _output_values_by_address
end
-- This is provided by the runtime before handling events (= before each tick), and before the setup function is called.
-- The copy provided for setup() contains the whole address space.
-- Subsequent copies provided for tick() only contain inputs the program is registered for.
input_values_by_address = {}
function get_alias(alias)
local addr = input_alias_to_address(alias)
local value = input_values_by_address[addr]
if value == nil then
error("invalid address: " .. addr)
end
return value
end
-- This will be set up by the runtime.
_event_handlers = {}
-- Event type constants, keep synchronized with Rust and the readme!
EVENT_TYPE_UPDATE = "update"
EVENT_TYPE_BUTTON_DOWN = "button_down"
EVENT_TYPE_BUTTON_UP = "button_up"
EVENT_TYPE_BUTTON_CLICKED = "button_clicked"
EVENT_TYPE_BUTTON_LONG_PRESS = "button_long_press"
EVENT_TYPE_ERROR = "error"
-- This is called by the runtime to handle events.
-- Do not modify, please.
-- Events are passed into Lua as one string. (Extensive research has shown this to be most performant...)
-- Within that string, events are separated by a ";".
-- Within one event, fields are separated by a space.
function _handle_events(events)
for event in string.gmatch(events, "[^;]*") do
--print(event)
local i = 0
local address = -1
local typ = ""
for field in string.gmatch(event, "%S*") do
--print(field)
if i == 0 then
address = tonumber(field)
elseif i == 1 then
typ = field
-- Depending on the type we might be able to call the handler already...
if typ == EVENT_TYPE_BUTTON_DOWN or typ == EVENT_TYPE_BUTTON_UP then
_handle_no_arg_event(address, typ)
break
end
else
-- A third part! That means we can definitely call the handler now!
_handle_one_arg_event(address, typ, tonumber(field))
end
i = i + 1
end
end
end
function _handle_no_arg_event(address, typ)
local handlers_for_address = _event_handlers[address]
if handlers_for_address == nil then
return
end
for _, h in pairs(handlers_for_address) do
-- TODO match by more than just even type
if h["type"] == typ then
_G[h["handler"]](address, typ, -1)
end
end
end
function _handle_one_arg_event(address, typ, arg)
local handlers_for_address = _event_handlers[address]
if handlers_for_address == nil then
return
end
for _, h in pairs(handlers_for_address) do
-- TODO match by more than just even type
if h["type"] == typ then
_G[h["handler"]](address, typ, arg)
end
end
end