vibrate.trigger({pattern}, loop_index)
Opened this issue · 16 comments
{pattern} in the style of
{pause, time, pause, time, ...}
{0, 100, 200, 100} would mean no initial delay and then a 200 delay
loop_index of -1 means don't loop
of 0 means loop forever (until canceled)
These would work on Android but don't know about iOS
@adamwestman Could you do this or should I try?
If you wanna give it a go please feel free to, I will not have time to implement it in the short term
Alright, I'll give it a shot for Android first. I don't have enough iOS devices handy to effectively test the different methods of doing custom vibrations.
Sounds Good, Android is the straightforward one when it comes to the described pattern. Just be mindful of the backward compatibility, but it looks like the suggested interface would translate well
Also, Apple may reject applications that vibrate too much. So we should add that warning to the Readme along with an implementation like infinite repeat
@mathiaswking is there an existing example of converting a Lua table with a list of numbers to a C array for use in a native extension? I want to use it in this project for example vibrate.trigger({0,100,200,100})
I tried to find any existing native extensions using Lua tables but could not find one with a table as a parameter.
Think I got it... but I'm still a noob.
luaL_checktype(L, 1, LUA_TTABLE);
int size;
size = lua_objlen(L, 1);
long pattern[ size ];
int i;
for (i = 0; i < size; i++ )
{
lua_rawgeti(L, 1, 1);
lua_pop(L, 1);
pattern[i] = lua_tointeger(L, -1);
}
Now I'm stuck again. I think I need to convert the C array to a Java array with JNI? Or create the Java array with JNI in the first place, but examples online are confusing.
I think this is close but still erroring
luaL_checktype(L, 1, LUA_TTABLE);
int size;
size = lua_objlen(L, 1);
jlongArray pattern = env->NewLongArray(size);
long temp_pattern[size];
int i;
for (i = 0; i < size; i++ )
{
lua_rawgeti(L, 1, 1);
lua_pop(L, 1);
temp_pattern[i] = (long)lua_tointeger(L, -1);
}
env->SetLongArrayRegion(env,pattern,0,size,temp_pattern);
jclass cls = GetClass(env, "com.defold.android.vibrate.Vibrate");
jmethodID vibrate_method = env->GetStaticMethodID(cls, "vibratePhonePattern", "(Landroid/content/Context;[J)V");
env->CallStaticObjectMethod(cls, vibrate_method, dmGraphics::GetNativeAndroidActivity(), pattern);
return 0;
Ah
env->SetLongArrayRegion(env,pattern,0,size,temp_pattern);
Should be
env->SetLongArrayRegion(pattern,0,size,temp_pattern);
It compiles without error now at least...
Got it all working now I think.
Here is progress, I will merge into this extension soon https://github.com/subsoap/vibrate
Looks great, especially the JNI attachment and class retrieval cleanup.