-
Notifications
You must be signed in to change notification settings - Fork 0
/
button_control_led.rb
executable file
·57 lines (50 loc) · 1.49 KB
/
button_control_led.rb
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
#!/usr/bin/env ruby
require "rpi_gpio"
# Set #17 as LED pin
LED_PIN_NUMBER = 17
# Set #18 as button pin
BUTTON_PIN_NUMBER = 18
puts "========================================"
puts "| Button control LED |"
puts "| ------------------------------ |"
puts "| LED connect to GPIO0 |"
puts "| Button connect to GPIO1 |"
puts "| |"
puts "| Press button to turn on/off LED. |"
puts "| |"
puts "| codenamev |"
puts "========================================\n"
puts "Program is running..."
puts "Please press Ctrl+C to end the program..."
puts "Press Enter to begin"
raw_input = STDIN.gets.chomp
def inline_puts(output)
STDOUT.write "\r#{output}"
STDOUT.flush
end
# Set the GPIO modes to BCM Numbering
RPi::GPIO.set_numbering :bcm
# Set LED_PIN_NUMBER's mode to output,
# and initial level to high (3.3v)
RPi::GPIO.setup LED_PIN_NUMBER, as: :output, initialize: :high
# Set BUTTON_PIN_NUMBER's mode to input,
# and pull up to high (3.3V)
RPi::GPIO.setup BUTTON_PIN_NUMBER, as: :input, pull: :up
# Clean up everything after the script finishes
at_exit do
inline_puts "Cleaning up..."
# Turn off LED
RPi::GPIO.set_high LED_PIN_NUMBER
# Release resource
RPi::GPIO.clean_up
exit
end
loop do
if RPi::GPIO.high? BUTTON_PIN_NUMBER
RPi::GPIO.set_high LED_PIN_NUMBER
else
RPi::GPIO.set_low LED_PIN_NUMBER
end
rescue Interrupt, SignalException
exit
end