beaglebone 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/beaglebone.gemspec +1 -1
- data/examples/i2c.rb +2 -2
- data/lib/beaglebone/ain.rb +19 -21
- data/lib/beaglebone/beaglebone.rb +5 -4
- data/lib/beaglebone/gpio.rb +10 -10
- data/lib/beaglebone/i2c.rb +111 -37
- data/lib/beaglebone/pwm.rb +1 -1
- data/lib/beaglebone/shiftregister.rb +19 -0
- data/lib/beaglebone/spi.rb +145 -38
- data/lib/beaglebone/uart.rb +308 -78
- 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: 7ebe3395979d7ccd5ddbd563705281820b5ed54d
|
4
|
+
data.tar.gz: ef2f2c1bb2f50761e5837472c3777781ff767297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 911649b330a3dfbd7ba1f21bfb0b4763b4ba9842513138bc460c71297d34ca63cac7005ce133f9aff2b43b321473ffc711ab02a1b0a63c1ecbe2902ab961e5d3
|
7
|
+
data.tar.gz: e9b5b5dd6fd8ebf76c40758fe7a6d44fa9187df710d948cb8007f8aa020b4705a48417b7d8db630188fb0fc5f468b147fc79ff1927e15cef0c28f5bfbfe397c7
|
data/beaglebone.gemspec
CHANGED
data/examples/i2c.rb
CHANGED
@@ -12,7 +12,7 @@ i2c = I2CDevice.new(:I2C2)
|
|
12
12
|
i2c.write(0x1e, [0x02, 0x00].pack("C*"))
|
13
13
|
|
14
14
|
#enable temperatuer sensor, 15hz register update
|
15
|
-
i2c.write(0x1e, [0x00,
|
15
|
+
i2c.write(0x1e, [0x00, 0b10010000].pack("C*") )
|
16
16
|
|
17
17
|
#delay for the settings to take effect
|
18
18
|
sleep(0.1)
|
@@ -66,7 +66,7 @@ I2C.setup(:I2C2)
|
|
66
66
|
#put mag into continuous conversation mode
|
67
67
|
I2C.write(:I2C2, 0x1e, [0x02, 0x00].pack("C*"))
|
68
68
|
#enable temperatuer sensor, 15hz register update
|
69
|
-
I2C.write(:I2C2, 0x1e, [0x00,
|
69
|
+
I2C.write(:I2C2, 0x1e, [0x00, 0b10010000].pack("C*") )
|
70
70
|
#delay for the settings to take effect
|
71
71
|
sleep(0.1)
|
72
72
|
|
data/lib/beaglebone/ain.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# == ain.rb
|
2
2
|
# This file contains the Analog Input methods
|
3
3
|
module Beaglebone #:nodoc:
|
4
|
-
# AIN
|
4
|
+
# == AIN
|
5
5
|
# procedural methods for Analog Input
|
6
6
|
# == Summary
|
7
|
-
# #read is called to get the analog value of a pin
|
8
|
-
#
|
7
|
+
# #read is called to get the analog value of a pin.
|
8
|
+
# More advanced polling methods are also available
|
9
9
|
module AIN
|
10
10
|
class << self
|
11
11
|
|
@@ -45,8 +45,8 @@ module Beaglebone #:nodoc:
|
|
45
45
|
ain_fd.read.strip.to_i
|
46
46
|
end
|
47
47
|
|
48
|
-
# Runs a callback after voltage changes by specified amount
|
49
|
-
# This creates a new thread that runs in the background and polls at specified interval
|
48
|
+
# Runs a callback after voltage changes by specified amount.
|
49
|
+
# This creates a new thread that runs in the background and polls at specified interval.
|
50
50
|
#
|
51
51
|
# @param callback A method to call when the change is detected. This method should take 4 arguments: the pin, the previous voltage, the current voltage, and the counter
|
52
52
|
# @param pin should be a symbol representing the header pin, i.e. :P9_11
|
@@ -99,9 +99,9 @@ module Beaglebone #:nodoc:
|
|
99
99
|
run_on_change(callback, pin, mv_change, interval, 1)
|
100
100
|
end
|
101
101
|
|
102
|
-
# Runs a callback after voltage changes beyond a certain threshold
|
103
|
-
# This creates a new thread that runs in the background and polls at specified interval
|
104
|
-
# When the voltage crosses the specified thresholds the callback is run
|
102
|
+
# Runs a callback after voltage changes beyond a certain threshold.
|
103
|
+
# This creates a new thread that runs in the background and polls at specified interval.
|
104
|
+
# When the voltage crosses the specified thresholds the callback is run.
|
105
105
|
#
|
106
106
|
# @param callback A method to call when the change is detected. This method should take 6 arguments: the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter
|
107
107
|
# @param pin should be a symbol representing the header pin, i.e. :P9_11
|
@@ -153,7 +153,7 @@ module Beaglebone #:nodoc:
|
|
153
153
|
Beaglebone::set_pin_status(pin, :thread, thread)
|
154
154
|
end
|
155
155
|
|
156
|
-
# Runs a callback once after voltage crosses a specified threshold
|
156
|
+
# Runs a callback once after voltage crosses a specified threshold.
|
157
157
|
# Convenience method for run_on_threshold
|
158
158
|
# @see #run_on_threshold
|
159
159
|
def run_once_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset=10, interval=0.01)
|
@@ -162,9 +162,9 @@ module Beaglebone #:nodoc:
|
|
162
162
|
|
163
163
|
# noinspection RubyScope
|
164
164
|
|
165
|
-
# Runs a callback after voltage changes beyond a certain threshold
|
166
|
-
# This creates a new thread that runs in the background and polls at specified interval
|
167
|
-
# When the voltage crosses the specified thresholds the callback is run
|
165
|
+
# Runs a callback after voltage changes beyond a certain threshold.
|
166
|
+
# This creates a new thread that runs in the background and polls at specified interval.
|
167
|
+
# When the voltage crosses the specified thresholds the callback is run.
|
168
168
|
#
|
169
169
|
# This method should take 6 arguments:
|
170
170
|
# the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter
|
@@ -374,8 +374,8 @@ module Beaglebone #:nodoc:
|
|
374
374
|
AIN::read(@pin)
|
375
375
|
end
|
376
376
|
|
377
|
-
# Runs a callback after voltage changes by specified amount
|
378
|
-
# This creates a new thread that runs in the background and polls at specified interval
|
377
|
+
# Runs a callback after voltage changes by specified amount.
|
378
|
+
# This creates a new thread that runs in the background and polls at specified interval.
|
379
379
|
#
|
380
380
|
# @param callback A method to call when the change is detected
|
381
381
|
# This method should take 4 arguments: the pin, the previous voltage, the current voltage, and the counter
|
@@ -399,13 +399,11 @@ module Beaglebone #:nodoc:
|
|
399
399
|
end
|
400
400
|
|
401
401
|
|
402
|
-
# Runs a callback after voltage changes beyond a certain threshold
|
403
|
-
# This creates a new thread that runs in the background and polls at specified interval
|
404
|
-
# When the voltage crosses the specified thresholds the callback is run
|
402
|
+
# Runs a callback after voltage changes beyond a certain threshold.
|
403
|
+
# This creates a new thread that runs in the background and polls at specified interval.
|
404
|
+
# When the voltage crosses the specified thresholds the callback is run.
|
405
405
|
#
|
406
|
-
# @param callback A method to call when the change is detected
|
407
|
-
# This method should take 6 arguments:
|
408
|
-
# the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter
|
406
|
+
# @param callback A method to call when the change is detected. This method should take 6 arguments: the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter
|
409
407
|
# @param mv_lower an integer specifying the lower threshold voltage
|
410
408
|
# @param mv_upper an integer specifying the upper threshold voltage
|
411
409
|
# @param mv_reset an integer specifying the range in mv required to reset the threshold trigger
|
@@ -425,7 +423,7 @@ module Beaglebone #:nodoc:
|
|
425
423
|
end
|
426
424
|
|
427
425
|
|
428
|
-
# Runs a callback once after voltage crosses a specified threshold
|
426
|
+
# Runs a callback once after voltage crosses a specified threshold.
|
429
427
|
# Convenience method for run_on_threshold
|
430
428
|
def run_once_on_threshold(callback, mv_lower, mv_upper, mv_reset=10, interval=0.01)
|
431
429
|
AIN::run_once_on_threshold(callback, @pin, mv_lower, mv_upper, mv_reset, interval)
|
@@ -89,10 +89,8 @@ module Beaglebone
|
|
89
89
|
:P9_15 => { :gpio => 48 },
|
90
90
|
:P9_16 => { :gpio => 51, :pwm => 'pwm_1b', :pwm_id => 1, :pwm_mux => 6 },
|
91
91
|
#17 and 18 are not currently working for gpio in 3.8
|
92
|
-
|
93
|
-
|
94
|
-
:P9_17 => { :i2c => 'i2c1_scl', :i2c_id => 1, :spi => 'spi0_cs0', :spi_id => 0 },
|
95
|
-
:P9_18 => { :i2c => 'i2c1_sda', :i2c_id => 1, :spi => 'spi0_d1', :spi_id => 0 },
|
92
|
+
:P9_17 => { :gpio => 4, :i2c => 'i2c1_scl', :i2c_id => 1, :spi => 'spi0_cs0', :spi_id => 0 },
|
93
|
+
:P9_18 => { :gpio => 5, :i2c => 'i2c1_sda', :i2c_id => 1, :spi => 'spi0_d1', :spi_id => 0 },
|
96
94
|
:P9_19 => { :i2c => 'i2c2_scl', :i2c_id => 2, :uart => 'uart1_rtsn', :uart_id => 1, :spi => 'spi1_cs1', :spi_id => 1 },
|
97
95
|
:P9_20 => { :i2c => 'i2c2_sda', :i2c_id => 2, :uart => 'uart1_ctsn', :uart_id => 1, :spi => 'spi1_cs0', :spi_id => 1 },
|
98
96
|
:P9_21 => { :gpio => 3, :pwm => 'pwm_0b', :pwm_id => 0, :pwm_mux => 3, :i2c => 'i2c2_scl', :i2c_id => 2, :uart => 'uart2_txd', :uart_id => 2, :spi => 'spi0_d0', :spi_id => 0 },
|
@@ -175,6 +173,7 @@ module Beaglebone
|
|
175
173
|
class << self
|
176
174
|
attr_accessor :pinstatus, :pinmutex, :loaded_dtbs
|
177
175
|
|
176
|
+
# @private
|
178
177
|
# get hash entry for pin
|
179
178
|
def get_pin_status(pin, key = nil)
|
180
179
|
pinmutex.synchronize do
|
@@ -186,6 +185,7 @@ module Beaglebone
|
|
186
185
|
end
|
187
186
|
end
|
188
187
|
|
188
|
+
# @private
|
189
189
|
# set hash entry for pin
|
190
190
|
def set_pin_status(pin, key, value)
|
191
191
|
pinmutex.synchronize do
|
@@ -194,6 +194,7 @@ module Beaglebone
|
|
194
194
|
end
|
195
195
|
end
|
196
196
|
|
197
|
+
# @private
|
197
198
|
# delete pin's hash entry
|
198
199
|
def delete_pin_status(pin, key = nil)
|
199
200
|
pinmutex.synchronize do
|
data/lib/beaglebone/gpio.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# This file contains the GPIO methods
|
3
3
|
|
4
4
|
module Beaglebone #:nodoc:
|
5
|
-
# GPIO
|
5
|
+
# == GPIO
|
6
6
|
# procedural methods for GPIO control
|
7
7
|
# == Summary
|
8
8
|
# #pin_mode is called to initialize a pin.
|
@@ -101,7 +101,7 @@ module Beaglebone #:nodoc:
|
|
101
101
|
Beaglebone::set_pin_status(pin, :state, state)
|
102
102
|
end
|
103
103
|
|
104
|
-
# Runs a callback on an edge trigger event
|
104
|
+
# Runs a callback on an edge trigger event.
|
105
105
|
# This creates a new thread that runs in the background
|
106
106
|
#
|
107
107
|
# @param callback A method to call when the edge trigger is detected. This method should take 3 arguments, the pin, the edge, and the counter
|
@@ -139,8 +139,8 @@ module Beaglebone #:nodoc:
|
|
139
139
|
Beaglebone::set_pin_status(pin, :thread, thread)
|
140
140
|
end
|
141
141
|
|
142
|
-
# Runs a callback one time on an edge trigger event
|
143
|
-
#
|
142
|
+
# Runs a callback one time on an edge trigger event.
|
143
|
+
# This is a convenience method for run_on_edge
|
144
144
|
# @see #run_on_edge
|
145
145
|
def run_once_on_edge(callback, pin, edge, timeout = nil)
|
146
146
|
run_on_edge(callback, pin, edge, timeout, 1)
|
@@ -156,7 +156,7 @@ module Beaglebone #:nodoc:
|
|
156
156
|
thread.join if thread
|
157
157
|
end
|
158
158
|
|
159
|
-
# Wait for an edge trigger
|
159
|
+
# Wait for an edge trigger.
|
160
160
|
# Returns the type that triggered the event, e.g. :RISING, :FALLING, :BOTH
|
161
161
|
#
|
162
162
|
# @returns [Symbol] :RISING, :FALLING, or :BOTH
|
@@ -221,8 +221,8 @@ module Beaglebone #:nodoc:
|
|
221
221
|
# Sends data to a shift register
|
222
222
|
#
|
223
223
|
# @param latch_pin should be a symbol representing the header pin, i.e. :P9_12
|
224
|
-
# @param clock_pin should be a symbol representing the header pin, i.e. :
|
225
|
-
# @param data_pin should be a symbol representing the header pin, i.e. :
|
224
|
+
# @param clock_pin should be a symbol representing the header pin, i.e. :P9_13
|
225
|
+
# @param data_pin should be a symbol representing the header pin, i.e. :P9_14
|
226
226
|
# @param data Integer value to write to the shift register
|
227
227
|
# @param lsb optional, send least significant bit first if set
|
228
228
|
#
|
@@ -505,7 +505,7 @@ module Beaglebone #:nodoc:
|
|
505
505
|
GPIO::digital_read(@pin)
|
506
506
|
end
|
507
507
|
|
508
|
-
# Runs a callback on an edge trigger event
|
508
|
+
# Runs a callback on an edge trigger event.
|
509
509
|
# This creates a new thread that runs in the background
|
510
510
|
#
|
511
511
|
# @param callback A method to call when the edge trigger is detected. This method should take 3 arguments, the pin, the edge, and the counter
|
@@ -520,7 +520,7 @@ module Beaglebone #:nodoc:
|
|
520
520
|
GPIO::run_on_edge(callback, @pin, edge, timeout, repeats)
|
521
521
|
end
|
522
522
|
|
523
|
-
# Runs a callback one time on an edge trigger event
|
523
|
+
# Runs a callback one time on an edge trigger event.
|
524
524
|
# this is a convenience method for run_on_edge
|
525
525
|
# @see #run_on_edge
|
526
526
|
def run_once_on_edge(callback, edge, timeout=nil)
|
@@ -533,7 +533,7 @@ module Beaglebone #:nodoc:
|
|
533
533
|
GPIO::stop_edge_wait(@pin)
|
534
534
|
end
|
535
535
|
|
536
|
-
# Wait for an edge trigger
|
536
|
+
# Wait for an edge trigger.
|
537
537
|
# Returns the type that triggered the event, e.g. :RISING, :FALLING, :BOTH
|
538
538
|
#
|
539
539
|
# @return [Symbol] :RISING, :FALLING, or :BOTH
|
data/lib/beaglebone/i2c.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# == i2c.rb
|
2
|
+
# This file contains I2C methods
|
3
|
+
module Beaglebone #:nodoc:
|
4
|
+
# == I2C
|
5
|
+
# Procedural methods for I2C control
|
6
|
+
# == Summary
|
7
|
+
# #setup is called to initialize an I2C device
|
3
8
|
module I2C
|
4
9
|
|
5
10
|
I2C_SLAVE = 0x0703
|
@@ -10,6 +15,48 @@ module Beaglebone
|
|
10
15
|
class << self
|
11
16
|
attr_accessor :i2cstatus, :i2cmutex
|
12
17
|
|
18
|
+
# Initialize an I2C device
|
19
|
+
#
|
20
|
+
# @param i2c should be a symbol representing the I2C device
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
# I2C.setup(:I2C2)
|
24
|
+
def setup(i2c)
|
25
|
+
check_i2c_valid(i2c)
|
26
|
+
|
27
|
+
#make sure i2c not already enabled
|
28
|
+
return if get_i2c_status(i2c)
|
29
|
+
|
30
|
+
i2cinfo = I2CS[i2c]
|
31
|
+
|
32
|
+
#ensure dtb is loaded
|
33
|
+
Beaglebone::device_tree_load("#{i2cinfo[:devicetree]}") if i2cinfo[:devicetree]
|
34
|
+
|
35
|
+
#open the i2c device
|
36
|
+
i2c_fd = File.open(i2cinfo[:dev], 'r+')
|
37
|
+
|
38
|
+
Beaglebone::set_pin_status(i2cinfo[:scl], :i2c, i2cinfo[:id])
|
39
|
+
Beaglebone::set_pin_status(i2cinfo[:scl], :type, :i2c)
|
40
|
+
Beaglebone::set_pin_status(i2cinfo[:scl], :fd_i2c, i2c_fd)
|
41
|
+
|
42
|
+
Beaglebone::set_pin_status(i2cinfo[:sda], :i2c, i2cinfo[:id])
|
43
|
+
Beaglebone::set_pin_status(i2cinfo[:sda], :type, :i2c)
|
44
|
+
Beaglebone::set_pin_status(i2cinfo[:sda], :fd_i2c, i2c_fd)
|
45
|
+
|
46
|
+
set_i2c_status(i2c, :fd_i2c, i2c_fd)
|
47
|
+
set_i2c_status(i2c, :mutex, Mutex.new)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Write data to an I2C device
|
51
|
+
#
|
52
|
+
# @param i2c should be a symbol representing the I2C device
|
53
|
+
# @param address the address of the slave device
|
54
|
+
# @param data the data to write
|
55
|
+
#
|
56
|
+
# @return Integer the number of bytes written
|
57
|
+
#
|
58
|
+
# @example
|
59
|
+
# I2C.write(:I2C2, 0x1e, [0x00, 0b10010000].pack("C*") )
|
13
60
|
def write(i2c, address, data)
|
14
61
|
check_i2c_enabled(i2c)
|
15
62
|
|
@@ -21,9 +68,19 @@ module Beaglebone
|
|
21
68
|
|
22
69
|
i2c_fd.syswrite(data)
|
23
70
|
end
|
24
|
-
|
25
71
|
end
|
26
72
|
|
73
|
+
# Read data from an I2C device
|
74
|
+
#
|
75
|
+
# @param i2c should be a symbol representing the I2C device
|
76
|
+
# @param address the address of the slave device
|
77
|
+
# @param bytes bytes to read
|
78
|
+
# @param register optional register to read from
|
79
|
+
#
|
80
|
+
# @example
|
81
|
+
# # read 3 big endian signed shorts starting at register 0x03
|
82
|
+
# data = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
|
83
|
+
# x,z,y = raw.unpack("s>*")
|
27
84
|
def read(i2c, address, bytes=1, register=nil)
|
28
85
|
check_i2c_enabled(i2c)
|
29
86
|
|
@@ -42,37 +99,19 @@ module Beaglebone
|
|
42
99
|
data
|
43
100
|
end
|
44
101
|
|
102
|
+
# Return the file descriptor to the open I2C device
|
103
|
+
#
|
104
|
+
# @param i2c should be a symbol representing the I2C device
|
45
105
|
def file(i2c)
|
46
106
|
check_i2c_enabled(i2c)
|
47
107
|
get_i2c_status(i2c, :fd_i2c)
|
48
108
|
end
|
49
109
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
i2cinfo = I2CS[i2c]
|
57
|
-
|
58
|
-
#ensure dtb is loaded
|
59
|
-
Beaglebone::device_tree_load("#{i2cinfo[:devicetree]}") if i2cinfo[:devicetree]
|
60
|
-
|
61
|
-
#open the i2c device
|
62
|
-
i2c_fd = File.open(i2cinfo[:dev], 'r+')
|
63
|
-
|
64
|
-
Beaglebone::set_pin_status(i2cinfo[:scl], :i2c, i2cinfo[:id])
|
65
|
-
Beaglebone::set_pin_status(i2cinfo[:scl], :type, :i2c)
|
66
|
-
Beaglebone::set_pin_status(i2cinfo[:scl], :fd_i2c, i2c_fd)
|
67
|
-
|
68
|
-
Beaglebone::set_pin_status(i2cinfo[:sda], :i2c, i2cinfo[:id])
|
69
|
-
Beaglebone::set_pin_status(i2cinfo[:sda], :type, :i2c)
|
70
|
-
Beaglebone::set_pin_status(i2cinfo[:sda], :fd_i2c, i2c_fd)
|
71
|
-
|
72
|
-
set_i2c_status(i2c, :fd_i2c, i2c_fd)
|
73
|
-
set_i2c_status(i2c, :mutex, Mutex.new)
|
74
|
-
end
|
75
|
-
|
110
|
+
# Disable the specified I2C device.
|
111
|
+
#
|
112
|
+
# @note device trees cannot be unloaded at this time without kernel panic.
|
113
|
+
#
|
114
|
+
# @param i2c should be a symbol representing the I2C device
|
76
115
|
def disable(i2c)
|
77
116
|
check_i2c_valid(i2c)
|
78
117
|
check_i2c_enabled(i2c)
|
@@ -87,6 +126,7 @@ module Beaglebone
|
|
87
126
|
|
88
127
|
end
|
89
128
|
|
129
|
+
# Disable all active I2C interfaces
|
90
130
|
def cleanup
|
91
131
|
#reset all i2cs we've used and unload the device tree
|
92
132
|
i2cstatus.clone.keys.each { |i2c| disable(i2c)}
|
@@ -94,12 +134,14 @@ module Beaglebone
|
|
94
134
|
|
95
135
|
private
|
96
136
|
|
137
|
+
# disable i2c pin
|
97
138
|
def disable_i2c_pin(pin)
|
98
139
|
Beaglebone::check_valid_pin(pin, :i2c)
|
99
140
|
|
100
141
|
Beaglebone::delete_pin_status(pin)
|
101
142
|
end
|
102
143
|
|
144
|
+
# ensure valid i2c device
|
103
145
|
def check_i2c_valid(i2c)
|
104
146
|
raise ArgumentError, "Invalid i2c Specified #{i2c.to_s}" unless I2CS[i2c] && I2CS[i2c][:sda]
|
105
147
|
i2cinfo = I2CS[i2c.to_sym.upcase]
|
@@ -114,10 +156,12 @@ module Beaglebone
|
|
114
156
|
|
115
157
|
end
|
116
158
|
|
159
|
+
# ensure i2c device is enabled
|
117
160
|
def check_i2c_enabled(i2c)
|
118
161
|
raise ArgumentError, "i2c not enabled #{i2c.to_s}" unless get_i2c_status(i2c)
|
119
162
|
end
|
120
163
|
|
164
|
+
# lock i2c device
|
121
165
|
def lock_i2c(i2c)
|
122
166
|
check_i2c_enabled(i2c)
|
123
167
|
mutex = get_i2c_status(i2c, :mutex)
|
@@ -127,6 +171,7 @@ module Beaglebone
|
|
127
171
|
end
|
128
172
|
end
|
129
173
|
|
174
|
+
# i2c hash getter
|
130
175
|
def get_i2c_status(i2c, key = nil)
|
131
176
|
i2cmutex.synchronize do
|
132
177
|
if key
|
@@ -137,6 +182,7 @@ module Beaglebone
|
|
137
182
|
end
|
138
183
|
end
|
139
184
|
|
185
|
+
# i2c hash setter
|
140
186
|
def set_i2c_status(i2c, key, value)
|
141
187
|
i2cmutex.synchronize do
|
142
188
|
i2cstatus[i2c] ||= {}
|
@@ -144,6 +190,7 @@ module Beaglebone
|
|
144
190
|
end
|
145
191
|
end
|
146
192
|
|
193
|
+
# i2c hash delete
|
147
194
|
def delete_i2c_status(i2c, key = nil)
|
148
195
|
i2cmutex.synchronize do
|
149
196
|
if key.nil?
|
@@ -157,30 +204,57 @@ module Beaglebone
|
|
157
204
|
end
|
158
205
|
end
|
159
206
|
|
160
|
-
#
|
207
|
+
# Object Oriented I2C Implementation.
|
208
|
+
# This treats the I2C device as an object.
|
161
209
|
class I2CDevice
|
162
|
-
|
210
|
+
# Initialize an I2C device. Returns an I2CDevice object
|
211
|
+
#
|
212
|
+
# @param i2c should be a symbol representing the I2C device
|
213
|
+
#
|
214
|
+
# @example
|
215
|
+
# i2c = I2CDevice.new(:I2C2)
|
163
216
|
def initialize(i2c)
|
164
217
|
@i2c = i2c
|
165
218
|
I2C::setup(@i2c)
|
166
219
|
end
|
167
220
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
221
|
+
# Write data to an I2C device
|
222
|
+
#
|
223
|
+
# @param address the address of the slave device
|
224
|
+
# @param data the data to write
|
225
|
+
#
|
226
|
+
# @return Integer the number of bytes written
|
227
|
+
#
|
228
|
+
# @example
|
229
|
+
# i2c.write(0x1e, [0x00, 0b10010000].pack("C*") )
|
172
230
|
def write(address, data)
|
173
231
|
I2C::write(@i2c, address, data)
|
174
232
|
end
|
175
233
|
|
234
|
+
# Read data from an I2C device
|
235
|
+
#
|
236
|
+
# @param address the address of the slave device
|
237
|
+
# @param bytes bytes to read
|
238
|
+
# @param register optional register to read from
|
239
|
+
#
|
240
|
+
# @example
|
241
|
+
# # read 3 big endian signed shorts starting at register 0x03
|
242
|
+
# data = i2c.read(0x1e, 6, [0x03].pack("C*"))
|
243
|
+
# x,z,y = raw.unpack("s>*")
|
244
|
+
def read(address, bytes=1, register=nil)
|
245
|
+
I2C::read(@i2c, address, bytes, register)
|
246
|
+
end
|
247
|
+
|
248
|
+
# Disable the specified I2C device.
|
249
|
+
#
|
250
|
+
# @note device trees cannot be unloaded at this time without kernel panic.
|
176
251
|
def disable
|
177
252
|
I2C::disable(@i2c)
|
178
253
|
end
|
179
254
|
|
255
|
+
# Return the file descriptor to the open I2C device
|
180
256
|
def file
|
181
257
|
I2C::file(@i2c)
|
182
258
|
end
|
183
|
-
|
184
259
|
end
|
185
|
-
|
186
260
|
end
|