matrix_creator 0.0.0 → 1.0.0

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.
@@ -0,0 +1,67 @@
1
+ module MatrixCreator
2
+ module Everloop
3
+ ##
4
+ # Class to be inherited by Animations
5
+ class Animation
6
+ # Interval between animation updates in milliseconds
7
+ ANIMATION_SPEED = 0.1 # 100ms
8
+
9
+ ##
10
+ # Run a block of code while displaying an animation on the Everloop driver
11
+ #
12
+ # @param color [Hash] with the rgb+w values for the color
13
+ # @yield Block of code to be executed while displaying the spinner
14
+ # @return response from the block of code execution
15
+ #
16
+ # @example Run a block of code and display a spinner until it finishes
17
+ #
18
+ # MatrixCreator::Everloop::Spinner.run {
19
+ # // Do Something
20
+ # }
21
+ #
22
+ # @example Run a block of code and display a green spinner until it finishes
23
+ #
24
+ # color = MatrixCreator::Everloop::Color::GREEN
25
+ #
26
+ # MatrixCreator::Everloop::Spinner.run(color) {
27
+ # // Do Something
28
+ # }
29
+ #
30
+ # @example Run a block of code and display a pulse until it finishes
31
+ #
32
+ # MatrixCreator::Everloop::Pulse.run {
33
+ # // Do Something
34
+ # }
35
+ #
36
+ def self.run(color = Color::WHITE, &block)
37
+ result = nil
38
+
39
+ code_thread = Thread.new do
40
+ Thread.current[:finished] = false
41
+ result = yield if block
42
+ Thread.current[:finished] = true
43
+ end
44
+
45
+ animation_thread = Thread.new do
46
+ animation = new(color, code_thread)
47
+ animation.loop_animation
48
+ animation.destroy_context
49
+ end
50
+
51
+ # Turn off the leds on the Everloop driver
52
+ code_thread.join
53
+ animation_thread.join
54
+ Everloop.modify_color(Color::OFF)
55
+
56
+ # Return result of the code block
57
+ result
58
+ end
59
+
60
+ ##
61
+ # Sends a request to destroy the context of the everloop comm instance
62
+ def destroy_context
63
+ @everloop_comm.destroy
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,63 @@
1
+ module MatrixCreator
2
+ module Everloop
3
+ ##
4
+ # Contains the constants to be used for RGB+W Color values
5
+ module Color
6
+ # Contains the hash values for the color white
7
+ # @return [Hash] rgb+w values for this color
8
+ WHITE = { r: 0, g: 0, b: 0, w: 10 }.freeze
9
+
10
+ # Contains the hash values for the color red
11
+ # @return [Hash] rgb+w values for this color
12
+ RED = { r: 10, g: 0, b: 0, w: 0 }.freeze
13
+
14
+ # Contains the hash values for the color orange
15
+ # @return [Hash] rgb+w values for this color
16
+ ORANGE = { r: 10, g: 2, b: 0, w: 0 }.freeze
17
+
18
+ # Contains the hash values for the color yellow
19
+ # @return [Hash] rgb+w values for this color
20
+ YELLOW = { r: 10, g: 5, b: 0, w: 0 }.freeze
21
+
22
+ # Contains the hash values for the color spring green
23
+ # @return [Hash] rgb+w values for this color
24
+ SPRING_GREEN = { r: 8, g: 10, b: 0, w: 0 }.freeze
25
+
26
+ # Contains the hash values for the color green
27
+ # @return [Hash] rgb+w values for this color
28
+ GREEN = { r: 0, g: 10, b: 0, w: 0 }.freeze
29
+
30
+ # Contains the hash values for the color turquoise
31
+ # @return [Hash] rgb+w values for this color
32
+ TURQUOISE = { r: 0, g: 10, b: 2, w: 0 }.freeze
33
+
34
+ # Contains the hash values for the color cyan
35
+ # @return [Hash] rgb+w values for this color
36
+ CYAN = { r: 0, g: 10, b: 10, w: 0 }.freeze
37
+
38
+ # Contains the hash values for the color ocean
39
+ # @return [Hash] rgb+w values for this color
40
+ OCEAN = { r: 0, g: 6, b: 10, w: 0 }.freeze
41
+
42
+ # Contains the hash values for the color blue
43
+ # @return [Hash] rgb+w values for this color
44
+ BLUE = { r: 0, g: 0, b: 10, w: 0 }.freeze
45
+
46
+ # Contains the hash values for the color violet
47
+ # @return [Hash] rgb+w values for this color
48
+ VIOLET = { r: 4, g: 0, b: 10, w: 0 }.freeze
49
+
50
+ # Contains the hash values for the color magenta
51
+ # @return [Hash] rgb+w values for this color
52
+ MAGENTA = { r: 10, g: 0, b: 10, w: 0 }.freeze
53
+
54
+ # Contains the hash values for the color raspberry
55
+ # @return [Hash] rgb+w values for this color
56
+ RASPBERRY = { r: 10, g: 0, b: 3, w: 0 }.freeze
57
+
58
+ # Contains the hash values to turn off the Led
59
+ # @return [Hash] rgb+w values for this color
60
+ OFF = { r: 0, g: 0, b: 0, w: 0 }.freeze
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,62 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/everloop/animation'
3
+
4
+ module MatrixCreator
5
+ module Everloop
6
+ ##
7
+ # Pulse animation class
8
+ class Pulse < Animation
9
+ ##
10
+ # Initializes the variables on the instance to prepare for the
11
+ # loop animation
12
+ #
13
+ # @param color [Hash] with the rgb+w values for the color
14
+ # @param code_thread [Thread] instance with main thread
15
+ def initialize(color, code_thread)
16
+ @everloop_comm = MatrixCreator::Comm.new(BASE_PORT)
17
+ @code_thread = code_thread
18
+ @intensity = 0
19
+ @intensity_next_value = 1
20
+
21
+ # Generate everloop messages
22
+ @everloop_msgs = (0..10).map do |msg_intensity|
23
+ image = (1..35).map do
24
+ MatrixMalos::LedValue.new(
25
+ red: ((color[:r] / 10) * msg_intensity).round,
26
+ green: ((color[:g] / 10) * msg_intensity).round,
27
+ blue: ((color[:b] / 10) * msg_intensity).round,
28
+ white: ((color[:w] / 10) * msg_intensity).round
29
+ )
30
+ end
31
+
32
+ MatrixMalos::DriverConfig.new(
33
+ image: MatrixMalos::EverloopImage.new(led: image)
34
+ )
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Loop animation until main code thread finishes
40
+ def loop_animation
41
+ loop do
42
+ @everloop_comm.send_configuration(@everloop_msgs[@intensity])
43
+
44
+ @intensity += @intensity_next_value
45
+
46
+ # Pulse intensity behavior
47
+ if @intensity == 11
48
+ @intensity = 10
49
+ @intensity_next_value = -1
50
+ elsif @intensity == -1
51
+ @intensity = 0
52
+ @intensity_next_value = 1
53
+ end
54
+
55
+ sleep(ANIMATION_SPEED)
56
+
57
+ break if @code_thread[:finished]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,52 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/everloop/animation'
3
+
4
+ module MatrixCreator
5
+ module Everloop
6
+ ##
7
+ # Spinner animation class
8
+ class Spinner < Animation
9
+ ##
10
+ # Initializes the variables on the instance to prepare for the
11
+ # loop animation
12
+ #
13
+ # @param color [Hash] with the rgb+w values for the color
14
+ # @param code_thread [Thread] instance with main thread
15
+ def initialize(color, code_thread)
16
+ @everloop_comm = MatrixCreator::Comm.new(BASE_PORT)
17
+ @code_thread = code_thread
18
+
19
+ # Generating array of led messages
20
+ @led_array = (1..35).map do |i|
21
+ if i <= 5
22
+ MatrixMalos::LedValue.new(
23
+ red: (color[:r] * i * 2) / 10,
24
+ green: (color[:g] * i * 2) / 10,
25
+ blue: (color[:b] * i * 2) / 10,
26
+ white: (color[:w] * i * 2) / 10
27
+ )
28
+ else
29
+ MatrixMalos::LedValue.new(red: 0, green: 0, blue: 0, white: 0)
30
+ end
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Loop animation until main code thread finishes
36
+ def loop_animation
37
+ loop do
38
+ everloop_image = MatrixMalos::EverloopImage.new(led: @led_array)
39
+ msg = MatrixMalos::DriverConfig.new(image: everloop_image)
40
+ @everloop_comm.send_configuration(msg)
41
+
42
+ sleep(ANIMATION_SPEED)
43
+
44
+ break if @code_thread[:finished]
45
+
46
+ # Rotate the 5 led instances order in the array
47
+ @led_array.rotate!(-1)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,48 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/driver_base'
3
+
4
+ module MatrixCreator
5
+ # Module: Humidity
6
+ #
7
+ # Communicate with the Humidity driver
8
+ module Humidity
9
+ # Configuration values for the Humidity driver
10
+ HUMIDITY_CONFIG = MatrixCreator.settings[:devices][:humidity]
11
+
12
+ # Base port to send data to Humidity driver
13
+ BASE_PORT = HUMIDITY_CONFIG[:port]
14
+
15
+ ##
16
+ # Detects and returns information from the Humidity driver
17
+ #
18
+ # @param options [Hash] of keys and values that can contain speed, max_resp and/or max_secs
19
+ # @return [Array] elements detected in JSON format
20
+ #
21
+ # @example Detect 3 values for the Humidity driver
22
+ # MatrixCreator::Humidity.detect(max_resp: 3)
23
+ #
24
+ # @example Detect values for the Humidity driver for 30 seconds
25
+ # MatrixCreator::Humidity.detect(max_secs: 30)
26
+ #
27
+ # @example Detect values for the Humidity driver with a speed of 0.5 seconds per response
28
+ # MatrixCreator::Humidity.detect(max_secs: 30, speed: 0.5)
29
+ #
30
+ # @example Detect values for the Humidity driver for 15 seconds and process data when received
31
+ # MatrixCreator::Humidity.detect(max_resp: 10){ |data|
32
+ # // Do something with the data
33
+ # }
34
+ #
35
+ def self.detect(options = {}, &block)
36
+ MatrixCreator::DriverBase.detect(BASE_PORT, MatrixMalos::Humidity, options, block)
37
+ end
38
+
39
+ ##
40
+ # Detects one response from the Humidity driver and returns its value
41
+ #
42
+ # @return [Hash] object with the Humidity response values
43
+ #
44
+ def self.detect_once
45
+ detect(max_resp: 1).first
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/driver_base'
3
+
4
+ module MatrixCreator
5
+ # Module: IMU
6
+ #
7
+ # Communicate with the IMU driver
8
+ module Imu
9
+ # Configuration values for the IMU driver
10
+ IMU_CONFIG = MatrixCreator.settings[:devices][:imu]
11
+
12
+ # Base port to send data to IMU driver
13
+ BASE_PORT = IMU_CONFIG[:port]
14
+
15
+ ##
16
+ # Detects and returns information from the IMU driver
17
+ #
18
+ # @param options [Hash] of keys and values that can contain speed, max_resp and/or max_secs
19
+ # @return [Array] elements detected in JSON format
20
+ #
21
+ # @example Detect 3 values for the IMU driver
22
+ # MatrixCreator::Imu.detect(max_resp: 3)
23
+ #
24
+ # @example Detect values for the IMU driver for 30 seconds
25
+ # MatrixCreator::Imu.detect(max_secs: 30)
26
+ #
27
+ # @example Detect values for the IMU driver with a speed of 0.5 seconds per response
28
+ # MatrixCreator::Imu.detect(max_secs: 30, speed: 0.5)
29
+ #
30
+ # @example Detect values for the IMU driver for 15 seconds and process data when received
31
+ # MatrixCreator::Imu.detect(max_resp: 10){ |data|
32
+ # // Do something with the data
33
+ # }
34
+ #
35
+ def self.detect(options = {}, &block)
36
+ MatrixCreator::DriverBase.detect(BASE_PORT, MatrixMalos::Imu, options, block)
37
+ end
38
+
39
+ ##
40
+ # Detects one response from the IMU driver and returns its value
41
+ #
42
+ # @return [Hash] object with the IMU response values
43
+ #
44
+ def self.detect_once
45
+ detect(max_resp: 1).first
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/driver_base'
3
+
4
+ module MatrixCreator
5
+ # Module: Pressure
6
+ #
7
+ # Communicate with the Pressure driver
8
+ module Pressure
9
+ # Configuration values for the Pressure driver
10
+ PRESSURE_CONFIG = MatrixCreator.settings[:devices][:pressure]
11
+
12
+ # Base port to send data to Pressure driver
13
+ BASE_PORT = PRESSURE_CONFIG[:port]
14
+
15
+ ##
16
+ # Detects and returns information from the Pressure driver
17
+ #
18
+ # @param options [Hash] of keys and values that can contain speed, max_resp and/or max_secs
19
+ # @return [Array] elements detected in JSON format
20
+ #
21
+ # @example Detect 3 values for the Pressure driver
22
+ # MatrixCreator::Pressure.detect(max_resp: 3)
23
+ #
24
+ # @example Detect values for the Pressure driver for 30 seconds
25
+ # MatrixCreator::Pressure.detect(max_secs: 30)
26
+ #
27
+ # @example Detect values for the Pressure driver with a speed of 0.5 seconds per response
28
+ # MatrixCreator::Pressure.detect(max_secs: 30, speed: 0.5)
29
+ #
30
+ # @example Detect values for the Pressure driver for 15 seconds and process data when received
31
+ # MatrixCreator::Pressure.detect(max_resp: 10){ |data|
32
+ # // Do something with the data
33
+ # }
34
+ #
35
+ def self.detect(options = {}, &block)
36
+ MatrixCreator::DriverBase.detect(BASE_PORT, MatrixMalos::Pressure, options, block)
37
+ end
38
+
39
+ ##
40
+ # Detects one response from the Pressure driver and returns its value
41
+ #
42
+ # @return [Hash] object with the Pressure response values
43
+ #
44
+ def self.detect_once
45
+ detect(max_resp: 1).first
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ # Load Dependencies
2
+ require 'matrix_creator/driver_base'
3
+
4
+ module MatrixCreator
5
+ # Module: UV
6
+ #
7
+ # Communicate with the UV driver
8
+ module Uv
9
+ # Configuration values for the UV driver
10
+ UV_CONFIG = MatrixCreator.settings[:devices][:uv]
11
+
12
+ # Base port to send data to UV driver
13
+ BASE_PORT = UV_CONFIG[:port]
14
+
15
+ ##
16
+ # Detects and returns information from the UV driver
17
+ #
18
+ # @param options [Hash] of keys and values that can contain speed, max_resp and/or max_secs
19
+ # @return [Array] elements detected in JSON format
20
+ #
21
+ # @example Detect 3 values for the UV driver
22
+ # MatrixCreator::Uv.detect(max_resp: 3)
23
+ #
24
+ # @example Detect values for the UV driver for 30 seconds
25
+ # MatrixCreator::Uv.detect(max_secs: 30)
26
+ #
27
+ # @example Detect values for the UV driver with a speed of 0.5 seconds per response
28
+ # MatrixCreator::Uv.detect(max_secs: 30, speed: 0.5)
29
+ #
30
+ # @example Detect values for the UV driver for 15 seconds and process data when received
31
+ # MatrixCreator::Uv.detect(max_resp: 10){ |data|
32
+ # // Do something with the data
33
+ # }
34
+ #
35
+ def self.detect(options = {}, &block)
36
+ MatrixCreator::DriverBase.detect(BASE_PORT, MatrixMalos::UV, options, block)
37
+ end
38
+
39
+ ##
40
+ # Detects one response from the UV driver and returns its value
41
+ #
42
+ # @return [Hash] object with the UV response values
43
+ #
44
+ def self.detect_once
45
+ detect(max_resp: 1).first
46
+ end
47
+ end
48
+ end