Articole similare

Newsletter



PiLowLib - librarie low level I/O pentru Raspberry Pi

python code

PiLowLib este o librarie pentru acces low level la perifericele lui Raspberry Pi. Varianta curenta, pe langa acces direct la registre, ofera suport pentru urmatoarele perifierice:

  • acces I/O simplu (intrari/ieisiri digitale)
  • PWM
  • clock

PiLowLib este rapid si performat deoarece acceseaza direct registrele perifericelor lui Raspberry Pi si in plus codul este scris direct in Python si nu este nevoie de compilare (cum sunt alte librarii scrise in C).

PiLowLib a fost scris de Calin Crisan si este publicat sub licienta LGPLv3.
Anumite parti din aceasta librarie au fost inspirate din proiectul wiringPi, scris de Gordon Henderson.

Instalare

Libraria functioneaza exclusiv pe Raspberry Pi si are nevoie de acces root la /dev/mem.

Downloadeaza cea mai recenta versiune a codului sursa de pe github: https://github.com/ccrisan/pilowlib

Pentru testare, poti folosi direct libraria de unde ai facut download, prin configurarea corespunzatoarea a PYTHONPATH: export PYTHONPATH=$PYTHONPATH:/path/to/pilowlib Daca vrei sa o instalezi pe sistem, ruleaza urmatoarea comanda (ca root): python setup.py install

Testare

Directorul examples include cateva exemple cu cod sursa:

digital.py

# Copyright (c) 2012 Calin Crisan
# This file is part of PiLowLib.

from pilowlib import funcs, digital

# sets the GPIO pin 18 to digital out
funcs.set_gpio_func(18, funcs.DIGITAL_OUT)

# sets the GPIO pin 23 to digital in
funcs.set_gpio_func(23, funcs.DIGITAL_IN)

# sets the GPIO pin 18 to 'high'
digital.set_value(18, True)

# reads the level of the GPIO pin 23
value = digital.get_value(23)
if value:
    print('GPIO 23 is high')
    
else:
    print('GPIO 23 is low')

gpclock.py

# Copyright (c) 2012 Calin Crisan
# This file is part of PiLowLib.

from pilowlib import funcs, clock

# sets the GPIO pin 4 to GPCLK0
funcs.set_gpio_func(4, funcs.GPCLK0)

# set the GPCLK0 source to oscillator (said to be 19.2 MHz),
# and the divider to 32, resulting a 19.2MHz/32 = 600KHz
clock.configure(0, clock.SRC_OSC, 32)

# start the GPCLK0 peripheral
clock.start(0)

pwm.py

# Copyright (c) 2012 Calin Crisan
# This file is part of PiLowLib.

from pilowlib import funcs, pwm, clock

# sets the GPIO pin 18 to PWM0
funcs.set_gpio_func(18, funcs.PWM0)

# configures PWM0 with the default settings (plain PWM)
pwm.configure(0)

# set the PWM clock source to oscillator (said to be 19.2 MHz),
# and the divider to 32, resulting a 19.2MHz/32 = 600KHz
pwm.configure_clock(clock.SRC_OSC, 32)

# set the range to 1024 and the data to 512 for PWM0,
# resulting in a duty cycle of 50%
pwm.set_range(0, 1024)
pwm.set_data(0, 512)

# start the PWM0 peripheral
pwm.start(0)

Mai multe detalii despre modul de functionare si configurare al perifericelor sunt disponibile in datasheet: http://www.designspark.com/files/ds/supporting_materials/Broadcom%20BCM2835.pdf