pixoo 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fc65707994af8e5635fe01b03090f0a250b071ed72a526fe00fddff767c5bc07
4
+ data.tar.gz: '09201de550befea118058074b74977f9641c6b5883da5461df724d42ba075853'
5
+ SHA512:
6
+ metadata.gz: de0492ce16d867309a80009e2c7ae69818d8dd252844f7040369ae9c27861f74e71c96024972f2c109aaa3244fa2bd0f63020e755bfddb88811d7d0fbfb325e3
7
+ data.tar.gz: 033d67a4d1d48c59a7450284d44d8e4840fce26a66e8312a5757aea682021efc28fc3dcff170b63d4e952fa0b47949563e7150471e2ddbde1cdde03269d1eb08
@@ -0,0 +1,77 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to make participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies within all project spaces, and it also applies when
49
+ an individual is representing the project or its community in public spaces.
50
+ Examples of representing a project or community include using an official
51
+ project e-mail address, posting via an official social media account, or acting
52
+ as an appointed representative at an online or offline event. Representation of
53
+ a project may be further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at aaron.patterson at gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
77
+
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2021 Aaron Patterson
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Pixoo sign client for Ruby
2
+
3
+ This is just a client for the [Pixoo 64 sign](https://divoom.com/products/pixoo-64) written in Ruby.
4
+
5
+ ## Example usage
6
+
7
+ Draw some stuff and then display it as an animated image:
8
+
9
+ ```ruby
10
+ include Pixoo::Utils
11
+
12
+ images = [7, 17, 29].map { |m|
13
+ img = Pixoo::ImageBuffer.new 64, speed: 500
14
+ 64.times do |x|
15
+ 64.times do |y|
16
+ if ((x | y) % m).zero?
17
+ img.set_pixel x, y, *(rainbow_color(x))
18
+ end
19
+ end
20
+ end
21
+ img
22
+ }
23
+
24
+ dev = Pixoo::Client.find_all.first
25
+ dev.send_animation images
26
+ sleep 1
27
+
28
+ # add some text
29
+ dev.send_text "Hello", x: 0, y: 0, id: 1
30
+ dev.send_text "World", x: 0, y: 40, id: 2, font: 6
31
+ ```
32
+
33
+ Read a PNG and display it:
34
+
35
+ ```ruby
36
+ img = Pixoo::ImageBuffer.from_png ARGV[0]
37
+
38
+ dev = Pixoo::Client.find_all.first
39
+ dev.send_animation [img]
40
+ ```
41
+
42
+ ### Reading from a remote server
43
+
44
+ This thing supports reading values from a remote server.
45
+ I have a server that collects PM2.5 / CO2 sensors I have in the house.
46
+ On the server, I have a webrick server that response to requests for sensor info.
47
+ For example to get the PM2.5 data from my office, I can do this:
48
+
49
+ ```
50
+ $ curl 'http://tender.home:9292/reading?floor=main&room=office&measurement=pm2.5'
51
+ {"DispData":"25"}
52
+ ```
53
+
54
+ The response is important because this display will parse the response and display it on the sign.
55
+ I've configured the sign as follows:
56
+
57
+
58
+ ```ruby
59
+ dev = Pixoo::Client.find_all.first
60
+
61
+ label = dev.new_display_item(id: 1,
62
+ type: Pixoo::DisplayItem::TEXT_MESSAGE,
63
+ x: 0,
64
+ y: 0,
65
+ dir: 0,
66
+ font: 18,
67
+ width: 64,
68
+ height: 5,
69
+ string: "PM2-5:",
70
+ speed: 10,
71
+ color: "#FF0000",
72
+ align: 1)
73
+
74
+ value = dev.new_display_item(id: 2,
75
+ type: Pixoo::DisplayItem::NET_TEXT_MESSAGE,
76
+ x: 23,
77
+ y: 0,
78
+ dir: 0,
79
+ font: 18,
80
+ width: 64,
81
+ height: 5,
82
+ string: "http://tender.home:9292/reading?floor=main&room=office&measurement=pm2.5",
83
+ speed: 10,
84
+ color: "#FFFFFF",
85
+ align: 1)
86
+ ```
87
+
88
+ The sign will automatically fetch data from my server and update the display.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "rake/testtask"
2
+ require "bundler"
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ t.warning = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,186 @@
1
+ require "zlib"
2
+
3
+ module Pixoo
4
+ class ImageBuffer
5
+ PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack('C*')
6
+ private_constant :PNG_SIGNATURE
7
+
8
+ IHDR = Struct.new(:width, :height, :bit_depth, :color_type)
9
+ private_constant :IHDR
10
+
11
+ def self.from_png filename
12
+ File.open(filename, "rb") { |f| parse_png f }
13
+ end
14
+
15
+ def self.parse_png file
16
+ # Check PNG signature
17
+ sig = file.read(8)
18
+ raise "Not a PNG file" unless sig == PNG_SIGNATURE
19
+
20
+ # Read chunks
21
+ idat_data = "".b
22
+
23
+ ihdr = nil
24
+
25
+ loop do
26
+ length = file.read(4).unpack1('N') # Big-endian 32-bit
27
+ type = file.read(4)
28
+ data = file.read(length)
29
+ file.read(4) # We ignore CRC validation
30
+
31
+ case type
32
+ when 'IHDR'
33
+ ihdr = parse_ihdr(data)
34
+ when 'IDAT'
35
+ idat_data << data
36
+ when 'IEND'
37
+ break
38
+ end
39
+ end
40
+
41
+ if ihdr.height != 64 && ihdr.width != 64
42
+ raise "unsupported size height: #{ihdr.height} #{ihdr.width}"
43
+ end
44
+
45
+ # Decompress and unfilter
46
+ buffer = decompress_and_unfilter(ihdr, idat_data)
47
+ new(ihdr.height, buffer)
48
+ end
49
+ private_class_method :parse_png
50
+
51
+ def self.parse_ihdr(data)
52
+ width, height, bit_depth, color_type = data.unpack('NNC2')
53
+
54
+ # Only support bit depth 8
55
+ raise "Only bit depth 8 supported" unless bit_depth == 8
56
+
57
+ # Only support RGB (2) and RGBA (6)
58
+ raise "Only RGB/RGBA supported" unless [2, 6].include?(color_type)
59
+
60
+ # Check compression, filter, interlace (should be 0, 0, 0)
61
+ compression, filter, interlace = data.unpack('@10C3')
62
+ raise "Unsupported compression" unless compression == 0
63
+ raise "Unsupported filter method" unless filter == 0
64
+ raise "Interlacing not supported" unless interlace == 0
65
+
66
+ IHDR.new(width, height, bit_depth, color_type)
67
+ end
68
+ private_class_method :parse_ihdr
69
+
70
+ def self.decompress_and_unfilter ihdr, idat_data
71
+ output_buffer = "".b
72
+
73
+ # Decompress with zlib
74
+ raw_data = Zlib::Inflate.inflate(idat_data)
75
+
76
+ # Calculate bytes per pixel
77
+ bytes_per_pixel = ihdr.color_type == 2 ? 3 : 4 # RGB = 3, RGBA = 4
78
+ scanline_length = ihdr.width * bytes_per_pixel
79
+
80
+ # Unfilter each scanline
81
+ previous_scanline = "\0".b * scanline_length
82
+ pos = 0
83
+
84
+ ihdr.height.times do
85
+ filter_type = raw_data.getbyte(pos)
86
+ pos += 1
87
+
88
+ current_scanline = raw_data.byteslice(pos, scanline_length)
89
+
90
+ pos += scanline_length
91
+
92
+ # Unfilter the scanline
93
+ unfiltered = unfilter_scanline(filter_type, current_scanline,
94
+ previous_scanline, bytes_per_pixel)
95
+
96
+ (unfiltered.bytesize / bytes_per_pixel).times do |i|
97
+ # We don't care about the `a` channel
98
+ output_buffer << unfiltered.byteslice(i * bytes_per_pixel, 3)
99
+ end
100
+
101
+ previous_scanline = unfiltered
102
+ end
103
+
104
+ output_buffer
105
+ end
106
+ private_class_method :decompress_and_unfilter
107
+
108
+ def self.unfilter_scanline filter_type, current, previous, bpp
109
+ result = "\0".b * current.bytesize
110
+
111
+ current.bytesize.times do |i|
112
+ a = i >= bpp ? result.getbyte(i - bpp) : 0
113
+ b = previous.getbyte(i)
114
+ c = i >= bpp ? previous.getbyte(i - bpp) : 0
115
+
116
+ byte = current.getbyte(i)
117
+ newbyte = case filter_type
118
+ when 0 # None
119
+ byte
120
+ when 1 # Sub
121
+ (byte + a) & 0xFF
122
+ when 2 # Up
123
+ (byte + b) & 0xFF
124
+ when 3 # Average
125
+ (byte + ((a + b) / 2).floor) & 0xFF
126
+ when 4 # Paeth
127
+ (byte + paeth_predictor(a, b, c)) & 0xFF
128
+ else
129
+ raise "Unknown filter type: #{filter_type}"
130
+ end
131
+
132
+ result.setbyte(i, newbyte)
133
+ end
134
+
135
+ result
136
+ end
137
+ private_class_method :unfilter_scanline
138
+
139
+ def self.paeth_predictor a, b, c
140
+ p = a + b - c
141
+ pa = (p - a).abs
142
+ pb = (p - b).abs
143
+ pc = (p - c).abs
144
+
145
+ if pa <= pb && pa <= pc
146
+ a
147
+ elsif pb <= pc
148
+ b
149
+ else
150
+ c
151
+ end
152
+ end
153
+ private_class_method :paeth_predictor
154
+
155
+ attr_reader :size, :speed
156
+
157
+ def initialize size, buffer = "\0".b * (size * size * 3), speed: 100
158
+ raise "wrong buffer" unless buffer.bytesize == (size * size * 3)
159
+ @size = size
160
+ @speed = speed
161
+ @buffer = buffer
162
+ end
163
+
164
+ def set_pixel row, column, r, g, b
165
+ raise ArgumentError if row > (@size - 1) || column > (@size - 1)
166
+
167
+ row_offset = row * (@size * 3)
168
+ offset = row_offset + (3 * column)
169
+ @buffer.setbyte(offset, r)
170
+ @buffer.setbyte(offset + 1, g)
171
+ @buffer.setbyte(offset + 2, b)
172
+ end
173
+
174
+ def get_pixel row, column
175
+ raise ArgumentError if row > (@size - 1) || column > (@size - 1)
176
+
177
+ row_offset = row * (@size * 3)
178
+ byte_offset = 3 * column
179
+ @buffer.unpack("CCC", offset: row_offset + byte_offset)
180
+ end
181
+
182
+ def encoded_data
183
+ Base64.strict_encode64 @buffer
184
+ end
185
+ end
186
+ end
data/lib/pixoo.rb ADDED
@@ -0,0 +1,335 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "base64"
4
+
5
+ module Pixoo
6
+ autoload :ImageBuffer, "pixoo/image_buffer"
7
+
8
+ class Client
9
+ def self.find_all
10
+ http = Net::HTTP.new("app.divoom-gz.com", 443)
11
+ http.use_ssl = true
12
+ response = http.request(Net::HTTP::Post.new("/Device/ReturnSameLANDevice"))
13
+ JSON.parse(response.body)["DeviceList"].map {
14
+ new(_1["DevicePrivateIP"], name: _1["DeviceName"], id: _1["DeviceId"], mac: _1["DeviceMac"])
15
+ }
16
+ end
17
+
18
+ attr_reader :ip
19
+
20
+ def initialize ip, name: nil, id: nil, mac: nil
21
+ @ip = ip
22
+ @name = name
23
+ @id = id
24
+ @mac = mac
25
+ end
26
+
27
+ def settings
28
+ post({ "Command" => "Channel/GetAllConf" })
29
+ end
30
+
31
+ def time
32
+ post({ "Command" => "Device/GetDeviceTime" })
33
+ end
34
+
35
+ def clear_all_text
36
+ post({ "Command" => "Draw/ClearHttpText" })
37
+ end
38
+
39
+ def channel
40
+ post({ "Command" => "Channel/GetIndex" })
41
+ end
42
+
43
+ def set_weather_area lat, lon
44
+ post({ "Command" => "Sys/LogAndLat", "Longitude" => lon, "Latitude" => lat })
45
+ end
46
+
47
+ def set_brightness number
48
+ raise ArgumentError if number < 0 || number > 100
49
+ post({ "Command" => "Channel/SetBrightness", "Brightness" => number })
50
+ end
51
+
52
+ def countdown min, sec, start
53
+ post({ "Command" => "Tools/SetTimer", "Minute" => min, "Second" => sec, "Status" => (start ? 1 : 0) })
54
+ end
55
+
56
+ def stopwatch cmd
57
+ number = case cmd
58
+ when :start then 1
59
+ when :stop then 0
60
+ when :reset then 2
61
+ else
62
+ raise ArgumentError
63
+ end
64
+ post({ "Command" => "Tools/SetStopWatch", "Status" => number })
65
+ end
66
+
67
+ def scoreboard blue:, red:
68
+ post({ "Command" => "Tools/SetScoreBoard", "BlueScore" => blue, "RedScore" => red })
69
+ end
70
+
71
+ def noise status
72
+ number = case status
73
+ when :start then 1
74
+ when :stop then 0
75
+ else
76
+ raise ArgumentError
77
+ end
78
+ post({ "Command" => "Tools/SetNoiseStatus", "NoiseStatus" => number })
79
+ end
80
+
81
+ ##
82
+ # Set the channel:
83
+ #
84
+ # 0: Faces
85
+ # 1: Cloud Channel
86
+ # 2: Visualizer
87
+ # 3: Custom
88
+ # 4: Black Screen
89
+ def set_channel number
90
+ raise ArgumentError if number < 0 || number > 4
91
+ post({ "Command" => "Channel/SetIndex", "SelectIndex" => number })
92
+ end
93
+
94
+ def select_custom_page number
95
+ raise ArgumentError if number < 0 || number > 2
96
+ post({ "Command" => "Channel/SetCustomPageIndex", "CustomPageIndex" => number })
97
+ end
98
+
99
+ def send_animation images, id: current_image_id
100
+ images.each_with_index do |img, i|
101
+ res = post({ "Command" => "Draw/SendHttpGif",
102
+ "PicNum" => images.length,
103
+ "PicWidth" => img.size,
104
+ "PicOffset" => i,
105
+ "PicSpeed" => img.speed,
106
+ "PicID" => id,
107
+ "PicData" => img.encoded_data
108
+ })
109
+ raise unless res["error_code"].zero?
110
+ end
111
+ end
112
+
113
+ def reset_images
114
+ post({ "Command" => "Draw/ResetHttpGifId" })
115
+ end
116
+
117
+ def current_image_id
118
+ post({ "Command" => "Draw/GetHttpGifId" })["PicId"]
119
+ end
120
+
121
+ ##
122
+ # Send text to the sign.
123
+ #
124
+ # "text_width" is "number of pixels the text can use, wide"
125
+ # "font" can only be 0 - 7
126
+ # "align": 1 left, 2 middle, 3 right
127
+ def send_text text, x:, y:, dir: 0, font: 0, text_width: 64, color: "#FFFFFF", align: 1, id: 1, scroll_speed: 20
128
+ post({
129
+ "Command" => "Draw/SendHttpText",
130
+ "LcdId" => 0,
131
+ "TextId" => id,
132
+ "x" => x,
133
+ "y" => y,
134
+ "dir" => dir,
135
+ "font" => font,
136
+ "TextWidth" => text_width,
137
+ "speed" => scroll_speed,
138
+ "TextString" => text,
139
+ "color" => color,
140
+ "align" => align
141
+ })
142
+ end
143
+
144
+ def new_display_item id:, type:, x:, y:, dir:, font:, width:, height:, string: nil, speed:, color:, update_time: nil, align: 1
145
+ DisplayItem.new id, type, x, y, dir, font, width, height, string, speed, color, update_time, align
146
+ end
147
+
148
+ def send_display_list items
149
+ post({ "Command" => "Draw/SendHttpItemList", "ItemList" => items.map(&:to_json_hash) })
150
+ end
151
+
152
+ private
153
+
154
+ def post data
155
+ http = Net::HTTP.new(ip, 80)
156
+ req = Net::HTTP::Post.new("/post")
157
+ req.body = JSON.dump data
158
+ response = http.request(req)
159
+ JSON.parse(response.body)
160
+ end
161
+ end
162
+
163
+ module Utils
164
+ def hsv_to_rgb h, s, v
165
+ # h: 0.0 to 1.0 (hue)
166
+ # s: 0.0 to 1.0 (saturation)
167
+ # v: 0.0 to 1.0 (value/brightness)
168
+
169
+ c = v * s
170
+ x = c * (1 - ((h * 6) % 2 - 1).abs)
171
+ m = v - c
172
+
173
+ r, g, b = case (h * 6).floor
174
+ when 0 then [c, x, 0]
175
+ when 1 then [x, c, 0]
176
+ when 2 then [0, c, x]
177
+ when 3 then [0, x, c]
178
+ when 4 then [x, 0, c]
179
+ when 5 then [c, 0, x]
180
+ else [0, 0, 0]
181
+ end
182
+
183
+ [(r + m) * 255, (g + m) * 255, (b + m) * 255].map(&:round)
184
+ end
185
+ module_function :hsv_to_rgb
186
+
187
+ def rainbow_color row, total_rows = 64
188
+ # Map row to hue: 0 (red) to 0.75 (violet)
189
+ # 0.75 = 270 degrees, stops before wrapping back to red
190
+ hue = row.to_f / (total_rows - 1) * 0.75
191
+ hsv_to_rgb(hue, 1.0, 1.0)
192
+ end
193
+ module_function :rainbow_color
194
+ end
195
+
196
+ class DisplayItem
197
+ SECOND = 1 # sceocnd , font should include digit
198
+ MIN = 2 # min, font should include digit
199
+ HOUR = 3 # hour, font should include digit
200
+ TIME_AM_PM = 4 # am or pm, font should include a,m,p
201
+ HOUR_MIN = 5 # hour:min , font should include digit
202
+ HOUR_MIN_SEC = 6 # hour:min:sec, , font should include digit
203
+ YEAR = 7 # year,, font should include digit
204
+ DAY = 8 # day, font should include digit
205
+ MON = 9 # month, font should include digit
206
+ MON_YEAR = 10 # mon-year, font should include digit
207
+ ENG_MONTH_DOT_DAY = 11# month, font should include english letters
208
+ DATE_WEEK_YEAR = 12 # day:month:year, font should include digit
209
+ ENG_WEEK = 13 # /weekday-"SU","MO","TU","WE","TH","FR","SA", font should include english letters
210
+ ENG_WEEK_THREE = 14 # weekday-"SUN","MON","TUE","WED","THU","FRI","SAT", font should include english letters
211
+ ENG_WEEK_ALL = 15 # weekday-"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY", font should include english letters
212
+ ENG_MON = 16 # month-"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC", font should include english letters
213
+ TEMP_DIGIT = 17 # temperature, font should include digit and c,f
214
+ TODAY_MAX_TEMP = 18 # the max temperature, font should include digit and c,f
215
+ TODAY_MIN_TEMP = 19 # the min temperature, font should include digit and c,f
216
+ WEATHER_WORD = 20 # the weather, font should include english letters
217
+ NOISE_DIGIT = 21 # the nosie value, font should include digit
218
+ TEXT_MESSAGE = 22 # the text string, font should include text information
219
+ NET_TEXT_MESSAGE = 23 # the url request string, font should include url information, respone should be json encode including the "DispData" string element, eg:http:# appin.divoom-gz.com/Device/ReturnCurrentDate?test=0 repone {"DispData": "2022-01-22 13:51:56"}
220
+
221
+ def initialize id, type, x, y, dir, font, width, height, string, speed, color, update_time, align
222
+ raise ArgumentError unless id.positive?
223
+
224
+ @id = id
225
+ @type = type
226
+ @x = x
227
+ @y = y
228
+ @dir = dir
229
+ @font = font
230
+ @width = width
231
+ @height = height
232
+ @string = string
233
+ @speed = speed
234
+ @color = color
235
+ @update_time = update_time
236
+ @align = align
237
+ end
238
+
239
+ def to_json_hash
240
+ hash = {
241
+ "TextId" => @id,
242
+ "type" => @type,
243
+ "x" => @x,
244
+ "y" => @y,
245
+ "dir" => @dir,
246
+ "font" => @font,
247
+ "TextWidth" => @width,
248
+ "Textheight" => @height,
249
+ "speed" => @speed,
250
+ "align" => @align,
251
+ "color" => @color
252
+ }
253
+ hash["TextString"] = @string if @string
254
+ hash["update_time"] = @update_time if @update_time
255
+ hash
256
+ end
257
+ end
258
+ end
259
+
260
+ if $0 == __FILE__
261
+ p Pixoo::Client.find_all
262
+ dev = Pixoo::Client.find_all.first
263
+
264
+ #img = Pixoo::ImageBuffer.from_png ARGV[0]
265
+ #dev.send_animation [img]
266
+ #sleep 1
267
+
268
+ #item = dev.new_display_item(id: 1,
269
+ # type: Pixoo::DisplayItem::HOUR_MIN_SEC,
270
+ # x: 0,
271
+ # y: 0,
272
+ # dir: 0,
273
+ # font: 18,
274
+ # width: 64,
275
+ # height: 13,
276
+ # speed: 20,
277
+ # color: "#FFFFFF",
278
+ # align: 2)
279
+
280
+ label = dev.new_display_item(id: 1,
281
+ type: Pixoo::DisplayItem::TEXT_MESSAGE,
282
+ x: 0,
283
+ y: 0,
284
+ dir: 0,
285
+ font: 18,
286
+ width: 64,
287
+ height: 5,
288
+ string: "PM2-5:",
289
+ speed: 10,
290
+ color: "#FF0000",
291
+ align: 1)
292
+
293
+ value = dev.new_display_item(id: 2,
294
+ type: Pixoo::DisplayItem::NET_TEXT_MESSAGE,
295
+ x: 23,
296
+ y: 0,
297
+ dir: 0,
298
+ font: 18,
299
+ width: 64,
300
+ height: 5,
301
+ string: "http://tender.home:9292/reading?floor=main&room=office&measurement=pm2.5",
302
+ speed: 10,
303
+ color: "#FFFFFF",
304
+ align: 1)
305
+
306
+ label2 = dev.new_display_item(id: 3,
307
+ type: Pixoo::DisplayItem::TEXT_MESSAGE,
308
+ x: 0,
309
+ y: 6,
310
+ dir: 0,
311
+ font: 18,
312
+ width: 64,
313
+ height: 5,
314
+ string: "CO2:",
315
+ speed: 10,
316
+ color: "#FF0000",
317
+ align: 1)
318
+
319
+ co2v = dev.new_display_item(id: 4,
320
+ type: Pixoo::DisplayItem::NET_TEXT_MESSAGE,
321
+ x: 23,
322
+ y: 6,
323
+ dir: 0,
324
+ font: 18,
325
+ width: 64,
326
+ height: 5,
327
+ string: "http://tender.home:9292/reading?floor=main&room=office&measurement=co2",
328
+ speed: 10,
329
+ color: "#FFFFFF",
330
+ align: 1)
331
+
332
+
333
+ #dev.clear_all_text
334
+ p dev.send_display_list [label, value, label2, co2v]
335
+ end
data/pixoo.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "pixoo"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["Aaron Patterson"]
7
+ spec.email = "tenderlove@ruby-lang.org"
8
+
9
+ spec.summary = "A Ruby client for Pixoo displays"
10
+ spec.description = "This is just a Ruby client for Pixoo displays"
11
+ spec.homepage = "https://github.com/tenderlove/pixoo-rb"
12
+ spec.license = "Apache-2.0"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.test_files = spec.files.grep(%r{^test/})
16
+
17
+ spec.required_ruby_version = ">= 3.0.0"
18
+
19
+ spec.add_dependency "base64", ">= 0.3.0"
20
+ spec.add_dependency "json", ">= 2.18.0"
21
+ spec.add_dependency "net-http", ">= 0.9.1"
22
+ spec.add_dependency "zlib", ">= 3.2.2"
23
+
24
+ # Development dependencies
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "rake", "~> 13.0"
27
+ end
@@ -0,0 +1,39 @@
1
+ require "minitest/autorun"
2
+ require "pixoo"
3
+
4
+ module Pixoo
5
+ class ImageBufferTest < Minitest::Test
6
+ def test_set_get_pixel
7
+ buffer = Pixoo::ImageBuffer.new 64
8
+ buffer.set_pixel 0, 0, 15, 20, 35
9
+ assert_equal([15, 20, 35], buffer.get_pixel(0, 0))
10
+
11
+ buffer = Pixoo::ImageBuffer.new 64
12
+ buffer.set_pixel 5, 5, 15, 20, 35
13
+ assert_equal([15, 20, 35], buffer.get_pixel(5, 5))
14
+
15
+ buffer = Pixoo::ImageBuffer.new 64
16
+ buffer.set_pixel 63, 63, 15, 20, 35
17
+ assert_equal([15, 20, 35], buffer.get_pixel(63, 63))
18
+ end
19
+
20
+ def test_pixel_out_of_bounds
21
+ buffer = Pixoo::ImageBuffer.new 64
22
+ assert_raises ArgumentError do
23
+ buffer.set_pixel 64, 5, 15, 20, 35
24
+ end
25
+
26
+ assert_raises ArgumentError do
27
+ buffer.set_pixel 5, 64, 15, 20, 35
28
+ end
29
+
30
+ assert_raises ArgumentError do
31
+ buffer.get_pixel 5, 64
32
+ end
33
+
34
+ assert_raises ArgumentError do
35
+ buffer.get_pixel 64, 5
36
+ end
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Patterson
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: base64
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.3.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 0.3.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.18.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.18.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: net-http
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.1
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.1
54
+ - !ruby/object:Gem::Dependency
55
+ name: zlib
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.2.2
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.2.2
68
+ - !ruby/object:Gem::Dependency
69
+ name: minitest
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: rake
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '13.0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '13.0'
96
+ description: This is just a Ruby client for Pixoo displays
97
+ email: tenderlove@ruby-lang.org
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - CODE_OF_CONDUCT.md
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - lib/pixoo.rb
108
+ - lib/pixoo/image_buffer.rb
109
+ - pixoo.gemspec
110
+ - test/pixoo_test.rb
111
+ homepage: https://github.com/tenderlove/pixoo-rb
112
+ licenses:
113
+ - Apache-2.0
114
+ metadata: {}
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 3.0.0
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubygems_version: 4.0.2
130
+ specification_version: 4
131
+ summary: A Ruby client for Pixoo displays
132
+ test_files:
133
+ - test/pixoo_test.rb