-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_lcd.c
More file actions
68 lines (50 loc) · 1.44 KB
/
Copy pathlua_lcd.c
File metadata and controls
68 lines (50 loc) · 1.44 KB
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
/*
* Copyright (c) 2024 Hakan Candar
* All rights reserved.
*
* This source code is licensed under the BSD 2-Clause License found in the
* LICENSE file in the root directory of this source tree.
*/
#include "lua_lcd.h"
int lua_lcd_clear(lua_State *L) {
printf("clear from within lua\n");
lcd_clear();
return 0;
}
int lua_lcd_set_cursor(lua_State *L) {
int row = luaL_checknumber(L, 1);
int col = luaL_checknumber(L, 2);
lcd_set_cursor(row, col);
return 0;
}
int lua_lcd_string(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
lcd_string(str);
return 0;
}
int lua_lcd_init(lua_State *L) {
printf("init from within lua\n");
lcd_init();
return 0;
}
// Create the module and register functions
int luaopen_lcd(lua_State *L) {
// Create a new table for the `lcd` module
lua_newtable(L);
lua_pushcfunction(L, lua_lcd_string);
lua_setfield(L, -2, "string");
lua_pushcfunction(L, lua_lcd_clear);
lua_setfield(L, -2, "clear");
lua_pushcfunction(L, lua_lcd_init);
lua_setfield(L, -2, "init");
lua_pushcfunction(L, lua_lcd_set_cursor);
lua_setfield(L, -2, "set_cursor");
// Return the table as the result of require("lcd")
return 1;
}
void register_lcd(lua_State *L) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
lua_pushcfunction(L, luaopen_lcd);
lua_setfield(L, -2, "lcd");
lua_pop(L, 1); // remove PRELOAD table
}