Skip to content

Commit

Permalink
Added lib/bin directories/lcd_backligth method to lcd class
Browse files Browse the repository at this point in the history
  • Loading branch information
jescarri committed Jun 25, 2015
1 parent ddf3351 commit 36848c6
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ dist
build
eggs
parts
bin
#bin
var
sdist
develop-eggs
.installed.cfg
lib
#lib
lib64
__pycache__

Expand Down
17 changes: 17 additions & 0 deletions bin/lcd_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# on importe le pilote
import sys
sys.path.append("./lib")
import lcddriver
from time import *

# on initialise le lcd
lcd = lcddriver.lcd()

# on reinitialise le lcd
lcd.lcd_clear()

# on affiche des caracteres sur chaque ligne
lcd.lcd_display_string(" Hello world !", 1)
lcd.lcd_display_string(" Je suis", 2)
lcd.lcd_display_string(" un", 3)
lcd.lcd_display_string(" Raspberry Pi !", 4)
156 changes: 156 additions & 0 deletions bin/lcd_leds_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env python2.7

import sys
sys.path.append("./lib")
from time import sleep
import subprocess
import os

# wiringpi
import RPi.GPIO as GPIO

# LCD
import lcddriver
from time import *

GPIO.setmode(GPIO.BOARD) # choose BCM or BOARD numbering schemes. I use BOARD

button_A = 18 # GPIO channel 24 is on pin 18 of connector P1
# it will work on any GPIO channel
# Optional 50K Ohm pull up to 3.3v. A push button will ground the pin, creating a falling edge.

long_press = 1 #s
very_long_press = 4 #s

red = 16 # set pin 16 for red led
green = 11 # set pin 11 for green led
blue = 15 # set pin 15 for blue led

Freq = 60 #Hz
pause_time = 0.01 # you can change this to slow down/speed up

GPIO.setup(button_A, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set button pin as input
GPIO.setup(red, GPIO.OUT) # set red led pin as output
GPIO.setup(green, GPIO.OUT) # set green led pin as output
GPIO.setup(blue, GPIO.OUT) # set blue led pin as output
RED = GPIO.PWM(red, Freq)
GREEN = GPIO.PWM(green, Freq)
BLUE = GPIO.PWM(blue, Freq)

RED.start(0)
GREEN.start(0)
BLUE.start(0)

# lcd init
lcd = lcddriver.lcd()
lcd.lcd_clear()

empty_line = " "

state = 1

def lcd_string(string, line):
lcd.lcd_display_string(empty_line, line)
lcd.lcd_display_string(string, line)

def get_iface_ip(iface):
# Returns the IP address for the given interface e.g. eth0
if os.path.exists('/sys/class/net/'+iface):
try:
s = subprocess.check_output(["ip","addr","show",iface])
ip = s.split('\n')[2].strip().split(' ')[1].split('/')[0]
field = ip
# print("NETWORK - "+iface+" : "+field+"")
return field
except:
field = "ipnull"
# print("NETWORK - "+iface+" : "+field+"")
return field
else:
field = "noiface"
# print("NETWORK - "+iface+" : "+field+"")
return field

def system_button(button_A):
global state

button_press_timer = 0
while True:
if (GPIO.input(button_A) == False) : # while button is still pressed down
button_press_timer += 0.1 # keep counting until button is released
else: # button is released, count for how long
if (button_press_timer > very_long_press) : # pressed for > 5 seconds
# do what you want with a very long press
print "very long press : ", button_press_timer
state = 0
lcd_string(" SHUTDOWN !", 2)
subprocess.call(['shutdown -h now "System halted by GPIO action" &'], shell=True)

elif (button_press_timer > long_press) : # press for > 1 < 5 seconds
# do what you want with a long press
print "long press : ", button_press_timer
state = 2
lcd_string(" RESTARTING !", 2)
subprocess.call(['sudo reboot &'], shell=True)

elif (button_press_timer > 0.1):
# do what you want with a short press
print "short press : ", button_press_timer

button_press_timer = 0
sleep(0.1)

GPIO.add_event_detect(button_A, GPIO.FALLING, callback=system_button, bouncetime=100)
# setup the thread, detect a falling edge on channel and debounce it with 100mSec

# assume this is the main code...
try:
# welcome message
lcd_string(" Hello world !", 1)
lcd_string(" I am", 2)
lcd_string(" a", 3)
lcd_string(" Banana Pi !", 4)

sleep(2)

lcd.lcd_clear()
lcd_string(" Banana Pi Status", 1)
lcd_string(" READY...", 2)
lcd_string("wlan : "+get_iface_ip("wlan0")+"", 4)

while True:

# do whatever while "waiting" for falling edge on channel
for i in range(0,101): # 101 because it stops when it finishes 100
if state == 0:
RED.ChangeDutyCycle(i)
GREEN.ChangeDutyCycle(0)
BLUE.ChangeDutyCycle(0)
if state == 1:
RED.ChangeDutyCycle(0)
GREEN.ChangeDutyCycle(i)
BLUE.ChangeDutyCycle(0)
if state == 2:
RED.ChangeDutyCycle(i)
GREEN.ChangeDutyCycle(0)
BLUE.ChangeDutyCycle(100 - i)
sleep(pause_time)

for i in range(100,-1,-1): # from 100 to zero in steps of -1
if state == 0:
RED.ChangeDutyCycle(i)
GREEN.ChangeDutyCycle(0)
BLUE.ChangeDutyCycle(0)
if state == 1:
RED.ChangeDutyCycle(0)
GREEN.ChangeDutyCycle(i)
BLUE.ChangeDutyCycle(0)
if state == 2:
RED.ChangeDutyCycle(i)
GREEN.ChangeDutyCycle(0)
BLUE.ChangeDutyCycle(100 - i)
sleep(pause_time)

except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
36 changes: 36 additions & 0 deletions lib/i2c_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
sys.path.append("./lib")
import smbus
from time import *

class i2c_device:
def __init__(self, addr, port=1):
self.addr = addr
self.bus = smbus.SMBus(port)

# Write a single command
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)

# Write a command and argument
def write_cmd_arg(self, cmd, data):
self.bus.write_byte_data(self.addr, cmd, data)
sleep(0.0001)

# Write a block of data
def write_block_data(self, cmd, data):
self.bus.write_block_data(self.addr, cmd, data)
sleep(0.0001)

# Read a single byte
def read(self):
return self.bus.read_byte(self.addr)

# Read
def read_data(self, cmd):
return self.bus.read_byte_data(self.addr, cmd)

# Read a block of data
def read_block_data(self, cmd):
return self.bus.read_block_data(self.addr, cmd)
113 changes: 113 additions & 0 deletions lib/lcddriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import sys
sys.path.append("./lib")

import i2c_lib
from time import *

# LCD Address
ADDRESS = 0x27

# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80

# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00

# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00

# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00

# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00

# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00

En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit

class lcd:
#initializes objects and lcd
def __init__(self):
self.lcd_device = i2c_lib.i2c_device(ADDRESS)

self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x02)

self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
sleep(0.2)

# clocks EN to latch command
def lcd_strobe(self, data):
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
sleep(.0001)

def lcd_write_four_bits(self, data):
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
self.lcd_strobe(data)

# write a command to lcd
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
#turn on/off the lcd backlight
def lcd_backligth(self,state):
if state in ("on","On","ON"):
self.lcd_device.write_cmd(LCD_BACKLIGHT)
elif state in ("off","Off","OFF"):
self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
else:
print "Unknown State!"

# put string function
def lcd_display_string(self, string, line):
if line == 1:
self.lcd_write(0x80)
if line == 2:
self.lcd_write(0xC0)
if line == 3:
self.lcd_write(0x94)
if line == 4:
self.lcd_write(0xD4)

for char in string:
self.lcd_write(ord(char), Rs)

# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_RETURNHOME)

0 comments on commit 36848c6

Please sign in to comment.