class Adafruit_RGBCharLCD
Not comfertable with this bit of code just yet …
Public Class Methods
new(rs, en, d4, d5, d6, d7, cols, lines, red, green, blue, invert_polarity=True, enable_pwm=False, initial_color=[1.0, 1.0, 1.0])
click to toggle source
Calls superclass method
Adafruit_CharLCD::new
# File lib/Adafruit_CharLCD.rb, line 346 def initialize(rs, en, d4, d5, d6, d7, cols, lines, red, green, blue, invert_polarity=True, enable_pwm=False, initial_color=[1.0, 1.0, 1.0]) # Initialize the LCD with RGB backlight. RS, EN, and D4...D7 parameters # should be the pins connected to the LCD RS, clock enable, and data line # 4 through 7 connections. The LCD will be used in its 4-bit mode so these # 6 lines are the only ones required to use the LCD. You must also pass in # the number of columns and lines on the LCD. # The red, green, and blue parameters define the pins which are connected # to the appropriate backlight LEDs. The invert_polarity parameter is a # boolean that controls if the LEDs are on with a LOW or HIGH signal. By # default invert_polarity is True, i.e. the backlight LEDs are on with a # low signal. If you want to enable PWM on the backlight LEDs (for finer # control of colors) and the hardware supports PWM on the provided pins, # set enable_pwm to True. Finally you can set an explicit initial backlight # color with the initial_color parameter. The default initial color is # white (all LEDs lit). # You can optionally pass in an explicit GPIO class, # for example if you want to use an MCP230xx GPIO extender. If you don't # pass in an GPIO instance, the default GPIO for the running platform will # be used. super(rs, en, d4, d5, d6, d7,cols,lines,false,invert_polarity,enable_pwm) @_red = red @_green = green @_blue = blue # Setup backlight pins. [@_red, @_green, @_blue].each{|pin| RPi::GPIO.setup pin, :as => :output, :initialize => :low } if enable_pwm # Determine initial backlight duty cycles. @_backlightPWM=[] inital_color=_rgb_to_duty_cycle(inital_color) {@_red=>initial_color[0], @_green=>initial_color[1], @_blue=>initial_color[2]}.each{|pin,color| @_backlightPWM.push<<RPi::GPIO::PWM.new(pin, PWM_FREQUENCY) @_backlightPWM[-1].start(color) } rdc, gdc, bdc = _rgb_to_duty_cycle(initial_color) pwm.start(red, rdc) pwm.start(green, gdc) pwm.start(blue, bdc) else _rgb_to_pins(rgb).each{|pin,value| if(value) RPi::GPIO.set_high pin end } end end
Public Instance Methods
_rgb_to_duty_cycle(rgb)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 397 def _rgb_to_duty_cycle(rgb) # Convert tuple of RGB 0-1 values to tuple of duty cycles (0-100). rgb.each{|color| color=color.clamp(0.0,1.0) color=_pwm_duty_cycle(color) } return rgb end
_rgb_to_pins(rgb)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 405 def _rgb_to_pins(rgb) # Convert tuple of RGB 0-1 values to dict of pin values. retDict={} {@_red=>rgb[0],@_green=>rgb[1],@_blue=>rgb[2]}.each{|pin,color| if(color>0)#FIXME There has to be a more elegant way of doing this ... retDict[pin]=@_blpol else retDict[pin]=!@_blpol end } end
set_backlight(backlight)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 437 def set_backlight(backlight) # Enable or disable the backlight. If PWM is not enabled (default), a # non-zero backlight value will turn on the backlight and a zero value will # turn it off. If PWM is enabled, backlight can be any value from 0.0 to # 1.0, with 1.0 being full intensity backlight. On an RGB display this # function will set the backlight to all white. set_color(backlight, backlight, backlight) end
set_color(red, green, blue)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 416 def set_color(red, green, blue) # Set backlight color to provided red, green, and blue values. If PWM # is enabled then color components can be values from 0.0 to 1.0, otherwise # components should be zero for off and non-zero for on. if @_pwm_enabled # Set duty cycle of PWM pins. rgb=_rgb_to_duty_cycle([red, green, blue]) for i in (0..2) @_backlightPWM[i].duty_cycle=rgb[i] end else # Set appropriate backlight pins based on polarity and enabled colors. {@_red=>red,@_green=>green,@_blue=>blue}.each{|pin,value| if value>0 RPi::GPIO.output(pin, @_blpol) else RPi::GPIO.output(pin,!@_blpol) end } end end