sound.py 5.02 KB
Newer Older
Administrator's avatar
Administrator committed
1
# coding: utf-8
Administrator's avatar
Administrator committed
2 3
# SNEO trigger V1.1
# Author - Sneo - Baptiste Doré
Administrator's avatar
Administrator committed
4 5
import RPi.GPIO as GPIO
import os, datetime, atexit, time
Administrator's avatar
Administrator committed
6 7 8 9 10 11 12 13 14 15 16 17
 
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
 
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import sys, getopt

screen = "on"
rotary = "on"
Administrator's avatar
Administrator committed
18
increment = 2
Administrator's avatar
Administrator committed
19
delai = 0
Administrator's avatar
Administrator committed
20 21

def main(argv): 
Administrator's avatar
Administrator committed
22
    global screen,rotary,increment,delai
Administrator's avatar
Administrator committed
23 24 25 26
    
    inputfile = ''
    outputfile = ''
    try:
Administrator's avatar
Administrator committed
27
        opts, args = getopt.getopt(argv,"hs:r:i:d:",["screen=","rotary=","increment=","delai="])
Administrator's avatar
Administrator committed
28
    except getopt.GetoptError:
Administrator's avatar
Administrator committed
29
       print 'sound.py --screen on(default)|off --rotary on(default)|off --increment 2(in ms) --delai 10(in ms)'
Administrator's avatar
Administrator committed
30 31 32
       sys.exit(2)
    for opt, arg in opts:
       if opt == '-h':
Administrator's avatar
Administrator committed
33
          print 'sound.py --screen on(default)|off --rotary on(default)|off --increment 2(in ms) --delai 10(in ms)'
Administrator's avatar
Administrator committed
34 35 36 37 38 39
          sys.exit()
       elif opt in ("-s", "--screen"):
          screen = arg
       elif opt in ("-r", "--rotary"):
          rotary = arg
       elif opt in ("-i", "--increment"):
Administrator's avatar
Administrator committed
40
        increment = float(arg)/1000
Administrator's avatar
Administrator committed
41 42
       elif opt in ("-d", "--delai"):
        delai = float(arg)/1000
Administrator's avatar
Administrator committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    
    #print 'screen "',screen,'"'
    #print 'rotary "', rotary,'"'


if __name__ == "__main__":
   main(sys.argv[1:])
   
if screen == "on":
    #GPIO SETUP
    RST = 24
    DC = 23
    SPI_PORT = 0
    SPI_DEVICE = 0
    
    disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
    
    disp.begin()
    
    
    # Clear display.
    disp.clear()
    disp.display()
    
    # Create blank image for drawing.
    width = disp.width
    height = disp.height
    image = Image.new('1', (width, height))
    # Get drawing object to draw on image.
    draw = ImageDraw.Draw(image)
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)
    
    # Draw some shapes.
    # First define some constants to allow easy resizing of shapes.
    padding = -2
    top = padding
    bottom = height-padding
    # Move left to right keeping track of the current x position for drawing shapes.
    x = 0
    # Load default font.
    font = ImageFont.load_default()



Administrator's avatar
Administrator committed
88 89 90
sound = 17 #capteur SON
out = 5 #Relay qui declenche Flash / APN
bounceTime =  0 #afin devitrer que le declencheur semballe
Administrator's avatar
Administrator committed
91
counter =  delai
Administrator's avatar
Administrator committed
92 93 94

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
Administrator's avatar
Administrator committed
95

Administrator's avatar
Administrator committed
96 97 98 99 100 101
GPIO.setup(sound, GPIO.IN)
GPIO.setup(out,GPIO.OUT)
#on passe le relay en OFF
GPIO.output(out,0)


Administrator's avatar
Administrator committed
102 103 104 105 106 107 108 109
if rotary == "on":
    clk_rotary = 27 # bouton tourne CLK
    dt_rotary = 22 # bouton tourne DT
    lastRotary = 0 #determinera si on  met a jours levent
    
    GPIO.setup(clk_rotary,GPIO.IN)
    GPIO.setup(dt_rotary,GPIO.IN)
        
Administrator's avatar
Administrator committed
110 111 112 113 114 115 116 117 118


def majBounceTime():
    global counter
    global bounceTime
    global sound
    
    bounceTime = int(counter*1000*3)
    if bounceTime < 1000:
Administrator's avatar
Administrator committed
119
        bounceTime = 1000
Administrator's avatar
Administrator committed
120 121 122 123 124 125 126 127

    GPIO.remove_event_detect(sound)
    GPIO.add_event_detect(sound, GPIO.BOTH, bouncetime=bounceTime, callback=sound_rotaryTriggered)
    
def turnflashON():
    global counter
    
    time.sleep(counter)
Administrator's avatar
Administrator committed
128
    #
Administrator's avatar
Administrator committed
129
    GPIO.output(out,1)
Administrator's avatar
Administrator committed
130
    refreshScreen(2)
Administrator's avatar
Administrator committed
131 132
    time.sleep(0.4)
    GPIO.output(out,0)
Administrator's avatar
Administrator committed
133 134
    
    
Administrator's avatar
Administrator committed
135 136

def rotaryTurned(channel):  
Administrator's avatar
Administrator committed
137 138 139
    global counter,increment,lastRotary,rotary
    
    if rotary == "on":
Administrator's avatar
Administrator committed
140 141 142 143
        clk_rotaryState = GPIO.input(clk_rotary)
        dt_rotaryState = GPIO.input(dt_rotary)
        
        if dt_rotaryState == 1 and clk_rotaryState == 0:
Administrator's avatar
Administrator committed
144
            counter -= increment
Administrator's avatar
Administrator committed
145
    
Administrator's avatar
Administrator committed
146
        if dt_rotaryState == 0 and clk_rotaryState == 0:
Administrator's avatar
Administrator committed
147
            counter += increment
Administrator's avatar
Administrator committed
148
    
Administrator's avatar
Administrator committed
149
        if counter <= increment/5:
Administrator's avatar
Administrator committed
150 151
            counter = 0
	
Administrator's avatar
Administrator committed
152
       
Administrator's avatar
Administrator committed
153
        tmpTime = time.time() - lastRotary
Administrator's avatar
Administrator committed
154

Administrator's avatar
Administrator committed
155
        if tmpTime >= 0.2:
Administrator's avatar
Administrator committed
156 157 158
            #on met a jours bounce time en fonction du nouveaux delay counter
            majBounceTime()
            lastRotary = time.time();
Administrator's avatar
Administrator committed
159 160
            
            showCounter()
Administrator's avatar
Administrator committed
161

Administrator's avatar
Administrator committed
162
   
Administrator's avatar
Administrator committed
163 164 165

#Afficher le delay
def showCounter():
Administrator's avatar
Administrator committed
166
    global counter
Administrator's avatar
Administrator committed
167
    refreshScreen(0)
Administrator's avatar
Administrator committed
168
    print("Delaying relay by %s ms" % (counter*1000))
Administrator's avatar
Administrator committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    
def refreshScreen(triggered):
    global counter
    if screen == "on":
        draw.rectangle((0,0,width,height), outline=0, fill=0)
        draw.text((x, top), "SNEO trigger",  font=font, fill=255)
        draw.text((x, top+8), "Delay : %s ms" % (counter*1000),  font=font, fill=255)
        
        if(triggered == 1):
            draw.text((x, top+16), "Sound detected",  font=font, fill=255)
            
        if(triggered == 2):
            draw.text((x, top+24), "Sound Triggered",  font=font, fill=255)
            
        disp.image(image)
        disp.display()
Administrator's avatar
Administrator committed
185 186 187

def sound_rotaryTriggered(sound):
    global bounceTime
Administrator's avatar
Administrator committed
188 189 190
    
    refreshScreen(1)
    #print (bounceTime)
Administrator's avatar
Administrator committed
191
    if GPIO.input(sound):
Administrator's avatar
Administrator committed
192
        print("- Sound Detected! HIGH") 
Administrator's avatar
Administrator committed
193 194 195 196
        turnflashON()
    else:
        print("- Sound Detected! LOW")
        turnflashON()
Administrator's avatar
Administrator committed
197
    
Administrator's avatar
Administrator committed
198

Administrator's avatar
Administrator committed
199
    refreshScreen(0)
Administrator's avatar
Administrator committed
200 201 202 203



majBounceTime()
Administrator's avatar
Administrator committed
204 205 206 207 208
 
if rotary == "on":
    GPIO.add_event_detect(clk_rotary, GPIO.FALLING , callback=rotaryTurned, bouncetime=10)
     
refreshScreen(0)
Administrator's avatar
Administrator committed
209 210
# infinite loop
while True:
Administrator's avatar
Administrator committed
211 212 213 214
    time.sleep(0.1)
    

GPIO.cleanup()
Administrator's avatar
Administrator committed
215