-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.cpp
More file actions
85 lines (76 loc) · 2.3 KB
/
gpio.cpp
File metadata and controls
85 lines (76 loc) · 2.3 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extern "C" {
#include "dependencies\includes\lualib.h"
#include "dependencies\includes\lauxlib.h"
#include "dependencies\includes\lua.h"
}
#include <iostream>
#include <winrt/windows.devices.gpio.h>
using namespace winrt::Windows::Devices::Gpio;
void Write_GPIO(unsigned int pinNumber, bool state) {
GpioController gpio = GpioController::GetDefault();
if (gpio == nullptr) {
std::cout << "Error: GPIO access.";
return;
}
GpioPin pin = gpio.OpenPin(pinNumber);
pin.SetDriveMode(GpioPinDriveMode::Output);
if (state) {
pin.Write(GpioPinValue::High);
}
else {
pin.Write(GpioPinValue::Low);
}
}
GpioPinValue Read_GPIO(unsigned int pinNumber) {
GpioController gpio = GpioController::GetDefault();
if (gpio == nullptr) {
std::cout << "Error: GPIO access.";
return GpioPinValue::Low;
}
GpioPin pin = gpio.OpenPin(pinNumber);
pin.SetDriveMode(GpioPinDriveMode::Input);
return pin.Read();
}
extern "C" static int PinOn(lua_State* L) {
unsigned int pinNumber = luaL_checkinteger(L, 1);
Write_GPIO(pinNumber, true);
return 0;
}
extern "C" static int PinOff(lua_State * L) {
unsigned int pinNumber = luaL_checkinteger(L, 1);
Write_GPIO(pinNumber, false);
return 0;
}
extern "C" static int PinToggle(lua_State * L) {
unsigned int pinNumber = luaL_checkinteger(L, 1);
if (Read_GPIO(pinNumber) == GpioPinValue::Low) {
Write_GPIO(pinNumber, true);
}
else {
Write_GPIO(pinNumber, false);
}
return 0;
}
extern "C" static int PinRead(lua_State * L) {
unsigned int pinNumber = luaL_checkinteger(L, 1);
if (Read_GPIO(pinNumber) == GpioPinValue::High) {
lua_pushboolean(L, 1);
}
else {
lua_pushboolean(L, 0);
}
return 1;
}
extern "C" static int info(lua_State * L) {
std::cout << "Lua GPIO win 0.1";
lua_pushstring(L, "Lua GPIO win 0.1");
return 0;
}
extern "C" __declspec(dllexport) int __cdecl luaopen_LuaGpioWin(lua_State* L) {
lua_register(L, "info", info);
lua_register(L, "PinOn", PinOn);
lua_register(L, "PinOff", PinOff);
lua_register(L, "PinToggle", PinToggle);
lua_register(L, "PinRead", PinRead);
return 0;
}