beaglebone 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +658 -4
- data/beaglebone.gemspec +1 -1
- data/examples/shiftregister.rb +1 -1
- data/examples/uart.rb +16 -16
- data/lib/beaglebone/gpio.rb +2 -0
- data/lib/beaglebone/shiftregister.rb +2 -2
- data/lib/beaglebone/uart.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b19c474402f31c4baf1e2965ba0f49327cb40ba8
|
4
|
+
data.tar.gz: 2b05ac032cefc71bfc53561c82b1bdf62ed02ce6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f5ab52cdace0e05a025c9b43fdaeb9133e52ef0ca941f374dbed18fc695a7c97c0d65e48d548125e88a561b8e5be361c6d8fa7cbc75ffd2f852b9d233220cfc
|
7
|
+
data.tar.gz: 67586adfc78e6d4ba5c719a8d53e657bed0608933c643349c407b522ae908fd51467d18eff86aef564e910e4d2f0ca00da442e2e5ed161d7fd769aad66f75c2d
|
data/README.md
CHANGED
@@ -1,6 +1,660 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Beaglebone Ruby Library
|
2
|
+
Documentation is in progress and will be completed shortly.
|
3
3
|
|
4
|
-
|
4
|
+
**Table of Contents**
|
5
|
+
- [Overview](#overview)
|
6
|
+
- [Installation](#installation)
|
7
|
+
- [Installing Ruby](#installing-ruby)
|
8
|
+
- [Installing Beaglebone Gem](#installing-beaglebone-gem)
|
9
|
+
- [Usage](#usage)
|
10
|
+
- [Reference](#reference)
|
11
|
+
- [Examples (Object Oriented)](#examples-object-oriented)
|
12
|
+
- [GPIO](#gpio)
|
13
|
+
- [GPIO Writing](#gpio-writing)
|
14
|
+
- [GPIO Reading](#gpio-reading)
|
15
|
+
- [LEDs](#leds)
|
16
|
+
- [Edge Triggers](#edge-triggers)
|
17
|
+
- [Edge Triggers in the Background](#edge-triggers-in-the-background)
|
18
|
+
- [Shift Registers](#shift-registers)
|
19
|
+
- [Analog Inputs](#analog-inputs)
|
20
|
+
- [Reading](#reading)
|
21
|
+
- [Waiting for Change](#waiting-for-change)
|
22
|
+
- [Waiting for Change in the Background](#waiting-for-change-in-the-background)
|
23
|
+
- [Waiting for Threshold](#waiting-for-threshold)
|
24
|
+
- [Waiting for Threshold in the Background](#waiting-for-Threshold-in-the-background)
|
25
|
+
- [PWM](#pwm)
|
26
|
+
- [UART](#uart)
|
27
|
+
- [UART Writing](#uart-writing)
|
28
|
+
- [UART Reading](#uart-reading)
|
29
|
+
- [UART Reading and Iterating](#uart-reading-and-iterating)
|
30
|
+
- [UART Reading and Iterating in the Background](#uart-reading-and-iterating-in-the-background)
|
31
|
+
- [I2C](#i2c)
|
32
|
+
- [I2C Writing](#i2c-writing)
|
33
|
+
- [I2C Reading](#i2c-reading)
|
34
|
+
- [LSM303DLHC Example](#lsm303dlhc-example)
|
35
|
+
- [SPI](#spi)
|
36
|
+
- [SPI Data Transfer](#spi-data-transfer)
|
37
|
+
- [MCP3008 Example](#mcp3008-example)
|
38
|
+
- [Examples (Procedural)](#examples-procedural)
|
39
|
+
- [Pin Reference](#pin-reference)
|
40
|
+
- [License](#license)
|
5
41
|
|
6
|
-
|
42
|
+
## Overview
|
43
|
+
The purpose of this library is to provide easy access to all of the IO features of the Beaglebone in a highly flexible programming language (Ruby). This gem includes object oriented methods as well as procedural methods, so those familiar with Bonescript, the Adafruit Python library, or Arduino programming will be familiar with the syntax. This was developed and tested on a Beaglebone Black running the official Debian images. The code will need to be executed as root in order to function properly and utilize all of the features of the Beaglebone.
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
### Installing Ruby
|
47
|
+
Ruby and Rubygems are required to use this gem. To install, simply run the command below. This will install Ruby 1.9.1 which includes Rubygems.
|
48
|
+
|
49
|
+
```
|
50
|
+
sudo apt-get install ruby
|
51
|
+
```
|
52
|
+
|
53
|
+
### Installing Beaglebone Gem
|
54
|
+
Once Ruby is installed installed, install the gem by running the command below.
|
55
|
+
|
56
|
+
```
|
57
|
+
sudo gem install beaglebone
|
58
|
+
```
|
59
|
+
|
60
|
+
## Usage
|
61
|
+
To use this gem, require it in the Ruby script. An example follows
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
#!/usr/bin/env ruby
|
65
|
+
require 'beaglebone'
|
66
|
+
include Beaglebone
|
67
|
+
```
|
68
|
+
|
69
|
+
## Rereference
|
70
|
+
A full reference is available [here](http://rubydoc.info/gems/beaglebone/1.0.5/frames).
|
71
|
+
|
72
|
+
## Examples (Object Oriented)
|
73
|
+
These examples will show the various ways to interact with the Beaglebones IO hardware. They will need to be executed as root in order to function correctly.
|
74
|
+
|
75
|
+
### GPIO
|
76
|
+
The GPIO pins on the Beaglebone run at **3.3v**. Do not provide more than 3.3v to any GPIO pin or risk damaging the hardware.
|
77
|
+
|
78
|
+
GPIO pins have two modes, input and output. These modes are represented by the symbols **:IN** and **:OUT**.
|
79
|
+
|
80
|
+
To initialize the pin **P9_11**, pass the symbol for that pin and the mode to the **GPIOPin** constructor.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# Initialize pin P9_11 in INPUT mode
|
84
|
+
p9_11 = GPIOPin.new(:P9_11, :IN)
|
85
|
+
|
86
|
+
# Initialize pin P9_12 in OUTPUT mode
|
87
|
+
p9_12 = GPIOPin.new(:P9_12, :OUT)
|
88
|
+
|
89
|
+
# Change pin P9_12 to INPUT mode
|
90
|
+
p9_12.set_gpio_mode(:IN)
|
91
|
+
|
92
|
+
# Disable pin P9_12
|
93
|
+
p9_12.disable_gpio_pin
|
94
|
+
|
95
|
+
# Unassign to prevent re-use
|
96
|
+
p9_12 = nil
|
97
|
+
```
|
98
|
+
|
99
|
+
#### GPIO Writing
|
100
|
+
To set the state of a GPIO pin, the method **#digital_write** is used. The states that can be set are **:HIGH** to provide 3.3v and **:LOW** to provide ground.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
# Initialize pin P9_12 in OUTPUT mode
|
104
|
+
p9_12 = GPIOPin.new(:P9_12, :OUT)
|
105
|
+
|
106
|
+
# Provide 3.3v on pin P9_12
|
107
|
+
p9_12.digital_write(:HIGH)
|
108
|
+
|
109
|
+
# Provide ground on pin P9_12
|
110
|
+
p9_12.digital_write(:LOW)
|
111
|
+
```
|
112
|
+
|
113
|
+
#### GPIO Reading
|
114
|
+
To read the current state of a GPIO pin, the method **#digital_read** is used. It will return the symbol **:HIGH** or **:LOW** depending on the state of the pin.
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
# Initialize pin P9_11 in INPUT mode
|
118
|
+
p9_11 = GPIOPin.new(:P9_11, :IN)
|
119
|
+
|
120
|
+
# Get the current state of P9_11
|
121
|
+
state = p9_11.digital_read => :LOW
|
122
|
+
```
|
123
|
+
|
124
|
+
#### LEDs
|
125
|
+
The on-board LEDs are addressable via GPIO output. They are available on pins **:USR0** through **:USR3**.
|
126
|
+
|
127
|
+
This example will blink each LED in order 5 times.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
# Create an led object for each LED
|
131
|
+
led1 = GPIOPin.new(:USR0, :OUT)
|
132
|
+
led2 = GPIOPin.new(:USR1, :OUT)
|
133
|
+
led3 = GPIOPin.new(:USR2, :OUT)
|
134
|
+
led4 = GPIOPin.new(:USR3, :OUT)
|
135
|
+
|
136
|
+
# Run the following block 5 times
|
137
|
+
5.times do
|
138
|
+
# Iterate over each LED
|
139
|
+
[led1,led2,led3,led4].each do |led|
|
140
|
+
# Turn on the LED
|
141
|
+
led.digital_write(:HIGH)
|
142
|
+
# Delay 0.25 seconds
|
143
|
+
sleep 0.25
|
144
|
+
# Turn off the LED
|
145
|
+
led.digital_write(:LOW)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
#### Edge Triggers
|
151
|
+
The Beaglebone can also monitor for changes on a GPIO pin. This is called an edge trigger. Since this is interrupt based on the Beaglebone, waiting for a change does not waste CPU cycles by constantly polling the pin.
|
152
|
+
|
153
|
+
The following trigger types are supported
|
154
|
+
- Rising: Triggered when the state goes from low to high
|
155
|
+
- Falling: Triggered when the state goes from high to low
|
156
|
+
- Both: Triggered at any change in state
|
157
|
+
- None: Triggering is disabled
|
158
|
+
|
159
|
+
These trigger types are represented by the symbols :RISING, :FALLING, :BOTH, and :NONE
|
160
|
+
|
161
|
+
This example will wait for a rising edge to continue, then output the type of edge trigger that was detected.
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
# Initialize pin P9_11 in INPUT mode
|
165
|
+
p9_11 = GPIOPin.new(:P9_11, :IN)
|
166
|
+
|
167
|
+
# Wait here until a rising edge is detected
|
168
|
+
edge = p9_11.wait_for_edge(:RISING) => :RISING
|
169
|
+
|
170
|
+
# Output the trigger type detected
|
171
|
+
puts "Saw a #{edge} edge"
|
172
|
+
```
|
173
|
+
|
174
|
+
#### Edge Triggers in the Background
|
175
|
+
To avoid blocking while waiting for an edge trigger, the method **#run_on_edge** will run a callback when an edge trigger is detected. This method will spawn a new thread and wait for an edge trigger in the background. Only one of these threads may be active per pin.
|
176
|
+
|
177
|
+
This example will detect edge triggers in the background and output information when triggered.
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
# Initialize pin P9_11 in INPUT mode
|
181
|
+
p9_11 = GPIOPin.new(:P9_11, :IN)
|
182
|
+
|
183
|
+
# Define callback to run when an edge trigger is detected
|
184
|
+
# This method takes 3 arguments.
|
185
|
+
# pin: The pin that triggered the event
|
186
|
+
# edge: The event that triggered it
|
187
|
+
# count: How many times it has been triggered
|
188
|
+
callback = lambda { |pin,edge,count| puts "[#{count}] #{pin} #{edge}"}
|
189
|
+
|
190
|
+
# Run the callback every time a change in state is detected
|
191
|
+
# This method has two additional arguments that are optional.
|
192
|
+
# Timeout: How long to wait for an event before terminating the thread
|
193
|
+
# Repeats: How many times to run the event
|
194
|
+
# By default, it will run forever every time the specified trigger is detected
|
195
|
+
p9_11.run_on_edge(callback, :BOTH)
|
196
|
+
|
197
|
+
# This code will run immediately after the previous call, as it does not block
|
198
|
+
sleep 10
|
199
|
+
|
200
|
+
# Stop the background thread waiting for an edge trigger after 10 seconds
|
201
|
+
p9_11.stop_edge_wait
|
202
|
+
|
203
|
+
# This convenience method will run the callback only on the first detected change
|
204
|
+
p9_11.run_once_on_edge(callback, :BOTH)
|
205
|
+
|
206
|
+
# Change the trigger detection for the specified pin
|
207
|
+
p9_11.set_gpio_edge(:RISING)
|
208
|
+
```
|
209
|
+
|
210
|
+
#### Shift Registers
|
211
|
+
This library will also support writing to shift registers using GPIO pins. Create a **ShiftRegister** object by initializing it with the latch pin, clock pin, and data pin.
|
212
|
+
|
213
|
+
This example will trigger 8 pins of a shift register.
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
# P9_11 is connected to the latch pin
|
217
|
+
# P9_12 is connected to the clock pin
|
218
|
+
# P9_13 is connected to the data pin
|
219
|
+
|
220
|
+
# Initialize shift register
|
221
|
+
shiftreg = ShiftRegister.new(:P9_11, :P9_12, :P9_13)
|
222
|
+
|
223
|
+
# Write value to shift register
|
224
|
+
shiftreg.shift_out(0b11111111)
|
225
|
+
```
|
226
|
+
|
227
|
+
|
228
|
+
### Analog Inputs
|
229
|
+
The Analog pins on the Beaglebone run at **1.8v**. Do not provide more than 1.8v to any analog pin or risk damaging the hardware. The header has pins available to provide a 1.8v for analog devices as well as a dedicated analog ground. Analog pins are only capable of reading input values.
|
230
|
+
|
231
|
+
To initialize the pin **P9_33**, pass the symbol for that pin and the mode to the **AINPin** constructor.
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
# Initialize pin P9_33 for Analog Input
|
235
|
+
p9_33 = AINPin.new(:P9_33)
|
236
|
+
```
|
237
|
+
|
238
|
+
#### Reading
|
239
|
+
To read the value from an analog pin, the method **#read** is used. This will return a value between 0 and 1799.
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
# Initialize pin P9_33 for Analog Input
|
243
|
+
p9_33 = AINPin.new(:P9_33)
|
244
|
+
|
245
|
+
# Read the input value in millivolts.
|
246
|
+
mv = p9_33.read => 1799
|
247
|
+
```
|
248
|
+
|
249
|
+
#### Waiting for Change
|
250
|
+
To wait for the value of an analog pin to change by a specified voltage, the method **#wait_for_change** is used.
|
251
|
+
|
252
|
+
**#wait_for_change** takes the following arguments.
|
253
|
+
- mv_change: The amount of change in millivolts required before returning
|
254
|
+
- interval: How often to poll the value of the pin in seconds
|
255
|
+
- mv_last: (optional) The initial value to use as a point to detect change
|
256
|
+
|
257
|
+
This method returns an array containing the initial voltage, the last polled voltage, and the number of times the pin was polled.
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
# Initialize pin P9_33 for Analog Input
|
261
|
+
p9_33 = AINPin.new(:P9_33)
|
262
|
+
|
263
|
+
# Wait for 100mv of change on pin P9_33. Poll 10 times a second
|
264
|
+
mv_start, mv_current, count = p9_33.wait_for_change(100, 0.1)
|
265
|
+
```
|
266
|
+
|
267
|
+
#### Waiting for Change in the Background
|
268
|
+
To avoid blocking while waiting for voltage change, the method **#run_on_change** will run a callback every time the specified change is detected. This method will spawn a new thread and wait for change in the background. The method **#run_once_on_change** is a convenience method to only be triggered once. Only one of these threads may be active per pin.
|
269
|
+
|
270
|
+
This example waits for voltage change in the background and outputs information when change is detected.
|
271
|
+
|
272
|
+
```ruby
|
273
|
+
# Initialize pin P9_33 for Analog Input
|
274
|
+
p9_33 = AINPin.new(:P9_33)
|
275
|
+
|
276
|
+
# Define callback to run when condition is met
|
277
|
+
# This method takes 4 arguments.
|
278
|
+
# pin: The pin that triggered the event
|
279
|
+
# mv_last: The initial voltage used to determine change
|
280
|
+
# mv: The current voltage on the pin
|
281
|
+
# count: How many times it has been triggered
|
282
|
+
callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
|
283
|
+
|
284
|
+
# Run the callback every time the specified voltage change is detected
|
285
|
+
# This method has one additional argument that is optional.
|
286
|
+
# Repeats: How many times to will run the event
|
287
|
+
# By default, it will run forever every time the specified condition is detected
|
288
|
+
# Detect 10mv of change polling 10 times a second.
|
289
|
+
p9_33.run_on_change(callback, 10, 0.1)
|
290
|
+
|
291
|
+
# This code will run immediately after the previous call, as it does not block
|
292
|
+
sleep 20
|
293
|
+
|
294
|
+
# Stop the background thread after 20 seconds
|
295
|
+
p9_33.stop_wait
|
296
|
+
```
|
297
|
+
|
298
|
+
#### Waiting for Threshold
|
299
|
+
To wait for the value of an analog pin to cross certain threshold voltages, the method **#wait_for_threshold** is used.
|
300
|
+
|
301
|
+
**#wait_for_threshold** takes the following arguments.
|
302
|
+
- mv_lower: The lower threshold value in millivolts
|
303
|
+
- mv_upper: The upper threshold value in millivolts
|
304
|
+
- mv_reset: The voltage change required to cross out of the lower or upper threshold ranges.
|
305
|
+
- interval: How often to poll the value of the pin in seconds
|
306
|
+
- mv_last: (optional) The initial value to use as a point to detect change
|
307
|
+
- state_last: (optional) The initial state to use as a point to detect change
|
308
|
+
|
309
|
+
Three states are available.
|
310
|
+
- :LOW: below or equal to mv_lower
|
311
|
+
- :MID: above mv_lower and below mv_upper
|
312
|
+
- :HIGH: above or equal to mv_upper
|
313
|
+
|
314
|
+
This method returns an array containing the initial voltage, the last polled voltage, the initial state, the last polled state, and the number of times the pin was polled.
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
# Initialize pin P9_33 for Analog Input
|
318
|
+
p9_33 = AINPin.new(:P9_33)
|
319
|
+
|
320
|
+
# Wait for the voltage on pin P9_33 to go below 200mv or above 1600mv.
|
321
|
+
# To enter the :MID state from :HIGH or :LOW, it will have to cross the thresholds by at least 100mv.
|
322
|
+
# Poll 10 times a second
|
323
|
+
data = p9_33.wait_for_threshold(200, 1600, 100, 0.1) => [ 500, 150, :MID, :LOW, 53 ]
|
324
|
+
|
325
|
+
# Assign variables from array
|
326
|
+
mv_start, mv_current, state_start, state_current, count = data
|
327
|
+
```
|
328
|
+
|
329
|
+
#### Waiting for Threshold in the Background
|
330
|
+
To avoid blocking while waiting for a voltage threshold to be crossed, the method **#run_on_threshold** will run a callback every time the specified change is detected. This method will spawn a new thread and wait for change in the background. The method **#run_once_on_threshold** is a convenience method to only be triggered once. Only one of these threads may be active per pin.
|
331
|
+
|
332
|
+
This example waits for voltage change in the background and outputs information when the specified threshold is crossed.
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
# Initialize pin P9_33 for Analog Input
|
336
|
+
p9_33 = AINPin.new(:P9_33)
|
337
|
+
|
338
|
+
# Define callback to run when condition is met
|
339
|
+
# This method takes 6 arguments.
|
340
|
+
# pin: The pin that triggered the event
|
341
|
+
# mv_last: The initial voltage used to determine change
|
342
|
+
# mv: The current voltage on the pin
|
343
|
+
# state_last: The initial state to use as a point to detect change
|
344
|
+
# state: The current state of the pin
|
345
|
+
# count: How many times it has been triggered
|
346
|
+
callback = lambda { |pin, mv_last, mv, state_last, state, count|
|
347
|
+
puts "[#{count}] #{pin} #{state_last} -> #{state} #{mv_last} -> #{mv}"
|
348
|
+
}
|
349
|
+
|
350
|
+
# Run the callback every time the specified voltage threshold is crossed
|
351
|
+
# This method has one additional argument that is optional.
|
352
|
+
# Repeats: How many times to will run the event
|
353
|
+
# By default, it will run forever every time the specified condition is detected
|
354
|
+
# Wait for the voltage on pin P9_33 to go below 200mv or above 1600mv.
|
355
|
+
# To enter the :MID state from :HIGH or :LOW, it will have to cross the thresholds by at least 100mv.
|
356
|
+
# Poll 10 times a second
|
357
|
+
# Run callback when state changes
|
358
|
+
p9_33.run_on_threshold(callback, 200, 1600, 100, 0.1)
|
359
|
+
|
360
|
+
# This code will run immediately after the previous call, as it does not block
|
361
|
+
sleep 20
|
362
|
+
|
363
|
+
# Stop the background thread after 20 seconds
|
364
|
+
p9_33.stop_wait
|
365
|
+
```
|
366
|
+
|
367
|
+
### PWM
|
368
|
+
The beaglebone supports PWM (pulse width modulated) output on certain pins. These pins output 3.3v. The output is controlled based on frequency and duty cycle.
|
369
|
+
|
370
|
+
To initialize the pin **P9_14**, pass the symbol for that pin, the duty cycle, and the frequency in Hz to the **PWMPin** constructor.
|
371
|
+
|
372
|
+
This example shows how to control PWM output of a specified pin.
|
373
|
+
|
374
|
+
```ruby
|
375
|
+
# Initialize pin P9_14 for PWM output
|
376
|
+
# This pin will now output a square wave at 10Hz with a 90% duty cycle.
|
377
|
+
p9_14 = PWMPin.new(:P9_14, 90, 10)
|
378
|
+
|
379
|
+
# Change frequency to 20Hz. Duty cycle remains 90%
|
380
|
+
p9_14.set_frequency(20)
|
381
|
+
|
382
|
+
# Change the duty cycle to 50%
|
383
|
+
p9_14.set_duty_cycle(50)
|
384
|
+
|
385
|
+
# Adjust the frequency by setting the period in nanoseconds.
|
386
|
+
p9_14.set_period_ns(31250000)
|
387
|
+
|
388
|
+
# Adjust the duty cycle by setting the period in nanoseconds.
|
389
|
+
p9_14.set_duty_cycle_ns(31250000)
|
390
|
+
|
391
|
+
# Invert the output signal
|
392
|
+
p9_14.set_polarity(:INVERTED)
|
393
|
+
|
394
|
+
# Disable the output signal
|
395
|
+
p9_14.disable_pwm_pin
|
396
|
+
```
|
397
|
+
|
398
|
+
### UART
|
399
|
+
The beaglebone has a number of UART devices. These operate in TTL mode at 3.3v. Do not provide more than 3.3v to the pins or risk damaging the hardware.
|
400
|
+
|
401
|
+
Please note, UART3 does not have an RX pin, and UART5 is only available if the HDMI device tree is not enabled.
|
402
|
+
|
403
|
+
To initialize the UART device **UART1**, pass the symbol for that device and the speed to the **UARTDevice** constructor.
|
404
|
+
|
405
|
+
```ruby
|
406
|
+
# Initialize the pins for device UART1 into UART mode.
|
407
|
+
uart1 = UARTDevice.new(:UART1, 9600)
|
408
|
+
|
409
|
+
# Change the speed of a UART device by calling #set_speed
|
410
|
+
uart1.set_speed(115200)
|
411
|
+
|
412
|
+
# Disable UART device
|
413
|
+
uart1.disable
|
414
|
+
```
|
415
|
+
|
416
|
+
#### UART Writing
|
417
|
+
Writing to a UART device is accomplished by calling the **#write** or **#writeln** methods
|
418
|
+
```ruby
|
419
|
+
# Initialize the pins for device UART1 into UART mode.
|
420
|
+
uart1 = UARTDevice.new(:UART1, 9600)
|
421
|
+
|
422
|
+
# Write data to a UART1
|
423
|
+
uart1.write("DATA DATA DATA!")
|
424
|
+
|
425
|
+
# Write data to UART1 followed by a line feed
|
426
|
+
uart1.writeln("A line feed follows")
|
427
|
+
```
|
428
|
+
|
429
|
+
#### UART Reading
|
430
|
+
There are many methods available for reading from UART devices. These are blocking methods and will not return until the requested is available.
|
431
|
+
|
432
|
+
```ruby
|
433
|
+
# Initialize the pins for device UART1 into UART mode.
|
434
|
+
uart1 = UARTDevice.new(:UART1, 9600)
|
435
|
+
|
436
|
+
# Read one character from UART1
|
437
|
+
c = uart1.readchar => "X"
|
438
|
+
|
439
|
+
# Read 10 characters from UART1
|
440
|
+
str = uart1.readchars(10) => "0123456789"
|
441
|
+
|
442
|
+
# Read a line from UART1
|
443
|
+
line = uart1.readline => "All the text up until the linefeed"
|
444
|
+
```
|
445
|
+
|
446
|
+
#### UART Reading and Iterating
|
447
|
+
Data read from the UART device may be iterated with the following methods. These are blocking methods and will run until the loop is broken.
|
448
|
+
|
449
|
+
```ruby
|
450
|
+
# Initialize the pins for device UART1 into UART mode.
|
451
|
+
uart1 = UARTDevice.new(:UART1, 9600)
|
452
|
+
|
453
|
+
# Run block on every character read from UART1
|
454
|
+
uart1.each_char { |c| puts c }
|
455
|
+
|
456
|
+
# Run block on every 5 character read from UART1
|
457
|
+
uart1.each_char(5) { |str| puts str }
|
458
|
+
|
459
|
+
# Run block on each line read from UART1
|
460
|
+
uart1.each_line { |line| puts line }
|
461
|
+
```
|
462
|
+
|
463
|
+
#### UART Reading and Iterating in the Background
|
464
|
+
Data read from the UART device may be iterated in the background with the following methods. The data read is passed to the specified callback. These method will spawn a new thread and wait for data in the background. Only one of these threads may be active per pin.
|
465
|
+
|
466
|
+
This example shows various methods of reading and processing data read from UART1 in the background.
|
467
|
+
|
468
|
+
```ruby
|
469
|
+
# Initialize the pins for device UART1 into UART mode.
|
470
|
+
uart1 = UARTDevice.new(:UART1, 9600)
|
471
|
+
|
472
|
+
# Define the callback to be run. It takes 3 arguments
|
473
|
+
# uart: the UART device that triggered the callback
|
474
|
+
# data: the data read from the UART
|
475
|
+
# count: how many times this was triggered
|
476
|
+
callback = lambda { |uart, data, count| puts "[#{uart}:#{count}] #{data}" }
|
477
|
+
|
478
|
+
# Run callback for every character read
|
479
|
+
uart1.run_on_each_char(callback)
|
480
|
+
|
481
|
+
# Run callback for every 3 characters read
|
482
|
+
uart1.run_on_each_chars(callback, 3)
|
483
|
+
|
484
|
+
# Run callback for every line read
|
485
|
+
uart1.run_on_each_line(callback)
|
486
|
+
|
487
|
+
# Run callback once after a character is read
|
488
|
+
uart1.run_once_on_each_char(callback)
|
489
|
+
|
490
|
+
# Run callback once after 3 characters are read
|
491
|
+
uart1.run_once_on_each_chars(callback, 3)
|
492
|
+
|
493
|
+
# Run callback once after reading a line
|
494
|
+
uart1.run_once_on_each_line(callback)
|
495
|
+
|
496
|
+
# Stop the currently running background thread
|
497
|
+
uart1.stop_read_wait
|
498
|
+
```
|
499
|
+
|
500
|
+
### I2C
|
501
|
+
The beaglebone has a number of I2C devices. These operate at 3.3v. Do not provide more than 3.3v to the pins or risk damaging the hardware.
|
502
|
+
|
503
|
+
To initialize the I2C device **I2C2**, pass the symbol for that device to the **I2CDevice** constructor.
|
504
|
+
|
505
|
+
```ruby
|
506
|
+
# Initialize I2C device I2C2
|
507
|
+
i2c = I2CDevice.new(:I2C2)
|
508
|
+
```
|
509
|
+
|
510
|
+
#### I2C Writing
|
511
|
+
To write to an I2C device, the method **#write** is used.
|
512
|
+
|
513
|
+
**#write** takes the following arguments.
|
514
|
+
- address: address of slave device
|
515
|
+
- data: data to write
|
516
|
+
|
517
|
+
#### I2C Reading
|
518
|
+
To read from an I2C device, the method **#read** is used.
|
519
|
+
|
520
|
+
**#read** takes the following arguments.
|
521
|
+
- address: address of slave device
|
522
|
+
- bytes: bytes to read
|
523
|
+
- register: (optional) register to start reading from
|
524
|
+
|
525
|
+
#### LSM303DLHC Example
|
526
|
+
|
527
|
+
This example communicates with an [LSM303DLHC](https://www.adafruit.com/products/1120) Accelerometer/Compass/Thermometer device.
|
528
|
+
|
529
|
+
```ruby
|
530
|
+
# Initialize I2C device I2C2
|
531
|
+
i2c = I2CDevice.new(:I2C2)
|
532
|
+
|
533
|
+
# Put compass into continuous conversation mode
|
534
|
+
i2c.write(0x1e, [0x02, 0x00].pack("C*"))
|
535
|
+
|
536
|
+
# Enable temperatuer sensor, 15hz register update
|
537
|
+
i2c.write(0x1e, [0x00, 0b10010000].pack("C*") )
|
538
|
+
|
539
|
+
# Delay for the settings to take effect
|
540
|
+
sleep(0.1)
|
541
|
+
|
542
|
+
# Read axis data. It is made up of 3 big endian signed shorts starting at register 0x03
|
543
|
+
raw = i2c.read(0x1e, 6, [0x03].pack("C*"))
|
544
|
+
|
545
|
+
# Coordinates are big endian signed shorts in x,z,y order
|
546
|
+
x,z,y = raw.unpack("s>*")
|
547
|
+
|
548
|
+
# Calculate angle of degrees from North
|
549
|
+
degrees = (Math::atan2(y, x) * 180) / Math::PI
|
550
|
+
degrees += 360 if degrees < 0
|
551
|
+
|
552
|
+
# Read 2 byte big endian signed short from temperature register
|
553
|
+
raw = i2c.read(0x1e, 2, [0x31].pack("C*"))
|
554
|
+
|
555
|
+
# Temperature is sent big endian, least significant digit last
|
556
|
+
temp = raw.unpack("s>").first
|
557
|
+
|
558
|
+
# Temperature data is 12 bits, last 4 are unused
|
559
|
+
temp = temp >> 4
|
560
|
+
|
561
|
+
# Each bit is 8c
|
562
|
+
temp /= 8
|
563
|
+
|
564
|
+
# Correction factor
|
565
|
+
temp += 18
|
566
|
+
|
567
|
+
# Convert to f
|
568
|
+
temp = (temp * 1.8 + 32).to_i
|
569
|
+
|
570
|
+
# Output data
|
571
|
+
puts "#{Time.now.strftime("%H:%M")} Temperature: #{temp} degrees f Direction: #{degrees.to_i} degrees"
|
572
|
+
|
573
|
+
# Disable I2C device
|
574
|
+
i2c.disable
|
575
|
+
```
|
576
|
+
|
577
|
+
### SPI
|
578
|
+
The beaglebone has a number of SPI devices. These operate at 3.3v. Do not provide more than 3.3v to the pins or risk damaging the hardware.
|
579
|
+
|
580
|
+
To initialize the SPI device **SPI0**, pass the symbol for that device to the **SPIDevice** constructor.
|
581
|
+
|
582
|
+
The optional arguments are also available
|
583
|
+
- mode: SPI mode, :SPI_MODE_0 through :SPI_MODE_3
|
584
|
+
- speed: Speed of the SPI device
|
585
|
+
- bpw: Bits per word
|
586
|
+
|
587
|
+
```ruby
|
588
|
+
# Initialize SPI device SPI0
|
589
|
+
spi = SPIDevice.new(:SPI0, :SPI_MODE_0, 1000000, 8)
|
590
|
+
|
591
|
+
# You can change SPI with the methods below.
|
592
|
+
|
593
|
+
# Set mode of SPI0
|
594
|
+
spi.set_mode(:SPI_MODE_3)
|
595
|
+
|
596
|
+
# Set speed of SPI0
|
597
|
+
spi.set_speed(100000)
|
598
|
+
|
599
|
+
# Set bits per word of SPI0
|
600
|
+
spi.set_bpw(10)
|
601
|
+
|
602
|
+
# Disable SPI device
|
603
|
+
spi.disable
|
604
|
+
```
|
605
|
+
|
606
|
+
#### SPI Data Tramsfer
|
607
|
+
To transfer data to an SPI device, the method **#xfer** is used.
|
608
|
+
|
609
|
+
**#xfer** takes the following arguments
|
610
|
+
- tx_data: data to transmit
|
611
|
+
- readbytes: (optional) number of bytes to read, otherwise it sizeof tx_data is used
|
612
|
+
- speed: (optional) speed of the transfer
|
613
|
+
- delay: (optional) delay
|
614
|
+
- bpw: (optonal) bits per word
|
615
|
+
|
616
|
+
**#xfer** returns the bytes read from the SPI device.
|
617
|
+
|
618
|
+
#### MCP3008 Example
|
619
|
+
This example communicates with an [MCP3008](http://www.adafruit.com/products/856) ADC device.
|
620
|
+
|
621
|
+
```ruby
|
622
|
+
# Initialize SPI device SPI0
|
623
|
+
spi = SPIDevice.new(:SPI0)
|
624
|
+
|
625
|
+
# communicate with MCP3008
|
626
|
+
# byte 1: start bit
|
627
|
+
# byte 2: single(1)/diff(0),3 bites for channel, null pad
|
628
|
+
# byte 3: don't care
|
629
|
+
# Read value from channel 0
|
630
|
+
raw = spi.xfer([ 0b00000001, 0b10000000, 0].pack("C*"))
|
631
|
+
|
632
|
+
# split data read into an array of characters
|
633
|
+
data = raw.unpack("C*")
|
634
|
+
|
635
|
+
# The returned data is stored starting at the last two bits of the second byte
|
636
|
+
val = ((data[1] & 0b00000011) << 8 ) | data[2]
|
637
|
+
|
638
|
+
# Display the value of channel 0
|
639
|
+
puts "Value of channel 0: #{val}"
|
640
|
+
|
641
|
+
# Read value from channel 1
|
642
|
+
raw = spi.xfer([ 0b00000001, 0b10010000, 0].pack("C*"))
|
643
|
+
|
644
|
+
# split data read into an array of characters
|
645
|
+
data = raw.unpack("C*")
|
646
|
+
|
647
|
+
# The returned data is stored starting at the last two bits of the second byte
|
648
|
+
val = ((data[1] & 0b00000011) << 8 ) | data[2]
|
649
|
+
|
650
|
+
# Display the value of channel 1
|
651
|
+
puts "Value of channel 1: #{val}"
|
652
|
+
```
|
653
|
+
|
654
|
+
## Examples (Procedural)
|
655
|
+
|
656
|
+
|
657
|
+
## Pin Reference
|
658
|
+
|
659
|
+
## License
|
660
|
+
Copyright (c) 2014 Rob Mosher. Distributed under the GPL-v3 License. See LICENSE for more information.
|
data/beaglebone.gemspec
CHANGED
data/examples/shiftregister.rb
CHANGED
data/examples/uart.rb
CHANGED
@@ -3,31 +3,31 @@ require 'beaglebone'
|
|
3
3
|
|
4
4
|
#uart example
|
5
5
|
#oo method
|
6
|
-
|
6
|
+
uart1 = UARTDevice.new(:uart1, 9600)
|
7
7
|
|
8
|
-
|
8
|
+
uart1.writeln("TEST")
|
9
9
|
|
10
|
-
puts
|
11
|
-
puts
|
12
|
-
puts
|
10
|
+
puts uart1.readchars(10)
|
11
|
+
puts uart1.readchar
|
12
|
+
puts uart1.readline
|
13
13
|
|
14
14
|
callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
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
22
|
|
23
23
|
sleep(5)
|
24
|
-
|
24
|
+
uart1.stop_read_wait
|
25
25
|
|
26
26
|
|
27
|
-
|
28
|
-
#
|
27
|
+
uart1.each_chars(2) { |c| puts c }
|
28
|
+
#uart1.each_line { |line| puts line }
|
29
29
|
|
30
|
-
|
30
|
+
uart1.disable
|
31
31
|
|
32
32
|
exit
|
33
33
|
|
data/lib/beaglebone/gpio.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# == shiftregister.rb
|
2
2
|
# This file contains the shiftregister control methods
|
3
|
-
module Beaglebone
|
3
|
+
module Beaglebone #:nodoc:
|
4
4
|
class ShiftRegister
|
5
5
|
|
6
6
|
# Create a shiftregister object based on 3 GPIO pins
|
@@ -32,7 +32,7 @@ module Beaglebone
|
|
32
32
|
# @example
|
33
33
|
# shiftregister = ShiftRegister.new(:P9_11, :P9_12, :P9_13)
|
34
34
|
# shiftregister.shift_out(255)
|
35
|
-
def
|
35
|
+
def shift_out(data, lsb=nil)
|
36
36
|
GPIO::shift_out(@latch_pin, @clock_pin, @data_pin, data, lsb || @lsb)
|
37
37
|
end
|
38
38
|
|
data/lib/beaglebone/uart.rb
CHANGED