Prototipare col raspberry pi

Post on 29-Jan-2018

311 views 1 download

Transcript of Prototipare col raspberry pi

Prototipare col Raspberry Pi

Gabriele Guizzardiguizzardi@gmail.com

Perché?

Il costo?

La potenza?

I linguaggi o i vari sistemi operativi?

LA COMUNITA’ ONLINE!!!

Sensori

Motori

Principali Sistemi Operativi

Raspbian (da Debian)

Ubuntu Mate

Snappy Ubuntu Core

Arch Linux Arc

Gentoo

Debian ARM

Windows 10 IoT (Pi 2)

Principali linguaggi con libreria per la GPIO

Python (RPi.GPIO)

C (wiringPi)

Processing (processing.io.*)

Java (Pi4J)

da riga di comando o Bash usando i relativi files (/sys/class/gpio/….)

La GPIO

Nomenclatura dei pin

Accendereun LED

Accendere un LEDimport RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT)

# ciclo 10 volte, on/off di 1 sec.

for i in range(10):

GPIO.output(7,True)

time.sleep(1)

GPIO.output(7,False)

time.sleep(1)

GPIO.cleanup()

Accendere un LED

Pulsanti

PulsantiPull Down

Pull Up

Pulsantiimport RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM) # Broadcom SOC channel

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

input_state = GPIO.input(4)

if input_state == False:

print('Pulsante premuto')

time.sleep(0.5)

I2C (inter integrated circuit)

MAX112

I2C

I2C

I2C

IC2 #!/usr/bin/python

import smbus

bus = smbus.SMBus(1) # 0 oppure 1 se RPi è 256 o 512 Mb di RAM

DEVICE_ADDRESS = 0x70 # indirizzo 7 bit

DEVICE_REG_MODE1 = 0x00

DEVICE_REG_LEDOUT0 = 0x1d

#Scrive un singolo char [long write_byte_data(int addr,char cmd,char val)]

bus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)

#Lettura

(msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)

PWM (pulse-width modulation)

PWM (duty-cycle)

PWM

def color(R, G, B): # range colore da 0-100% RED.ChangeDutyCycle(R) GREEN.ChangeDutyCycle(G) BLUE.ChangeDutyCycle(B)

# spegne tutti i LED time.sleep(.02) RED.ChangeDutyCycle(0) GREEN.ChangeDutyCycle(0) BLUE.ChangeDutyCycle(0)

# Settaggio dei pin come outputGPIO.setup(17, GPIO.OUT)GPIO.setup(18, GPIO.OUT)GPIO.setup(27, GPIO.OUT) RED = GPIO.PWM(17, 100) # 100HzRED.start(0) # 0 = spentoGREEN = GPIO.PWM(18, 100)GREEN.start(0)BLUE = GPIO.PWM(27, 100)BLUE.start(0)

while 1: for r in range(0,2): for g in range(0,2): for b in range(0,2): # loop per il 100% di ogni colore for i in range(0,101): color((x*i),(y*i),(z*i))

SPI (serial peripheral interface)

SPI

SPI#!/usr/bin/python

import spidev

import time

spi = spidev.SpiDev()

spi.open(0, 0)

spi.max_speed_hz = 7629

# Divide un intero in un array di 2 byte per spedirlo via SPI

def write_pot(input):

msb = input >> 8

lsb = input & 0xFF

spi.xfer([msb, lsb])

# Loop infinito di apertura/chiusura della porta del MCP4151

while True:

write_pot(0x1FF)

time.sleep(0.5)

write_put(0x00)

time.sleep(0.5)

UART Universal Async. Receiver-Transmitter

UART

UARTimport serial

ser = serial.Serial("/dev/ttyAMA0")

ser.write("messaggio da spedire")

read = ser.read()

print read

ser.close()