class I2C::AlphaDisplay

Constants

DEVICE_ID
DIGIT_VALUES
HT16K33_CMD_BRIGHTNESS
HT16K33_OSCILLATOR
HT16K33_SYSTEM_SETUP
VERSION

Attributes

debug[RW]

Public Class Methods

new(device_id = DEVICE_ID, should_init = true) click to toggle source
# File lib/i2c/alpha_display.rb, line 64
def initialize(device_id = DEVICE_ID, should_init = true)
  @display_size = 4
  @device = WiringPi::I2C.new device_id
  @buffer_size = 16
  @buffer = []
  @device_id = device_id

  self.debug = false
  self.clear

  # init the device to the default state, if thats what we want
  if should_init
    self.display_init
  end
end
open(device_id = DEVICE_ID) click to toggle source
# File lib/i2c/alpha_display.rb, line 86
def self.open(device_id = DEVICE_ID)
  return AlphaDisplay.new(device_id, false)
end

Public Instance Methods

blank() click to toggle source
# File lib/i2c/alpha_display.rb, line 91
def blank()
  self.clear()
  self.write_buffer
end
brightness=(v) click to toggle source
# File lib/i2c/alpha_display.rb, line 102
def brightness=(v)
  @device.write HT16K33_CMD_BRIGHTNESS | (v.to_i & 0xf)
end
clear() click to toggle source
# File lib/i2c/alpha_display.rb, line 96
def clear()
  @buffer_size.times do |i|
    @buffer[i] = 0
  end
end
display_init() click to toggle source
# File lib/i2c/alpha_display.rb, line 80
def display_init
  @device.write HT16K33_SYSTEM_SETUP | HT16K33_OSCILLATOR
  self.stop_blink
  self.brightness = 1
end
set(s, rjust = true) click to toggle source
# File lib/i2c/alpha_display.rb, line 119
def set(s, rjust = true)
  if s.respond_to? :to_alpha
    s = s.to_alpha
  end
  
  s = s.to_s
  s_no_decimals = s.gsub('.', '')

  puts s if self.debug

  if rjust && s_no_decimals.length < @display_size
    decimal_count = s.length - s_no_decimals.length
    s = s.rjust(@display_size + decimal_count)
  end

  addr = 0
  (0..s.length-1).each do |pos|
    next if s[pos] == '.'

    decimal = (s[pos+1] == '.' && addr != @display_size -1)
    write_char addr, s[pos], decimal
    addr += 1

    break if addr >= @display_size
  end

  self.write_buffer
end
write_buffer() click to toggle source
# File lib/i2c/alpha_display.rb, line 161
def write_buffer
  @buffer_size.times do |i|
    @device.write_reg_8 i, @buffer[i] & 0xFF
  end
end
write_char(pos, c, decimal = false) click to toggle source
# File lib/i2c/alpha_display.rb, line 148
def write_char(pos, c, decimal = false)
  raise "Invalid address (#{pos})" if pos >= @display_size || pos.class != Fixnum
  raise "Expecting string data, but got #{c.class}" if c.class != String
  raise "Can only set a single character (string length is #{c.length})" if c.length != 1

  a = DIGIT_VALUES[c.upcase[0]]
  raise "Character not in character set (#{c})" if a.nil?
  
  a = a | 0x4000 if decimal
  @buffer[pos*2] = a & 0xff
  @buffer[pos*2+1] = (a >> 8) & 0xff
end