odroid_lcd 0.0.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6006d3b5b173420c1df81e8498d1a68b182c7eef
4
- data.tar.gz: 1904a72869fe8cae59bcb2e94409b9f4e1e07170
3
+ metadata.gz: f626e738883e445715d2ab87b0fe9ea2c8f35b3d
4
+ data.tar.gz: 713a07322f33321d1f20d30731bb4e913cb0a3b3
5
5
  SHA512:
6
- metadata.gz: 7aba291d2ff468fcc8330544c08f36e103dac6d948bbfd6e6202db21cee80fe1d561c67ce94b15e1f9405ee831f0902972802d8afde4b35a24cb2cb60d2dfc57
7
- data.tar.gz: 650d24ba418a37ae228ef35198c4ee3cf1a1f3ab0f3196295567929212318a6ddf939a9dd6bd0e466b49fa3a5159b7a794dd901a5f32a88f0be97691e8340a90
6
+ metadata.gz: 1745a497c7ee5b639704bc2c733cfe03f721514fd345002e539d937a9a91a68bd65077cebac406fbfd382d729a8261978d23fbec573264b4a9d27e130daab335
7
+ data.tar.gz: 7afb8aaafab6fcb60b339e88b6dde1774702cd7162a2642134ad1ba334edfa6c8bb51b55af1e2731cef2553b515eedadfb264d84476277b575a07b9c2053ced7
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'odroid_lcd'
3
+ require_relative '../lib/odroid_lcd'
4
4
 
5
- cli = OdroidLCD::CLI.new
6
- cli.run(ARGV)
5
+ cli = OdroidLCD::CLI.new(ARGV)
6
+ cli.run()
@@ -1,7 +1,7 @@
1
1
 
2
- require 'odroid_lcd/hw'
3
- require "odroid_lcd/lcd"
4
- require "odroid_lcd/cli"
2
+ require_relative "odroid_lcd/lcd"
3
+ require_relative "odroid_lcd/cli"
4
+ require_relative "odroid_lcd/hw_mock"
5
5
 
6
6
  module OdroidLCD
7
7
  end
@@ -1,22 +1,93 @@
1
+
2
+ require 'optparse'
3
+
1
4
  module OdroidLCD
2
5
  class CLI
3
6
 
4
- def initialize
5
- @lcd = OdroidLCD::LCD.new
7
+ def initialize(argv, mocks = {})
8
+ @options = parse_arguments(argv)
9
+ @argv = argv
10
+
11
+ # If we're testing via the command line, set the mock rather than LCD.
12
+ @lcd = if @options[:test]
13
+ @hw_mock = OdroidLCD::HWMock.new
14
+ mock_hw = mocks.merge({odroidlcd_hw: @hw_mock})
15
+ OdroidLCD::LCD.new(mocks: mock_hw)
16
+ else
17
+ OdroidLCD::LCD.new(mocks: mocks)
18
+ end
19
+
20
+ # set the default text alignment
21
+ @align = @options[:align] || "left"
22
+
23
+ freeze()
6
24
  end
7
25
 
8
- def run(argv)
9
- string = argv[0]
10
26
 
11
- rows = argv[0].lines
12
- if rows.length > 1
13
- @lcd.set_string(row: 0, string: rows[0].chomp)
14
- @lcd.set_string(row: 1, string: rows[1].chomp)
27
+ def run
28
+ lines = if @options.has_key?(:file)
29
+ read_file(@options[:file])
15
30
  else
16
- @lcd.set_string(row: 0, string: rows[0].chomp)
31
+ @argv[0,2].map {|line| line.chomp }
17
32
  end
18
33
 
34
+ @lcd.set_lines(lines: lines, align: @align)
35
+
36
+ if @options[:test]
37
+ show_mocked_lcd()
38
+ end
39
+ end
40
+
41
+ def show_mocked_lcd
42
+ puts "[#{@hw_mock.get(row: 0).join}]"
43
+ puts "[#{@hw_mock.get(row: 1).join}]"
19
44
  end
20
45
 
46
+
47
+ def parse_arguments(argv)
48
+ options = {}
49
+ OptionParser.new do |opts|
50
+ opts.banner = "Usage: odroid-lcd [options] [first line] [second line]"
51
+
52
+ opts.on("-f", "--file=FILE", String, "Read content from a file") do |x|
53
+ if x == nil
54
+ puts opts
55
+ exit 2
56
+ end
57
+ options[:file] = x
58
+ end
59
+
60
+ opts.on("--test", "Dry run test (does not update the screen)") do |x|
61
+ options[:test] = x
62
+ end
63
+
64
+ opts.on("--align=ALIGN", String, "Align text: left, right, center") do |x|
65
+ if x == nil
66
+ puts opts
67
+ exit 2
68
+ end
69
+ options[:align] = x
70
+ end
71
+
72
+ opts.on("-h", "--help", "Prints this help") do
73
+ puts opts
74
+ exit
75
+ end
76
+
77
+ end.parse!
78
+ options
79
+ end
80
+
81
+ def read_file(filename)
82
+ file = if (filename == '-')
83
+ $stdin
84
+ else
85
+ File.open(filename, "r")
86
+ end
87
+
88
+ file.readlines.map {|line| line.chomp }
89
+ end
90
+
91
+
21
92
  end
22
93
  end
@@ -0,0 +1,46 @@
1
+ module OdroidLCD
2
+ class HWMock
3
+
4
+ attr_reader :max_column
5
+ attr_reader :max_row
6
+
7
+ def initialize
8
+ @max_column = 16
9
+ @max_row = 2
10
+
11
+ @display = [
12
+ [],
13
+ []
14
+ ]
15
+
16
+ end
17
+
18
+ def clear
19
+ @display = [
20
+ [],
21
+ []
22
+ ]
23
+ end
24
+
25
+ #
26
+ # See: https://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller
27
+ def set_character(row, column, character)
28
+ chr = case character
29
+ when "\u005c" then "\u00a5"
30
+ when "\u007e" then "\u2192"
31
+ when "\u007f" then "\u2190"
32
+ else character
33
+ end
34
+
35
+ @display[row][column] = chr
36
+ end
37
+
38
+ # Extra methods for use as a mock
39
+ #
40
+
41
+ def get(row: 0)
42
+ @display[row]
43
+ end
44
+
45
+ end
46
+ end
@@ -1,31 +1,103 @@
1
1
 
2
2
  module OdroidLCD
3
+
4
+ class LCDError < StandardError; end
5
+
3
6
  class LCD
7
+ # The main interface to the LCD screen.
4
8
 
5
9
  attr_reader :max_column
6
10
  attr_reader :max_row
7
11
 
8
12
  def initialize(mocks: {})
9
- @hw = mocks[:odroidlcd_hw] || OdroidLCD::HW.new
13
+ @hw = if mocks[:odroidlcd_hw]
14
+ mocks[:odroidlcd_hw]
15
+ else
16
+ require 'odroid_lcd/hw'
17
+ OdroidLCD::HW.new
18
+ end
19
+
10
20
  @max_column = @hw.max_column
11
21
  @max_row = @hw.max_row
12
22
  freeze()
13
23
  end
14
24
 
25
+ # Clear the display
15
26
  def clear
16
27
  @hw.clear
17
28
  end
18
29
 
30
+ # set_character
31
+ #
32
+ # Sets a single character at a particular position on the display.
33
+ #
34
+ # Note that the display uses a Japanese display driver, so
35
+ # the following quirks / features are present:
36
+ #
37
+ # "\" is displayed as the yen character
38
+ # "~" is displayed as a right arrow
39
+ # delete (or rubout) is displayed as a left arrow
40
+ #
41
+ # Row 0 is the top row, row 1 is the bottom row.
42
+ #
19
43
  def set_character(row:, column:, character:)
20
- @hw.set_character(row, column, character)
44
+ begin
45
+ # It's a Japanese display, seemingly using Windows-31J
46
+ #
47
+ # Note that \ displays as a yen sign, ~ displays as a right arrow, DEL displays as a left arrow
48
+ chr = character.encode("Windows-31J")
49
+ @hw.set_character(row, column, chr)
50
+ rescue Encoding::UndefinedConversionError => e
51
+ raise OdroidLCD::LCDError, "Could not display character: #{character}, the display uses Japanese Windows-31J"
52
+ end
21
53
  end
22
54
 
23
- def set_string(row: 0, string:)
55
+ # set_string
56
+ #
57
+ # Sets a string on the display.
58
+ #
59
+ # +::row::+
60
+ # Which row of the display to update.
61
+ # Row 0 is the top row, row 1 is the bottom row.
62
+ #
63
+ # +::string::+
64
+ # The string to send to the display.
65
+ #
66
+ # +::align::+
67
+ # Optional, either "left", "right", or "center"
68
+ # Defaults to: "left"
69
+ #
70
+ def set_string(row: 0, string:, align: "left")
24
71
  column = 0
25
- string[0, @max_column].ljust(@max_column - 1).chars do |chr|
72
+ str = case align
73
+ when "left" then string[0, @max_column].ljust(@max_column)
74
+ when "right" then string[0, @max_column].rjust(@max_column)
75
+ when "center" then string[0, @max_column].center(@max_column)
76
+ else string[0, @max_column].ljust(@max_column)
77
+ end
78
+
79
+ str.chars do |chr|
26
80
  set_character(row: row, column: column, character: chr)
27
81
  column += 1
28
82
  end
29
83
  end
84
+
85
+ # +::lines::+
86
+ # ["line one", "line two"]
87
+ # ["line one", "line two", "line three"]
88
+ #
89
+ # +::align::+
90
+ # Optional, either "left", "right", or "center"
91
+ # Defaults to: "left"
92
+ #
93
+ def set_lines(lines:, align: "left")
94
+ if lines.length >= @max_row
95
+ set_string(row: 0, string: lines[0].to_s, align: align)
96
+ set_string(row: 1, string: lines[1].to_s, align: align)
97
+ else
98
+ set_string(row: 0, string: lines[0].to_s, align: align)
99
+ end
100
+ end
101
+
30
102
  end
31
103
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: odroid_lcd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Helliwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-12 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.6'
13
27
  description: 'Allows writing strings to the LCD screen accessory, see: https://wiki.odroid.com/accessory/display/16x2_lcd_io_shield/c/start'
14
28
  email: phil.helliwell@gmail.com
15
29
  executables:
@@ -24,6 +38,7 @@ files:
24
38
  - lib/odroid_lcd.rb
25
39
  - lib/odroid_lcd/cli.rb
26
40
  - lib/odroid_lcd/hw.rb
41
+ - lib/odroid_lcd/hw_mock.rb
27
42
  - lib/odroid_lcd/lcd.rb
28
43
  homepage: http://github.com/kill9zombie/odroid_lcd
29
44
  licenses: