2016-06-26 14:19:06 +02:00
|
|
|
/*
|
|
|
|
* Module for interfacing with Switec instrument steppers (and
|
2019-02-17 19:26:29 +01:00
|
|
|
* similar devices). These are the steppers that are used in automotive
|
2016-06-26 14:19:06 +02:00
|
|
|
* instrument panels and the like. Run off 5 volts at low current.
|
|
|
|
*
|
|
|
|
* Code inspired by:
|
|
|
|
*
|
|
|
|
* SwitecX25 Arduino Library
|
|
|
|
* Guy Carpenter, Clearwater Software - 2012
|
|
|
|
*
|
|
|
|
* Licensed under the BSD2 license, see license.txt for details.
|
|
|
|
*
|
|
|
|
* NodeMcu integration by Philip Gladstone, N1DQ
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2016-06-26 14:19:06 +02:00
|
|
|
#include "task/task.h"
|
|
|
|
#include "driver/switec.h"
|
|
|
|
|
|
|
|
// This is the reference to the callbacks for when the pointer
|
|
|
|
// stops moving.
|
|
|
|
static int stopped_callback[SWITEC_CHANNEL_COUNT] = { LUA_NOREF, LUA_NOREF, LUA_NOREF };
|
|
|
|
static task_handle_t tasknumber;
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
static void callback_free(lua_State* L, unsigned int id)
|
2016-06-26 14:19:06 +02:00
|
|
|
{
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, stopped_callback[id]);
|
|
|
|
stopped_callback[id] = LUA_NOREF;
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
static void callback_set(lua_State* L, unsigned int id, int argNumber)
|
2016-06-26 14:19:06 +02:00
|
|
|
{
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, argNumber)) {
|
2016-06-26 14:19:06 +02:00
|
|
|
lua_pushvalue(L, argNumber); // copy argument (func) to the top of stack
|
|
|
|
callback_free(L, id);
|
|
|
|
stopped_callback[id] = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
static void callback_execute(lua_State* L, unsigned int id)
|
2016-06-26 14:19:06 +02:00
|
|
|
{
|
|
|
|
if (stopped_callback[id] != LUA_NOREF) {
|
|
|
|
int callback = stopped_callback[id];
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
|
|
|
|
callback_free(L, id);
|
|
|
|
|
2020-06-16 09:19:55 +02:00
|
|
|
luaL_pcallx(L, 0, 0);
|
2016-06-26 14:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int platform_switec_exists( unsigned int id )
|
|
|
|
{
|
|
|
|
return (id < SWITEC_CHANNEL_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: setup(id, P1, P2, P3, P4, maxSpeed)
|
|
|
|
static int lswitec_setup( lua_State* L )
|
|
|
|
{
|
|
|
|
unsigned int id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-06-26 14:19:06 +02:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( switec, id );
|
|
|
|
int pin[4];
|
|
|
|
|
|
|
|
if (switec_close(id)) {
|
|
|
|
return luaL_error( L, "Unable to setup stepper." );
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
uint32_t gpio = luaL_checkinteger(L, 2 + i);
|
|
|
|
|
|
|
|
luaL_argcheck(L, platform_gpio_exists(gpio), 2 + i, "Invalid pin");
|
|
|
|
|
|
|
|
pin[i] = pin_num[gpio];
|
|
|
|
|
|
|
|
platform_gpio_mode(gpio, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int deg_per_sec = 0;
|
|
|
|
if (lua_gettop(L) >= 6) {
|
|
|
|
deg_per_sec = luaL_checkinteger(L, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switec_setup(id, pin, deg_per_sec, tasknumber)) {
|
|
|
|
return luaL_error(L, "Unable to setup stepper.");
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2016-06-26 14:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: close( id )
|
|
|
|
static int lswitec_close( lua_State* L )
|
|
|
|
{
|
|
|
|
unsigned int id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-06-26 14:19:06 +02:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( switec, id );
|
|
|
|
callback_free(L, id);
|
|
|
|
if (switec_close( id )) {
|
|
|
|
return luaL_error( L, "Unable to close stepper." );
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2016-06-26 14:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: reset( id )
|
|
|
|
static int lswitec_reset( lua_State* L )
|
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( switec, id );
|
|
|
|
if (switec_reset( id )) {
|
|
|
|
return luaL_error( L, "Unable to reset stepper." );
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2016-06-26 14:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: moveto( id, pos [, cb] )
|
|
|
|
static int lswitec_moveto( lua_State* L )
|
|
|
|
{
|
|
|
|
unsigned int id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-06-26 14:19:06 +02:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( switec, id );
|
|
|
|
int pos;
|
|
|
|
pos = luaL_checkinteger( L, 2 );
|
|
|
|
|
|
|
|
if (lua_gettop(L) >= 3) {
|
|
|
|
callback_set(L, id, 3);
|
|
|
|
} else {
|
|
|
|
callback_free(L, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switec_moveto( id, pos )) {
|
|
|
|
return luaL_error( L, "Unable to move stepper." );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: getpos( id ) -> position, moving
|
|
|
|
static int lswitec_getpos( lua_State* L )
|
|
|
|
{
|
|
|
|
unsigned int id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-06-26 14:19:06 +02:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( switec, id );
|
|
|
|
int32_t pos;
|
|
|
|
int32_t dir;
|
|
|
|
int32_t target;
|
|
|
|
if (switec_getpos( id, &pos, &dir, &target )) {
|
|
|
|
return luaL_error( L, "Unable to get position." );
|
|
|
|
}
|
2020-08-29 18:48:24 +02:00
|
|
|
lua_pushinteger(L, pos);
|
|
|
|
lua_pushinteger(L, dir);
|
2016-06-26 14:19:06 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lswitec_dequeue(lua_State* L)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
|
|
|
|
for (id = 0; id < SWITEC_CHANNEL_COUNT; id++) {
|
|
|
|
if (stopped_callback[id] != LUA_NOREF) {
|
|
|
|
int32_t pos;
|
|
|
|
int32_t dir;
|
|
|
|
int32_t target;
|
|
|
|
if (!switec_getpos( id, &pos, &dir, &target )) {
|
|
|
|
if (dir == 0 && pos == target) {
|
|
|
|
callback_execute(L, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
static void lswitec_task(os_param_t param, uint8_t prio)
|
2016-06-26 14:19:06 +02:00
|
|
|
{
|
|
|
|
(void) param;
|
|
|
|
(void) prio;
|
|
|
|
|
|
|
|
lswitec_dequeue(lua_getstate());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int switec_open(lua_State *L)
|
|
|
|
{
|
|
|
|
(void) L;
|
|
|
|
|
|
|
|
tasknumber = task_get_id(lswitec_task);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(switec, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( setup, lswitec_setup )
|
|
|
|
LROT_FUNCENTRY( close, lswitec_close )
|
|
|
|
LROT_FUNCENTRY( reset, lswitec_reset )
|
|
|
|
LROT_FUNCENTRY( moveto, lswitec_moveto )
|
|
|
|
LROT_FUNCENTRY( getpos, lswitec_getpos )
|
2016-06-26 14:19:06 +02:00
|
|
|
#ifdef SQITEC_DEBUG
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( dequeue, lswitec_dequeue )
|
2016-06-26 14:19:06 +02:00
|
|
|
#endif
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(switec, NULL, 0)
|
2016-06-26 14:19:06 +02:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(SWITEC, "switec", switec, switec_open);
|