texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Wed Nov 07, 2012 8:51 pm

This problem has been seen before - this post may solve it :
http://www.raspberrypi.org/phpBB3/viewt ... ls#p163705

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

paultnl
Posts: 103
Joined: Fri Oct 12, 2012 6:25 pm
Location: Cambridge

Re: Nokia Pi LCD

Wed Nov 07, 2012 9:45 pm

OK now hitting this problem when I try to do the last bit of Step 4

Code: Select all

[email protected] ~/WiringPi-Python/WiringPi/gpio $ make
[CC] gpio.c
make: *** No rule to make target `/usr/local/lib/libwiringPi.a', needed by `gpio'.  Stop.
I don't like Russian dolls, they are so full of themselves

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Wed Nov 07, 2012 10:14 pm

Sorry I've never tried to 'make' it. I've always used git or the pip method.
I,ll see if I can get some full instructions from a clean raspbian image.
You could always try the RPi.GPIO method with one of my earlier programs, but wiringpi is the prefered method due to PWM support.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Nokia Pi LCD

Wed Nov 07, 2012 10:54 pm

I did this a couple of days ago on a fresh install of September Raspbian. Here's what worked for me...

Code: Select all

sudo apt-get update

sudo apt-get install python-dev python-pip

sudo pip install wiringpi
Hope this helps :)
Alex Eames RasPi.TV, RasP.iO

paultnl
Posts: 103
Joined: Fri Oct 12, 2012 6:25 pm
Location: Cambridge

Re: Nokia Pi LCD

Thu Nov 08, 2012 3:29 pm

Nearly there

Installed wiringPi with

Code: Select all

sudo apt-get update

sudo apt-get install python-dev python-pip

sudo pip install wiringpi
Then run a file (I called lcd2.py) with this code

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-
# using wiringPI GPIO method
# also using PWM on gpio pin 12
# now using wiringpi and not BCM ID method
# this should work with both v1 and v2 Pi boards

import wiringpi

import time

#gpio's :
SCE = 2 # gpio pin 13 = wiringpi no. 2
SCLK = 5 # gpio pin 18 = wiringpi no. 5
DIN =  4 # gpio pin 16 = wiringpi no. 4
DC =  3 # gpio pin 15 = wiringpi no. 3
RST =  0 # gpio pin 11 = wiringpi no. 0
LED = 1 # gpio pin 12 = wiringpi no. 1

font =[
0x7E, 0x11, 0x11, 0x11, 0x7E, # A
0x7F, 0x49, 0x49, 0x49, 0x36, # B
0x3E, 0x41, 0x41, 0x41, 0x22, # C
0x7F, 0x41, 0x41, 0x22, 0x1C, # D
0x7F, 0x49, 0x49, 0x49, 0x41, # E
0x7F, 0x09, 0x09, 0x09, 0x01, # F
0x3E, 0x41, 0x49, 0x49, 0x7A, # G
0x7F, 0x08, 0x08, 0x08, 0x7F, # H
0x00, 0x41, 0x7F, 0x41, 0x00, # I
0x20, 0x40, 0x41, 0x3F, 0x01, # J
0x7F, 0x08, 0x14, 0x22, 0x41, # K
0x7F, 0x40, 0x40, 0x40, 0x40, # L
0x7F, 0x02, 0x0C, 0x02, 0x7F, # M
0x7F, 0x04, 0x08, 0x10, 0x7F, # N
0x3E, 0x41, 0x41, 0x41, 0x3E, # O
0x7F, 0x09, 0x09, 0x09, 0x06, # P
0x3E, 0x41, 0x51, 0x21, 0x5E, # Q
0x7F, 0x09, 0x19, 0x29, 0x46, # R
0x46, 0x49, 0x49, 0x49, 0x31, # S
0x01, 0x01, 0x7F, 0x01, 0x01, # T
0x3F, 0x40, 0x40, 0x40, 0x3F, # U
0x1F, 0x20, 0x40, 0x20, 0x1F, # V
0x3F, 0x40, 0x38, 0x40, 0x3F, # W
0x63, 0x14, 0x08, 0x14, 0x63, # X
0x07, 0x08, 0x70, 0x08, 0x07, # Y
0x61, 0x51, 0x49, 0x45, 0x43, # Z
]

def main():
  start = time.time()
  begin(0xbc) # contrast - may need tweaking for each display ( b3 )
  gotoxy(28,0)
  text("TAKE A")
  gotoxy(18,2)
  text("RASPBERRY")
  gotoxy(26,4)
  text("PI BYTE!")
  finish = time.time()
  print ("Total time : ",finish - start)
  print


def gotoxy(x,y):
  lcd_cmd(x+128)
  lcd_cmd(y+64)

def text(words):
  for i in range(len(words)):
    display_char(words[i])

def display_char(char):
  index=(ord(char)-65)*5
  if ord(char) >=65 and ord(char) <=90:
    for i in range(5):
      lcd_data(font[index+i])
    lcd_data(0) # space inbetween characters
  elif ord(char)==32:
      lcd_data(0)
      lcd_data(0)
      lcd_data(0)
      lcd_data(0)
      lcd_data(0)
      lcd_data(0)

def cls():
  gotoxy(0,0)
  for i in range(84):
    for j in range(6):
      lcd_data(0)

def setup():
  # set pin directions
  wiringpi.wiringPiSetup()
  wiringpi.pinMode(SCE, 1) # output
  wiringpi.pinMode(DIN, 1) # output
  wiringpi.pinMode(SCLK, 1) # output
  wiringpi.pinMode(DC, 1) # output
  wiringpi.pinMode(RST, 1) # output
  wiringpi.pinMode(LED, 2) # LED set up as PWM
  print ("setup ok")
  wiringpi.pwmWrite(LED, 255)

def begin(contrast):
  setup()
  # toggle RST low to reset
  wiringpi.digitalWrite(SCE, 0)
  wiringpi.digitalWrite(RST, 0)
  wiringpi.digitalWrite(LED, 0)
  time.sleep(0.100)
  wiringpi.digitalWrite(RST, 1)
  lcd_cmd(0x21) # extended mode
  lcd_cmd(0x14) # bias
  lcd_cmd(contrast) # vop
  lcd_cmd(0x20) # basic mode
  lcd_cmd(0xc) # non-inverted display
  cls()

def SPI(c):
  # data = DIN
  # clock = SCLK
  # MSB first
  # value = c
  for i in xrange(8):
    wiringpi.digitalWrite(DIN,((c & (1 << (7-i))) > 0))
    wiringpi.digitalWrite(SCLK, 1)
    wiringpi.digitalWrite(SCLK, 0)

def lcd_cmd(c):
  wiringpi.digitalWrite(DC, 0)
  SPI(c)

def lcd_data(c):
  wiringpi.digitalWrite(DC, 1)
  SPI(c)

if __name__ == "__main__":
  main()
It works but only if you use

Code: Select all

sudo python lcd2.py
If you try without sudo you get

Code: Select all

[email protected] ~ $ python lcd2.py
wiringPiSetup: Unable to open /dev/mem: Permission denied
Segmentation fault
If anyone has a way of running without root it would be better.
I don't like Russian dolls, they are so full of themselves

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Thu Nov 08, 2012 3:49 pm

Sorry but you do have to use Sudo with wiringpi. Glad you got it to work.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
dliloch
Posts: 168
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: Nokia Pi LCD

Thu Nov 08, 2012 3:58 pm

The post by BlackJack » Fri Aug 10, 2012 has the code to replace the array with a dictionary and the full character set.. Works nicely

OS1
Posts: 17
Joined: Sat Sep 22, 2012 3:27 pm

Re: Nokia Pi LCD

Thu Nov 08, 2012 10:37 pm

I have created a small GUI in Python Tk below.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# code improvements
# 9/10/12

import LCD
from LCD import *
import Tkinter
from Tkinter import *
import tkFont

class App:

    def __init__(self, parent):

        frame = Frame(parent);
        frame.pack();

        parent.title("LCD GUI");
 
        self.txtLabel = Label(frame, text="Text");
        self.txtLabel.grid(row=0, column=0);

        self.txtEntry = Text(frame, width=16, height=5, font=("systemfixed", 12));
        self.txtEntry.grid(row=0, column=1, columnspan=3);

        self.sndButt = Button(frame, text="Send", command=self.send_txt);
        self.sndButt.grid(row=0, column=4, padx=5, pady=5, sticky=W);

        self.txtLabel = Label(frame, text="X: ");
        self.txtLabel.grid(row=1, column=1, sticky=E);
        self.xEntry = Entry(frame);
        self.xEntry.grid(row=1, column=2, pady=5);

        self.txtLabel = Label(frame, text="Y: ");
        self.txtLabel.grid(row=2, column=1, sticky=E);
        self.yEntry = Entry(frame);
        self.yEntry.grid(row=2, column=2, pady=5);

        self.lightVar = IntVar();
        self.chkB = Checkbutton(frame, text="Backlight On/Off", variable=self.lightVar, offvalue=0, onvalue=256, command=self.back_off);
        self.chkB.grid(row=3, columnspan=3, sticky=W);
        self.chkB.select();

        self.clsButt = Button(frame, text="CLS", command=LCD.cls);
        self.clsButt.grid(row=3, column=4, padx=5, pady=5);

        self.quitButt = Button(frame, text="Quit", fg="red", command=frame.quit);
        self.quitButt.grid(row=4, column=4);

    def send_txt(self):
        try:
            LCD.gotoxy(int(self.xEntry.get()), int(self.yEntry.get()));
        except ValueError:
            LCD.gotoxy(0, 0);
        LCD.text(self.txtEntry.get(1.0, END));

    def back_off(self):
        LCD.switch_led(self.lightVar.get());

root = Tk();
LCD.init();

def callback():
    print "called the callback!";

# create a toolbar
toolbar = Frame(root);

#b = Button(toolbar, text="new", width=6, command=callback);
#b.pack(side=LEFT, padx=2, pady=2);

#b = Button(toolbar, text="open", width=6, command=callback);
#b.pack(side=LEFT, padx=2, pady=2);

toolbar.pack(side=TOP, fill=X);

app = App(root);

root.mainloop();
You need to rename wiringpi.py to LCD.py or replace LCD.py with whatever your LCD controller script is called. Then it should run. It's very basic but maybe of use to somebody.

I made some changes to wiringpi.py, which I called LCD.py, so I'll include it here as I can't remember exactly what they were!

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# code improvements
# 9/10/12

import time
import wiringpi

ROWS = 6
COLUMNS = 14
PIXELS_PER_ROW = 6
ON = 1
OFF = 0

#gpio's :
SCE = 2 # gpio pin 13 = wiringpi no. 2
SCLK = 5 # gpio pin 18 = wiringpi no. 5
DIN = 4 # gpio pin 16 = wiringpi no. 4
DC = 3 # gpio pin 15 = wiringpi no. 3
RST = 0 # gpio pin 11 = wiringpi no. 0
LED = 1 # gpio pin 12 = wiringpi no. 1

FONT = {
 ' ': [0x00, 0x00, 0x00, 0x00, 0x00],
 '!': [0x00, 0x00, 0x5f, 0x00, 0x00],
 '"': [0x00, 0x07, 0x00, 0x07, 0x00],
 '#': [0x14, 0x7f, 0x14, 0x7f, 0x14],
 '$': [0x24, 0x2a, 0x7f, 0x2a, 0x12],
 '%': [0x23, 0x13, 0x08, 0x64, 0x62],
 '&': [0x36, 0x49, 0x55, 0x22, 0x50],
 "'": [0x00, 0x05, 0x03, 0x00, 0x00],
 '(': [0x00, 0x1c, 0x22, 0x41, 0x00],
 ')': [0x00, 0x41, 0x22, 0x1c, 0x00],
 '*': [0x14, 0x08, 0x3e, 0x08, 0x14],
 '+': [0x08, 0x08, 0x3e, 0x08, 0x08],
 ',': [0x00, 0x50, 0x30, 0x00, 0x00],
 '-': [0x08, 0x08, 0x08, 0x08, 0x08],
 '.': [0x00, 0x60, 0x60, 0x00, 0x00],
 '/': [0x20, 0x10, 0x08, 0x04, 0x02],
 '0': [0x3e, 0x51, 0x49, 0x45, 0x3e],
 '1': [0x00, 0x42, 0x7f, 0x40, 0x00],
 '2': [0x42, 0x61, 0x51, 0x49, 0x46],
 '3': [0x21, 0x41, 0x45, 0x4b, 0x31],
 '4': [0x18, 0x14, 0x12, 0x7f, 0x10],
 '5': [0x27, 0x45, 0x45, 0x45, 0x39],
 '6': [0x3c, 0x4a, 0x49, 0x49, 0x30],
 '7': [0x01, 0x71, 0x09, 0x05, 0x03],
 '8': [0x36, 0x49, 0x49, 0x49, 0x36],
 '9': [0x06, 0x49, 0x49, 0x29, 0x1e],
 ':': [0x00, 0x36, 0x36, 0x00, 0x00],
 ';': [0x00, 0x56, 0x36, 0x00, 0x00],
 '<': [0x08, 0x14, 0x22, 0x41, 0x00],
 '=': [0x14, 0x14, 0x14, 0x14, 0x14],
 '>': [0x00, 0x41, 0x22, 0x14, 0x08],
 '?': [0x02, 0x01, 0x51, 0x09, 0x06],
 '@': [0x32, 0x49, 0x79, 0x41, 0x3e],
 'A': [0x7e, 0x11, 0x11, 0x11, 0x7e],
 'B': [0x7f, 0x49, 0x49, 0x49, 0x36],
 'C': [0x3e, 0x41, 0x41, 0x41, 0x22],
 'D': [0x7f, 0x41, 0x41, 0x22, 0x1c],
 'E': [0x7f, 0x49, 0x49, 0x49, 0x41],
 'F': [0x7f, 0x09, 0x09, 0x09, 0x01],
 'G': [0x3e, 0x41, 0x49, 0x49, 0x7a],
 'H': [0x7f, 0x08, 0x08, 0x08, 0x7f],
 'I': [0x00, 0x41, 0x7f, 0x41, 0x00],
 'J': [0x20, 0x40, 0x41, 0x3f, 0x01],
 'K': [0x7f, 0x08, 0x14, 0x22, 0x41],
 'L': [0x7f, 0x40, 0x40, 0x40, 0x40],
 'M': [0x7f, 0x02, 0x0c, 0x02, 0x7f],
 'N': [0x7f, 0x04, 0x08, 0x10, 0x7f],
 'O': [0x3e, 0x41, 0x41, 0x41, 0x3e],
 'P': [0x7f, 0x09, 0x09, 0x09, 0x06],
 'Q': [0x3e, 0x41, 0x51, 0x21, 0x5e],
 'R': [0x7f, 0x09, 0x19, 0x29, 0x46],
 'S': [0x46, 0x49, 0x49, 0x49, 0x31],
 'T': [0x01, 0x01, 0x7f, 0x01, 0x01],
 'U': [0x3f, 0x40, 0x40, 0x40, 0x3f],
 'V': [0x1f, 0x20, 0x40, 0x20, 0x1f],
 'W': [0x3f, 0x40, 0x38, 0x40, 0x3f],
 'X': [0x63, 0x14, 0x08, 0x14, 0x63],
 'Y': [0x07, 0x08, 0x70, 0x08, 0x07],
 'Z': [0x61, 0x51, 0x49, 0x45, 0x43],
 '[': [0x00, 0x7f, 0x41, 0x41, 0x00],
 '\\': [0x02, 0x04, 0x08, 0x10, 0x20],
 ']': [0x00, 0x41, 0x41, 0x7f, 0x00],
 '^': [0x04, 0x02, 0x01, 0x02, 0x04],
 '_': [0x40, 0x40, 0x40, 0x40, 0x40],
 '`': [0x00, 0x01, 0x02, 0x04, 0x00],
 'a': [0x20, 0x54, 0x54, 0x54, 0x78],
 'b': [0x7f, 0x48, 0x44, 0x44, 0x38],
 'c': [0x38, 0x44, 0x44, 0x44, 0x20],
 'd': [0x38, 0x44, 0x44, 0x48, 0x7f],
 'e': [0x38, 0x54, 0x54, 0x54, 0x18],
 'f': [0x08, 0x7e, 0x09, 0x01, 0x02],
 'g': [0x0c, 0x52, 0x52, 0x52, 0x3e],
 'h': [0x7f, 0x08, 0x04, 0x04, 0x78],
 'i': [0x00, 0x44, 0x7d, 0x40, 0x00],
 'j': [0x20, 0x40, 0x44, 0x3d, 0x00],
 'k': [0x7f, 0x10, 0x28, 0x44, 0x00],
 'l': [0x00, 0x41, 0x7f, 0x40, 0x00],
 'm': [0x7c, 0x04, 0x18, 0x04, 0x78],
 'n': [0x7c, 0x08, 0x04, 0x04, 0x78],
 'o': [0x38, 0x44, 0x44, 0x44, 0x38],
 'p': [0x7c, 0x14, 0x14, 0x14, 0x08],
 'q': [0x08, 0x14, 0x14, 0x18, 0x7c],
 'r': [0x7c, 0x08, 0x04, 0x04, 0x08],
 's': [0x48, 0x54, 0x54, 0x54, 0x20],
 't': [0x04, 0x3f, 0x44, 0x40, 0x20],
 'u': [0x3c, 0x40, 0x40, 0x20, 0x7c],
 'v': [0x1c, 0x20, 0x40, 0x20, 0x1c],
 'w': [0x3c, 0x40, 0x30, 0x40, 0x3c],
 'x': [0x44, 0x28, 0x10, 0x28, 0x44],
 'y': [0x0c, 0x50, 0x50, 0x50, 0x3c],
 'z': [0x44, 0x64, 0x54, 0x4c, 0x44],
 '{': [0x00, 0x08, 0x36, 0x41, 0x00],
 '|': [0x00, 0x00, 0x7f, 0x00, 0x00],
 '}': [0x00, 0x41, 0x36, 0x08, 0x00],
 '~': [0x10, 0x08, 0x08, 0x10, 0x08],
 '\x7f': [0x00, 0x7e, 0x42, 0x42, 0x7e],
}

ORIGINAL_CUSTOM = FONT['\x7f']


def bit_reverse(value, width=8):
 result = 0
 for _ in xrange(width):
  result = (result << 1) | (value & 1)
  value >>= 1

  return result


BITREVERSE = map(bit_reverse, xrange(256))


def setup():
 # Set pin directions.
 wiringpi.wiringPiSetup()
#  wiringpi.wiringPiGpioMode(ON)
 for pin in [DIN, SCLK, DC, RST, SCE]:
  wiringpi.pinMode(pin, ON)

 wiringpi.pinMode(LED,2)
 wiringpi.pwmWrite(LED,128)


def SPI(value):
 # data = DIN
 # clock = SCLK
 # MSB first
 for i in reversed(xrange(8)):
  wiringpi.digitalWrite(DIN, (value >> i) & 1)
  wiringpi.digitalWrite(SCLK, ON)
  wiringpi.digitalWrite(SCLK, OFF)


def lcd_cmd(value):
 wiringpi.digitalWrite(DC, OFF)
 SPI(value)


def lcd_data(value):
 wiringpi.digitalWrite(DC, ON)
 SPI(value)


def gotoxy(x, y):
 lcd_cmd(x + 128)
 lcd_cmd(y + 64)


def cls():
 gotoxy(0, 0)
 for _ in xrange(ROWS * COLUMNS * PIXELS_PER_ROW):
  lcd_data(0)


def begin(contrast):
 setup()
 # Toggle RST low to reset.
 wiringpi.digitalWrite(SCE, OFF)
 wiringpi.digitalWrite(RST, OFF)
 wiringpi.digitalWrite(LED, ON)
 time.sleep(0.100)
 wiringpi.digitalWrite(RST, ON)
 # Extended mode, bias, vop, basic mode, non-inverted display.
 for value in [0x21, 0x14, contrast, 0x20, 0x0c]:
  lcd_cmd(value) # extended mode 
 cls()


def init():
 begin(0xbb)


def led(led_value):
 wiringpi.digitalWrite(LED, led_value)


def switch_led(led_value):
 wiringpi.pwmWrite(LED, led_value);


def load_bitmap(filename, reverse=False):
 mask = 0xff if reverse else 0x00
 gotoxy(0, 0)
 with open(filename, 'rb') as bitmap_file:
  for x in xrange(6):
   for y in xrange(84):
    bitmap_file.seek(0x3e + y * 8 + x)
    lcd_data(BITREVERSE[ord(bitmap_file.read(1))] ^ mask)


def show_custom(font=FONT):
 display_char('\x7f', font)


def define_custom(values):
 FONT['\x7f'] = values


def restore_custom():
 define_custom(ORIGINAL_CUSTOM)


def alt_custom():
 define_custom([0x00, 0x50, 0x3C, 0x52, 0x44])


def pi_custom():
 define_custom([0x19, 0x25, 0x5A, 0x25, 0x19])


def display_char(char, font=FONT):
 try:
  for value in font[char]:
   lcd_data(value)

  lcd_data(0) # Space inbetween characters.
 except KeyError:
  pass # Ignore undefined characters.


def text(string, font=FONT):
 for char in string:
  display_char(char, font)


def gotorc(r, c):
 lcd_cmd(c * 6 + 128)
 lcd_cmd(r + 64)


def centre_word(r, word):
 gotorc(r, max(0, (COLUMNS - len(word)) // 2))
 text(word)


def main():
 start, end = 32, 116
 print 'LCD Display Test: ASCII %d to %d' % (start, end)
 start_time = time.time()
 init()
 led(1)
 for i in xrange(start, end + 1):
  display_char(chr(i))
  
 finish_time = time.time()
 print 'Init, LED on, %d chars, total time = %.2f' % (
  end - start, finish_time - start_time
  )
 print


if __name__ == '__main__':
 main()
Somewhere along the way I had to "apt-get install git-core python-dev python-setup-tools build-essential python-setuptools"

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sat Nov 10, 2012 9:53 am

Hi,
it works very well :)
Some useful additional features would be :
1..able to change the contrast value
2..as well as backlight on/off, also change the pwm value
3..import bitmap picture

Many thanks for sharing,

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
dliloch
Posts: 168
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: Nokia Pi LCD

Sat Nov 10, 2012 2:04 pm

in case anyone wanted the lcd display for a fritzing diagram under contributed
parts...

http://code.google.com/p/fritzing/issues/detail?id=1933

paultnl
Posts: 103
Joined: Fri Oct 12, 2012 6:25 pm
Location: Cambridge

Re: Nokia Pi LCD

Sat Nov 10, 2012 6:18 pm

OS1 sorry for being a NOOb but I get this when I try to run your code

Code: Select all

[email protected] ~ $ sudo python os1.py
Traceback (most recent call last):
  File "os1.py", line 6, in <module>
    import LCD
ImportError: No module named LCD
Can you let me know the silly thing that I missed.
I don't like Russian dolls, they are so full of themselves

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sat Nov 10, 2012 7:26 pm

Did you save the 2nd program OS1 listed as LCD.py?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

paultnl
Posts: 103
Joined: Fri Oct 12, 2012 6:25 pm
Location: Cambridge

Re: Nokia Pi LCD

Sat Nov 10, 2012 7:41 pm

yes
I don't like Russian dolls, they are so full of themselves

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sat Nov 10, 2012 7:55 pm

And the other program(the one that you run) is in the same directory?
And you are running the program after starting X?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

paultnl
Posts: 103
Joined: Fri Oct 12, 2012 6:25 pm
Location: Cambridge

Re: Nokia Pi LCD

Sat Nov 10, 2012 9:19 pm

Yes in x and the same directory. Do I have to do anything to make lcd.py a module?
I don't like Russian dolls, they are so full of themselves

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sun Dec 02, 2012 4:20 pm

Many thanks to OS1 for introducing me to TKinter. I have modified his original code to the features i wanted, namely pwm control and contrast settings. Firstly you need to create LCD.py from this code :

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# code improvements
# 2/12/12

import time
import wiringpi

ROWS = 6
COLUMNS = 14
PIXELS_PER_ROW = 6
ON = 1
OFF = 0

#gpio's :
SCE = 2 # gpio pin 13 = wiringpi no. 2
SCLK = 5 # gpio pin 18 = wiringpi no. 5
DIN = 4 # gpio pin 16 = wiringpi no. 4
DC = 3 # gpio pin 15 = wiringpi no. 3
RST = 0 # gpio pin 11 = wiringpi no. 0
LED = 1 # gpio pin 12 = wiringpi no. 1

FONT = {
 ' ': [0x00, 0x00, 0x00, 0x00, 0x00],
 '!': [0x00, 0x00, 0x5f, 0x00, 0x00],
 '"': [0x00, 0x07, 0x00, 0x07, 0x00],
 '#': [0x14, 0x7f, 0x14, 0x7f, 0x14],
 '$': [0x24, 0x2a, 0x7f, 0x2a, 0x12],
 '%': [0x23, 0x13, 0x08, 0x64, 0x62],
 '&': [0x36, 0x49, 0x55, 0x22, 0x50],
 "'": [0x00, 0x05, 0x03, 0x00, 0x00],
 '(': [0x00, 0x1c, 0x22, 0x41, 0x00],
 ')': [0x00, 0x41, 0x22, 0x1c, 0x00],
 '*': [0x14, 0x08, 0x3e, 0x08, 0x14],
 '+': [0x08, 0x08, 0x3e, 0x08, 0x08],
 ',': [0x00, 0x50, 0x30, 0x00, 0x00],
 '-': [0x08, 0x08, 0x08, 0x08, 0x08],
 '.': [0x00, 0x60, 0x60, 0x00, 0x00],
 '/': [0x20, 0x10, 0x08, 0x04, 0x02],
 '0': [0x3e, 0x51, 0x49, 0x45, 0x3e],
 '1': [0x00, 0x42, 0x7f, 0x40, 0x00],
 '2': [0x42, 0x61, 0x51, 0x49, 0x46],
 '3': [0x21, 0x41, 0x45, 0x4b, 0x31],
 '4': [0x18, 0x14, 0x12, 0x7f, 0x10],
 '5': [0x27, 0x45, 0x45, 0x45, 0x39],
 '6': [0x3c, 0x4a, 0x49, 0x49, 0x30],
 '7': [0x01, 0x71, 0x09, 0x05, 0x03],
 '8': [0x36, 0x49, 0x49, 0x49, 0x36],
 '9': [0x06, 0x49, 0x49, 0x29, 0x1e],
 ':': [0x00, 0x36, 0x36, 0x00, 0x00],
 ';': [0x00, 0x56, 0x36, 0x00, 0x00],
 '<': [0x08, 0x14, 0x22, 0x41, 0x00],
 '=': [0x14, 0x14, 0x14, 0x14, 0x14],
 '>': [0x00, 0x41, 0x22, 0x14, 0x08],
 '?': [0x02, 0x01, 0x51, 0x09, 0x06],
 '@': [0x32, 0x49, 0x79, 0x41, 0x3e],
 'A': [0x7e, 0x11, 0x11, 0x11, 0x7e],
 'B': [0x7f, 0x49, 0x49, 0x49, 0x36],
 'C': [0x3e, 0x41, 0x41, 0x41, 0x22],
 'D': [0x7f, 0x41, 0x41, 0x22, 0x1c],
 'E': [0x7f, 0x49, 0x49, 0x49, 0x41],
 'F': [0x7f, 0x09, 0x09, 0x09, 0x01],
 'G': [0x3e, 0x41, 0x49, 0x49, 0x7a],
 'H': [0x7f, 0x08, 0x08, 0x08, 0x7f],
 'I': [0x00, 0x41, 0x7f, 0x41, 0x00],
 'J': [0x20, 0x40, 0x41, 0x3f, 0x01],
 'K': [0x7f, 0x08, 0x14, 0x22, 0x41],
 'L': [0x7f, 0x40, 0x40, 0x40, 0x40],
 'M': [0x7f, 0x02, 0x0c, 0x02, 0x7f],
 'N': [0x7f, 0x04, 0x08, 0x10, 0x7f],
 'O': [0x3e, 0x41, 0x41, 0x41, 0x3e],
 'P': [0x7f, 0x09, 0x09, 0x09, 0x06],
 'Q': [0x3e, 0x41, 0x51, 0x21, 0x5e],
 'R': [0x7f, 0x09, 0x19, 0x29, 0x46],
 'S': [0x46, 0x49, 0x49, 0x49, 0x31],
 'T': [0x01, 0x01, 0x7f, 0x01, 0x01],
 'U': [0x3f, 0x40, 0x40, 0x40, 0x3f],
 'V': [0x1f, 0x20, 0x40, 0x20, 0x1f],
 'W': [0x3f, 0x40, 0x38, 0x40, 0x3f],
 'X': [0x63, 0x14, 0x08, 0x14, 0x63],
 'Y': [0x07, 0x08, 0x70, 0x08, 0x07],
 'Z': [0x61, 0x51, 0x49, 0x45, 0x43],
 '[': [0x00, 0x7f, 0x41, 0x41, 0x00],
 '\\': [0x02, 0x04, 0x08, 0x10, 0x20],
 ']': [0x00, 0x41, 0x41, 0x7f, 0x00],
 '^': [0x04, 0x02, 0x01, 0x02, 0x04],
 '_': [0x40, 0x40, 0x40, 0x40, 0x40],
 '`': [0x00, 0x01, 0x02, 0x04, 0x00],
 'a': [0x20, 0x54, 0x54, 0x54, 0x78],
 'b': [0x7f, 0x48, 0x44, 0x44, 0x38],
 'c': [0x38, 0x44, 0x44, 0x44, 0x20],
 'd': [0x38, 0x44, 0x44, 0x48, 0x7f],
 'e': [0x38, 0x54, 0x54, 0x54, 0x18],
 'f': [0x08, 0x7e, 0x09, 0x01, 0x02],
 'g': [0x0c, 0x52, 0x52, 0x52, 0x3e],
 'h': [0x7f, 0x08, 0x04, 0x04, 0x78],
 'i': [0x00, 0x44, 0x7d, 0x40, 0x00],
 'j': [0x20, 0x40, 0x44, 0x3d, 0x00],
 'k': [0x7f, 0x10, 0x28, 0x44, 0x00],
 'l': [0x00, 0x41, 0x7f, 0x40, 0x00],
 'm': [0x7c, 0x04, 0x18, 0x04, 0x78],
 'n': [0x7c, 0x08, 0x04, 0x04, 0x78],
 'o': [0x38, 0x44, 0x44, 0x44, 0x38],
 'p': [0x7c, 0x14, 0x14, 0x14, 0x08],
 'q': [0x08, 0x14, 0x14, 0x18, 0x7c],
 'r': [0x7c, 0x08, 0x04, 0x04, 0x08],
 's': [0x48, 0x54, 0x54, 0x54, 0x20],
 't': [0x04, 0x3f, 0x44, 0x40, 0x20],
 'u': [0x3c, 0x40, 0x40, 0x20, 0x7c],
 'v': [0x1c, 0x20, 0x40, 0x20, 0x1c],
 'w': [0x3c, 0x40, 0x30, 0x40, 0x3c],
 'x': [0x44, 0x28, 0x10, 0x28, 0x44],
 'y': [0x0c, 0x50, 0x50, 0x50, 0x3c],
 'z': [0x44, 0x64, 0x54, 0x4c, 0x44],
 '{': [0x00, 0x08, 0x36, 0x41, 0x00],
 '|': [0x00, 0x00, 0x7f, 0x00, 0x00],
 '}': [0x00, 0x41, 0x36, 0x08, 0x00],
 '~': [0x10, 0x08, 0x08, 0x10, 0x08],
 '\x7f': [0x00, 0x7e, 0x42, 0x42, 0x7e],
}

ORIGINAL_CUSTOM = FONT['\x7f']


def bit_reverse(value, width=8):
 result = 0
 for _ in xrange(width):
  result = (result << 1) | (value & 1)
  value >>= 1

  return result


BITREVERSE = map(bit_reverse, xrange(256))


def setup():
 # Set pin directions.
 wiringpi.wiringPiSetup()
#  wiringpi.wiringPiGpioMode(ON)
 for pin in [DIN, SCLK, DC, RST, SCE]:
  wiringpi.pinMode(pin, ON)

 wiringpi.pinMode(LED,2)
 wiringpi.pwmWrite(LED,128)


def SPI(value):
 # data = DIN
 # clock = SCLK
 # MSB first
 for i in reversed(xrange(8)):
  wiringpi.digitalWrite(DIN, (value >> i) & 1)
  wiringpi.digitalWrite(SCLK, ON)
  wiringpi.digitalWrite(SCLK, OFF)


def lcd_cmd(value):
 wiringpi.digitalWrite(DC, OFF)
 SPI(value)


def lcd_data(value):
 wiringpi.digitalWrite(DC, ON)
 SPI(value)


def gotoxy(x, y):
 lcd_cmd(x + 128)
 lcd_cmd(y + 64)


def cls():
 gotoxy(0, 0)
 for _ in xrange(ROWS * COLUMNS * PIXELS_PER_ROW):
  lcd_data(0)


def begin(contrast):
 setup()
 # Toggle RST low to reset.
 wiringpi.digitalWrite(SCE, OFF)
 wiringpi.digitalWrite(RST, OFF)
 wiringpi.digitalWrite(LED, ON)
 time.sleep(0.100)
 wiringpi.digitalWrite(RST, ON)
 # Extended mode, bias, vop, basic mode, non-inverted display.
 for value in [0x21, 0x14, contrast, 0x20, 0x0c]:
  lcd_cmd(value) # extended mode 
 cls()


def init():
 begin(0xbb)


def led(led_value):
 wiringpi.pwmWrite(LED, led_value)
# print led_value


def switch_led(led_value):
    print "got here"
    print led_value
    if led_value==0:
        wiringpi.pinMode(LED,1)
        wiringpi.digitalWrite(LED,0)
    else:
        wiringpi.pinMode(LED,2)
        wiringpi.pwmWrite(LED, 128);


def load_bitmap(filename, reverse=False):
 mask = 0xff if reverse else 0x00
 gotoxy(0, 0)
 with open(filename, 'rb') as bitmap_file:
  for x in xrange(6):
   for y in xrange(84):
    bitmap_file.seek(0x3e + y * 8 + x)
    lcd_data(BITREVERSE[ord(bitmap_file.read(1))] ^ mask)


def show_custom(font=FONT):
 display_char('\x7f', font)


def define_custom(values):
 FONT['\x7f'] = values


def restore_custom():
 define_custom(ORIGINAL_CUSTOM)


def alt_custom():
 define_custom([0x00, 0x50, 0x3C, 0x52, 0x44])


def pi_custom():
 define_custom([0x19, 0x25, 0x5A, 0x25, 0x19])


def display_char(char, font=FONT):
 try:
  for value in font[char]:
   lcd_data(value)

  lcd_data(0) # Space inbetween characters.
 except KeyError:
  pass # Ignore undefined characters.


def text(string, font=FONT):
 for char in string:
  display_char(char, font)


def gotorc(r, c):
 lcd_cmd(c * 6 + 128)
 lcd_cmd(r + 64)


def centre_word(r, word):
 gotorc(r, max(0, (COLUMNS - len(word)) // 2))
 text(word)


def main():
 start, end = 32, 116
 print 'LCD Display Test: ASCII %d to %d' % (start, end)
 start_time = time.time()
 init()
 led(1)
 for i in xrange(start, end + 1):
  display_char(chr(i))
  
 finish_time = time.time()
 print 'Init, LED on, %d chars, total time = %.2f' % (
  end - start, finish_time - start_time
  )
 print


if __name__ == '__main__':
 main()

Then the main code from this listing :

Code: Select all

import LCD
from LCD import *
import Tkinter
from Tkinter import *
import tkFont


        
class App:

    def __init__(self, parent):

        frame = Frame(parent);
        frame.pack();

        parent.title("Nokia Pi LCD GUI");
 
        self.txtLabel = Label(frame, text="Text");
        self.txtLabel.grid(row=0, column=1);

        self.txtEntry = Text(frame, width=16, height=5, font=("systemfixed", 12));
        self.txtEntry.grid(row=0, column=2, columnspan=1);

        self.sndButt = Button(frame, text="Send", command=self.send_txt);
        self.sndButt.grid(row=0, column=4, padx=5, pady=5, sticky=W);

        self.txtLabel = Label(frame, text="X: ");
        self.txtLabel.grid(row=1, column=1, sticky=E);
        self.xEntry = Entry(frame);
        self.xEntry.grid(row=1, column=2, pady=5);

        self.txtLabel = Label(frame, text="Y: ");
        self.txtLabel.grid(row=2, column=1, sticky=E);
        self.yEntry = Entry(frame);
        self.yEntry.grid(row=2, column=2, pady=5);

        self.lightVar = IntVar();
        self.chkB = Checkbutton(frame, text="Backlight On/Off", variable=self.lightVar, offvalue=0, onvalue=1, command=self.back_off);
        self.chkB.grid(row=3, columnspan=3, sticky=W);
        self.chkB.select();

        self.clsButt = Button(frame, text="CLS", command=LCD.cls);
        self.clsButt.grid(row=3, column=4, padx=5, pady=5);

        self.quitButt = Button(frame, text="Quit", fg="red", command=frame.quit);
        self.quitButt.grid(row=4, column=4);

    def send_txt(self):
        try:
            LCD.gotoxy(int(self.xEntry.get()), int(self.yEntry.get()));
        except ValueError:
            LCD.gotoxy(0, 0);
        LCD.text(self.txtEntry.get(1.0, END));

    def back_off(self):
        LCD.switch_led(self.lightVar.get());

def sel_con():
    contrast = var.get()
#    selection = "Contrast = " + str(value)
#    label.config(text = selection)
    print int(contrast)
    LCD.begin(int(contrast))

def sel_pwm():
    pwm = var2.get()
#    selection = "Contrast = " + str(value)
#    label.config(text = selection)
    print int(pwm)
    LCD.led(int(pwm))
   
root = Tk();
LCD.init();

var = IntVar()
scale = Scale( root, orient = HORIZONTAL, from_ = 160, to = 207, variable = var )
scale.pack(anchor=CENTER)
button = Button(root, text="Contrast Setting", command=sel_con)
button.pack(anchor=CENTER)

var2 = IntVar()
scale2 = Scale( root, orient = HORIZONTAL, from_ = 0, to = 255, variable = var2 )
scale2.pack(anchor=CENTER)
button2 = Button(root, text="Backlight PWM", command=sel_pwm)
button2.pack(anchor=CENTER)

def callback():
    print "called the callback!";

# create a toolbar
toolbar = Frame(root);


toolbar.pack(side=TOP, fill=X);

label = Label(root)
label.pack()
app = App(root);

root.mainloop();
You'll need to have wiringpi for python installed as well as TKInker of course.
New style LCD with white backlight :
Image
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
tzj
Posts: 302
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK

Re: Nokia Pi LCD

Sun Dec 02, 2012 6:43 pm

I got it working now, had the sclk on pin 19 instead of 18...

Only prob now is the message shows for about a second then goes black, glad the screen responds tho!
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sun Dec 02, 2012 7:00 pm

Does it go blank once the program has terminated? (show us your program ;) )

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
tzj
Posts: 302
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK

Re: Nokia Pi LCD

Sun Dec 02, 2012 7:21 pm

The same one as 'Paultnl 8th nov'

The screen is a 5110, is does show very faintly under the black the text but as far as I can see its characters are too wide and could be too big... so its working but needs a tweak some how.
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sun Dec 02, 2012 7:39 pm

Well it could be a contrast setting - they do vary from display to display. I should know - I,ve tested 35 of them today :D

I highly recommend you install TKinter and try the programs I,ve posted today. That way you can tweak the contrast and backlight setting without editing the program.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
tzj
Posts: 302
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK

Re: Nokia Pi LCD

Sun Dec 02, 2012 7:53 pm

Well I've sort of solved the character width problem, in the 'def display_char' the one next to space between characters comment I commented out but now there's no space between characters... but am getting there :)

Whats the range of the contrast settings? (For learning purposes)

Once I have the screen working properly then I'll try the newer one.
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Nokia Pi LCD

Sun Dec 02, 2012 8:11 pm

Decimal 160 to 207 is the slider range I have in the program. It's typically 165 to 180.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
tzj
Posts: 302
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK

Re: Nokia Pi LCD

Sun Dec 02, 2012 8:50 pm

You may want to extend that range, my optimum is between 145-146 (0x91-0x92) 145 flickers slightly while 146 shows very faint lines but both OK looking directly at it.
140 is the limit, it flickers quite a bit and fairly light.

I'd rather not go beyond 207 (0xcf)
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

duncanbojangles
Posts: 1
Joined: Fri Dec 07, 2012 7:58 am

Re: Nokia Pi LCD

Tue Dec 11, 2012 3:18 am

I don't know Python, but I managed to get the code into a class module of its own. If it's of any use to anyone, here's the code. (I was having trouble with the BITREVERSE code so I put it in __init__, sorry).

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import wiringpi

class nokia_lcd:

    def __init__(self):
        self.BITREVERSE = map(self.bit_reverse, xrange(256))

    ROWS = 0x06
    COLUMNS = 14
    PIXELS_PER_ROW = 0x06
    ON = 0x01
    OFF = 0x00

    SCE = 0x02  # gpio pin 13 = wiringpi no. 2
    SCLK = 0x05  # gpio pin 18 = wiringpi no. 5
    DIN = 0x04  # gpio pin 16 = wiringpi no. 4
    DC = 0x03  # gpio pin 15 = wiringpi no. 3
    RST = 0x01  # gpio pin 11 = wiringpi no. 0
    LED = 0x00  # gpio pin 12 = wiringpi no. 1

    FONT = {
        ' ': [0x00, 0x00, 0x00, 0x00, 0x00],
        '!': [0x00, 0x00, 0x5f, 0x00, 0x00],
        '"': [0x00, 0x07, 0x00, 0x07, 0x00],
        '#': [0x14, 0x7f, 0x14, 0x7f, 0x14],
        '$': [0x24, 0x2a, 0x7f, 0x2a, 0x12],
        '%': [0x23, 0x13, 0x08, 0x64, 0x62],
        '&': [0x36, 0x49, 0x55, 0x22, 0x50],
        "'": [0x00, 0x05, 0x03, 0x00, 0x00],
        '(': [0x00, 0x1c, 0x22, 0x41, 0x00],
        ')': [0x00, 0x41, 0x22, 0x1c, 0x00],
        '*': [0x14, 0x08, 0x3e, 0x08, 0x14],
        '+': [0x08, 0x08, 0x3e, 0x08, 0x08],
        ',': [0x00, 0x50, 0x30, 0x00, 0x00],
        '-': [0x08, 0x08, 0x08, 0x08, 0x08],
        '.': [0x00, 0x60, 0x60, 0x00, 0x00],
        '/': [0x20, 0x10, 0x08, 0x04, 0x02],
        '0': [0x3e, 0x51, 0x49, 0x45, 0x3e],
        '1': [0x00, 0x42, 0x7f, 0x40, 0x00],
        '2': [0x42, 0x61, 0x51, 0x49, 0x46],
        '3': [0x21, 0x41, 0x45, 0x4b, 0x31],
        '4': [0x18, 0x14, 0x12, 0x7f, 0x10],
        '5': [0x27, 0x45, 0x45, 0x45, 0x39],
        '6': [60, 0x4a, 0x49, 0x49, 0x30],
        '7': [0x01, 0x71, 0x09, 0x05, 0x03],
        '8': [0x36, 0x49, 0x49, 0x49, 0x36],
        '9': [0x06, 0x49, 0x49, 0x29, 0x1e],
        ':': [0x00, 0x36, 0x36, 0x00, 0x00],
        ';': [0x00, 0x56, 0x36, 0x00, 0x00],
        '<': [0x08, 0x14, 0x22, 0x41, 0x00],
        '=': [0x14, 0x14, 0x14, 0x14, 0x14],
        '>': [0x00, 0x41, 0x22, 0x14, 0x08],
        '?': [0x02, 0x01, 0x51, 0x09, 0x06],
        '@': [0x32, 0x49, 0x79, 0x41, 0x3e],
        'A': [0x7e, 0x11, 0x11, 0x11, 0x7e],
        'B': [0x7f, 0x49, 0x49, 0x49, 0x36],
        'C': [0x3e, 0x41, 0x41, 0x41, 0x22],
        'D': [0x7f, 0x41, 0x41, 0x22, 0x1c],
        'E': [0x7f, 0x49, 0x49, 0x49, 0x41],
        'F': [0x7f, 0x09, 0x09, 0x09, 0x01],
        'G': [0x3e, 0x41, 0x49, 0x49, 0x7a],
        'H': [0x7f, 0x08, 0x08, 0x08, 0x7f],
        'I': [0x00, 0x41, 0x7f, 0x41, 0x00],
        'J': [0x20, 0x40, 0x41, 0x3f, 0x01],
        'K': [0x7f, 0x08, 0x14, 0x22, 0x41],
        'L': [0x7f, 0x40, 0x40, 0x40, 0x40],
        'M': [0x7f, 0x02, 0x0c, 0x02, 0x7f],
        'N': [0x7f, 0x04, 0x08, 0x10, 0x7f],
        'O': [0x3e, 0x41, 0x41, 0x41, 0x3e],
        'P': [0x7f, 0x09, 0x09, 0x09, 0x06],
        'Q': [0x3e, 0x41, 0x51, 0x21, 0x5e],
        'R': [0x7f, 0x09, 0x19, 0x29, 0x46],
        'S': [0x46, 0x49, 0x49, 0x49, 0x31],
        'T': [0x01, 0x01, 0x7f, 0x01, 0x01],
        'U': [0x3f, 0x40, 0x40, 0x40, 0x3f],
        'V': [0x1f, 0x20, 0x40, 0x20, 0x1f],
        'W': [0x3f, 0x40, 0x38, 0x40, 0x3f],
        'X': [0x63, 0x14, 0x08, 0x14, 0x63],
        'Y': [0x07, 0x08, 0x70, 0x08, 0x07],
        'Z': [0x61, 0x51, 0x49, 0x45, 0x43],
        '[': [0x00, 0x7f, 0x41, 0x41, 0x00],
        '\\': [0x02, 0x04, 0x08, 0x10, 0x20],
        ']': [0x00, 0x41, 0x41, 0x7f, 0x00],
        '^': [0x04, 0x02, 0x01, 0x02, 0x04],
        '_': [0x40, 0x40, 0x40, 0x40, 0x40],
        '`': [0x00, 0x01, 0x02, 0x04, 0x00],
        'a': [0x20, 0x54, 0x54, 0x54, 0x78],
        'b': [0x7f, 0x48, 0x44, 0x44, 0x38],
        'c': [0x38, 0x44, 0x44, 0x44, 0x20],
        'd': [0x38, 0x44, 0x44, 0x48, 0x7f],
        'e': [0x38, 0x54, 0x54, 0x54, 0x18],
        'f': [0x08, 0x7e, 0x09, 0x01, 0x02],
        'g': [0x0c, 0x52, 0x52, 0x52, 0x3e],
        'h': [0x7f, 0x08, 0x04, 0x04, 0x78],
        'i': [0x00, 0x44, 0x7d, 0x40, 0x00],
        'j': [0x20, 0x40, 0x44, 0x3d, 0x00],
        'k': [0x7f, 0x10, 0x28, 0x44, 0x00],
        'l': [0x00, 0x41, 0x7f, 0x40, 0x00],
        'm': [0x7c, 0x04, 0x18, 0x04, 0x78],
        'n': [0x7c, 0x08, 0x04, 0x04, 0x78],
        'o': [0x38, 0x44, 0x44, 0x44, 0x38],
        'p': [0x7c, 0x14, 0x14, 0x14, 0x08],
        'q': [0x08, 0x14, 0x14, 0x18, 0x7c],
        'r': [0x7c, 0x08, 0x04, 0x04, 0x08],
        's': [0x48, 0x54, 0x54, 0x54, 0x20],
        't': [0x04, 0x3f, 0x44, 0x40, 0x20],
        'u': [60, 0x40, 0x40, 0x20, 0x7c],
        'v': [0x1c, 0x20, 0x40, 0x20, 0x1c],
        'w': [60, 0x40, 0x30, 0x40, 60],
        'x': [0x44, 0x28, 0x10, 0x28, 0x44],
        'y': [0x0c, 0x50, 0x50, 0x50, 60],
        'z': [0x44, 0x64, 0x54, 0x4c, 0x44],
        '{': [0x00, 0x08, 0x36, 0x41, 0x00],
        '|': [0x00, 0x00, 0x7f, 0x00, 0x00],
        '}': [0x00, 0x41, 0x36, 0x08, 0x00],
        '~': [0x10, 0x08, 0x08, 0x10, 0x08],
        '\x7f': [0x00, 0x7e, 0x42, 0x42, 0x7e],
        }

    ORIGINAL_CUSTOM = FONT['\x7f']

    def bit_reverse(self, value, width=0x08):
        result = 0x00
        for _ in xrange(width):
            result = result << 0x01 | value & 0x01
            value >>= 0x01

        return result

#    BITREVERSE = map(bit_reverse, xrange(256))

    def setup(self):
        wiringpi.wiringPiSetup()

        for pin in [self.DIN, self.SCLK, self.DC, self.RST, self.SCE]:
            wiringpi.pinMode(pin, self.ON)

        wiringpi.pinMode(self.LED, 0x02)
        wiringpi.pwmWrite(self.LED, 128)

    def SPI(self, value):
        for i in reversed(xrange(0x08)):
            wiringpi.digitalWrite(self.DIN, value >> i & 0x01)
            wiringpi.digitalWrite(self.SCLK, self.ON)
            wiringpi.digitalWrite(self.SCLK, self.OFF)

    def lcd_cmd(self, value):
        wiringpi.digitalWrite(self.DC, self.OFF)
        self.SPI(value)

    def lcd_data(self, value):
        wiringpi.digitalWrite(self.DC, self.ON)
        self.SPI(value)

    def gotoxy(self, x, y):
        self.lcd_cmd(x + 128)
        self.lcd_cmd(y + 0x40)

    def cls(self):
        self.gotoxy(0x00, 0x00)
        for _ in xrange((self.ROWS * self.COLUMNS) * self.PIXELS_PER_ROW):
            self.lcd_data(0x00)

    def begin(self, contrast):
        self.setup()

        wiringpi.digitalWrite(self.SCE, self.OFF)
        wiringpi.digitalWrite(self.RST, self.OFF)
        wiringpi.digitalWrite(self.LED, self.ON)
        time.sleep(0.100)
        wiringpi.digitalWrite(self.RST, self.ON)

        for value in [0x21, 0x14, contrast, 0x20, 0x0c]:
            self.lcd_cmd(value)  # extended mode
        self.cls()

    def init(self):
        self.begin(0xc3)

    def led(self, led_value):
        wiringpi.digitalWrite(self.LED, led_value)
    
	def switch_led(self, led_value):
		if led_value==0:
			wiringpi.pinMode(self.LED,1)
			wiringpi.digitalWrite(self.LED,0)
		else:
			wiringpi.pinMode(self.LED,2)
			wiringpi.pwmWrite(self.LED, 128);    

    def load_bitmap(self, filename, reverse=False):
        mask = 0xff if reverse else 0x00
        self.gotoxy(0x00, 0x00)
        with open(filename, 'rb') as bitmap_file:
            for x in xrange(0x06):
                for y in xrange(0x54):
                    bitmap_file.seek(0x3e + y * 0x08 + x)
                    lcd_data(self.BITREVERSE[ord(bitmap_file.read(0x01))] ^ mask)

    def show_custom(self, font=FONT):
        self.display_char('\x7f', font)

    def define_custom(self, values):
        self.FONT['\x7f'] = values

    def restore_custom(self):
        self.define_custom(self.ORIGINAL_CUSTOM)

    def alt_custom(self):
        self.define_custom([0x00, 0x50, 60, 0x52, 0x44])

    def pi_custom(self):
        self.define_custom([0x19, 0x25, 0x5A, 0x25, 0x19])

    def display_char(self, char, font=FONT):
        try:
            for value in font[char]:
                self.lcd_data(value)

            self.lcd_data(0x00)  # Space inbetween characters.
        except KeyError:
            pass  # Ignore undefined characters.

    def text(self, string, font=FONT):
        for char in string:
            self.display_char(char, font)

    def gotorc(self, r, c):
        self.lcd_cmd(c * 0x06 + 128)
        self.lcd_cmd(r + 0x40)

    def centre_word(self, r, word):
        self.gotorc(r, max(0x00, (self.COLUMNS - len(word)) // 0x02))
        self.text(word)
And this is how I used it in main():

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# code improvements
#  9/10/12

import time
import wiringpi
from nokia_lcd_class import nokia_lcd

lcd = nokia_lcd()

def main():
  start, end = 32, 116
  print 'LCD Display Test: ASCII %d to %d' % (start, end)
  start_time = time.time()
  lcd.init()
  lcd.led(1)
  for i in xrange(start, end + 1):
    lcd.display_char(chr(i))
    
  finish_time = time.time()
  print 'Init, LED on, %d chars, total time = %.2f' % (
    end - start, finish_time - start_time
    )
  print

if __name__ == '__main__':
  main()

User avatar
RaTTuS
Posts: 10506
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Nokia Pi LCD

Tue Dec 11, 2012 12:02 pm

Texy - could you post the circuit diagram ?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Python”