bonekit 0.0.3-arm-linux → 0.0.4-arm-linux
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 +8 -8
- data/README.md +9 -2
- data/examples/analog/photo_resistor.rb +31 -0
- data/examples/analog/potentiometer.rb +18 -0
- data/examples/devices/compass.rb +17 -0
- data/examples/devices/i2c_device.rb +32 -0
- data/ext/bonekit/hmc5883l.c +43 -52
- data/ext/bonekit/hmc5883l.h +5 -27
- data/ext/bonekit/i2c.c +79 -0
- data/ext/bonekit/i2c.h +65 -0
- data/ext/bonekit/i2c_class.c +133 -0
- data/ext/bonekit/i2c_class.h +33 -0
- data/ext/bonekit/rbinit.c +1 -0
- data/lib/bonekit/bonekit.so +0 -0
- data/lib/bonekit/version.rb +1 -1
- data/spec/compass_spec.rb +17 -0
- data/spec/i2c_spec.rb +30 -0
- data/test/unit/i2c_test.rb +30 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NjI5NDRkNjgwMzIyYWI2NTQ3ZjVmMDE5YzIzZmVhZjM4NWVhZjcyNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YWNjMWNiMmE3N2Y4MGZlNjAzNGVjMTdmMTNiMDZlZmY2MjlkNGUwMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjgyOTkxNjFmZTc2Yjg2YjIyZjg2ZTE0ZDUwODFhOGFhYzczYWQ4YTM5Njgy
|
10
|
+
MWQzZmRlMzUzZTRjYTI5MWI2YTExNTVmYjE2ZGU0Yzk2ODg5Njk4Y2M5ODI2
|
11
|
+
NTQ2NjAwZjBkODk1OWY4ZDBjNzdjYTc2YzYwNDU5ZWUxNTNhN2Q=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGI1NGU4YWU0ZDYyNTBiNDZmODk4YmQyNTRmNjQ4OTMyNTJlOWQ1ZmRmZDYw
|
14
|
+
ZGMwMDY3NGJjMTEyZWQ0OTk2MTA4OTFjZjRjOWQ4ODc0N2RhMzlkMmE5NDJl
|
15
|
+
NGY1MmFjYmQyYjgyMDdkZTMxMjViYjVlNjRlMjM4ODZmZjRmZTg=
|
data/README.md
CHANGED
@@ -23,7 +23,7 @@ or build and install:
|
|
23
23
|
```
|
24
24
|
bundle
|
25
25
|
rake
|
26
|
-
gem install --local pkg/bonekit-0.0.
|
26
|
+
gem install --local pkg/bonekit-0.0.4.gem
|
27
27
|
```
|
28
28
|
|
29
29
|
## Example
|
@@ -65,6 +65,13 @@ analog_value = pin.analog_value # Read analog value (0.0 to 1.0)
|
|
65
65
|
pin = Pin.new P9_42 # Pulse-Width Modulation pin
|
66
66
|
pin.analog_value = 0.3 # Write analog value (0.0 to 1.0)
|
67
67
|
```
|
68
|
+
__I2C__
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
device = I2c.new address
|
72
|
+
device.read(1) # read 1 byte
|
73
|
+
device.write([0x03,0x00]) # write 2 bytes
|
74
|
+
```
|
68
75
|
|
69
76
|
__Devices (ICs)__
|
70
77
|
|
@@ -78,7 +85,7 @@ heading = compass.heading # degrees
|
|
78
85
|
* Digital Input/Output (implemented)
|
79
86
|
* Analog Input (implemented)
|
80
87
|
* Analog Output (implemented)
|
81
|
-
* I2C (
|
88
|
+
* I2C (implemented)
|
82
89
|
* SPI (planned)
|
83
90
|
* Interrupts (planned)
|
84
91
|
* Serial Communication (planned)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Photo resistor - Simple test of the functionality of the photo resistor
|
2
|
+
#
|
3
|
+
# Connect the photoresistor one leg to pin P9-39 and other leg to pin P9-32 (VDD_ADC)
|
4
|
+
# Connect a resistor (around 10k is a good value, higher values gives higher readings)
|
5
|
+
# from pin P9-39 to pin P9-34 (GND_ADC).
|
6
|
+
#
|
7
|
+
# Circuit:
|
8
|
+
#
|
9
|
+
# Photo Resistor 10K
|
10
|
+
# +1.8 (P9-32) o---/\/\/----.----/\/\/---o GND_ADC (P9-34)
|
11
|
+
# |
|
12
|
+
# AIN0 (P9-39) o------------+
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'bonekit'
|
16
|
+
|
17
|
+
photo_resistor = Pin.new P9_39 # AIN0
|
18
|
+
|
19
|
+
light_readings = [0.0]
|
20
|
+
light_threshold = 0.05 # Log all changes above threshold
|
21
|
+
|
22
|
+
loop do
|
23
|
+
light_reading = photo_resistor.analog_value
|
24
|
+
|
25
|
+
if (light_reading - light_readings.last).abs > light_threshold
|
26
|
+
light_readings << light_reading
|
27
|
+
puts "Light is at #{(light_reading*100.0).round}%"
|
28
|
+
end
|
29
|
+
|
30
|
+
sleep(0.1)
|
31
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Potentiometer
|
2
|
+
#
|
3
|
+
# This example shows how to control the brightness of an LED using a potentiometer.
|
4
|
+
#
|
5
|
+
# The circuit:
|
6
|
+
# * LED with 180 ohm resistor attached from pin P9_42 to DGND.
|
7
|
+
# * Potentiometer attached to pin P9_39, VDD_ADC and GNDA_ADC.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'bonekit'
|
11
|
+
|
12
|
+
led = Pin.new P9_42 # LED connected to a pin that supports PWM
|
13
|
+
pot = Pin.new P9_39 # Potentiometer connected to an ADC pin
|
14
|
+
|
15
|
+
loop do
|
16
|
+
led.analog_value = pot.analog_value
|
17
|
+
sleep(0.01) # 100Hz refresh rate
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Compass - Simple test of the functionality of the HMC5883L 3-Axis Digital Compass IC
|
2
|
+
#
|
3
|
+
# Connections:
|
4
|
+
# VCC to pin P9-4 (VDD_3V3)
|
5
|
+
# GND to pin P9-2 (DGND)
|
6
|
+
# SCL to P9_19 (I2C2_SCL)
|
7
|
+
# SDA to P9_20 (I2C2_SDA)
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'bonekit'
|
11
|
+
|
12
|
+
compass = HMC5883L.new
|
13
|
+
|
14
|
+
loop do
|
15
|
+
puts "Heading: #{compass.heading} degrees"
|
16
|
+
sleep(0.1)
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Device - Simple test of using HMC5883L I2C interface
|
2
|
+
#
|
3
|
+
# Connections:
|
4
|
+
# VCC to pin P9-4 (VDD_3V3)
|
5
|
+
# GND to pin P9-2 (DGND)
|
6
|
+
# SCL to P9_19 (I2C2_SCL)
|
7
|
+
# SDA to P9_20 (I2C2_SDA)
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'bonekit'
|
11
|
+
|
12
|
+
DeviceAddress = 0x1e
|
13
|
+
|
14
|
+
ModeRegister = 0x02
|
15
|
+
ReadRegister = 0x03
|
16
|
+
ModeContinuous = 0x00
|
17
|
+
|
18
|
+
device = I2c.new DeviceAddress
|
19
|
+
|
20
|
+
device.write([ModeRegister, ModeContinuous])
|
21
|
+
|
22
|
+
loop do
|
23
|
+
device.write([ReadRegister])
|
24
|
+
raw = device.read(6)
|
25
|
+
x = (raw[0] << 8 | raw[1])
|
26
|
+
z = (raw[2] << 8 | raw[3])
|
27
|
+
y = (raw[4] << 8 | raw[5])
|
28
|
+
axis = [x,y,z]
|
29
|
+
puts "axis #{axis}"
|
30
|
+
|
31
|
+
sleep(1)
|
32
|
+
end
|
data/ext/bonekit/hmc5883l.c
CHANGED
@@ -27,44 +27,24 @@
|
|
27
27
|
|
28
28
|
#include "hmc5883l.h"
|
29
29
|
|
30
|
-
#define
|
30
|
+
#define HMC5883L_Address 0x1e
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
fprintf(stderr, "%s not present\n", name);
|
37
|
-
exit(1);
|
38
|
-
}
|
39
|
-
}
|
32
|
+
#define HMC5883L_Register_ConfigurationA 0x00
|
33
|
+
#define HMC5883L_Register_ConfigurationB 0x01
|
34
|
+
#define HMC5883L_Register_Mode 0x02
|
35
|
+
#define HMC5883L_Register_DataBegin 0x03
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
buf[0]=reg;
|
45
|
-
buf[1]=val;
|
46
|
-
|
47
|
-
if (write(fd, buf, 2) != 2)
|
48
|
-
{
|
49
|
-
fprintf(stderr, "Can't write to device\n");
|
50
|
-
}
|
51
|
-
}
|
37
|
+
#define HMC5883L_Mode_Measurement_Continuous 0x00
|
38
|
+
#define HMC5883L_Mode_Measurement_SingleShot 0x01
|
39
|
+
#define HMC5883L_Mode_Measurement_Idle 0x03
|
52
40
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
else
|
61
|
-
{
|
62
|
-
if(read(fd, buf, len) != len)
|
63
|
-
{
|
64
|
-
//error
|
65
|
-
}
|
66
|
-
}
|
67
|
-
}
|
41
|
+
#define ErrorCode_1 "Scalenotvalid"//Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1"
|
42
|
+
#define ErrorCode_1_Num 1
|
43
|
+
|
44
|
+
#include <math.h>
|
45
|
+
|
46
|
+
#define M_180_div_PI 57.2957795131
|
47
|
+
#define M_2PI 6.28318530718
|
68
48
|
|
69
49
|
hmc5883l_t * hmc5883l_create()
|
70
50
|
{
|
@@ -72,16 +52,15 @@ hmc5883l_t * hmc5883l_create()
|
|
72
52
|
obj = malloc(sizeof(struct hmc5883l_s));
|
73
53
|
if(obj)
|
74
54
|
{
|
75
|
-
obj->_scale = 1;
|
55
|
+
//obj->_scale = 1;
|
56
|
+
|
57
|
+
// Initialize
|
58
|
+
obj->_i2c = i2c_alloc();
|
59
|
+
i2c_init(obj->_i2c, HMC5883L_Address);
|
76
60
|
|
77
|
-
|
78
|
-
{
|
79
|
-
|
80
|
-
//printf("HMC5883L::Failed to open i2c bus\n");
|
81
|
-
}
|
82
|
-
|
83
|
-
selectDevice(obj->_fd, HMC5883L_Address, HMC5883L_Name);
|
84
|
-
writeToDevice(obj->_fd, ModeRegister, Measurement_Continuous);
|
61
|
+
// Setup mode
|
62
|
+
uint8_t setup[2] = {HMC5883L_Register_Mode, HMC5883L_Mode_Measurement_Continuous};
|
63
|
+
i2c_write(obj->_i2c, setup, 2);
|
85
64
|
}
|
86
65
|
|
87
66
|
return obj;
|
@@ -89,19 +68,31 @@ hmc5883l_t * hmc5883l_create()
|
|
89
68
|
|
90
69
|
void hmc5883l_destroy(hmc5883l_t * obj)
|
91
70
|
{
|
92
|
-
|
71
|
+
if(obj)
|
72
|
+
{
|
73
|
+
i2c_destroy(obj->_i2c);
|
74
|
+
free(obj);
|
75
|
+
}
|
93
76
|
}
|
94
77
|
|
95
78
|
float hmc5883l_heading(hmc5883l_t * obj)
|
96
79
|
{
|
97
|
-
|
98
|
-
|
80
|
+
uint8_t write_data[1] = {HMC5883L_Register_DataBegin};
|
81
|
+
uint8_t read_data[6];
|
82
|
+
|
83
|
+
i2c_write(obj->_i2c, write_data, 1); // check if success before read
|
84
|
+
i2c_read(obj->_i2c, read_data, 6); // check if success before calculate heading
|
85
|
+
|
86
|
+
short x = (read_data[0] << 8) | read_data[1];
|
87
|
+
short y = (read_data[4] << 8) | read_data[5];
|
88
|
+
short z = (read_data[2] << 8) | read_data[3];
|
99
89
|
|
100
|
-
|
101
|
-
short y = (buf[4] << 8) | buf[5];
|
102
|
-
short z = (buf[2] << 8) | buf[3];
|
90
|
+
float angle_radians = atan2(y,x);
|
103
91
|
|
104
|
-
|
92
|
+
if(angle_radians < 0.0)
|
93
|
+
angle_radians += M_2PI;
|
94
|
+
|
95
|
+
float angle_degrees = angle_radians * M_180_div_PI;
|
105
96
|
|
106
|
-
return
|
97
|
+
return angle_degrees;
|
107
98
|
}
|
data/ext/bonekit/hmc5883l.h
CHANGED
@@ -31,32 +31,8 @@
|
|
31
31
|
#ifdef __cplusplus
|
32
32
|
extern "C" {
|
33
33
|
#endif
|
34
|
-
|
35
|
-
#include
|
36
|
-
#include <stdlib.h>
|
37
|
-
#include <stdint.h>
|
38
|
-
#include <fcntl.h>
|
39
|
-
#include <unistd.h>
|
40
|
-
#include <string.h>
|
41
|
-
#include <sys/ioctl.h>
|
42
|
-
#include <sys/types.h>
|
43
|
-
#include <sys/stat.h>
|
44
|
-
#include <linux/i2c-dev.h>
|
45
|
-
#include <math.h>
|
46
|
-
|
47
|
-
#define HMC5883L_Address 0x1E
|
48
|
-
#define HMC5883L_Name "HMC5883L"
|
49
|
-
#define ConfigurationRegisterA 0x00
|
50
|
-
#define ConfigurationRegisterB 0x01
|
51
|
-
#define ModeRegister 0x02
|
52
|
-
#define DataRegisterBegin 0x03
|
53
|
-
|
54
|
-
#define Measurement_Continuous 0x00
|
55
|
-
#define Measurement_SingleShot 0x01
|
56
|
-
#define Measurement_Idle 0x03
|
57
|
-
|
58
|
-
#define ErrorCode_1 "Scalenotvalid"//Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1"
|
59
|
-
#define ErrorCode_1_Num 1
|
34
|
+
|
35
|
+
#include "i2c.h"
|
60
36
|
|
61
37
|
typedef struct
|
62
38
|
{
|
@@ -74,9 +50,11 @@ typedef struct
|
|
74
50
|
|
75
51
|
struct hmc5883l_s
|
76
52
|
{
|
77
|
-
float _scale;
|
53
|
+
//float _scale;
|
78
54
|
float _fd;
|
79
55
|
float _buffer[32];
|
56
|
+
|
57
|
+
i2c_t * _i2c;
|
80
58
|
};
|
81
59
|
|
82
60
|
typedef struct hmc5883l_s hmc5883l_t;
|
data/ext/bonekit/i2c.c
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
i2c.c
|
4
|
+
BoneKit
|
5
|
+
|
6
|
+
Copyright (cc) 2012 Luis Laugga.
|
7
|
+
Some rights reserved, all wrongs deserved.
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
11
|
+
the Software without restriction, including without limitation the rights to
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
14
|
+
subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
*/
|
27
|
+
|
28
|
+
#include "i2c.h"
|
29
|
+
|
30
|
+
i2c_t * i2c_alloc()
|
31
|
+
{
|
32
|
+
i2c_t * obj;
|
33
|
+
obj = calloc(1, sizeof(struct i2c_s));
|
34
|
+
return obj;
|
35
|
+
}
|
36
|
+
|
37
|
+
int i2c_init(i2c_t * obj, int addr)
|
38
|
+
{
|
39
|
+
if(obj)
|
40
|
+
{
|
41
|
+
// open the i2c bus
|
42
|
+
if ((obj->_fd = open("/dev/i2c-1", O_RDWR)) < 0)
|
43
|
+
{
|
44
|
+
printf("Failed to open /dev/i2c-1 bus.");
|
45
|
+
exit(1); // ERROR HANDLING check errno to see what went wrong
|
46
|
+
}
|
47
|
+
|
48
|
+
if (ioctl(obj->_fd, I2C_SLAVE, addr) < 0)
|
49
|
+
{
|
50
|
+
printf("Failed to acquire bus access to i2c slave.\n");
|
51
|
+
exit(1); // ERROR HANDLING check errno to see what went wrong
|
52
|
+
}
|
53
|
+
|
54
|
+
obj->_addr = addr;
|
55
|
+
}
|
56
|
+
|
57
|
+
return 0;
|
58
|
+
}
|
59
|
+
|
60
|
+
void i2c_destroy(i2c_t * obj)
|
61
|
+
{
|
62
|
+
if(obj)
|
63
|
+
{
|
64
|
+
if(obj->_fd)
|
65
|
+
close(obj->_fd);
|
66
|
+
|
67
|
+
free(obj);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
int i2c_read(i2c_t * obj, uint8_t * buffer, int length)
|
72
|
+
{
|
73
|
+
return read(obj->_fd, buffer, length);
|
74
|
+
}
|
75
|
+
|
76
|
+
int i2c_write(i2c_t * obj, uint8_t * buffer, int length)
|
77
|
+
{
|
78
|
+
return write(obj->_fd, buffer, length);
|
79
|
+
}
|
data/ext/bonekit/i2c.h
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
i2c.h
|
4
|
+
BoneKit
|
5
|
+
|
6
|
+
Copyright (cc) 2012 Luis Laugga.
|
7
|
+
Some rights reserved, all wrongs deserved.
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
11
|
+
the Software without restriction, including without limitation the rights to
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
14
|
+
subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
*/
|
27
|
+
|
28
|
+
#ifndef BONEKIT_I2C_H__
|
29
|
+
#define BONEKIT_I2C_H__
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
extern "C" {
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#include <errno.h>
|
36
|
+
#include <string.h>
|
37
|
+
#include <stdio.h>
|
38
|
+
#include <stdlib.h>
|
39
|
+
#include <stdint.h>
|
40
|
+
#include <unistd.h>
|
41
|
+
#include <linux/i2c-dev.h>
|
42
|
+
#include <sys/ioctl.h>
|
43
|
+
#include <sys/types.h>
|
44
|
+
#include <sys/stat.h>
|
45
|
+
#include <fcntl.h>
|
46
|
+
|
47
|
+
struct i2c_s
|
48
|
+
{
|
49
|
+
int _fd;
|
50
|
+
int _addr;
|
51
|
+
};
|
52
|
+
|
53
|
+
typedef struct i2c_s i2c_t;
|
54
|
+
|
55
|
+
i2c_t * i2c_alloc();
|
56
|
+
int i2c_init(i2c_t *, int);
|
57
|
+
void i2c_destroy(i2c_t *);
|
58
|
+
int i2c_read(i2c_t *, uint8_t *, int);
|
59
|
+
int i2c_write(i2c_t *, uint8_t *, int);
|
60
|
+
|
61
|
+
#ifdef __cplusplus
|
62
|
+
}
|
63
|
+
#endif
|
64
|
+
|
65
|
+
#endif
|
@@ -0,0 +1,133 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
i2c_class.c
|
4
|
+
BoneKit
|
5
|
+
|
6
|
+
Copyright (cc) 2012 Luis Laugga.
|
7
|
+
Some rights reserved, all wrongs deserved.
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
11
|
+
the Software without restriction, including without limitation the rights to
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
14
|
+
subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include "i2c_class.h"
|
30
|
+
|
31
|
+
#include "ruby.h"
|
32
|
+
|
33
|
+
#include "beaglebone.h"
|
34
|
+
#include "i2c.h"
|
35
|
+
|
36
|
+
VALUE cBoneKit_I2c;
|
37
|
+
|
38
|
+
static void I2c_free(i2c_t * ptr)
|
39
|
+
{
|
40
|
+
if(ptr == NULL)
|
41
|
+
return;
|
42
|
+
|
43
|
+
i2c_destroy(ptr);
|
44
|
+
}
|
45
|
+
|
46
|
+
static VALUE I2c_alloc(VALUE class)
|
47
|
+
{
|
48
|
+
i2c_t * ptr = i2c_alloc();
|
49
|
+
|
50
|
+
VALUE self = Data_Wrap_Struct(class, 0, I2c_free, ptr);
|
51
|
+
return self;
|
52
|
+
}
|
53
|
+
|
54
|
+
/*
|
55
|
+
* call-seq:
|
56
|
+
* initialize(address) -> I2c
|
57
|
+
*
|
58
|
+
* Returns a new I2c object associated with a given address
|
59
|
+
* @param [Integer] address the i2c slave address
|
60
|
+
*/
|
61
|
+
static VALUE I2c_initialize(int argc, VALUE* argv, VALUE self)
|
62
|
+
{
|
63
|
+
if (argc > 1 || argc == 0) // there should only 1 argument
|
64
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
|
65
|
+
|
66
|
+
i2c_t * ptr;
|
67
|
+
Data_Get_Struct(self, i2c_t, ptr);
|
68
|
+
|
69
|
+
int address = NUM2INT(argv[0]);
|
70
|
+
|
71
|
+
if(i2c_init(ptr, address) < 0) // check i2c bus and device address
|
72
|
+
rb_raise(rb_eArgError, "I2c bus error");
|
73
|
+
|
74
|
+
return self;
|
75
|
+
}
|
76
|
+
|
77
|
+
/*
|
78
|
+
* call-seq:
|
79
|
+
* read(n) -> Array
|
80
|
+
*
|
81
|
+
* Reads n bytes from the device.
|
82
|
+
*/
|
83
|
+
static VALUE I2c_read(VALUE self, VALUE nBytes)
|
84
|
+
{
|
85
|
+
i2c_t * ptr;
|
86
|
+
Data_Get_Struct(self, i2c_t, ptr);
|
87
|
+
|
88
|
+
int n = NUM2INT(nBytes);
|
89
|
+
uint8_t * buffer = (uint8_t *)calloc(n, sizeof(uint8_t));
|
90
|
+
i2c_read(ptr, buffer, n);
|
91
|
+
|
92
|
+
VALUE bytes = rb_ary_new2(n);
|
93
|
+
|
94
|
+
int i;
|
95
|
+
for(i=0;i<n;++i)
|
96
|
+
rb_ary_push(bytes, INT2FIX(buffer[i]));
|
97
|
+
|
98
|
+
return bytes;
|
99
|
+
}
|
100
|
+
|
101
|
+
/*
|
102
|
+
* call-seq:
|
103
|
+
* write(Array) -> n
|
104
|
+
*
|
105
|
+
* Write the bytes in Array to the device.
|
106
|
+
*/
|
107
|
+
static VALUE I2c_write(VALUE self, VALUE bytes)
|
108
|
+
{
|
109
|
+
i2c_t * ptr;
|
110
|
+
Data_Get_Struct(self, i2c_t, ptr);
|
111
|
+
|
112
|
+
int n = RARRAY_LEN(bytes);
|
113
|
+
uint8_t * buffer = (uint8_t *)calloc(n, sizeof(uint8_t));
|
114
|
+
|
115
|
+
int i;
|
116
|
+
for(i=0;i<n;++i)
|
117
|
+
buffer[i] = NUM2INT(rb_ary_entry(bytes, i));
|
118
|
+
|
119
|
+
VALUE nBytes = INT2FIX(i2c_write(ptr, buffer, n));
|
120
|
+
|
121
|
+
return nBytes;
|
122
|
+
}
|
123
|
+
|
124
|
+
void BoneKit_I2c_class_init()
|
125
|
+
{
|
126
|
+
cBoneKit_I2c = rb_define_class("I2c", rb_cObject);
|
127
|
+
|
128
|
+
rb_define_alloc_func(cBoneKit_I2c, I2c_alloc);
|
129
|
+
|
130
|
+
rb_define_method(cBoneKit_I2c, "initialize", I2c_initialize, -1);
|
131
|
+
rb_define_method(cBoneKit_I2c, "read", I2c_read, 1);
|
132
|
+
rb_define_method(cBoneKit_I2c, "write", I2c_write, 1);
|
133
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
i2c_class.h
|
4
|
+
BoneKit
|
5
|
+
|
6
|
+
Copyright (cc) 2012 Luis Laugga.
|
7
|
+
Some rights reserved, all wrongs deserved.
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
11
|
+
the Software without restriction, including without limitation the rights to
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
14
|
+
subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
*/
|
27
|
+
|
28
|
+
#ifndef BONEKIT_I2C_CLASS_H__
|
29
|
+
#define BONEKIT_I2C_CLASS_H__
|
30
|
+
|
31
|
+
void BoneKit_I2c_class_init();
|
32
|
+
|
33
|
+
#endif
|
data/ext/bonekit/rbinit.c
CHANGED
data/lib/bonekit/bonekit.so
CHANGED
Binary file
|
data/lib/bonekit/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HMC5883L do
|
4
|
+
|
5
|
+
describe "heading" do
|
6
|
+
|
7
|
+
let :compass do
|
8
|
+
HMC5883L.new
|
9
|
+
end
|
10
|
+
|
11
|
+
context "when default" do
|
12
|
+
it "returns float in degrees" do
|
13
|
+
compass.heading.class.should eq(Float)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/i2c_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe I2c do
|
4
|
+
|
5
|
+
describe "read" do
|
6
|
+
|
7
|
+
let :i2c do
|
8
|
+
I2c.new 0x1e
|
9
|
+
end
|
10
|
+
|
11
|
+
context "when default" do
|
12
|
+
it "returns read bytes array" do
|
13
|
+
pin.read(1).should be > 0
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "write" do
|
19
|
+
|
20
|
+
let :i2c do
|
21
|
+
I2c.new 0x1e
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when default" do
|
25
|
+
it "returns number of write bytes" do
|
26
|
+
pin.write([0x00]).should eq(1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class I2cTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialization
|
12
|
+
assert_not_nil(I2c.new 0x1e)
|
13
|
+
assert_nil(I2c.new 0x00)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_read
|
17
|
+
i2c = I2c.new 0x1e
|
18
|
+
|
19
|
+
assert_not_nil(i2c)
|
20
|
+
assert_not_nil(i2c.read(1))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_write
|
24
|
+
i2c = I2c.new 0x1e
|
25
|
+
|
26
|
+
assert_not_nil(i2c)
|
27
|
+
assert_equal(1, i2c.write([0x00]))
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bonekit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: arm-linux
|
6
6
|
authors:
|
7
7
|
- Luis Laugga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,9 +96,13 @@ files:
|
|
96
96
|
- bonekit.gemspec
|
97
97
|
- examples/analog/analog_read.rb
|
98
98
|
- examples/analog/fading.rb
|
99
|
+
- examples/analog/photo_resistor.rb
|
100
|
+
- examples/analog/potentiometer.rb
|
99
101
|
- examples/basics/blink.rb
|
100
102
|
- examples/basics/digital_read.rb
|
101
103
|
- examples/basics/switch.rb
|
104
|
+
- examples/devices/compass.rb
|
105
|
+
- examples/devices/i2c_device.rb
|
102
106
|
- ext/bonekit/adc.c
|
103
107
|
- ext/bonekit/adc.h
|
104
108
|
- ext/bonekit/beaglebone.c
|
@@ -112,6 +116,10 @@ files:
|
|
112
116
|
- ext/bonekit/hmc5883l.h
|
113
117
|
- ext/bonekit/hmc5883l_class.c
|
114
118
|
- ext/bonekit/hmc5883l_class.h
|
119
|
+
- ext/bonekit/i2c.c
|
120
|
+
- ext/bonekit/i2c.h
|
121
|
+
- ext/bonekit/i2c_class.c
|
122
|
+
- ext/bonekit/i2c_class.h
|
115
123
|
- ext/bonekit/pin.c
|
116
124
|
- ext/bonekit/pin.h
|
117
125
|
- ext/bonekit/pin_class.c
|
@@ -121,11 +129,14 @@ files:
|
|
121
129
|
- ext/bonekit/rbinit.c
|
122
130
|
- lib/bonekit.rb
|
123
131
|
- lib/bonekit/version.rb
|
132
|
+
- spec/compass_spec.rb
|
133
|
+
- spec/i2c_spec.rb
|
124
134
|
- spec/pin_spec.rb
|
125
135
|
- spec/spec_helper.rb
|
126
136
|
- test/bonekit-c/beaglebone_test.c
|
127
137
|
- test/bonekit-c/pin_test.c
|
128
138
|
- test/test_helper.rb
|
139
|
+
- test/unit/i2c_test.rb
|
129
140
|
- test/unit/pin_adc_test.rb
|
130
141
|
- test/unit/pin_gpio_test.rb
|
131
142
|
- test/unit/pin_pwm_test.rb
|
@@ -155,11 +166,14 @@ signing_key:
|
|
155
166
|
specification_version: 4
|
156
167
|
summary: Physical interaction toolkit for the beaglebone.
|
157
168
|
test_files:
|
169
|
+
- spec/compass_spec.rb
|
170
|
+
- spec/i2c_spec.rb
|
158
171
|
- spec/pin_spec.rb
|
159
172
|
- spec/spec_helper.rb
|
160
173
|
- test/bonekit-c/beaglebone_test.c
|
161
174
|
- test/bonekit-c/pin_test.c
|
162
175
|
- test/test_helper.rb
|
176
|
+
- test/unit/i2c_test.rb
|
163
177
|
- test/unit/pin_adc_test.rb
|
164
178
|
- test/unit/pin_gpio_test.rb
|
165
179
|
- test/unit/pin_pwm_test.rb
|