i2c-alpha_display 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20684e599b86fa327f9015464c32319871e6b2f8
4
+ data.tar.gz: 3775a3ee22721a4be8878881883a0984565c1c06
5
+ SHA512:
6
+ metadata.gz: a112dd8b3ca6c9c7d0c429ce33ec1892ad0753c4c71424b6f2455380942d4c1ae59810212d3fafad654601a60a2b0a44d58c7836324e5489e52a11d4e54511af
7
+ data.tar.gz: a471164318b27af93fbdda40724f3f31774ae85c4b6097a72d6ce98c4dec94aa7e47268a012640eb2ad3b77a217d157463af4271dde7ce7ebb32c9d6315ff14d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i2c-alpha_display.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nathan Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # I2c::AlphaDisplay
2
+
3
+ ## Installation
4
+
5
+ $ gem install i2c-alpha_display
6
+
7
+ ## Usage
8
+
9
+ ## License
10
+
11
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
12
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "i2c/alpha_display"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/i2c-disp ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'i2c/alpha_display_app'
3
+ I2C::AlphaDisplayApp.new.main
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'i2c/alpha_display/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i2c-alpha_display"
8
+ spec.version = I2C::AlphaDisplay::VERSION
9
+ spec.authors = ["Nathan Reed"]
10
+ spec.email = ["reednj@gmail.com"]
11
+
12
+ spec.summary = %q{command line gem to control the i2c alphanumeric displays on the RPi}
13
+ spec.homepage = "https://github.com/reednj/i2c-alpha_display"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ #spec.add_dependency "wiringpi2"
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'wiringpi2'
3
+
4
+ module WiringPi
5
+ class I2C
6
+ Wiringpi = Wiringpi2
7
+ end
8
+ end
9
+
10
+ rescue Exception => e
11
+ module WiringPi
12
+ class I2C
13
+ def initialize(device)
14
+ puts 'wiringpi2 not present, using mock class'
15
+ end
16
+
17
+ def write(data)
18
+ end
19
+
20
+ def write_reg_8(reg, data)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module I2C
2
+ class AlphaDisplay
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,167 @@
1
+ require "i2c/alpha_display/version"
2
+ require "i2c/alpha_display/hardware"
3
+
4
+ module I2C
5
+ class AlphaDisplay
6
+ DEVICE_ID = 0x70
7
+
8
+ HT16K33_SYSTEM_SETUP = 0x20
9
+ HT16K33_OSCILLATOR = 0x01
10
+ HT16K33_CMD_BRIGHTNESS = 0xE0
11
+
12
+ HT16K33_BLINK_CMD = 0x80
13
+ HT16K33_BLINK_DISPLAYON = 0x01
14
+ HT16K33_BLINK_OFF = 0x00
15
+ HT16K33_BLINK_2HZ = 0x02
16
+ HT16K33_BLINK_1HZ = 0x04
17
+ HT16K33_BLINK_HALFHZ = 0x06
18
+
19
+ DIGIT_VALUES = {
20
+ ' ' => 0b0000000000000000,
21
+ '+' => 0b0001001011000000,
22
+ '-' => 0b0000000011000000,
23
+ '0' => 0b0000110000111111,
24
+ '1' => 0b0000000000000110,
25
+ '2' => 0b0000000011011011,
26
+ '3' => 0b0000000010001111,
27
+ '4' => 0b0000000011100110,
28
+ '5' => 0b0010000001101001,
29
+ '6' => 0b0000000011111101,
30
+ '7' => 0b0000000000000111,
31
+ '8' => 0b0000000011111111,
32
+ '9' => 0b0000000011101111,
33
+ 'A' => 0b0000000011110111,
34
+ 'B' => 0b0001001010001111,
35
+ 'C' => 0b0000000000111001,
36
+ 'D' => 0b0001001000001111,
37
+ 'E' => 0b0000000011111001,
38
+ 'F' => 0b0000000001110001,
39
+ 'G' => 0b0000000010111101,
40
+ 'H' => 0b0000000011110110,
41
+ 'I' => 0b0001001000000000,
42
+ 'J' => 0b0000000000011110,
43
+ 'K' => 0b0010010001110000,
44
+ 'L' => 0b0000000000111000,
45
+ 'M' => 0b0000010100110110,
46
+ 'N' => 0b0010000100110110,
47
+ 'O' => 0b0000000000111111,
48
+ 'P' => 0b0000000011110011,
49
+ 'Q' => 0b0010000000111111,
50
+ 'R' => 0b0010000011110011,
51
+ 'S' => 0b0000000011101101,
52
+ 'T' => 0b0001001000000001,
53
+ 'U' => 0b0000000000111110,
54
+ 'V' => 0b0000110000110000,
55
+ 'W' => 0b0010100000110110,
56
+ 'X' => 0b0010110100000000,
57
+ 'Y' => 0b0001010100000000,
58
+ 'Z' => 0b0000110000001001
59
+ }
60
+
61
+ attr_accessor :debug
62
+
63
+ def initialize(device_id = DEVICE_ID, should_init = true)
64
+ @display_size = 4
65
+ @device = WiringPi::I2C.new device_id
66
+ @buffer_size = 16
67
+ @buffer = []
68
+ @device_id = device_id
69
+
70
+ self.debug = false
71
+ self.clear
72
+
73
+ # init the device to the default state, if thats what we want
74
+ if should_init
75
+ self.display_init
76
+ end
77
+ end
78
+
79
+ def display_init
80
+ @device.write HT16K33_SYSTEM_SETUP | HT16K33_OSCILLATOR
81
+ self.stop_blink
82
+ self.brightness = 1
83
+ end
84
+
85
+ def self.open(device_id = DEVICE_ID)
86
+ return AlphaDisplay.new(device_id, false)
87
+ end
88
+
89
+
90
+ def blank()
91
+ self.clear()
92
+ self.write_buffer
93
+ end
94
+
95
+ def clear()
96
+ @buffer_size.times do |i|
97
+ @buffer[i] = 0
98
+ end
99
+ end
100
+
101
+ def brightness=(v)
102
+ @device.write HT16K33_CMD_BRIGHTNESS | (v.to_i & 0xf)
103
+ end
104
+
105
+ def start_blink(type = HT16K33_BLINK_1HZ)
106
+ self.blink = type
107
+ end
108
+
109
+ def stop_blink
110
+ self.blink = HT16K33_BLINK_OFF
111
+ end
112
+
113
+ def blink=(v)
114
+ raise if ![HT16K33_BLINK_OFF, HT16K33_BLINK_2HZ, HT16K33_BLINK_1HZ, HT16K33_BLINK_HALFHZ].include? v
115
+ @device.write HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | v.to_i
116
+ end
117
+
118
+ def set(s, rjust = true)
119
+ if s.respond_to? :to_alpha
120
+ s = s.to_alpha
121
+ end
122
+
123
+ s = s.to_s
124
+ s_no_decimals = s.gsub('.', '')
125
+
126
+ puts s if self.debug
127
+
128
+ if rjust && s_no_decimals.length < @display_size
129
+ decimal_count = s.length - s_no_decimals.length
130
+ s = s.rjust(@display_size + decimal_count)
131
+ end
132
+
133
+ addr = 0
134
+ (0..s.length-1).each do |pos|
135
+ next if s[pos] == '.'
136
+
137
+ decimal = (s[pos+1] == '.' && addr != @display_size -1)
138
+ write_char addr, s[pos], decimal
139
+ addr += 1
140
+
141
+ break if addr >= @display_size
142
+ end
143
+
144
+ self.write_buffer
145
+ end
146
+
147
+ def write_char(pos, c, decimal = false)
148
+ raise "Invalid address (#{pos})" if pos >= @display_size || pos.class != Fixnum
149
+ raise "Expecting string data, but got #{c.class}" if c.class != String
150
+ raise "Can only set a single character (string length is #{c.length})" if c.length != 1
151
+
152
+ a = DIGIT_VALUES[c.upcase[0]]
153
+ raise "Character not in character set (#{c})" if a.nil?
154
+
155
+ a = a | 0x4000 if decimal
156
+ @buffer[pos*2] = a & 0xff
157
+ @buffer[pos*2+1] = (a >> 8) & 0xff
158
+ end
159
+
160
+ def write_buffer
161
+ @buffer_size.times do |i|
162
+ @device.write_reg_8 i, @buffer[i] & 0xFF
163
+ end
164
+ end
165
+
166
+ end
167
+ end
@@ -0,0 +1,35 @@
1
+
2
+ require 'i2c/alpha_display'
3
+
4
+ module I2C
5
+ class AlphaDisplayApp
6
+ def main
7
+ @display = AlphaDisplay.new(0x70)
8
+
9
+ STDIN.each_line do |line|
10
+ begin
11
+ a = parse_line(line)
12
+ @display.set a
13
+ rescue => e
14
+ puts e.to_s
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ def parse_line(s)
21
+ s.strip!
22
+
23
+ if s.to_i.to_s == s
24
+ return s.to_i
25
+ end
26
+
27
+ begin
28
+ return Float(s)
29
+ rescue
30
+ return s
31
+ end
32
+ end
33
+ end
34
+ end
35
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i2c-alpha_display
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Reed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - reednj@gmail.com
44
+ executables:
45
+ - i2c-disp
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - exe/i2c-disp
58
+ - i2c-alpha_display.gemspec
59
+ - lib/i2c/alpha_display.rb
60
+ - lib/i2c/alpha_display/hardware.rb
61
+ - lib/i2c/alpha_display/version.rb
62
+ - lib/i2c/alpha_display_app.rb
63
+ homepage: https://github.com/reednj/i2c-alpha_display
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.6.6
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: command line gem to control the i2c alphanumeric displays on the RPi
87
+ test_files: []