pedalog 0.1.3

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.
Files changed (2) hide show
  1. data/lib/pedalog.rb +159 -0
  2. metadata +72 -0
data/lib/pedalog.rb ADDED
@@ -0,0 +1,159 @@
1
+ # pedalog.rb
2
+ #
3
+ # Copyright (c) 2011 Dan Haughey (http://www.powwow-pedal-power.org.uk)
4
+ #
5
+ # This file is part of libpedalog-ruby.
6
+ #
7
+ # libpedalog-ruby is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # libpedalog-ruby is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with libpedalog-ruby. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'rubygems'
21
+ require 'ffi'
22
+
23
+ module Pedalog
24
+
25
+ # Error codes
26
+ PEDALOG_OK = 0
27
+ PEDALOG_ERROR_UNKNOWN = 1
28
+ PEDALOG_ERROR_NO_DEVICE_FOUND = 2
29
+ PEDALOG_ERROR_FAILED_TO_OPEN = 3
30
+ PEDALOG_ERROR_BAD_RESPONSE = 4
31
+ PEDALOG_ERROR_DEVICE_BUSY = 5
32
+ PEDALOG_ERROR_OUT_OF_MEMORY = 6
33
+
34
+ class Device
35
+ attr_accessor :serial
36
+
37
+ # Finds all the connected Pedalog devices, and returns an array of Device instances to describe them.
38
+ def self.find_all
39
+ max_devices = Interface::pedalog_get_max_devices
40
+
41
+ device = FFI::MemoryPointer.new(Interface::PedalogDevice, max_devices, false)
42
+ devices = max_devices.times.collect do |i|
43
+ Interface::PedalogDevice.new(device + i * Interface::PedalogDevice.size)
44
+ end
45
+
46
+ device_count = Interface::pedalog_find_devices(device)
47
+
48
+ result = device_count.times.collect do |i|
49
+ devices[i].to_native
50
+ end
51
+
52
+ device.free
53
+
54
+ result
55
+ end
56
+
57
+ # Gets a string describing an error code.
58
+ def self.get_error_message(error)
59
+ max_error_message = Interface::pedalog_get_max_error_message
60
+
61
+ ptr = FFI::MemoryPointer.new(:char, max_error_message, false)
62
+
63
+ Interface::pedalog_get_error_message(error, ptr)
64
+
65
+ message = ptr.read_string
66
+
67
+ ptr.free
68
+ message
69
+ end
70
+
71
+ # Returns a Data instance with this device's current values, or nil if the device has been disconnected.
72
+ # If nil is returned, Device.find_all should be called again to update the list of connected devices.
73
+ def read_data
74
+ device = Interface::PedalogDevice.new
75
+ device.from_native(self)
76
+
77
+ data = Interface::PedalogData.new
78
+
79
+ result = Interface::pedalog_read_data(device, data)
80
+
81
+ return nil if result == PEDALOG_ERROR_NO_DEVICE_FOUND
82
+ throw Device.get_error_message(result) unless result == PEDALOG_OK
83
+
84
+ data.to_native
85
+ end
86
+ end
87
+
88
+ # A class to hold the data returned from a Pedalog device.
89
+ class Data
90
+ attr_accessor :voltage
91
+ attr_accessor :current
92
+ attr_accessor :power
93
+ attr_accessor :energy
94
+ attr_accessor :max_power
95
+ attr_accessor :avg_power
96
+ attr_accessor :time
97
+
98
+ # Creates a string listing the data in an easily-readable format.
99
+ def readable_string
100
+ "voltage: %f, current: %f, power: %f, energy: %f, max_power: %f, avg_power: %f, time: %d\n" % [ voltage, current, power, energy, max_power, avg_power, time ]
101
+ end
102
+ end
103
+
104
+ # Module containing methods that directly interface with libpedalog
105
+ module Interface
106
+ extend FFI::Library
107
+
108
+ class PedalogDevice < FFI::Struct
109
+ layout :id, :int
110
+
111
+ def to_native
112
+ device = Device.new
113
+
114
+ device.serial = self[:id]
115
+
116
+ device
117
+ end
118
+
119
+ def from_native(device)
120
+ self[:id] = device.serial
121
+ end
122
+ end
123
+
124
+ class PedalogData < FFI::Struct
125
+ layout :voltage, :double,
126
+ :current, :double,
127
+ :power, :double,
128
+ :energy, :double,
129
+ :max_power, :double,
130
+ :avg_power, :double,
131
+ :time, :int
132
+
133
+ def to_native
134
+ data = Data.new
135
+
136
+ data.voltage = self[:voltage]
137
+ data.current = self[:current]
138
+ data.power = self[:power]
139
+ data.energy = self[:energy]
140
+ data.max_power = self[:max_power]
141
+ data.avg_power = self[:avg_power]
142
+ data.time = self[:time]
143
+
144
+ data
145
+ end
146
+ end
147
+
148
+ ffi_lib 'libpedalog'
149
+
150
+ attach_function :pedalog_init, [ ], :int
151
+ attach_function :pedalog_get_max_devices, [ ], :int
152
+ attach_function :pedalog_get_max_error_message, [ ], :int
153
+ attach_function :pedalog_find_devices, [ :pointer ], :int
154
+ attach_function :pedalog_read_data, [ :pointer, :pointer ], :int
155
+ attach_function :pedalog_get_error_message, [ :int, :pointer ], :void
156
+ end
157
+ end
158
+
159
+ Pedalog::Interface::pedalog_init
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pedalog
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 3
10
+ version: 0.1.3
11
+ platform: ruby
12
+ authors:
13
+ - Dan Haughey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-16 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: |-
23
+ This is a simple tool for reading data from Pedalog devices. It relies on the libpedalog library to do the actual
24
+ communication with the device: https://github.com/greenlynx/libpedalog
25
+
26
+ The Pedalog is a device made by Renewable Energy Innovations (http://www.re-innovation.co.uk).
27
+ It has been specially designed to monitor the power and energy generated by pedal powered electricity generators.
28
+ email: http://www.powwow-pedal-power.org.uk
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/pedalog.rb
37
+ has_rdoc: true
38
+ homepage: https://github.com/greenlynx/libpedalog
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Ruby wrapper to the libpedalog library
71
+ test_files: []
72
+