aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2020-03-08 12:13:38 +0100
committercbdev <cb@cbcdn.com>2020-03-08 12:13:38 +0100
commitf9829ae90d4017940047b561e412c6eb7f431adb (patch)
treec94f4bcb67bd4680343303cfef998999acb314ea /backends
parentdde2cca435b2c314a08024825b1daffa6437b947 (diff)
downloadmidimonster-f9829ae90d4017940047b561e412c6eb7f431adb.tar.gz
midimonster-f9829ae90d4017940047b561e412c6eb7f431adb.tar.bz2
midimonster-f9829ae90d4017940047b561e412c6eb7f431adb.zip
Implement timestamp() callback for Lua
Diffstat (limited to 'backends')
-rw-r--r--backends/lua.c16
-rw-r--r--backends/lua.md5
2 files changed, 14 insertions, 7 deletions
diff --git a/backends/lua.c b/backends/lua.c
index 955341a..9a8091e 100644
--- a/backends/lua.c
+++ b/backends/lua.c
@@ -75,7 +75,7 @@ static int lua_update_timerfd(){
size_t n = 0;
#ifdef MMBACKEND_LUA_TIMERFD
struct itimerspec timer_config = {
- 0
+ {0}
};
#endif
@@ -260,7 +260,7 @@ static int lua_callback_value(lua_State* interpreter, uint8_t input){
//find correct channel & return value
for(n = 0; n < data->channels; n++){
if(!strcmp(channel_name, data->channel_name[n])){
- lua_pushnumber(data->interpreter, (input) ? data->input[n] : data->output[n]);
+ lua_pushnumber(interpreter, (input) ? data->input[n] : data->output[n]);
return 1;
}
}
@@ -283,6 +283,11 @@ static int lua_callback_input_channel(lua_State* interpreter){
return 1;
}
+static int lua_callback_timestamp(lua_State* interpreter){
+ lua_pushnumber(interpreter, mm_timestamp());
+ return 1;
+}
+
static int lua_configure(char* option, char* value){
LOG("No backend configuration possible");
return 1;
@@ -326,6 +331,7 @@ static int lua_instance(instance* inst){
lua_register(data->interpreter, "input_value", lua_callback_input_value);
lua_register(data->interpreter, "output_value", lua_callback_output_value);
lua_register(data->interpreter, "input_channel", lua_callback_input_channel);
+ lua_register(data->interpreter, "timestamp", lua_callback_timestamp);
//store instance pointer to the lua state
lua_pushstring(data->interpreter, LUA_REGISTRY_KEY);
@@ -417,9 +423,6 @@ static int lua_handle(size_t num, managed_fd* fds){
return 1;
}
#else
- if(!last_timestamp){
- last_timestamp = mm_timestamp();
- }
delta = mm_timestamp() - last_timestamp;
last_timestamp = mm_timestamp();
#endif
@@ -458,6 +461,7 @@ static int lua_start(size_t n, instance** inst){
&& strcmp(data->channel_name[p], "input_value")
&& strcmp(data->channel_name[p], "output_value")
&& strcmp(data->channel_name[p], "input_channel")
+ && strcmp(data->channel_name[p], "timestamp")
&& strcmp(data->channel_name[p], "interval")){
lua_getglobal(data->interpreter, data->channel_name[p]);
data->reference[p] = luaL_ref(data->interpreter, LUA_REGISTRYINDEX);
@@ -474,6 +478,8 @@ static int lua_start(size_t n, instance** inst){
if(mm_manage_fd(timer_fd, BACKEND_NAME, 1, NULL)){
return 1;
}
+ #else
+ last_timestamp = mm_timestamp();
#endif
return 0;
}
diff --git a/backends/lua.md b/backends/lua.md
index 0c592b7..db4cf39 100644
--- a/backends/lua.md
+++ b/backends/lua.md
@@ -17,6 +17,7 @@ The following functions are provided within the Lua interpreter for interaction
| `input_value(string)` | `input_value("foo")` | Get the last input value on a channel |
| `output_value(string)` | `output_value("bar")` | Get the last output value on a channel |
| `input_channel()` | `print(input_channel())` | Returns the name of the input channel whose handler function is currently running or `nil` if in an `interval`'ed function (or the initial parse step) |
+| `timestamp()` | `print(timestamp())` | Returns the core timestamp for this iteration with millisecond resolution. This is not a performance timer, but intended for timeouting, etc |
Example script:
```
@@ -58,8 +59,8 @@ lua1.foo > lua2.bar
#### Known bugs / problems
-Using any of the interface functions (`output`, `interval`, `input_value`, `output_value`, `input_channel`)
-as an input channel name to a Lua instance will not call any handler functions.
+Using any of the interface functions (`output`, `interval`, `input_value`, `output_value`, `input_channel`,
+`timestamp`) as an input channel name to a Lua instance will not call any handler functions.
Using these names as arguments to the output and value interface functions works as intended.
Output values will not trigger corresponding input event handlers unless the channel is mapped