勇闖新世界︰ 《 Kernel 4.X 》之整裝蓄勢‧PiTFT‧三

有關 SPI 界面顯示器的零零種種,在

音樂播放器之 CD 轉成 mp3《三》下‧上

音樂播放器之 CD 轉成 mp3《三》下‧中

音樂播放器之 CD 轉成 mp3《三》下‧下

已經作過很多介紹,雖然當時是用『 PiTFT 3.5″ 觸控螢幕』,事實上原理相同,此處就減筆帶過,僅補之以

【Kernel 4.0.9 開機訊息】

pi@raspberrypi ~ dmesg | grep spi [    1.967198] spi-bcm2835 3f204000.spi: no tx-dma configuration found - not using dma mode [    2.023855] spi spi0.1: setting up native-CS1 as GPIO 7 [    2.027364] stmpe-spi spi0.1: stmpe610 detected, chip id: 0x811 [    2.049302] spi spi0.0: setting up native-CS0 as GPIO 8  # 注意 DMA buffer 訊息 [    2.305710] graphics fb1: fb_ili9340 frame buffer, 320x240, 150 KiB video memory, 4 KiB DMA buffer memory, fps=20, spi0.0 at 32 MHz [    2.493734] fbtft_device:      stmpe610 spi0.1 500kHz 8 bits mode=0x00 [    2.493751] fbtft_device:      ili9340 spi0.0 32000kHz 8 bits mode=0x00 [    2.586974] input: stmpe-ts as /devices/platform/soc/3f204000.spi/spi_master/spi0/spi0.1/stmpe-ts/input/input0 [  198.507575] fb_ili9340 spi0.0: fbtft_update_display: start_line=239 is larger than end_line=0. Shouldn't happen, will do full display update </pre>    <span style="color: #ff0000;">【Kernel 4.1.4 開機訊息】</span> <pre class="lang:sh decode:true ">pi@raspberrypi ~ dmesg | grep spi
[    1.894438] spi spi0.1: setting up native-CS1 as GPIO 7
[    1.903764] stmpe-spi spi0.1: stmpe610 detected, chip id: 0x811
[    1.930096] spi spi0.0: setting up native-CS0 as GPIO 8
[    2.389676] graphics fb1: fb_ili9340 frame buffer, 320x240, 150 KiB video memory, 4 KiB DMA buffer memory, fps=20, spi0.0 at 32 MHz
[    2.584208] fbtft_device:      stmpe610 spi0.1 500kHz 8 bits mode=0x00
[    2.584225] fbtft_device:      ili9340 spi0.0 32000kHz 8 bits mode=0x00
[    2.676952] input: stmpe-ts as /devices/platform/soc/3f204000.spi/spi_master/spi0/spi0.1/stmpe-ts/input/input0

※ 由於此系列多篇文本是以 Kernel 4.0.9 為主作的驗證以及測試,此篇之後,若是 Kernel 4.1.4 之比對測試沒有問題, 將不再出校記 。

 

【中文化 pygame 測試程式】

# -*- coding: utf-8 -*- ← python3 optional, but still good to indicate encoding

# Source: http://inventwithpython.com/drawing.py
# 中文檔改自來源檔

import pygame, sys, os
from pygame.locals import *

# pygame PiTFT 環境設定
os.environ["SDL_VIDEODRIVER"] = "fbcon"
os.environ["SDL_FBDEV"] = "/dev/fb1"

# Uncomment if you have a touch panel and find the X value for your device
os.environ["SDL_MOUSEDRV"] = "TSLIB"
os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"

pygame.init()

# set up the window
顯示面 = pygame.display.set_mode((320, 240), 0, 32)
pygame.display.set_caption('畫圖')

# set up the colors
黑色 = (  0,   0,   0)
白色 = (255, 255, 255)
紅色   = (255,   0,   0)
綠色 = (  0, 255,   0)
藍色  = (  0,   0, 255)

# draw on the surface object
顯示面.fill(白色)
pygame.draw.polygon(顯示面, 綠色, ((16, 0), (111, 106), (36, 277), (56, 27), (0, 106)))
pygame.draw.line(顯示面, 藍色, (60, 60), (120, 60), 4)
pygame.draw.line(顯示面, 藍色, (120, 60), (60, 120))
pygame.draw.line(顯示面, 藍色, (60, 120), (120, 120), 4)
pygame.draw.circle(顯示面, 藍色, (40, 50), 20, 0)
pygame.draw.ellipse(顯示面, 紅色, (110, 200, 40, 80), 1)
盒子 = pygame.draw.rect(顯示面, 紅色, (100, 150, 100, 50))

pixObj = pygame.PixelArray(顯示面)
pixObj[120][144] = 黑色
pixObj[122][146] = 黑色
pixObj[124][148] = 黑色
pixObj[126][158] = 黑色
pixObj[126][158] = 黑色
del pixObj

# run the game loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            print("Pos: %sx%s\n" % pygame.mouse.get_pos())
            if 盒子.collidepoint(pygame.mouse.get_pos()):
                pygame.quit()
                sys.exit()
    pygame.display.update()

 

有興趣用 pygame 在 PiTFT 上,開發軟體的朋友,請注意

# pygame PiTFT 環境設定

os.environ[“SDL_VIDEODRIVER”] = “fbcon”
os.environ[“SDL_FBDEV”] = “/dev/fb1”

# Uncomment if you have a touch panel and find the X value for your device

# 觸控裝置宣告
os.environ[“SDL_MOUSEDRV”] = “TSLIB”
os.environ[“SDL_MOUSEDEV”] = “/dev/input/touchscreen”

。其次在

Physical computing ︰ python 《補充》︰ IDE 用法……

系列中,我們介紹過一個特別的 Python IDE︰

這個『 Ninja-IDE 』有一個特色,就是能找出 Python 程式不符合『 PEP8 』建議的『原始碼』,彰顯了『 Pythonic Way 』的重要組成部分。在此讓我們舉個出自美國 Simpson College 的 Paul Vincent Craven 所著的線上書《 Program Arcade Games With Python And Pygame 》第二十章中的『 fractal.py 』為例,比較符不符合『 PEP8 』的異同︰

feature-errors
Highlight Static and PEP8 errors in the document, you can also see that the files containing PEP8 errors are shown with an icon in the tab where the file is opened, and files containing code static errors are shown with a bug icon in that tab.

 

關於 pygame 的線上書,增加 Albert Sweigart 先生所寫的

Invent with Python 系列書籍。

header image

Automate the Boring Stuff with Python cover thumbnail Invent with Python cover thumbnail Making Games cover thumbnail Hacking Secret Ciphers cover thumbnail

 

以及簡單的 PiTFT

【按鍵測試程式】

pi@raspberrypi ~ $ sudo -s
root@raspberrypi:/home/pi# python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> import time
>>> GPIO.setmode(GPIO.BCM)
>>> 入浮針 = 17
>>> GPIO.setup(入浮針, GPIO.IN)
>>> GPIO.input(入浮針)
0
>>> GPIO.input(入浮針)
0
>>> GPIO.input(入浮針)
0
>>> GPIO.input(入浮針)
0
>>> GPIO.input(入浮針)
0
>>> GPIO.setup(入浮針, GPIO.IN,pull_up_down = GPIO.PUD_UP)
>>> GPIO.input(入浮針)
1
>>> GPIO.input(入浮針)
0
>>> GPIO.input(入浮針)
1
>>> 

※ 注意! Adafruit 不過是用 BCM 『編碼制』及『編號』的樹莓派 GPIO 輸入也。