Raspberry pie uses Python to write a few simple programs __python

Source: Internet
Author: User

1. Raspberry Pie Display Temperature

Import Commands
  
def get_cpu_temp ():
    tempfile = open ("/sys/class/thermal/thermal_zone0/temp")
    cpu_temp = Tempfile.read ()
    tempfile.close () return
    float (cpu_temp)/1000
    # Uncomment the next line if you want the temp I n Fahrenheit
    #return float (1.8*cpu_temp) +32
  
def get_gpu_temp ():
    gpu_temp = Commands.getoutput ('/opt/vc/ Bin/vcgencmd measure_temp '). Replace (' temp= ', '). Replace (' \ ' C ', ') return  float (gpu_temp)
    # Uncomment the next line if you want the temp in Fahrenheit
    # return float (1.8* gpu_temp) +32
  
def main ():
    pri NT "CPU temp:", str (get_cpu_temp ())
    print "GPU temp:", str (get_gpu_temp ())
  
if __name__ = ' __main__ ':
    ma In ()

2. Control Gpio

First write a module led.py:

Import Rpi.gpio as GPIO  
import time  
 
channels = [16,18,22,24,26,19,21,23]   
 
def init ():  
    Gpio.setmode ( GPIO. BOARD) for  
    x in channels:  
        Gpio.setup (X,gpio. Out)  
        pass  
 
def on (i):  
    gpio.output (Channels[i], GPIO. High)  
 
def off (i):  
    gpio.output (Channels[i], GPIO. Low)  
 
def CTRL + (data):  
    for I in channels:  
        gpio.output (i, Data & 0x1)  
        data = Data >> 1  
    pa SS  
 
def Test (): For  
    I in Xrange (a):  
        Ctrl (i)   
        Time.sleep (0.1)   
 
def clean ():  
    Gpio.cleanup ()

Then write a program called Module test.py:

Import led
 
led.init ()
led.test ()
Led.clean ()

Rpi.gpio Module Function Description:

RPi.GPIO.setmode (Naming_system)

Sets the naming method for the Gpio pin.

Naming_system available values are RPi.GPIO.BCM and RPi.GPIO.BOARD, representing the boardcom naming system and the naming system on the Raspberry Pie Board respectively. The same set of programs is recommended for use with board when used on multiple raspberry systems because of the different versions of the BVM pins, which are said to be BCM.

RPi.GPIO.setup (channel, state)
Set the pins labeled Channel to State mode.

Channel takes the value of 1~26,state as RPi.GPIO.IN or RPi.GPIO.OUT, representing input and output respectively. For example, RPi.GPIO.setup (1, RPi.GPIO.IN) indicates that the 1th pin is set to input mode, and RPi.GPIO.setup (3, RPi.GPIO.OUT) indicates that the 3rd pin is set to output mode. Which is the exact number depends on what is set in SetMode ().

RPi.GPIO.output (channel, state)
Sets the pin labeled channel to the state-specified level.

Channel values are 1~26,state values of RPi.GPIO.HIGH and RPi.GPIO.LOW, or 1 and 0, or true and false, indicating high power and low levels. For example, RPi.GPIO.output (1, 1) indicates that the 1th pin is set to a high level, and RPi.GPIO.output (3, flase) indicates that the 3rd pin is set to a low level. Which is the exact number depends on what is set in SetMode ().

RPi.GPIO.input (channel)
Get the level bricks that will be labeled as channel pins.

Channel value is 1~26. For example, RPi.GPIO.input (1) indicates the state of obtaining the 1th pin.

RPi.GPIO.cleanup ()
Clears the state of the RPi.GPIO.setup () setting before it is removed.

Be sure to call before exiting the program, otherwise the error will be the next time you call.


led.py Module Description

In channel, the label of the needle used in the connection is kept in order.

Init () is the code that initializes the Gpio interface, using the control LCD to be invoked before. The main job is to set the interface naming pattern and set the pins in the channel to output mode
On ()/Off () is to set the first needle in the channel to a high level/low level
CTRL () is the 8-pin level set according to the parameters. 0-bit low, 1-bit low, 2-bit low. The channel is labeled 0, 1, 2, respectively. The level state of the needle, 1 for the high level, 0 for the low level
Test () is a testing function. Use 8-bit binary to indicate the status of 8 lights, every 0.1 seconds to the next state: 0000 0000, 0000 0001, 0000 0010, 0000 0011,0000 0100 ... is actually counting from 0 to 255.
Clean () is an encapsulation of the RPi.GPIO.cleanup ()







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.