Modifications

Sauter à la navigation Sauter à la recherche
3 812 octets ajoutés ,  21 février 2016 à 13:02
Ligne 171 : Ligne 171 :     
== Faire encore mieux ==
 
== Faire encore mieux ==
xxx
+
Dans le code ci-dessous, nous allons transformer le code présenté ci-dessus en classe (plus pratique à manipuler) et présenter une seconde mélodie :-) .
 +
 
 +
<nowiki># Commande d'un Piezo Buzzer à l'aide de MicroPython PyBoard
 +
# Drive a Piezo Buzzer with MicroPython Pyboard
 +
#  http://shop.mchobby.be/product.php?id_product=57
 +
#
 +
# Voir Tutoriel - See our french tutorial
 +
#  http://wiki.mchobby.be/index.php?title=MicroPython-Hack-piezo
 +
# Voir les tutoriels "Pyb Discovery Kit" / see the "Pyb Discovery Kit" tutorials
 +
#  http://wiki.mchobby.be/index.php?title=MicroPython.Pyboard.Discovery
 +
#
 +
from pyb import Pin, Timer, udelay
 +
 
 +
class Piezo:
 +
    """ Driver piezo buzzer wired on a PWM Pin
 +
   
 +
    Use the Pin Y2 by default, wire the piezo with a 100 Ohms resistor """
 +
    notes = { ' ' : 0,  # silence,
 +
              'c' : 261, # Do
 +
              'd' : 294, # Ré
 +
              'e' : 329, # Mi
 +
              'f' : 349, # Fa
 +
              'g' : 392, # Sol
 +
              'a' : 440, # La
 +
              'b' : 493, # Si
 +
              'C' : 523 # Do
 +
            }
 +
 
 +
    def __init__(self, tempo=300, pin="Y2", timer=8, channel=2 ):
 +
        """ initialzation
 +
        :param tempo: tempo of tune. Lower the value to go faster
 +
        :param pin: name of the piezo pin
 +
        :param timer: timer attached to the pin (to control PWM signal
 +
        :param channel: channel to use with the timer """
 +
 
 +
        self._tempo = tempo
 +
        self.p = Pin( pin )
 +
        self.tim = Timer( timer, freq=3000 )
 +
        self.ch  = self.tim.channel( channel, Timer.PWM, pin=self.p )
 +
 
 +
    def get_tempo( self ):
 +
        return self._tempo
 +
 
 +
    def set_tempo( self, value ):
 +
        self._tempo = value
 +
 
 +
    tempo = property( get_tempo, set_tempo )
 +
 
 +
    def play_tone(self, freq ):
 +
        """ play a given tone (frequency) on the piezo """
 +
        if freq == 0:
 +
            self.ch.pulse_width_percent( 0 )
 +
        else:
 +
            self.tim.freq( freq )
 +
            self.ch.pulse_width_percent( 30 )
 +
 
 +
    def no_tone( self ):
 +
        self.play_tone( 0 )
 +
 
 +
    def play_note(self, note, duration ):
 +
        """ Play a note identified by a letter for a given duration """
 +
        # Transforme la note en frequence
 +
        freq = self.notes[ note ]
 +
        self.play_tone( freq )
 +
        # Transforme la duree en temp (1=1000micros, 2=2000micros, etc)
 +
        udelay( duration * 1000 ) # temps en micro-second
 +
 
 +
# Joue une mélodie encodée dans une chaine de caractere
 +
    def play_tune( self, tune_string ):
 +
        """ play a tune stored within a comma separated string.
 +
            each item being a note letter + duration digit (1 digit acting as time multiplier).
 +
            If the duration digit is missing then it is replaced by the value 1
 +
 
 +
            :param tune_string: eg "c2,c,d3,c3,f3,e3,c2,c,d3,c3,g3,f3, 4" where the space is a blank sound """
 +
    tune_list = tune_string.split( ',' )
 +
    duration = 1
 +
    for tune_item in tune_list:
 +
        # print( tune_item )
 +
        if len( tune_item )>1:
 +
            try:
 +
                duration = int( tune_item[1:] )
 +
            except:
 +
                raise ValueError( 'Invalid duration %s for note %s' % (tune_item[1:], tune_item[0]) )
 +
        else:
 +
            duration = 1
 +
 
 +
        # Jouer la note
 +
        self.play_note( tune_item[0], self._tempo * duration )
 +
        # Pause entre 2 notes
 +
        self.no_tone()
 +
        udelay( (self._tempo * 1000) // 2 ) 
 +
 
 +
 
 +
# a Tune is stored within a comma separated string.
 +
#  * Each item being a note letter + duration digit (acting as tempo time multiplier).
 +
#  * If the duration digit is missing then it is replaced by the value 1
 +
#
 +
piezo = Piezo( tempo = 300 )
 +
piezo.play_tune( "c,c,g,g,a,a,g2,f,f,e,e,d,d,c2, 4" )
 +
piezo.tempo = 150
 +
piezo.play_tune( "c2,c,d3,c3,f3,e3,c2,c,d3,c3,g3,f3, 4" )</nowiki>
    
== Internet ==
 
== Internet ==
29 837

modifications

Menu de navigation