Nokia Pi Shield board code


1 post
by texy » Sun Feb 03, 2013 11:16 am
Hi,
here is some code as an example of testing and using my Nokia Pi Shield board. As well as the Nokia display, it also has 5 switches. These rely on the onboard pull down resistors of the Pi and again uses wiringpi for python. Unfortunately I was not able to change the pcb design to utilise hardware SPI before they went to the fab house for this revision, so the code to drive the display is unchanged.
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# lcd nokia switch shield v1
# by Texy and the raspberry pi community
# bitmap display routine by POSitality
#  2/2/13

import time
import wiringpi

ROWS = 6
COLUMNS = 14
PIXELS_PER_ROW = 6
OUT = ON = 1
IN = OFF = 0
CONTRAST = 0xb8

#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

#switches
UP  = 13  # gpio pin 21
RIGHT  = 10  # gpio pin 24
DOWN  = 11  # gpio pin 26
LEFT  = 12 # gpio pin 19
SELECT  = 14 # gpio pin 23

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']

bitreverse =[
  0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
]

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_gpio():
  wiringpi.wiringPiSetup()
  # Set pin directions.
# outputs :
  for pin in [DIN, SCLK, DC, RST, SCE]:
    wiringpi.pinMode(pin, OUT)
# inputs :
  for pin in [UP, RIGHT, DOWN, LEFT, SELECT]:
    wiringpi.pinMode(pin, IN)

# enable pull downs for the switches
  wiringpi.pullUpDnControl(UP, wiringpi.PUD_DOWN)
  wiringpi.pullUpDnControl(RIGHT, wiringpi.PUD_DOWN)
  wiringpi.pullUpDnControl(DOWN, wiringpi.PUD_DOWN)
  wiringpi.pullUpDnControl(LEFT, wiringpi.PUD_DOWN)
  wiringpi.pullUpDnControl(SELECT, wiringpi.PUD_DOWN)

  wiringpi.pinMode(LED,2) # pwm mode
  wiringpi.pwmWrite(LED,128) # mid-level

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 init(contrast):
  setup_gpio()
  # Toggle RST low to reset.
  wiringpi.digitalWrite(SCE, OFF)
  wiringpi.digitalWrite(RST, OFF)
  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 load_bitmap(file):
  gotoxy(0,0)
  f = open(file,'r')
  for x in range(6):
    for y in range(84):
      f.seek(0x3e + y*8 + x)
      b = ord(f.read(1))
      rev = bitreverse[b]
      lcd_data(rev ^ 0xff)
  f.close()

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(CONTRAST)
  load_bitmap('r.bmp')   
  finish_time = time.time()
  print 'Init, LED on, %d chars, total time = %.2f' % (
    end - start, finish_time - start_time
    )
  print
  while 1==1:
    input_value1 = wiringpi.digitalRead(UP)
    input_value2 = wiringpi.digitalRead(RIGHT)
    input_value3 = wiringpi.digitalRead(DOWN)
    input_value4 = wiringpi.digitalRead(LEFT)
    input_value5 = wiringpi.digitalRead(SELECT)
    print "SW1 = ",input_value1,'\n'
    print "SW2 = ",input_value2,'\n'
    print "SW3 = ",input_value3,'\n'
    print "SW4 = ",input_value4,'\n'
    print "SW5 = ",input_value5,'\n'
    time.sleep(1)

if __name__ == '__main__':
  main()


Image

You'll need to download the attached zip, extract the bitmap file, and place the r.bmp file in the same folder as the python script.
Cheers,
Texy
Attachments
r.zip
(471 Bytes) Downloaded 302 times
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
Forum Moderator
Forum Moderator
Posts: 4883
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England