class Adafruit_CharLCD
Public Class Methods
new(rs, en, d4, d5, d6, d7, cols, lines, backlight=nil, invert_polarity=true, enable_pwm=false, initial_backlight=1.0)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 86 def initialize(rs, en, d4, d5, d6, d7, cols, lines, backlight=nil, invert_polarity=true, enable_pwm=false, initial_backlight=1.0) #Initialize the LCD. 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. #If you would like to control the backlight, pass in the pin connected to #the backlight with the backlight parameter. The invert_polarity boolean #controls if the backlight is one with a LOW signal or HIGH signal. The #default invert_polarity value is true, i.e. the backlight is on with a #LOW signal. #You can enable PWM of the backlight pin to have finer control on the #brightness. To enable PWM make sure your hardware supports PWM on the #provided backlight pin and set enable_pwm to true (the default is false). #The appropriate PWM library will be used depending on the platform, but #you can provide an explicit one with the pwm parameter. #The initial state of the backlight is ON, but you can set it to an #explicit initial state with the initial_backlight parameter (0 is off, #1 is on/full bright). # Save column and line state. @_cols = cols @_lines = lines # Save GPIO state and pin numbers. @_rs = rs @_en = en @_d4 = d4 @_d5 = d5 @_d6 = d6 @_d7 = d7 # Save backlight state. @_backlight = backlight @_pwm_enabled = enable_pwm @_blpol = !invert_polarity #Setting up the pins [rs, en, d4, d5, d6, d7].each do |pin| RPi::GPIO.setup pin, :as => :output, :initialize => :low end #setup backlight if @_backlight !=nil RPi::GPIO.setup @_backlight, :as =>:output,:initialize=>:low if enable_pwm @_backlightPWM=RPi::GPIO::PWM.new(@_backlight, PWM_FREQUENCY) @_backlightPWM.start(_pwm_duty_cycle(initial_backlight)) else #FIXME: I really need to not work through the logic at 03:21 ... https://github.com/adafruit/Adafruit_Python_CharLCD/blob/f5a43f9c331180aeeef9cc86395ad84ca7deb631/Adafruit_CharLCD/Adafruit_CharLCD.py#L148 if(initial_backlight>0) else end end end # Initialize the display. # Initialize the display. write8(0x33) write8(0x32) # Initialize display control, function, and mode registers. @_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF @_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_2LINE | LCD_5x8DOTS @_displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT # Write registers. write8(LCD_DISPLAYCONTROL | @_displaycontrol) write8(LCD_FUNCTIONSET | @_displayfunction) write8(LCD_ENTRYMODESET | @_displaymode) # set the entry mode clear() end
Public Instance Methods
_pulse_enable()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 325 def _pulse_enable() # Pulse the clock enable line off, on, off to send command. RPi::GPIO.set_low @_en sleep(0.000001) # 1 microsecond pause - enable pulse must be > 450ns RPi::GPIO.set_high @_en sleep(0.000001) # 1 microsecond pause - enable pulse must be > 450ns RPi::GPIO.set_low @_en sleep(0.000001) # commands need > 37us to settle end
_pwm_duty_cycle(intensity)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 334 def _pwm_duty_cycle(intensity) # Convert intensity value of 0.0 to 1.0 to a duty cycle of 0.0 to 100.0 intensity = 100.0*intensity # Invert polarity if required. if not @_blpol intensity = 100.0-intensity end return intensity end
autoscroll(autoscroll)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 218 def autoscroll(autoscroll) #Autoscroll will 'right justify' text from the cursor if set true, #otherwise it will 'left justify' the text. if autoscroll @_displaymode |= LCD_ENTRYSHIFTINCREMENT else @_displaymode &= ~LCD_ENTRYSHIFTINCREMENT end write8(LCD_ENTRYMODESET | @_displaymode) end
blink(blink)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 191 def blink(blink) #Turn on or off cursor blinking. Set blink to true to enable blinking.""" if blink @_displaycontrol |= LCD_BLINKON else @_displaycontrol &= ~LCD_BLINKON end write8(@_displaycontrol|LCD_DISPLAYCONTROL) end
clear()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 159 def clear() #Clear the LCD. write8(LCD_CLEARDISPLAY) # command to clear display sleep(0.003) # 3000 microsecond sleep, clearing the display takes a long time end
create_char(location, pattern)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 312 def create_char(location, pattern) #Fill one of the first 8 CGRAM locations with custom characters. #The location parameter should be between 0 and 7 and pattern should #provide an array of 8 bytes containing the pattern. E.g. you can easyly #design your custom character at http://www.quinapalus.com/hd44780udg.html #To show your custom character use eg. lcd.message('\x01') # only position 0..7 are allowed location &= 0x7 write8(LCD_SETCGRAMADDR | (location << 3)) (0..8).each{|i| write8(pattern[i], true) } end
enable_display(enable)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 173 def enable_display(enable) #Enable or disable the display. Set enable to true to enable. if(enable) @_displaycontrol |= LCD_DISPLAYON else @_displaycontrol &= ~LCD_DISPLAYON end write8(@_displaycontrol|LCD_DISPLAYCONTROL) end
home()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 154 def home() #Move the cursor back to its home (first line and first column). write8(LCD_RETURNHOME) # set cursor position to zero sleep(0.003) # this command takes a long time! end
message(text)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 228 def message(text) #Write text to display. Note that text can include newlines. line = 0 # Iterate through each character. text.split("").each{|char|#Bit of a hacky way to do it but 🤷 # Advance to next line if character is a new line. if(char == "\n") line += 1 # Move to left or right side depending on text direction. if (@_displaymode & LCD_ENTRYLEFT )> 0 col = 0 else @_cols-1 end set_cursor(col, line) # Write the character to the display. else write8(char.ord, true) end } end
move_left()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 200 def move_left() #Move display left one position. write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT) end
move_right()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 204 def move_right() #Move display right one position. write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT) end
set_backlight(backlight)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 249 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. if @_backlight !=nil if @_pwm_enabled @_backlightPWM=_pwm_duty_cycle(backlight) else puts backlight if(backlight>0) RPi::GPIO.set_high @_backlight else RPi::GPIO.set_low @_backlight end end end end
set_cursor(col, row)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 164 def set_cursor(col, row) #Move the cursor to an explicit column and row position. # Clamp row to the last row of the display. if row > @_lines row = @_lines - 1 end # Set location. write8(LCD_SETDDRAMADDR | (col + LCD_ROW_OFFSETS[row])) end
set_left_to_right()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 208 def set_left_to_right() #Set text direction left to right.""" @_displaymode |= LCD_ENTRYLEFT write8(LCD_ENTRYMODESET | @_displaymode) end
set_right_to_left()
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 213 def set_right_to_left() #Set text direction right to left. @_displaymode &= ~LCD_ENTRYLEFT write8(LCD_ENTRYMODESET | @_displaymode) end
show_cursor(show)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 182 def show_cursor(show) #Show or hide the cursor. Cursor is shown if show is true. if(show) @_displaycontrol |= LCD_CURSORON else @_displaycontrol &= ~LCD_CURSORON end write8(@_displaycontrol|LCD_DISPLAYCONTROL) end
write8(value, char_mode=false)
click to toggle source
# File lib/Adafruit_CharLCD.rb, line 267 def write8(value, char_mode=false) #Write 8-bit value in character or data mode. Value should be an int #value from 0-255, and char_mode is true if character data or false if #non-character data (default). # One millisecond delay to prevent writing too quickly. sleep(0.001) # Set character / data bit. if(char_mode) RPi::GPIO.set_high @_rs else RPi::GPIO.set_low @_rs end # Write upper 4 bits. {@_d4=>4,@_d5=>5,@_d6=>6,@_d7=>7}.each{|pin,bit|#This is super jankey, but it should work if((value>>bit)& 1)>0 RPi::GPIO.set_high pin else RPi::GPIO.set_low pin end } #RPi::GPIO.output([@_d4,@_d5,@_d6,@_d7], # ((value >> 4) & 1) > 0, # ((value >> 5) & 1) > 0, # ((value >> 6) & 1) > 0, # ((value >> 7) & 1) > 0) #RPi::GPIO.output_pins({ @_d4: ((value >> 4) & 1) > 0, # @_d5: ((value >> 5) & 1) > 0, # @_d6: ((value >> 6) & 1) > 0, # @_d7: ((value >> 7) & 1) > 0 }) _pulse_enable() # Write lower 4 bits. {@_d4=>0,@_d5=>1,@_d6=>2,@_d7=>3}.each{|pin,bit|#This is super jankey, but it should work if((value>>bit)&1)>0 RPi::GPIO.set_high pin else RPi::GPIO.set_low pin end } #RPi::GPIO.output([@_d4,@_d5,@_d6,@_d7], # (value & 1) > 0, # ((value >> 1) & 1) > 0, # ((value >> 2) & 1) > 0, # ((value >> 3) & 1) > 0) _pulse_enable() end