-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwiringPiNim.nim
210 lines (192 loc) · 5.48 KB
/
wiringPiNim.nim
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#[
Program: wiringPi wrapper to control a Raspberry Pi
Author: Thomas T. Jarløv (https://github.com/ThomasTJdev)
License: MIT
]#
## This library is a wrapper for the C library wiringPi
##
## More information on C library can be found here, on the
## official sites: (http://wiringpi.com / https://git.drogon.net/?p=wiringPi)
##
## The wrapper implements some of the wiringPi functions
## for quick access in Nim for controlling a Raspberry Pi.
##
## Activating a buzzer
## -------
##
## .. code-block::nim
## import wiringPiNim, os
##
## # Setting the pin for a buzzer
## const buzzer = 6
##
## if piSetup() >= 0:
## echo "RPi is setup and ready for use"
##
## # Setting the buzzers pin to output
## piPinModeOutput(buzzer)
##
## # Turning on the buzzer
## piDigitialWrite(buzzer, 1)
## echo piDigitalRead(buzzer)
## echo "Buzzer is activated"
##
## sleep(2000)
##
## # Turning off the buzzer
## piDigitalWrite(buzzer, 0)
## echo piDigitalRead(buzzer)
## echo "Buzzer is deactivated"
##
##
## LED flashing (async)
## -------
##
## .. code-block::nim
## import wiringPiNim, asyncdispatch
##
## # Setting the pin for a LED
## const led = 24
##
## proc flashLed() {.async.} =
## if piSetup() >= 0:
## # Setting the LEDs pin to output
## piPinModeOutput(led)
## echo "RPi is setup and ready for use"
##
## while true:
## piDigitalWrite(led, 1)
## await sleepAsync(800)
##
## piDigitalWrite(led, 0)
## await sleepAsync(800)
##
## waitFor flashLed()
##
##
## LED flashing (sync)
## -------
##
## .. code-block::nim
## import wiringPiNim, os
##
## # Setting the pin for a LED
## const led = 24
##
## proc flashLed() =
## if piSetup() >= 0:
## # Setting the LEDs pin to output
## piPinModeOutput(led)
## echo "RPi is setup and ready for use"
##
## while true:
## piDigitalWrite(led, 1)
## sleep(800)
##
## piDigitalWrite(led, 0)
## sleep(800)
##
## flashLed()
##
##
## Monitoring with a PIR sensor
## -------
##
## .. code-block::nim
## import wiringPiNim, os
##
## const pir = 1
##
## proc monitorPir() =
## if piSetup() >= 0:
## piPinModeInput(pir)
##
## while true:
## if piDigitalRead(pir) == 1:
## echo "Detected something!"
## sleep(200)
##
## monitorPir()
##
##
## Monitoring with a magnetic door contact
## -------
##
## .. code-block::nim
## import wiringPiNim, asyncdispatch
##
## const door = 29
##
## proc monitorDoor() {.async.} =
## if piSetup() >= 0:
## piPinModeOutput(door)
## piPullUp(door)
##
## while true:
## if piDigitalRead(door) == 1:
## echo "Door is open!"
## await sleepAsync(200)
##
## waitFor monitorDoor()
##
# {.passC: "-Wall".}
{.passL: "-lwiringPi".}
{.compile: "ressources/wiringPiWrap.c".}
proc piSetup*(): cint {.importc.}
## Setup/initialize the RPi.
##
## This maps the GPIO to the wiringPi pin numbering scheme
proc piSetupGPIO*(): cint {.importc.}
## Setup/initialize the RPi.
##
## This maps the GPIOs to Broadcom.
##
## Root is required.
proc piSetupPhys*(): cint {.importc.}
## Setup/initialize the RPi.
##
## This maps the GPIOs to Broadcom and use the physical pin numbers on
## the P1 connector only.
##
## Root is required.
proc piSetupSys*(): cint {.importc.}
## Setup/initialize the RPi.
##
## This initialises wiringPi but uses the /sys/class/gpio interface
## rather than accessing the hardware directly
proc piPinModeOutput*(pin: cint) {.importc.}
## Sets the pinmode to OUTPUT
proc piPinModeInput*(pin: cint) {.importc.}
## Sets the pinmode to INPUT
proc piPinModeGPIO*(pin: cint) {.importc.}
## Sets the pinmode to GPIO_CLOCK
##
## Note that only wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output
proc piPinModePWM*(pin: cint) {.importc.}
## Sets the pinmode to PWM_OUTPUT
##
## Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output
proc piDigitalPWM*(pin, value: cint) {.importc: "piPwmWrite".}
## Writes the value to the PWM register for the given pin. The Raspberry
## Pi has one on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and the
## range is 0-1024. Other PWM devices may have other PWM ranges.
proc piDigitalWrite*(pin, value: cint) {.importc.}
## Writes the value HIGH or LOW (1 or 0) to the given pin which must have
## been previously set as an output.
proc piDigitalRead*(pin: cint): cint {.importc.}
## This function returns the value read at the given pin. It will be HIGH
## or LOW (1 or 0) depending on the logic level at the pin.
proc piPullOff*(pin: cint) {.importc.}
## Sets the pull-up or pull-down resistor mode to no-pull up/down
proc piPullDown*(pin: cint) {.importc.}
## Sets the pull-up or pull-down resistor mode to pull to ground
proc piPullUp*(pin: cint) {.importc.}
## Sets the pull-up or pull-down resistor mode to pull to 3.3v
proc analogWrite*(pin, value: cint) {.importc.}
## This writes the given value to the supplied analog pin. You will need
## to register additional analog modules to enable this function for
## devices such as the Gertboard.
proc analogRead*(pin: cint): cint {.importc.}
## This returns the value read on the supplied analog input pin. You will
## need to register additional analog modules to enable this function for
## devices such as the Gertboard, quick2Wire analog board, etc.