beaglebone 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/examples/ain.rb DELETED
@@ -1,92 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
- require 'pp'
4
-
5
- include Beaglebone
6
-
7
- #analog testing
8
- #oo methods
9
-
10
- #analog read
11
- p9_33 = AINPin.new(:P9_33)
12
- loop do
13
- puts p9_33.read
14
- sleep 0.01
15
- end
16
-
17
- exit
18
-
19
- #waiting for analog input change
20
- p9_33 = AINPin.new(:P9_33)
21
- pp p9_33.wait_for_change(10, 0.01)
22
- pp p9_33.wait_for_change(10, 0.01)
23
- pp p9_33.wait_for_change(10, 0.01)
24
-
25
-
26
- #background analog input with callback
27
-
28
- #wait for threshold
29
- callback = lambda { |pin, mv_last, mv, state_last, state, count|
30
- puts "[#{count}] #{pin} #{state_last} -> #{state} #{mv_last} -> #{mv}"
31
- }
32
- p9_33 = AINPin.new(:P9_33)
33
- p9_33.run_on_threshold(callback, 400, 1200, 5, 0.001)
34
-
35
- sleep 5
36
- p9_33.stop_wait
37
-
38
- pp p9_33.wait_for_threshold(200, 1600, 100, 0.01)
39
- pp p9_33.wait_for_threshold(200, 1600, 100, 0.01)
40
- pp p9_33.wait_for_threshold(200, 1600, 100, 0.01)
41
-
42
- #wait for change
43
- callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
44
-
45
- p9_33 = AINPin.new(:P9_33)
46
-
47
- p9_33.run_on_change(callback, 10, 0.1)
48
- sleep 5
49
- p9_33.stop_wait
50
-
51
- exit
52
-
53
-
54
-
55
- #procedural methods
56
-
57
- #analog read
58
- loop do
59
- puts AIN.read(:P9_33)
60
- sleep 0.01
61
- end
62
- exit
63
-
64
- #wait for analog input to hit a certrain threshold limit
65
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
66
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
67
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
68
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
69
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
70
- exit
71
-
72
- #waiting for analog input change
73
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
74
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
75
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
76
-
77
- exit
78
-
79
- #background analog input
80
- callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
81
- AIN.run_on_change(callback, :P9_33, 10, 0.1)
82
- sleep 5
83
- AIN.stop_wait(:P9_33)
84
-
85
- #run when reaching a certain threshold
86
- callback = lambda { |pin, mv_last, mv, state_last, state, count|
87
- puts "[#{count}] #{pin} #{state_last} -> #{state} #{mv_last} -> #{mv}"
88
- }
89
- AIN.run_on_threshold(callback, :P9_33, 400, 1200, 5, 0.001)
90
- loop do sleep(4000);end
91
-
92
- exit
data/examples/gpio.rb DELETED
@@ -1,196 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
- include Beaglebone
5
-
6
- ###object oriented methods###
7
- #flash leds
8
- led1 = GPIOPin.new(:USR0, :OUT)
9
- led2 = GPIOPin.new(:USR1, :OUT)
10
- led3 = GPIOPin.new(:USR2, :OUT)
11
- led4 = GPIOPin.new(:USR3, :OUT)
12
-
13
- 5.times do
14
- led1.digital_write(:HIGH)
15
- sleep 0.25
16
- led1.digital_write(:LOW)
17
-
18
- led2.digital_write(:HIGH)
19
- sleep 0.25
20
- led2.digital_write(:LOW)
21
-
22
- led3.digital_write(:HIGH)
23
- sleep 0.25
24
- led3.digital_write(:LOW)
25
-
26
- led4.digital_write(:HIGH)
27
- sleep 0.25
28
- led4.digital_write(:LOW)
29
- end
30
-
31
- exit
32
-
33
- #basic GPIO reads and writes
34
- p9_11 = GPIOPin.new(:P9_11, :IN)
35
- p9_12 = GPIOPin.new(:P9_12, :OUT)
36
-
37
- puts p9_11.digital_read
38
- p9_12.digital_write(:HIGH)
39
- puts p9_11.digital_read
40
- p9_12.digital_write(:LOW)
41
- puts p9_11.digital_read
42
-
43
-
44
- exit
45
-
46
- ##gpio edge trigger with callback
47
- p9_11 = GPIOPin.new(:P9_11, :IN)
48
- p9_12 = GPIOPin.new(:P9_12, :OUT)
49
-
50
- #toggle output pin randomly in a separate there
51
- Thread.new do
52
- loop do
53
- sleep rand(100) / 100.0
54
- p9_12.digital_write(:LOW)
55
- sleep rand(100) / 100.0
56
- p9_12.digital_write(:HIGH)
57
- end
58
- end
59
-
60
- #run on edge trigger with callback
61
- callback = lambda { |pin,edge,count| p9_11.stop_edge_wait if count == 10;puts "[#{count}] #{pin} #{edge}"}
62
- p9_11.run_on_edge(callback, :BOTH)
63
-
64
-
65
- sleep 10000
66
- exit
67
-
68
- #or without callback
69
- loop do
70
- edge = p9_11.wait_for_edge(:RISING)
71
- puts "!! TRIGGERED ON #{edge}"
72
-
73
- edge = p9_11.wait_for_edge(:RISING)
74
- puts "!! TRIGGERED ON #{edge}"
75
-
76
- edge = p9_11.wait_for_edge(:BOTH)
77
- puts "!! TRIGGERED ON #{edge}"
78
- end
79
-
80
- #gpio edge trigger
81
- p9_11 = GPIOPin.new(:P9_11, :IN)
82
- p9_12 = GPIOPin.new(:P9_12, :OUT)
83
-
84
- Thread.new do
85
- loop do
86
- sleep rand(100) / 100.0
87
- p9_12.digital_write(:LOW)
88
- sleep rand(100) / 100.0
89
- p9_12.digital_write(:HIGH)
90
- end
91
- end
92
-
93
-
94
- loop do
95
- edge = p9_11.wait_for_edge(:RISING)
96
- puts "!! TRIGGERED ON #{edge}"
97
-
98
- edge = p9_11.wait_for_edge(:RISING)
99
- puts "!! TRIGGERED ON #{edge}"
100
-
101
- edge = p9_11.wait_for_edge(:BOTH)
102
- puts "!! TRIGGERED ON #{edge}"
103
- end
104
-
105
-
106
-
107
- #shift register via gpio, specified latch, clock, and data pins
108
- shiftreg = ShiftRegister.new(:P9_11, :P9_12, :P9_13)
109
- data = 255
110
- shiftreg.shiftout(data)
111
-
112
- exit
113
-
114
-
115
-
116
- ###procedural methods###
117
-
118
- #gpio edge trigger callback
119
- GPIO.pin_mode(:P9_12, :OUT)
120
- GPIO.pin_mode(:P9_11, :IN)
121
- GPIO.run_on_edge(lambda { |pin,edge,count| puts "[#{count}] #{pin} -- #{edge}" }, :P9_11, :BOTH)
122
-
123
- leds = [ :USR0, :USR1, :USR2, :USR3 ]
124
- leds.each do |ledpin|
125
- GPIO.pin_mode(ledpin, :OUT)
126
- end
127
-
128
- x = 0
129
- loop do
130
- #gpio write to led pins
131
- leds.each do |ledpin|
132
- GPIO.digital_write(ledpin, :LOW)
133
- sleep 0.25
134
- GPIO.digital_write(ledpin, :HIGH)
135
- end
136
- x += 1
137
-
138
- if x == 10
139
- GPIO.stop_edge_wait(:P9_11)
140
- puts 'STOP'
141
- end
142
- if x == 15
143
- GPIO.run_on_edge(lambda { |pin,edge,count| puts "[#{count}] #{pin} -- #{edge}" }, :P9_11, :BOTH)
144
- puts 'OK GO AGAIN'
145
- x = 0
146
- end
147
-
148
- end
149
-
150
-
151
- exit
152
-
153
- #gpio edge trigger
154
- GPIO.pin_mode(:P9_12, :OUT)
155
- GPIO.pin_mode(:P9_11, :IN)
156
-
157
- loop do
158
- edge = GPIO.wait_for_edge(:P9_11, :RISING)
159
- puts "!! TRIGGERED ON #{edge}"
160
- end
161
-
162
-
163
- #standard gpio setup and testing
164
- GPIO.pin_mode(:P9_12, :OUT)
165
- puts GPIO.enabled?(:P9_12)
166
- puts GPIO.get_gpio_mode(:P9_12)
167
-
168
- #gpio setup for led pins
169
- leds = [ :USR0, :USR1, :USR2, :USR3 ]
170
- leds.each do |ledpin|
171
- GPIO.pin_mode(ledpin, :OUT)
172
- end
173
-
174
- #gpio write to led pins
175
- leds.each do |ledpin|
176
- GPIO.digital_write(ledpin, :LOW)
177
- end
178
-
179
-
180
- #gpio write
181
- loop do
182
- GPIO.digital_write(:P9_12, :HIGH)
183
- sleep 0.25
184
- GPIO.digital_write(:P9_12, :LOW)
185
- sleep 0.25
186
- end
187
-
188
-
189
- GPIO.cleanup
190
- exit
191
-
192
- # gpio read
193
- GPIO.pin_mode(:P9_12, :IN)
194
- loop do
195
- puts GPIO.digital_read(:P9_12)
196
- end
data/examples/i2c.rb DELETED
@@ -1,110 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
-
5
-
6
- #i2c testing hooked up to LSM303DLHC
7
-
8
- #oo method
9
- i2c = I2CDevice.new(:I2C2)
10
-
11
- #put mag into continuous conversation mode
12
- i2c.write(0x1e, [0x02, 0x00].pack("C*"))
13
-
14
- #enable temperatuer sensor, 15hz register update
15
- i2c.write(0x1e, [0x00, 0b10010000].pack("C*") )
16
-
17
- #delay for the settings to take effect
18
- sleep(0.1)
19
-
20
- loop do
21
-
22
- #read axis data
23
- raw = i2c.read(0x1e, 6, [0x03].pack("C*"))
24
-
25
- #coordinates are signed shorts in x,z,y order
26
- x,z,y = raw.unpack("s>*")
27
-
28
- #calculate angle
29
- degrees = (Math::atan2(y, x) * 180) / Math::PI
30
- degrees += 360 if degrees < 0
31
-
32
- #read 2 bytes from temperature register
33
- raw = i2c.read(0x1e, 2, [0x31].pack("C*"))
34
- #temperature is sent big endian, lsd last
35
- temp = raw.unpack("S>").first
36
- #temp is 12 bits, last 4 are unused
37
- temp = temp >> 4
38
-
39
- #twos complement
40
- temp -= 65535 if temp > 32767
41
-
42
- #each bit is 8c
43
- temp /= 8
44
-
45
- #correction factor
46
- temp += 19
47
-
48
- #convert to f
49
- temp = (temp * 1.8 + 32).to_i
50
-
51
-
52
- puts "#{Time.now.strftime("%H:%M")} temp: #{temp} degrees f direction: #{degrees.to_i} degrees"
53
- sleep 1
54
- end
55
-
56
- exit
57
-
58
-
59
- #procedural method
60
-
61
-
62
- #i2c testing
63
- I2C.setup(:I2C2)
64
-
65
-
66
- #put mag into continuous conversation mode
67
- I2C.write(:I2C2, 0x1e, [0x02, 0x00].pack("C*"))
68
- #enable temperatuer sensor, 15hz register update
69
- I2C.write(:I2C2, 0x1e, [0x00, 0b10010000].pack("C*") )
70
- #delay for the settings to take effect
71
- sleep(0.1)
72
-
73
-
74
- loop do
75
-
76
- #read axis data
77
- raw = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
78
-
79
- #coordinates are signed shorts in x,z,y order
80
- x,z,y = raw.unpack("s>*")
81
-
82
- #calculate angle
83
- degrees = (Math::atan2(y, x) * 180) / Math::PI
84
- degrees += 360 if degrees < 0
85
-
86
- #read 2 bytes from temperature register
87
- raw = I2C.read(:I2C2, 0x1e, 2, [0x31].pack("C*"))
88
- #temperature is sent big endian, lsd last
89
- temp = raw.unpack("S>").first
90
- #temp is 12 bits, last 4 are unused
91
- temp = temp >> 4
92
-
93
- #twos complement
94
- temp -= 65535 if temp > 32767
95
-
96
- #each bit is 8c
97
- temp /= 8
98
-
99
- #correction factor
100
- temp += 19
101
-
102
- #convert to f
103
- temp = (temp * 1.8 + 32).to_i
104
-
105
-
106
- puts "#{Time.now.strftime("%H:%M")} temp: #{temp} degrees f direction: #{degrees.to_i} degrees"
107
- sleep 60
108
- end
109
-
110
- exit
data/examples/pwm.rb DELETED
@@ -1,63 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
- include Beaglebone
5
-
6
- #pwm example
7
-
8
- #oo method
9
- p9_14 = PWMPin.new(:P9_14, 90, 10, :NORMAL)
10
- sleep 1
11
- p9_14.set_frequency(20)
12
- sleep(1)
13
- p9_14.set_duty_cycle(95)
14
- sleep(1)
15
- p9_14.set_duty_cycle(50)
16
- sleep(1)
17
- p9_14.set_frequency(2)
18
- sleep(1)
19
- p9_14.stop
20
- sleep(1)
21
- p9_14.run
22
- sleep(1)
23
- p9_14.set_frequency(32)
24
- sleep(1)
25
- p9_14.set_duty_cycle(94)
26
- sleep(1)
27
- p9_14.set_period_ns(31250000)
28
- p9_14.set_duty_cycle_ns(31250000)
29
- p9_14.set_period_ns(31249999)
30
- p9_14.set_duty_cycle(10)
31
- p9_14.set_polarity(:INVERTED)
32
-
33
- p9_14.disable_pwm_pin
34
- exit
35
-
36
-
37
- #procedural method
38
- PWM.start(:P9_14, 90, 10, :NORMAL)
39
- sleep(1)
40
- PWM.set_frequency(:P9_14, 20)
41
- sleep(1)
42
- PWM.set_duty_cycle(:P9_14, 95)
43
- sleep(1)
44
- PWM.set_duty_cycle(:P9_14, 50)
45
- sleep(1)
46
- PWM.set_frequency(:P9_14, 2)
47
- sleep(1)
48
- PWM.stop(:P9_14)
49
- sleep(1)
50
- PWM.run(:P9_14)
51
- sleep(1)
52
- PWM.set_frequency(:P9_14, 32)
53
- sleep(1)
54
- PWM.set_duty_cycle(:P9_14, 94)
55
- sleep(1)
56
- PWM.set_period_ns(:P9_14, 31250000)
57
- PWM.set_duty_cycle_ns(:P9_14, 31250000)
58
- PWM.set_period_ns(:P9_14, 31249999)
59
- PWM.set_duty_cycle(:P9_14, 10)
60
- PWM.set_polarity(:P9_14, :INVERTED)
61
- sleep(1)
62
- PWM.cleanup
63
- exit
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
- include Beaglebone
5
-
6
- #shift reg via gpio
7
-
8
- shiftreg = ShiftRegister.new(:P9_11, :P9_12, :P9_13)
9
- data = 255
10
- shiftreg.shift_out(data)
11
-
12
- exit
data/examples/spi.rb DELETED
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
- include Beaglebone
5
-
6
- #SPI test hooked up to MCP3008
7
-
8
- #oo methods
9
- spi = SPIDevice.new(:SPI0)
10
-
11
- loop do
12
- # communicate with MCP3008
13
- # byte 1: start bit
14
- # byte 2: single(1)/diff(0),3 bites for channel, null pad
15
- # byte 3: don't care
16
- raw = spi.xfer([ 0b00000001, 0b10000000, 0].pack("C*"))
17
- data = raw.unpack("C*")
18
-
19
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
20
- puts val
21
-
22
-
23
- raw = spi.xfer([ 0b00000001, 0b10010000, 0].pack("C*"))
24
- data = raw.unpack("C*")
25
-
26
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
27
- puts val
28
-
29
-
30
- sleep 0.25
31
- end
32
-
33
- exit
34
-
35
-
36
- #procedural methods
37
-
38
- SPI.setup(:SPI0)
39
-
40
- loop do
41
- # communicate with MCP3008
42
- # byte 1: start bit
43
- # byte 2: single(1)/diff(0),3 bites for channel, null pad
44
- # byte 3: don't care
45
- raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10000000, 0].pack("C*"))
46
- data = raw.unpack("C*")
47
-
48
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
49
- puts val
50
-
51
-
52
- raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10010000, 0].pack("C*"))
53
- data = raw.unpack("C*")
54
-
55
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
56
- puts val
57
-
58
-
59
- sleep 0.25
60
- end
61
-
62
- exit
data/examples/uart.rb DELETED
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'beaglebone'
3
-
4
- #uart example
5
- #oo method
6
- uart1 = UARTDevice.new(:uart1, 9600)
7
-
8
- uart1.writeln("TEST")
9
-
10
- puts uart1.readchars(10)
11
- puts uart1.readchar
12
- puts uart1.readline
13
-
14
- callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
15
- #uart1.run_on_each_chars(callback, 3, 3)
16
- #uart1.run_on_each_chars(callback, 3)
17
- #uart1.run_on_each_char(callback, 3)
18
- #uart1.run_once_on_each_chars(callback, 3)
19
- #uart1.run_once_on_each_char(callback)
20
- #uart1.run_on_each_chars(callback, 2)
21
- uart1.run_on_each_line(callback)
22
-
23
- sleep(5)
24
- uart1.stop_read_wait
25
-
26
-
27
- uart1.each_chars(2) { |c| puts c }
28
- #uart1.each_line { |line| puts line }
29
-
30
- uart1.disable
31
-
32
- exit
33
-
34
-
35
-
36
-
37
- #procedural method
38
- UART.setup(:UART1, 9600)
39
-
40
- UART.write(:UART1, "test1")
41
-
42
- #puts UART.readchars(:UART1, 10)
43
- #puts UART.readchar(:UART1)
44
- #puts UART.readline(:UART1)
45
-
46
- callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
47
- #UART.run_on_each_chars(callback, :UART1, 3, 3)
48
- #UART.run_on_each_chars(callback, :UART1, 3)
49
- #UART.run_on_each_char(callback, :UART1, 3)
50
- #UART.run_once_on_each_chars(callback, :UART1, 3)
51
- #UART.run_once_on_each_char(callback, :UART1)
52
- #UART.run_on_each_chars(callback, :UART1, 2)
53
- UART.run_on_each_line(callback, :UART1)
54
-
55
- sleep(5)
56
- UART.stop_read_wait(:UART1)
57
-
58
-
59
- UART.each_chars(:UART1, 2) { |c| puts c }
60
- #UART.each_line(:UART1) { |line| puts line }
61
-
62
- UART.cleanup
63
-
64
- exit