quinta-feira, abril 10, 2014

Project 16 - Raspberry Pi vs Pibrella Expansion Board vs Python - Siren


As promissed and under the "influence" of Codebits 2014 that started on the 10th, I've explored the following Pribrella's physical interfaces through Python (just some basic functions):

Buzzer
LEDs
On-Off Button




Here the Python code I used to interface with the HW componentes referenced above:


Getting the source code from the RPi by FTP, I'll insert it here for better visibility:

# 2014-04-11 - MAAntao

import os,sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))

import pibrella, time, signal

# Pulsar todos os LEDs da board

TIME_ON = 0.2
TIME_OFF = 0.2
FADE_ON = 0.2
FADE_OFF = 0.2

# TOCAR SIRENE

siren_stopped = True

def siren():
if siren_stopped == True:
pibrella.buzzer.stop()
return True
for x in xrange(-30,30,2):
pibrella.buzzer.note(x)
time.sleep(0.01)
for x in reversed(xrange(-30,30,2)):
pibrella.buzzer.note(x)
time.sleep(0.01)

pibrella.async_start('sirene',siren)

def start_siren():
global siren_stopped
siren_stopped = False
pibrella.light.pulse(TIME_ON, TIME_OFF, FADE_ON, FADE_OFF)

def stop_siren():
global siren_stopped
siren_stopped = True
pibrella.light.stop()

def handle_button(button):
global siren_stopped
if siren_stopped == True:
start_siren()
print "Arranque da Sirene"
else:
stop_siren()
print "Parar a Sirene"

pibrella.button.released(handle_button)

# Suspender durante 20 segundos

#time.sleep(20)

signal.pause()

# Sair!

(if you want to fork/mock the abovementioned code, just upload it to your own Raspberry Pi)
  
By executing the python script with root ("sudo python sirene.py"):


The script launches ("Arranque da Sirene"):


See the three LEDs blinking on and off (photo and video):



By pressing the red button by the LED's the siren stops :


This piece of code can have several uses:

  1. Clock Alarm;
  2. Motion Detector Alarm;
  3. (...)

Happy hacking. You don’t learn to hack – you hack to learn…

MAAntão

Sem comentários: