musiclight2/lua_wrappers.c

91 lines
2.2 KiB
C
Raw Permalink Normal View History

/*
* vim: sw=2 ts=2 expandtab
*
2013-02-02 21:54:53 +01:00
* "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
* Thomas Kolb <cfr34k@tkolb.de> wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a pizza in return.
* - Thomas Kolb
*/
#include <semaphore.h>
2019-08-22 15:30:51 +02:00
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
#include "fft.h"
#include "utils.h"
#include "config.h"
#include "lua_utils.h"
#include "lua_wrappers.h"
extern sem_t fftSemaphore;
extern double fft[BLOCK_LEN];
extern sample signal[BLOCK_LEN];
extern double rms;
static int l_get_energy_in_band(lua_State *L) {
int lowerFreq, higherFreq;
luaL_checktype(L, 1, LUA_TNUMBER);
lowerFreq = lua_tointeger(L, 1);
luaL_checktype(L, 2, LUA_TNUMBER);
higherFreq = lua_tointeger(L, 2);
sem_wait(&fftSemaphore);
lua_pushnumber(L, get_energy_in_band(fft, lowerFreq, higherFreq));
sem_post(&fftSemaphore);
return 1; // number of arguments
}
static int l_get_fft(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushdoublearray(L, fft, BLOCK_LEN);
sem_post(&fftSemaphore);
return 1; // number of return values
}
static int l_get_signal(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushsamplearray(L, signal, BLOCK_LEN);
sem_post(&fftSemaphore);
return 1; // number of return values
}
static int l_get_rms(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushnumber(L, rms);
sem_post(&fftSemaphore);
return 1; // number of return values
}
// calculate the position in the output arrays from strip and module index
static int l_idx(lua_State *L) {
luaL_checktype(L, 1, LUA_TNUMBER);
int strip = lua_tointeger(L, 1) - 1; // -1 because Lua counts from 1
luaL_checktype(L, 2, LUA_TNUMBER);
int module = lua_tointeger(L, 2) - 1; // -1 because Lua counts from 1
lua_pushnumber(L, idx(strip, module) + 1); // +1 because Lua counts from 1
return 1; // number of return values
}
void lua_register_funcs(lua_State *L) {
lua_register(L, "get_energy_in_band", l_get_energy_in_band);
lua_register(L, "get_fft", l_get_fft);
lua_register(L, "get_signal", l_get_signal);
lua_register(L, "get_rms", l_get_rms);
lua_register(L, "idx", l_idx);
}