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

Tue Jan 22, 2013 5:24 pm

scruss wrote:
alexeames wrote:And now it's on the front page :D
Indeed it is! Bit surprised by that one, but glad people like it. Just as well I didn't post the version that randomly posts QR codes of rude URLs 1% of the time ... ;)
Would have been funny if you had. :lol:
Alex Eames RasPi.TV, RasP.iO

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: Nokia Pi LCD

Tue Jan 22, 2013 9:20 pm

bgreat wrote:Same software configuration I am using. I have two displays that are working without issue on my Rev 2 board. I will try my Rev 1 board tonight to see if it works differently.

One more question. How long is the interconnect from the Raspberry Pi to the display? Just so I can try a similar connection.
Hi Bill,

As you have already concluded, I am using a version 1 board which has all of the updates. The cable connection from the Pi to the breadboard is via a Adafruit cobbler and is 157mm between the two ribbon cable connectors. From the bread board there are 16.5mm jumper wires. The Nokia LCD is at one end of the breadboard. The Nokia LCD under test is the Adafruit board.

Thanks and best regards,

Will

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: Nokia Pi LCD

Tue Jan 22, 2013 10:51 pm

texy wrote: Here is some code that will display a bitmap image. You can create the image in Paint for windows, making the size 48 x 84 pixels and saving it as a monochrome bitmap. Here is an example :
Image
You'll need to save this on your Pi in the same directory as the following program, making sure the full filename of the bitmap is the same as in your program, in this case rasp.bmp :
Hi Texy,

While your earlier example of writing text on a Nokia LCD works as expected with my setup (version 1 model B Pi, fully up to date Raspbian image, Adafruit Nokia LCD and Adafruit cobbler), displaying this image does not work. The screen remains blank. The code from Bill also does not display the image. Do you have any ideas why the image might not be shown?

Code: Select all

$ identify rasp.bmp 
rasp.bmp BMP 48x84 48x84+0+0 1-bit PseudoClass 2c 734B 0.000u 0:00.000
Thanks and best regards,

Will

icepi
Posts: 1
Joined: Wed Jan 23, 2013 9:49 am

Re: Nokia Pi LCD

Wed Jan 23, 2013 9:52 am

I am trying out Cruss's Nokia LCD python routine, and when I run it I am getting a python SyntaxError: invalid syntax, on nokiaSPI.py line 31:

DC = 3
^

I can't get this to work, I am new to python (most famliar with PHP) -- any idea what's up with my nokiaSPI.py file? I copied it direct from the board.

Thanks for the help!

User avatar
scruss
Posts: 2615
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Nokia Pi LCD

Wed Jan 23, 2013 1:01 pm

I forgot to include bgreat's class in the zip archive. Fixed now.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

Re: Nokia Pi LCD

Wed Jan 23, 2013 8:28 pm

williamhbell wrote:
texy wrote: Here is some code that will display a bitmap image. You can create the image in Paint for windows, making the size 48 x 84 pixels and saving it as a monochrome bitmap. Here is an example :
Image
You'll need to save this on your Pi in the same directory as the following program, making sure the full filename of the bitmap is the same as in your program, in this case rasp.bmp :
Hi Texy,

While your earlier example of writing text on a Nokia LCD works as expected with my setup (version 1 model B Pi, fully up to date Raspbian image, Adafruit Nokia LCD and Adafruit cobbler), displaying this image does not work. The screen remains blank. The code from Bill also does not display the image. Do you have any ideas why the image might not be shown?

Code: Select all

$ identify rasp.bmp 
rasp.bmp BMP 48x84 48x84+0+0 1-bit PseudoClass 2c 734B 0.000u 0:00.000
Thanks and best regards,

Will
Hi Will,
Are you sure that the contrast setting is correct for your display?
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

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

Re: Nokia Pi LCD

Wed Jan 23, 2013 8:29 pm

icepi wrote:I am trying out Cruss's Nokia LCD python routine, and when I run it I am getting a python SyntaxError: invalid syntax, on nokiaSPI.py line 31:

DC = 3
^

I can't get this to work, I am new to python (most famliar with PHP) -- any idea what's up with my nokiaSPI.py file? I copied it direct from the board.

Thanks for the help!
Hi,
Please post the complete program and remember to use the code tag to show the correct indent.
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
scruss
Posts: 2615
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Nokia Pi LCD

Wed Jan 23, 2013 11:21 pm

texy wrote:Please post the complete program and remember to use the code tag to show the correct indent.
Nah, it's sorted now. It was an omission on my part in a zip archive of bgreat's code + my qrclock code. I've posted a revised archive (qrclock2.zip), and the poster has been in touch by e-mail.

Don't get me started on how hard Python is to paste into online forums, though ... syntactically-significant whitespace ... :roll:
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Wed Jan 23, 2013 11:27 pm

I was having problems with my Model B, Rev 1 (256MB) board. But this evening after fully updating the Raspbian image it is working flawlessly. I'm not sure what may have changed, but the SPI code works with Rev 1 and Rev 2 boards for me as of today. I am posting my code again, just in case there is some correction I unknowingly introduced.

nokiaSPI.py

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# code improvements
#  9/10/12
######
# WGG - picked up from Raspberry Pi forums and modified with a heavy hand
# -- added spidev support
# -- testing with PIL
# 16-Jan-2013
# -- initial NokiaSPI class

import time
import wiringpi
import spidev
from PIL import Image,ImageDraw,ImageFont

# White backlight
CONTRAST = 0xb8

# Blue backlight
#CONTRAST = 0xb4

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

#gpio's :
DC   = 3 # gpio pin 15 = wiringpi no. 3 (BCM 22)
RST  = 0 # gpio pin 11 = wiringpi no. 0 (BCM 17)
LED  = 1 # gpio pin 12 = wiringpi no. 1 (BCM 18)

# SPI connection
SCE  = 10 # gpio pin 24 = wiringpi no. 10 (CE0 BCM 8)
SCLK = 14 # gpio pin 23 = wiringpi no. 14 (SCLK BCM 11)
DIN  = 12 # gpio pin 19 = wiringpi no. 12 (MOSI BCM 10)

CLSBUF=[0]*(ROWS * COLUMNS * PIXELS_PER_ROW)

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))

class NokiaSPI:
    def __init__(self, dev=(0,0),speed=4000000, brightness=256, contrast=CONTRAST):
        self.spi = spidev.SpiDev()
        self.speed = speed
        self.dev = dev
        self.spi.open(self.dev[0],self.dev[1])
        self.spi.max_speed_hz=self.speed

        # Set pin directions.
        self.dc = DC
        self.rst = RST
        wiringpi.wiringPiSetup()
        for pin in [self.dc, self.rst]:
            wiringpi.pinMode(pin, 1)

        self.contrast=contrast
        self.brightness=brightness
        
        # Toggle RST low to reset.
        wiringpi.digitalWrite(self.rst, OFF)
        time.sleep(0.100)
        wiringpi.digitalWrite(self.rst, ON)
        # Extended mode, bias, vop, basic mode, non-inverted display.
        wiringpi.digitalWrite(self.dc, OFF)
        self.spi.writebytes([0x21, 0x14, self.contrast, 0x20, 0x0c])
        # cls()

        self.ledpin = LED
        if self.ledpin == 1:
            wiringpi.pinMode(self.ledpin, 2)
            wiringpi.pwmWrite(self.ledpin, self.brightness)
        else:
            wiringpi.pinMode(self.ledpin, 1)
            wiringpi.digitalWrite(self.ledpin, ON)



    def lcd_cmd(self,value):
        wiringpi.digitalWrite(self.dc, OFF)
        self.spi.writebytes([value])


    def lcd_data(self,value):
        wiringpi.digitalWrite(self.dc, ON)
        self.spi.writebytes([value])


    def gotoxy(self, x, y):
        wiringpi.digitalWrite(self.dc, OFF)
        self.spi.writebytes([x+128,y+64])


    def cls(self):
        self.gotoxy(0, 0)
        wiringpi.digitalWrite(self.dc, ON)
        self.spi.writebytes(CLSBUF)


    def led(self, led_value):
        if self.ledpin == 1:
            wiringpi.pwmWrite(self.ledpin,led_value)
        else:
            if led_value == 0:
                wiringpi.digitalWrite(self.ledpin, OFF)
            else:
                wiringpi.digitalWrite(self.ledpin, ON)


    def load_bitmap(self, filename, reverse=False):
        mask = 0xff if reverse else 0x00
        self.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)
                self.lcd_data(BITREVERSE[ord(bitmap_file.read(1))] ^ mask)


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


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


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


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


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


    def display_char(self, char, font=FONT):
        try:
            wiringpi.digitalWrite(self.dc, ON)
            self.spi.writebytes(font[char]+[0])

        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.gotoxy(c*6,r)


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

    def show_image(self,im):
        # Rotate and mirror the image
        rim = im.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)

        # Change display to vertical write mode for graphics
        wiringpi.digitalWrite(DC, OFF)
        self.spi.writebytes([0x22])

        # Start at upper left corner
        self.gotoxy(0, 0)
        # Put on display with reversed bit order
        wiringpi.digitalWrite(DC, ON)
        self.spi.writebytes( [ BITREVERSE[ord(x)] for x in list(rim.tostring()) ] )

        # Switch back to horizontal write mode for text
        wiringpi.digitalWrite(DC, OFF)
        self.spi.writebytes([0x20])

if __name__ == '__main__':
    start, end = 32, 116
    print 'LCD Display Test: ASCII %d to %d' % (start, end)
    # do not include init() in the timing tests
    ## init()
    noki = NokiaSPI()
    start_time = time.time()
    noki.cls()
    noki.led(768)
    for i in xrange(start, end):
        noki.display_char(chr(i))

    finish_time = time.time()
    print 'Cls, LED on, %d chars, total time = %.3f' % (
        end - start, finish_time - start_time
        )

    time.sleep(1)

    # Test a custom character for 0x7f (supposed to be a bell)
    # . . . - - - - -
    # . . . - - X - -
    # . . . - X X X -
    # . . . - X - X -
    # . . . X - - - X
    # . . . X X X X X
    # . . . - - X X -
    # . . . - - - - -
    noki.define_custom([0x30,0x2c,0x66,0x6c,0x30])

    noki.cls()
    noki.text("\x7f \x7f \x7f \x7f \x7f \x7f \x7f ")
    noki.text("    Hello     ")
    noki.text(" Raspberry Pi")

    # Backlight PWM testing -- off -> 25% -> off
    #for i in range(0,255):
    #  noki.led(i)
    #  time.sleep(0.025)
    #for i in range(255,0,-1):
    #  noki.led(i)
    #  time.sleep(0.025)

    time.sleep(1)

    ## Generate an image with PIL and put on the display
    ## First time through is slow as the fonts are not cached
    ##
    start_time = time.time()
    # load an available True Type font
    font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 14)

    # New b-w image
    im = Image.new('1', (84,48))
    # New drawable on image
    draw = ImageDraw.Draw(im)
    # Full screen and half-screen ellipses
    draw.ellipse((0,0,im.size[0]-1,im.size[1]-1), outline=1)
    draw.ellipse((im.size[0]/4,im.size[1]/4,im.size[0]/4*3-1,im.size[1]/4*3-1), outline=1)
    # Some simple text for a test (first with TT font, second with default
    draw.text((10,10), "hello", font=font, fill=1)
    draw.text((10,24), "world", fill=1)
    # Check what happens when text exceeds width (clipped)
    draw.text((0,0), "ABCabcDEFdefGHIghi", fill=1)
    # Copy it to the display
    noki.show_image(im)
    # clean up
    del draw
    del im

    finish_time = time.time()
    print 'PIL Drawing, total time = %.3f' % (finish_time - start_time)
And, if you have the lenna.png and raspi.bmp posted earlier in this thread, here is another test that reads and displays images plus does a font test using the python class nokiaSPI above.

spitest.py

Code: Select all

#!/usr/bin/python

import time
import os

import nokiaSPI

from PIL import Image,ImageDraw,ImageFont


noki = nokiaSPI.NokiaSPI(contrast=0xb4)

start_time = time.time()
noki.load_bitmap("raspi.bmp")
finish_time = time.time()
print 'BMP Load, total time = %.3f' % (finish_time - start_time)

time.sleep(1)

start_time = time.time()
noki.load_bitmap("raspi.bmp", True)
finish_time = time.time()
print 'BMP Load, total time = %.3f' % (finish_time - start_time)

time.sleep(1)

start_time = time.time()
noki.load_bitmap("raspi.bmp")
finish_time = time.time()
print 'BMP Load, total time = %.3f' % (finish_time - start_time)

time.sleep(1)

start_time = time.time()
noki.load_bitmap("raspi.bmp", True)
finish_time = time.time()
print 'BMP Load, total time = %.3f' % (finish_time - start_time)

time.sleep(1)

# Let's try some image manipulation
#start_time = time.time()
im = Image.open("lenna.png")
im = im.convert("L")
im.thumbnail((84,48))
for t in range(1,255):
    tim = im.point(lambda p: p > t and 255, "1")
    noki.cls()
    noki.show_image(tim)
    noki.gotorc(0,8)
    noki.text("Thresh")
    noki.gotorc(1,10)
    noki.text("%3d" % t)
    time.sleep(0.01)
    del tim
del im

#finish_time = time.time()
#print 'PIL Image, total time = %.3f' % (finish_time - start_time)

# Let's try some image manipulation
start_time = time.time()
im = Image.open("lenna.png")
#im = im.resize((84,48))
im.thumbnail((84,48))
im = im.convert("1")
noki.cls()
noki.show_image(im)
noki.gotorc(0,8)
noki.text("Dither")
del im

finish_time = time.time()
print 'PIL Image, total time = %.3f' % (finish_time - start_time)

time.sleep(2)

# New b-w image
im = Image.new('1', (84,48))
# New drawable on image
draw = ImageDraw.Draw(im)

xofs = im.size[0]/4
yofs = int((im.size[1]/4)/0.70)
for l in range(2):
  caption="%3d" % (l*2+1)
  ts = draw.getfont().getsize(caption)
  draw.rectangle((0,0)+ts,outline=0,fill=0)
  draw.text((0,0), caption, fill=1)
  for i in range(0,360):
    draw.ellipse((im.size[0]/2-xofs,im.size[1]/2-yofs,im.size[0]/2+xofs-1,im.size[1]/2+yofs-1), outline=1, fill=0)
    draw.pieslice((im.size[0]/2-xofs,im.size[1]/2-yofs,im.size[0]/2+xofs-1,im.size[1]/2+yofs-1), 0, i, outline=1, fill=1)

    # Copy it to the display
    noki.show_image(im)

  caption="%3d" % (l*2+2)
  ts = draw.getfont().getsize(caption)
  draw.rectangle((0,0)+ts,outline=0,fill=0)
  draw.text((0,0), caption, fill=1)
  for i in range(0,360):
    draw.ellipse((im.size[0]/2-xofs,im.size[1]/2-yofs,im.size[0]/2+xofs-1,im.size[1]/2+yofs-1), outline=1, fill=1)
    draw.pieslice((im.size[0]/2-xofs,im.size[1]/2-yofs,im.size[0]/2+xofs-1,im.size[1]/2+yofs-1), 0, i, outline=1, fill=0)

    # Copy it to the display
    noki.show_image(im)

# clean up
del draw
del im
noki.cls()


mono=ImageFont.truetype('/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf', 9)

ttf = []
for dirpath,dirnames,filenames in os.walk('/usr/share/fonts'):
  for f in filenames:
    if f.endswith('ttf'): ttf = ttf + [os.path.join(dirpath,f)]

ttf.sort()
for f in ttf:
  # load an available True Type font
  print "Font:", os.path.basename(f)
  font = ImageFont.truetype(f, 12)

  # New b-w image
  im = Image.new('1', (84,48))
  # New drawable on image
  draw = ImageDraw.Draw(im)
  # Some simple text for a test (first with TT font, second with default
  draw.text((0,0), font.getname()[0], font=mono, fill=1)
  draw.text((0,9), font.getname()[1], font=mono, fill=1)
  draw.text((2,20), "Hello World!", font=font, fill=1)
  # Copy it to the display
  noki.show_image(im)

  # clean up
  del draw
  del im
  del font

  time.sleep(1)

noki.cls()

I have no explanation for any prior problems when using the Rev 1 boards, but it works on all versions of Pi I have on hand as of this posting.

Enjoy!
Bill

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

Re: Nokia Pi LCD

Thu Jan 24, 2013 1:09 am

Got it working through spi, but just displyed lines when attempting to display a bitmap. (Damn forum wouldn't let me attach it :cry: )
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Thu Jan 24, 2013 3:33 am

The bitmap display routine is picky. There is an earlier post describing the steps for making a compatible BMP file. You could also try reading in as a PIL image and converting it to be compatible. The lenna display code is a good example of the steps needed. I would be willing to try the image on my system if you can make it available for download to confirm it is the image compatibility.

Enjoy!
Bill

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

Re: Nokia Pi LCD

Thu Jan 24, 2013 3:56 am

Just send an email to emailthemagpi at gmail dot com, then I can send you it.
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: Nokia Pi LCD

Thu Jan 24, 2013 4:31 am

I'm still having issues getting it to work again. Will try new script in a few hours when I get home to get the images the first time all I did was:

wget -U midori http://wggjr.com/images/lenna.png
wget -U midori http://wggjr.com/images/raspi.bmp
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

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

Re: Nokia Pi LCD

Thu Jan 24, 2013 4:53 am

http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: Nokia Pi LCD

Thu Jan 24, 2013 7:56 am

This is just crazy using the same screen when I first started off if I set contrast to 0xb4 nothing displayed. Using your code 0xa4 worked. Then I setup the board again and I couldn't get any images to display but using the new code 0xb4 works and 0xa4 doesn't?? But 0xa4 will display Texy's code? I know that's kind of hard to follow spent 2 days trying to figure out why it just stopped working then I notice your new code is using a different contrast one you had set for a blue backlight I now have a blue blacklight and your code works for both is it possible the screen finally burned in or something that allowed for the contrast value to need changing?

I downloaded The Magpi image and it shows.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: Nokia Pi LCD

Thu Jan 24, 2013 10:56 am

Hi malakai,

It looks like another reason not to work to late at night... another head slapping moment: it looks like the contrast setting in nokiaSPI.py was the cause and 0xb4 with the Adafruit board displays the graphics whereas 0xa4 does not. In fact, 0xbb seems to be a better choice for a black and white image and the Adafruit board. (This has been checked with version 1 and version 2 boards and using Bill's original posting that contained nokiaSPI.py.)

Bill, sorry for wasting your time.

Thanks and best regards,

Will

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

Re: Nokia Pi LCD

Thu Jan 24, 2013 11:24 am

I was right then ;)
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
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Thu Jan 24, 2013 12:32 pm

Glad its working for everyone again. The contrast seems to be a bit touchy. I have noticed that it is temperature sensitive. There is a display command for temperature compensation, but I have not looked at how it interacts with the other settings.

Enjoy!
Bill

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Thu Jan 24, 2013 12:36 pm

tzj wrote:This should be it.
I see nothing wrong with the image format. I will try it this evening when I get home to my Pi.

Enjoy!
Bill

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Thu Jan 24, 2013 1:02 pm

williamhbell wrote:It looks like another reason not to work to late at night... another head slapping moment: it looks like the contrast setting in nokiaSPI.py was the cause ...
The NokiaSPI class default contrast values can be changed when you create your display instance along with other settings:

As an example:

Code: Select all

noki = nokiaSPI.NokiaSPI(dev=(0,1), contrast=0xbb, speed=1000000, brightness=512)
This would use a display connected on /dev/spidev0.1 (CE1), with contrast setting 0xbb (187), maximum speed of 1 MHz, and LED brightness 512 (~50%).

Also, the contrast value is the actual SET Vop command for the LCD, which controls the operating voltage for the LCD. Valid values are in the range 0x81 to 0xff (129-255). The "correct" value to use is dependent on the specific LCD/LCD material and the temperature. An interesting point to note is the LED lighting can raise the temperature of the LCD enough to change the contrast setting required.

Enjoy!
Bill

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Fri Jan 25, 2013 1:16 am

To tzj:

I downloaded and tried the MagPi image. Other than being upside down, it displays correctly. If you are still having problems, maybe try reducing the speed setting when initializing the class.

Enjoy!
Bill

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

Re: Nokia Pi LCD

Fri Jan 25, 2013 1:27 am

It appears I need to use your code, texy's doesn't work with bitmaps properly... would you be able to post your code on github, I had to fix it the last time I copy and pasted :/
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Fri Jan 25, 2013 2:35 am

I don't "do" github currently.

I have attached the sources I used for testing to this message as nokimagpi.tar.gz. Should be simple to download and extract.

Code: Select all

-rw-r--r-- pi/pi           734 2013-01-24 20:06 MagPi-nokia-bw-rotated-mc.bmp
-rw-r--r-- pi/pi           653 2013-01-24 21:30 magpi.py
-rw-r--r-- pi/pi         10848 2013-01-21 20:46 nokiaSPI.py
Your image.
My test.
My class file.

Be sure to confirm the contrast value in magpi.py is appropriate.

Enjoy!
Bill
Attachments
nokimagpi.tar.gz
Test code for Nokia 5110 with MagPi BMP
(3.91 KiB) Downloaded 240 times

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

Re: Nokia Pi LCD

Sun Jan 27, 2013 1:54 pm

One thing I,ve noticed is that my 'old' code, one that uses the right wiring for SPI, but that does not use the spidev routines will upset subsequent use of the the spidev versions of the code. The spidev code will run without errors, but you'll get a blank screen. If the Pi is rebooted, and the spidev version is run 1st off, the display is correct. I suspect that a wiringpi cleanup may be required.
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
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Nokia Pi LCD

Sun Jan 27, 2013 3:30 pm

texy wrote:One thing I,ve noticed is that my 'old' code, one that uses the right wiring for SPI, but that does not use the spidev routines will upset subsequent use of the the spidev versions of the code. The spidev code will run without errors, but you'll get a blank screen. If the Pi is rebooted, and the spidev version is run 1st off, the display is correct. I suspect that a wiringpi cleanup may be required.
Good catch! I had not given this scenario a thought.

The spidev driver relies on the spi_bcm2708 driver which allocates and configures the SPI pin resources when it first loads. The spi_bcm2708 driver assumes it has complete ownership and no other software will modify the configuration of those resources. When the pin configuration is subsequently modified by an external application, the spidev driver will no longer function correctly as the spi_bcm2708 pin configuration is no longer valid. The user code for the LCD using spidev subsequently fails. So obvious now that you have pointed it out. :shock:

The fix is simple... If you run any application which modifies the SPI pin configuration, have the resources reconfigured by reloading the spi_bcm2708 module...

From the command line:

Code: Select all

$ sudo rmmod spi_bcm2708
$ sudo modprobe spi_bcm2708
Thanks texy!

Enjoy!
Bill

Return to “Python”